epoll.h
#ifndef EVENTPOLL
#define EVENTPOLL
#include "requestData.h"
const int MAXEVENTS = 5000;
const int LISTENQ = 1024;
int epoll_init();
int epoll_add(int epoll_fd, int fd, void *request, __uint32_t events);
int epoll_mod(int epoll_fd, int fd, void *request, __uint32_t events);
int epoll_del(int epoll_fd, int fd, void *request, __uint32_t events);
int my_epoll_wait(int epoll_fd, struct epoll_event *events, int max_events, int timeout);
#endif
epoll.cpp 是具体的实现
初始化函数
struct epoll_event* events;
int epoll_init()
{
int epoll_fd = epoll_create(LISTENQ + 1);
if(epoll_fd == -1)
return -1;
//events = (struct epoll_event*)malloc(sizeof(struct epoll_event) * MAXEVENTS);
events = new epoll_event[MAXEVENTS];
return epoll_fd;
}
注册新的描述符
requet是一个事件回调函数的指针
// 注册新描述符
int epoll_add(int epoll_fd, int fd, void *request, __uint32_t events)
{
struct epoll_event event;
event.data.ptr = request;
event.events = events;
//printf("add to epoll %d\n", fd);
if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event) < 0)
{
perror("epoll_add error");
return -1;
}
return 0;
}
// 修改描述符状态
int epoll_mod(int epoll_fd, int fd, void *request, __uint32_t events)
{
struct epoll_event event;
event.data.ptr = request;
event.events = events;
if(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &event) < 0)
{
perror("epoll_mod error");
return -1;
}
return 0;
}
// 从epoll中删除描述符
int epoll_del(int epoll_fd, int fd, void *request, __uint32_t events)
{
struct epoll_event event;
event.data.ptr = request;
event.events = events;
if(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &event) < 0)
{
perror("epoll_del error");
return -1;
}
return 0;
}
thread.h
struct threadpool_t
{
pthread_mutex_t lock;
pthread_cond_t notify;
pthread_t *threads;
threadpool_task_t *queue;
int thread_count;
int queue_size;
int head;
int tail;
int count;
int shutdown;
int started;
};
threadpool_t *threadpool_create(int thread_count, int queue_size, int flags);
int threadpool_add(threadpool_t *pool, void (*function)(void *), void *argument, int flags);
int threadpool_destroy(threadpool_t *pool, int flags);
int threadpool_free(threadpool_t *pool);
static void *threadpool_thread(void *threadpool);
其中任务队列用数组实现
typedef struct {
void (*function)(void *);
void *argument;
} threadpool_task_t;
创建线程池
threadpool_t *threadpool_create(int thread_count, int queue_size, int flags)
{
threadpool_t *pool;
int i;
//(void) flags;
do
{
if(thread_count <= 0 || thread_count > MAX_THREADS || queue_size <= 0 || queue_size > MAX_QUEUE) {
return NULL;
}
if((pool = (threadpool_t *)malloc(sizeof(threadpool_t))) == NULL)
{
break;
}
/* Initialize */
pool->thread_count = 0;
pool->queue_size = queue_size;
pool->head = pool->tail = pool->count = 0;
pool->shutdown = pool->started = 0;
/* Allocate thread and task queue */
pool->threads = (pthread_t *)malloc(sizeof(pthread_t) * thread_count);
pool->queue = (threadpool_task_t *)malloc(sizeof(threadpool_task_t) * queue_size);
/* Initialize mutex and conditional variable first */
if((pthread_mutex_init(&(pool->lock), NULL) != 0) ||
(pthread_cond_init(&(pool->notify), NULL) != 0) ||
(pool->threads == NULL) ||
(pool->queue == NULL))
{
break;
}
/* Start worker threads */
for(i = 0; i < thread_count; i++) {
if(pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void*)pool) != 0)
{
threadpool_destroy(pool, 0);
return NULL;
}
pool->thread_count++;
pool->started++;
}
return pool;
} while(false);
if (pool != NULL)
{
threadpool_free(pool);
}
return NULL;
}
这里使用do while(false)语句是为了方便再出错的时候,直接使用break,跳出语句。
这里执行的像任务队列添加任务,注意加锁操作。
int threadpool_add(threadpool_t *pool, void (*function)(void *), void *argument, int flags)
{
//printf("add to thread pool !\n");
int err = 0;
int next;
//(void) flags;
if(pool == NULL || function == NULL)
{
return THREADPOOL_INVALID;
}
if(pthread_mutex_lock(&(pool->lock)) != 0)
{
return THREADPOOL_LOCK_FAILURE;
}
next = (pool->tail + 1) % pool->queue_size;
do
{
/* Are we full ? */
if(pool->count == pool->queue_size) {
err = THREADPOOL_QUEUE_FULL;
break;
}
/* Are we shutting down ? */
if(pool->shutdown) {
err = THREADPOOL_SHUTDOWN;
break;
}
/* Add task to queue */
pool->queue[pool->tail].function = function;
pool->queue[pool->tail].argument = argument;
pool->tail = next;
pool->count += 1;
/* pthread_cond_broadcast */
if(pthread_cond_signal(&(pool->notify)) != 0) {
err = THREADPOOL_LOCK_FAILURE;
break;
}
} while(false);
if(pthread_mutex_unlock(&pool->lock) != 0) {
err = THREADPOOL_LOCK_FAILURE;
}
return err;
}
这一个动作实现的是线程的删除操作。
int threadpool_destroy(threadpool_t *pool, int flags)
{
printf("Thread pool destroy !\n");
int i, err = 0;
if(pool == NULL)
{
return THREADPOOL_INVALID;
}
if(pthread_mutex_lock(&(pool->lock)) != 0)
{
return THREADPOOL_LOCK_FAILURE;
}
do
{
/* Already shutting down */
if(pool->shutdown) {
err = THREADPOOL_SHUTDOWN;
break;
}
pool->shutdown = (flags & THREADPOOL_GRACEFUL) ?
graceful_shutdown : immediate_shutdown;
/* Wake up all worker threads */
if((pthread_cond_broadcast(&(pool->notify)) != 0) ||
(pthread_mutex_unlock(&(pool->lock)) != 0)) {
err = THREADPOOL_LOCK_FAILURE;
break;
}
/* Join all worker thread */
for(i = 0; i < pool->thread_count; ++i)
{
if(pthread_join(pool->threads[i], NULL) != 0)
{
err = THREADPOOL_THREAD_FAILURE;
}
}
} while(false);
/* Only if everything went well do we deallocate the pool */
if(!err)
{
threadpool_free(pool);
}
return err;
}
下面这个函数用来释放线程的所有资源
int threadpool_free(threadpool_t *pool)
{
if(pool == NULL || pool->started > 0)
{
return -1;
}
/* Did we manage to allocate ? */
if(pool->threads)
{
free(pool->threads);
free(pool->queue);
/* Because we allocate pool->threads after initializing the
mutex and condition variable, we're sure they're
initialized. Let's lock the mutex just in case. */
pthread_mutex_lock(&(pool->lock));
pthread_mutex_destroy(&(pool->lock));
pthread_cond_destroy(&(pool->notify));
}
free(pool);
return 0;
}
条件变量,等待线程被唤醒
static void *threadpool_thread(void *threadpool)
{
threadpool_t *pool = (threadpool_t *)threadpool;
threadpool_task_t task;
for(;;)
{
/* Lock must be taken to wait on conditional variable */
pthread_mutex_lock(&(pool->lock));
/* Wait on condition variable, check for spurious wakeups.
When returning from pthread_cond_wait(), we own the lock. */
while((pool->count == 0) && (!pool->shutdown))
{
pthread_cond_wait(&(pool->notify), &(pool->lock));
}
if((pool->shutdown == immediate_shutdown) ||
((pool->shutdown == graceful_shutdown) &&
(pool->count == 0)))
{
break;
}
/* Grab our task */
task.function = pool->queue[pool->head].function;
task.argument = pool->queue[pool->head].argument;
pool->head = (pool->head + 1) % pool->queue_size;
pool->count -= 1;
/* Unlock */
pthread_mutex_unlock(&(pool->lock));
/* Get to work */
(*(task.function))(task.argument);
}
--pool->started;
pthread_mutex_unlock(&(pool->lock));
pthread_exit(NULL);
return(NULL);
}