-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
144 lines (124 loc) · 2.91 KB
/
Copy paththreadpool.cpp
File metadata and controls
144 lines (124 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <pthread.h>
#include <memory>
#include <vector>
#include <functional>
#include <unordered_map>
#include <queue>
#include <pthread.h>
#include "mutexLock.hpp"
#include "timer.h"
#include "requestData.h"
#include "util.h"
#include "threadpool.h"
using namespace std;
pthread_mutex_t ThreadPool::lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ThreadPool::notify = PTHREAD_COND_INITIALIZER;
std::vector<pthread_t> ThreadPool::threads;
std::vector<ThreadTask> ThreadPool::queue;
int ThreadPool::thread_count = 0;
int ThreadPool::queue_size = 0;
int ThreadPool::head = 0;
int ThreadPool::tail = 0;
int ThreadPool::count = 0;
bool ThreadPool::shutdown = false;
int ThreadPool::start_num = 0;
int ThreadPool::threadpool_create(int _thread_num, int _queue_size)
{
if(_thread_num <= 0 || _thread_num > THREAD_MAX_NUM || _queue_size <= 0 || _queue_size > QUEUE_MAX_SIZE)
{
_thread_num = 4;
_queue_size = 1024;
}
thread_count = 0;
queue_size = _queue_size;
head = tail = count = 0;
shutdown = false;
start_num = 0;
threads.resize(_thread_num);
queue.resize(_queue_size);
for(int i = 0; i < _thread_num; ++i)
{
if(pthread_create(&threads[i], NULL, threadpool_main, (void*)(0)) != 0)
{
return -1;
}
++thread_count;
++start_num;
}
return 0;
}
int ThreadPool::threadpool_add(shared_ptr<void> args, function<void(shared_ptr<void>)> fun)
{
int next, err = 0;
if(pthread_mutex_lock(&lock) != 0)
return THREADPOOL_LOCK_FAILURE;
do
{
next = (tail + 1) % queue_size;
//queue is full
if(count == queue_size)
{
err = THREADPOOL_QUEUE_FULL;
break;
}
//webserver is close
if(shutdown)
{
err = THREADPOOL_SHUTDOWN;
break;
}
queue[tail].fun = fun;
queue[tail].args = args;
tail = next;
++count;
if(pthread_cond_signal(¬ify) != 0)
{
err = THREADPOOL_LOCK_FAILURE;
break;
}
}while(false);
if(pthread_mutex_unlock(&lock) != 0)
err = THREADPOOL_LOCK_FAILURE;
return err;
}
void* ThreadPool::threadpool_main(void *args)
{
while(true)
{
ThreadTask task;
pthread_mutex_lock(&lock);
while((count == 0) && (!shutdown))
{
pthread_cond_wait(¬ify, &lock);
}
if((shutdown == immediate_shutdown) ||
((shutdown == graceful_shutdown) && (count == 0)))
{
break;
}
task.fun = queue[head].fun;
task.args = queue[head].args;
queue[head].fun = NULL;
queue[head].args.reset();
head = (head + 1) % queue_size;
--count;
pthread_mutex_unlock(&lock);
(task.fun)(task.args);
}
pthread_mutex_lock(&lock);
--start_num;
pthread_mutex_unlock(&lock);
cout<<"This threadpool thread finished!"<<endl;
pthread_exit(NULL);
return NULL;
}
void myHandler(shared_ptr<void> req)
{
shared_ptr<RequestData> request = std::static_pointer_cast<RequestData>(req);
if(request->canWrite())
request->handleWrite();
else if(request->canRead())
request->handleRead();
request->handleConn();
}