1 关键数据结构

1 关键数据结构

1. connection.h

1.1 manager_s

定义:

struct manager_s {
    int ep_fd;// epoll 句柄
    struct epoll_event *ep_events; // epoll 事件数组
    int nep_events; // epoll 事件个数
    
    connections_t *conn;// 连接实体数组
    int nconnections;// 连接数

    threadpool_t *t_pool; // 线程池
    ukey_pool_t *m_pool; // 内存池
    
    pthread_mutex_t conn_lock; //锁
};

作用:
管理全局的句柄

1.2 connections_s

定义:

struct connections_s {
    event_t *read; // 读事件
    event_t *write; // 写事件

    ukey_pool_t *m_cpool; // 内存池
};

作用:
一个连接的数据结构

2. event.h

2.1 event_s

定义:

struct event_s {
    int ev_fd; // 事件的描述符
    void (*ev_callback)(int, int, void *ev_arg); // 执行事件的回调函数
    void *ev_arg; // 回调函数的参数
    int ev_events;
    int ev_status;
};

作用:
一个事件的数据结构

3. memory_pool.h

3.1 ukey_pool_large_s

定义:

struct ukey_pool_large_s {
    ukey_pool_large_t *next;
    void *alloc;
};

作用:
大块内存数据结构

3.2 ukey_pool_data_s

定义:

struct ukey_pool_data_s {
    char *last;
    char *end;
    ukey_pool_t *next;
    int failed;
};

作用:
小块内存结构体,用链表组织,预先分配

3.3 ukey_pool_s

定义:

struct ukey_pool_s {
    ukey_pool_data_t small; // 小内存块

    int max;

    ukey_pool_t *current;

    ukey_pool_large_t *large; //大内存块的链表
};

作用:
内存池结构体,维护了两个链表,一条管理大块内存,一条管理小块内存

4. thread_pool.c

4.1 threadpool_t

定义:

struct threadpool_t
{
    pthread_mutex_t struct_lock;    //该锁用于锁本结构体
    pthread_mutex_t thread_counter; //记录忙状态线程个数的锁
    pthread_cond_t queue_not_full;  //当任务队列满时,让添加任务的线程阻塞
    pthread_cond_t queue_not_empty; //任务队列里不为空时,通知等待任务的线程
    pthread_t *threads;             //存放线程池中每个线程的线程id
    pthread_t adjust_tid;       //存放管理线程的tid
    threadpool_task_t *task_queue;  //任务队列

    int min_thread_num;             //线程池中最小线程数
    int max_thread_num;             //线程池中最大线程数
    int live_thread_num;            //线程池中存活的线程数
    int busy_thread_num;            //线程池中处于忙碌状态的线程数
    int wait_exit_thread_num;       //线程池中即将销毁的线程数

    int queue_front;                //任务队列的队首下标
    int queue_rear;                 //任务队列的队尾下标
    int queue_size;                 //任务队列中实际的任务数
    int queue_max_size;             //任务队列的最大可容纳任务数

    int shutdown;                   //线程池的使用状态,为true则可用,为false则不可用
};

作用:
管理线程池

转载于:https://www.cnblogs.com/eemjwu/p/10588441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值