目录
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则不可用
};
作用:
管理线程池