Repository navigation
Expand file tree
/
Copy paththreadpool.hpp
More file actions
360 lines (289 loc) · 7.75 KB
/
Copy paththreadpool.hpp
File metadata and controls
360 lines (289 loc) · 7.75 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __THREADPOOL_H__
#define __THREADPOOL_H__
// common include
// #define NDEBUG
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
#include <list>
#include <queue>
#include <algorithm>
#include "cstdkbe/cstdkbe.hpp"
#include "cstdkbe/tasks.hpp"
#include "helper/debug_helper.hpp"
#include "thread/threadtask.hpp"
// windows include
#if KBE_PLATFORM == PLATFORM_WIN32
#include <windows.h> // for HANDLE
#include <process.h> // for _beginthread()
#include "helper/crashhandler.hpp"
#else
// linux include
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <pthread.h>
#endif
namespace KBEngine{ namespace thread{
// 线程池活动线程大于这个数目则处于繁忙状态
#define THREAD_BUSY_SIZE 32
/*
线程池的线程基类
*/
class ThreadPool;
class TPThread
{
public:
friend class ThreadPool;
// 线程状态 -1还未启动, 0睡眠, 1繁忙中
enum THREAD_STATE
{
THREAD_STATE_STOP = -1,
THREAD_STATE_SLEEP = 0,
THREAD_STATE_BUSY = 1,
THREAD_STATE_END = 2
};
public:
TPThread(ThreadPool* threadPool, int threadWaitSecond = 0):
threadWaitSecond_(threadWaitSecond),
currTask_(NULL),
threadPool_(threadPool)
{
state_ = THREAD_STATE_SLEEP;
initCond();
initMutex();
}
virtual ~TPThread()
{
deleteCond();
deleteMutex();
DEBUG_MSG(boost::format("TPThread::~TPThread(): %1%\n") % this);
}
virtual void onStart(){}
virtual void onEnd(){}
virtual void onProcessTaskStart(TPTask* pTask){}
virtual void processTask(TPTask* pTask){ pTask->process(); }
virtual void onProcessTaskEnd(TPTask* pTask){}
INLINE THREAD_ID getID(void)const;
INLINE void setID(THREAD_ID tidp);
/**
创建一个线程, 并将自己与该线程绑定
*/
THREAD_ID createThread(void);
virtual void initCond(void)
{
THREAD_SINGNAL_INIT(cond_);
}
virtual void initMutex(void)
{
THREAD_MUTEX_INIT(mutex_);
}
virtual void deleteCond(void)
{
THREAD_SINGNAL_DELETE(cond_);
}
virtual void deleteMutex(void)
{
THREAD_MUTEX_DELETE(mutex_);
}
virtual void lock(void)
{
THREAD_MUTEX_LOCK(mutex_);
}
virtual void unlock(void)
{
THREAD_MUTEX_UNLOCK(mutex_);
}
virtual TPTask* tryGetTask(void);
/**
发送条件信号
*/
int sendCondSignal(void)
{
return THREAD_SINGNAL_SET(cond_);
}
/**
线程通知 等待条件信号
*/
bool onWaitCondSignal(void);
bool join(void);
/**
获取本线程要处理的任务
*/
INLINE TPTask* getTask(void)const;
/**
设置本线程要处理的任务
*/
INLINE void setTask(TPTask* tpt);
INLINE int getState(void)const;
/**
本线程要处理的任务已经处理完毕 我们决定删除这个废弃的任务
*/
void onTaskCompleted(void);
#if KBE_PLATFORM == PLATFORM_WIN32
static unsigned __stdcall threadFunc(void *arg);
#else
static void* threadFunc(void* arg);
#endif
/**
设置本线程要处理的任务
*/
INLINE ThreadPool* threadPool();
protected:
THREAD_SINGNAL cond_; // 线程信号量
THREAD_MUTEX mutex_; // 线程互诉体
int threadWaitSecond_; // 线程空闲状态超过这个秒数则线程退出, 小于0为永久线程 秒单位
TPTask * currTask_; // 该线程的当前执行的任务
THREAD_ID tidp_; // 本线程的ID
ThreadPool* threadPool_; // 线程池指针
THREAD_STATE state_; // 线程状态 -1还未启动, 0睡眠, 1繁忙中
};
class ThreadPool
{
public:
ThreadPool();
virtual ~ThreadPool();
void finalise();
virtual void onMainThreadTick();
bool hasThread(TPThread* pTPThread);
/**
获取当前线程总数
*/
INLINE uint32 getCurrentThreadCount(void)const;
/**
获取当前空闲线程总数
*/
INLINE uint32 getCurrentFreeThreadCount(void)const;
/**
创建线程池
@param inewThreadCount : 当系统繁忙时线程池会新增加这么多线程(临时)
@param inormalMaxThreadCount : 线程池会一直保持这么多个数的线程
@param imaxThreadCount : 线程池最多只能有这么多个线程
*/
bool createThreadPool(uint32 inewThreadCount,
uint32 inormalMaxThreadCount, uint32 imaxThreadCount);
/**
向线程池添加一个任务
*/
bool addTask(TPTask* tptask);
INLINE bool addBackgroundTask(TPTask* tptask){ return addTask(tptask); }
INLINE bool pushTask(TPTask* tptask){ return addTask(tptask); }
/**
线程数量是否到达最大个数
*/
INLINE bool isThreadCountMax(void)const;
/**
线程池是否处于繁忙状态
未处理任务是否非常多 说明线程很繁忙
*/
INLINE bool isBusy(void)const;
/**
线程池是否已经被初始化
*/
INLINE bool isInitialize(void)const;
/**
返回是否已经销毁
*/
INLINE bool isDestroyed()const;
/**
返回是否已经销毁
*/
INLINE void destroy();
/**
获得缓存的任务数量
*/
INLINE uint32 bufferTaskSize()const;
/**
获得缓存的任务
*/
INLINE std::queue<thread::TPTask*>& bufferedTaskList();
/**
操作缓存的任务锁
*/
INLINE void lockBufferedTaskList();
INLINE void unlockBufferedTaskList();
/**
获得已经完成的任务数量
*/
INLINE uint32 finiTaskSize()const;
public:
static int timeout;
/**
创建一个线程池线程
*/
virtual TPThread* createThread(int threadWaitSecond = ThreadPool::timeout);
/**
将某个任务保存到未处理列表
*/
void bufferTask(TPTask* tptask);
/**
从未处理列表取出一个任务 并从列表中删除
*/
TPTask* popbufferTask(void);
/**
移动一个线程到空闲列表
*/
bool addFreeThread(TPThread* tptd);
/**
移动一个线程到繁忙列表
*/
bool addBusyThread(TPThread* tptd);
/**
添加一个已经完成的任务到列表
*/
void addFiniTask(TPTask* tptask);
/**
删除一个挂起(超时)线程
*/
bool removeHangThread(TPThread* tptd);
bool initializeWatcher();
protected:
bool isInitialize_; // 线程池是否被初始化过
std::queue<TPTask*> bufferedTaskList_; // 系统处于繁忙时还未处理的任务列表
std::list<TPTask*> finiTaskList_; // 已经完成的任务列表
THREAD_MUTEX bufferedTaskList_mutex_; // 处理bufferTaskList互斥锁
THREAD_MUTEX threadStateList_mutex_; // 处理bufferTaskList and freeThreadList_互斥锁
THREAD_MUTEX finiTaskList_mutex_; // 处理finiTaskList互斥锁
std::list<TPThread*> busyThreadList_; // 繁忙的线程列表
std::list<TPThread*> freeThreadList_; // 闲置的线程列表
std::list<TPThread*> allThreadList_; // 所有的线程列表
uint32 maxThreadCount_; // 最大线程总数
uint32 extraNewAddThreadCount_; // 如果normalThreadCount_不足够使用则会新创建这么多线程
uint32 currentThreadCount_; // 当前线程数
uint32 currentFreeThreadCount_; // 当前闲置的线程数
uint32 normalThreadCount_; // 标准状态下的线程总数 即:默认情况下一启动服务器就开启这么多线程
// 如果线程不足够,则会新创建一些线程, 最大能够到maxThreadNum.
bool isDestroyed_;
};
}
}
#ifdef CODE_INLINE
#include "threadpool.ipp"
#endif
#endif