zoukankan      html  css  js  c++  java
  • zebra线程管理源码简析

    http://blog.csdn.net/xuyanbo2008/article/details/7439733

    http://blog.csdn.net/xuyanbo2008/article/details/7439738

    ==========================================================

    1 zebra线程机制概述

    zebra这个软件包整体结构大致可分为两大块:协议模块和守护进程模块。协议模块实现各协议的功能,各协议以子模块的形式加载到zebra中;守护进程模块的功能主要是管理各协议的信令传输、表项操作、系统操作调用等事务,为各协议提供底层信息以及相关的硬件处理等功能支持。Zebra与各协议的交互采用的是C-S模式,在每个协议子模块中均有一个Client端与守护进程模块中的Server端交互,它们所使用的socketzebra内部使用的socket,不与外部交互。

    zebra中的线程是分队列调度的,每个队列以一个链表的方式实现。线程队列可以分成五个队列:eventtimerreadyreadwrite。队列的优先级由高到低排列。但是,readwrite队列并不参与到优先级的排列中,实际操作时,如果readwrite队列中的线程就绪,就加入ready队列中,等待调度。调度时,首先进行event队列中线程的调度,其次是timerready

    实际上,zebra中的线程是“假线程”,它并没有实现线程中的优先级抢占问题。在zebra的线程管理中,有个虚拟的时间轴,必须等前一时间的线程处理完,才看下一时间的线程是否触发。由于电脑处理速度较快且处理每个线程的时间间隔较小,所以处理其多线程来可以打到类似“并行处理”的效果。

    zebra源码中有关线程管理的各个函数放置于zebra-0.95a\lib文件夹的thread.hthread.c两个文件中。

    2 线程管理源码分析

    2.1 重要结构体介绍

    2.1.1 thread

    这是线程队列中每一个单个线程的代码描述,线程队列被描述成双向链表的形式,thread结构体是双向链表的元素。共有六种线程:readwritetimereventreadyunused。因此,线程队列也有六种。

    struct thread

    {

    unsigned char type;        /* thread类型,共有六种 */

    struct thread *next;        /* 指向下一thread的指针,双向链表 */

    struct thread *prev;        /*指向前一thread的指针*/

    struct thread_master *master;      /* 指向该thread所属thread_master结构体的指针 */

    int (*func) (struct thread *); /* event类型thread的函数指针 */

    void *arg;               /* event类型thread的参数 */

    union {

        int val;                /* event类型thread的第二个参数*/

        int fd;                  /* read/write类型thread相应的文件描述符 */

        struct timeval sands;   /* thread的剩余时间,timeval类型,此结构体定义在time.h中,有两个元素,秒和微秒 */

    } u;

    RUSAGE_T ru;                    /* 详细用法信息,RUSAGE这个宏在该thread有用法描述时定义为rusage类型,描述其详细进程资源信息,没有用法描述时定义为timeval类型 */

    };

    2.1.2 thread_list

    一个thread_list结构体描述一个thread双向链表,也即一个进程队列。

    struct thread_list

    {

    struct thread *head;/* 该线程队列头指针 */

    struct thread *tail; /* 该线程队列尾指针 */

    int count; /* 该线程队列元素数目 */

    };

    2.1.3 thread_master

    总的线程管理结构体,里面存有六种线程队列,三种文件描述符以及占用空间信息。

    struct thread_master

    {

    //六种线程队列

    struct thread_list read;

    struct thread_list write;

    struct thread_list timer;

    struct thread_list event;

    struct thread_list ready;

    struct thread_list unuse;

    //三种文件描述符

    fd_set readfd;

    fd_set writefd;

    fd_set exceptfd;

    //thread_master所占空间大小

    unsigned long alloc;

    };

    1.1 相关函数简介

    下面给出了zebra关于线程管理的相关函数的简要功能介绍。

    1.1.1 thread_master_create ()

    为创建一个新的thread_master结构体动态开辟一块内存空间。

    1.1.2 thread_list_add ()

    list双向链表尾部插入一个新的thread

    1.1.3 thread_list_add_before ()

    在函数参数point所指向的thread前面插入一个新的thread

    1.1.4 thread_list_delete ()

    删除参数中指定的thread

    1.1.5 thread_add_unuse ()

    向指定thead_master中的unused链表尾部插入新thread

    1.1.6 thread_list_free ()

    从内存中释放掉指定thread_master中的指定thread链表所占空间。

    1.1.7 thread_master_free ()

    彻底释放指定thread_master所占内存空间。

    1.1.8 thread_trim_head ()

    若指定thread链表中非空,删除该链表头指针所指thread,并将其返回,即从线程队列中取出一个线程。

    1.1.9 thread_empty ()

    判断指定thread链表是否为空。

    1.1.10 thread_timer_remain_second ()

    得到指定线程的剩余时间。

    1.1.11 thread_get ()

    若指定thread_master中的unuse链表非空,从该队列中取出一个thread,根据参数初始化并返回之。否则,给该thread_master多开辟一块空间给新的thread,根据参数初始化该thread并返回之。

    1.1.12 thread_add_read ()

    根据所给参数在指定thread_master中添加并初始化一个read类型的thread并返回之。

    1.1.13 thread_add_write ()

    根据所给参数在指定thread_master中添加并初始化一个write类型的thread并返回之。

    1.1.14 thread_add_timer ()

    根据所给参数在指定thread_master中添加并初始化一个timer类型的thread。若timer链表不要求排序,则直接返回新thread,若要求排序,则将新thread插入到队列的相应位置后再返回之。

    1.1.15 thread_add_event ()

    根据所给参数在指定thread_master中添加并初始化一个event类型的thread并返回之。

    1.1.16 thread_cancel ()

    删除指定thread,删除后将其类型置为THREAD_UNUSED,并将其插入到该thread_masterunuse链表中。

    1.1.17 thread_cancel_event ()

    将指定thread_masterevent链表中与参数中arg相匹配的thread删除。

    1.1.18 thread_timer_wait ()

    找出指定thread_mastertimer链表中最小的剩余时间并将其返回。

    1.1.19 thread_run ()

    将指定thread的值赋给thread类型的fetch,然后将其类型置为THREAD_UNUSED,并将其插入unuse链表,返回fetch

    1.1.20 thread_process_fd ()

    将指定thread链表中的元素取出插入到该thread_masterready链表中,返回该链表中插入元素的个数。

    1.1.21 thread_fetch ()

    若指定thread_masterevent队列非空取出其头元素并用run函数处理。取出并用run函数处理timer队列中每一个之前创建的线程。若指定thread_masterready队列非空取出其头元素并用run函数处理。拷贝该thread_master的文件描述符。将readwrite链表插到ready链表中,再从ready链表取头元素用run函数处理。如此无限循环下去直到所有进程都处理完。

    1.1.22 thread_consumed_time ()

    得到该进程所耗费的时间。

    1.1.23 thread_call ()

    执行该thread中的功能函数,如果该thread持续时间超过CPU规定的独占时间,发出警告。

    1.1.24 thread_execute ()

    根据参数创建一个event类型的thread并用thread_call()函数对其进行处理。


    Meet so Meet. C plusplus I-PLUS....
  • 相关阅读:
    SQL Server 2008 如何查看与创建约束
    Hibernate的主键生成策略
    sql server的id字段设置为自动生成的,那么该怎么写insert语句呢?
    org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.bjsxt.model.Student.courses
    INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX。
    在排序数组中查找符合条件的数
    求二叉树中节点的最大距离
    设计包含min 函数的栈
    寻找链表的倒数第K个节点
    翻转句子中单词的顺序
  • 原文地址:https://www.cnblogs.com/iplus/p/4467310.html
Copyright © 2011-2022 走看看