threadpool.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include "precompile.h"
  2. #include "threadpool.h"
  3. #include "list.h"
  4. #include "spinlock.h"
  5. #include "refcnt.h"
  6. #include "modCheck.h"
  7. struct strand_task_entry {
  8. struct list_head entry;
  9. strand_t *strand;
  10. threadpool_workitem_proc f;
  11. threadpool_workitem_proc2 f2;
  12. void *arg;
  13. int param1;
  14. int param2;
  15. };
  16. typedef struct strand_task_entry strand_task_entry;
  17. struct strand_t {
  18. struct list_head entry_list;
  19. int working;
  20. spinlock_t lock;
  21. DECLARE_REF_COUNT_MEMBER(ref_cnt);
  22. };
  23. static strand_task_entry *make_task_entry(strand_t *strand, threadpool_workitem_proc f, threadpool_workitem_proc2 f2, void *arg, int param1, int param2)
  24. {
  25. strand_task_entry *task_entry = MALLOC_T(strand_task_entry);
  26. task_entry->strand = strand;
  27. task_entry->f = f;
  28. task_entry->f2 = f2;
  29. task_entry->arg = arg;
  30. task_entry->param1 = param1;
  31. task_entry->param2 = param2;
  32. if (strand)
  33. strand_inc_ref(strand);
  34. return task_entry;
  35. }
  36. static void destroy_task_entry(strand_task_entry *task_entry)
  37. {
  38. if (task_entry->strand)
  39. strand_dec_ref(task_entry->strand);
  40. free(task_entry);
  41. }
  42. TOOLKIT_API strand_t *strand_create()
  43. {
  44. strand_t *strand = MALLOC_T(strand_t);
  45. INIT_LIST_HEAD(&strand->entry_list);
  46. REF_COUNT_INIT(&strand->ref_cnt);
  47. spinlock_init(&strand->lock);
  48. strand->working = 0;
  49. return strand;
  50. }
  51. TOOLKIT_API void strand_lock(strand_t *strand)
  52. {
  53. spinlock_enter(&strand->lock, 0);
  54. }
  55. TOOLKIT_API void strand_unlock(strand_t *strand)
  56. {
  57. spinlock_leave(&strand->lock);
  58. }
  59. static void __strand_destroy(strand_t *strand)
  60. {
  61. assert(list_empty(&strand->entry_list));
  62. free(strand);
  63. }
  64. IMPLEMENT_REF_COUNT_MT(strand, strand_t, ref_cnt, __strand_destroy)
  65. static void sp_strand_enqueue(strand_t *strand, strand_task_entry *task_entry)
  66. {
  67. list_add_tail(&task_entry->entry, &strand->entry_list);
  68. }
  69. static strand_task_entry *sp_strand_dequeue(strand_t *strand)
  70. {
  71. strand_task_entry *task_entry = NULL;
  72. if (!list_empty(&strand->entry_list)) {
  73. task_entry = list_first_entry(&strand->entry_list, strand_task_entry, entry);
  74. list_del(&task_entry->entry);
  75. }
  76. return task_entry;
  77. }
  78. static void sp_strand_work(void *threadpool, strand_task_entry *task_entry)
  79. {
  80. strand_t *strand = task_entry->strand;
  81. if (strand) {
  82. strand_inc_ref(strand);
  83. strand_lock(strand);
  84. if (strand->working == 0) {
  85. strand->working = 1;
  86. while (task_entry) {
  87. strand_unlock(strand);
  88. if (task_entry->f) {
  89. task_entry->f(threadpool, task_entry->arg);
  90. } else if (task_entry->f2) {
  91. task_entry->f2(threadpool, task_entry->arg, task_entry->param1, task_entry->param2);
  92. }
  93. destroy_task_entry(task_entry);
  94. strand_lock(strand);
  95. task_entry = sp_strand_dequeue(strand);
  96. }
  97. strand->working = 0;
  98. } else {
  99. sp_strand_enqueue(strand, task_entry);
  100. }
  101. strand_unlock(strand);
  102. strand_dec_ref(strand);
  103. } else {
  104. if (task_entry->f) {
  105. task_entry->f(threadpool, task_entry->arg);
  106. } else if (task_entry->f2) {
  107. task_entry->f2(threadpool, task_entry->arg, task_entry->param1, task_entry->param2);
  108. }
  109. destroy_task_entry(task_entry);
  110. }
  111. }
  112. struct threadpool_t
  113. {
  114. LONG lstop;
  115. DWORD tmp_thread_ttl; // thread max free time that can hold, in millisecond
  116. DWORD num_fix_thread; // fixed threads in the pool
  117. DWORD max_tmp_thread; // max threads in the pool
  118. DWORD stack_size;
  119. volatile DWORD curr_fix_threads; // current fixed thread in the pool
  120. volatile DWORD curr_tmp_threads; // current active temp thread count
  121. HANDLE workitem_sem;
  122. volatile DWORD freethread_count;
  123. //HANDLE freethread_sem;
  124. CRITICAL_SECTION lock;
  125. threadpool_decorator_callback decorator_init_cb;
  126. threadpool_decorator_callback decorator_term_cb;
  127. void *decorator_cb_user_data;
  128. void *user_data;
  129. struct list_head workitem_list;
  130. log_func log;
  131. };
  132. static void threadpool_lock(threadpool_t *threadpool)
  133. {
  134. EnterCriticalSection(&threadpool->lock);
  135. }
  136. static void threadpool_unlock(threadpool_t *threadpool)
  137. {
  138. LeaveCriticalSection(&threadpool->lock);
  139. }
  140. static void __threadpool_log(threadpool_t *threadpool, const char *fmt, va_list arg)
  141. {
  142. int n;
  143. n = _vscprintf(fmt, arg);
  144. if (n > 0) {
  145. char *buf = _alloca(n+1);
  146. vsprintf(buf, fmt, arg);
  147. threadpool->log(threadpool, buf);
  148. }
  149. }
  150. static void threadpool_log(threadpool_t *threadpool, const char *fmt, ...)
  151. {
  152. if (threadpool->log) {
  153. va_list arg;
  154. va_start(arg, fmt);
  155. __threadpool_log(threadpool, fmt, arg);
  156. va_end(arg);
  157. }
  158. }
  159. static void enqueue_workitem(threadpool_t *threadpool, strand_task_entry *task_entry, int fifo) //add a workItem
  160. {
  161. threadpool_lock(threadpool);
  162. if (fifo) {
  163. list_add_tail(&task_entry->entry, &threadpool->workitem_list);
  164. } else {
  165. list_add(&task_entry->entry, &threadpool->workitem_list);
  166. }
  167. threadpool_unlock(threadpool);
  168. }
  169. static strand_task_entry* dequeue_workitem(threadpool_t *threadpool) //remove a workItem
  170. {
  171. strand_task_entry *task_entry;
  172. threadpool_lock(threadpool);
  173. task_entry = list_first_entry(&threadpool->workitem_list, strand_task_entry, entry); //»ñÈ¡µÚÒ»¸ö¹¤×÷Ïî
  174. list_del(&task_entry->entry);
  175. threadpool_unlock(threadpool);
  176. return task_entry;
  177. }
  178. static unsigned int __stdcall fix_thread_proc(void *param)
  179. {
  180. threadpool_t *threadpool = (threadpool_t *)param;
  181. toolkit_setThreadGroupByAssign(threadpool);
  182. if (threadpool->decorator_init_cb)
  183. threadpool->decorator_init_cb(threadpool, threadpool->decorator_cb_user_data);
  184. InterlockedIncrement(&threadpool->freethread_count);
  185. while (!threadpool->lstop)
  186. {
  187. DWORD ret = WaitForSingleObject(threadpool->workitem_sem, threadpool->lstop ? 0 : INFINITE);
  188. if (ret == WAIT_OBJECT_0)
  189. {
  190. InterlockedDecrement(&threadpool->freethread_count);
  191. {
  192. strand_task_entry *task_entry = dequeue_workitem(threadpool);
  193. sp_strand_work(threadpool, task_entry);
  194. }
  195. InterlockedIncrement(&threadpool->freethread_count);
  196. }
  197. else if (ret == WAIT_TIMEOUT)
  198. {
  199. break;
  200. }
  201. else
  202. {
  203. assert(0);
  204. }
  205. }
  206. InterlockedDecrement(&threadpool->freethread_count);
  207. InterlockedDecrement((LONG*)&threadpool->curr_fix_threads);
  208. if (threadpool->decorator_term_cb)
  209. threadpool->decorator_term_cb(threadpool, threadpool->decorator_cb_user_data);
  210. return 0;
  211. }
  212. static unsigned int __stdcall tmp_thread_proc(void *param)
  213. {
  214. threadpool_t *threadpool = (threadpool_t *)param;
  215. //threadpool_log(threadpool, "tmp_thread_proc enter id(%d)", GetCurrentThreadId());
  216. toolkit_setThreadGroupByAssign(threadpool);
  217. if (threadpool->decorator_init_cb)
  218. threadpool->decorator_init_cb(threadpool, threadpool->decorator_cb_user_data);
  219. InterlockedIncrement(&threadpool->freethread_count);
  220. while (!threadpool->lstop)
  221. {
  222. DWORD ret = WaitForSingleObject(threadpool->workitem_sem, threadpool->lstop ? 0 : threadpool->tmp_thread_ttl);
  223. if (ret == WAIT_OBJECT_0)
  224. {
  225. InterlockedDecrement(&threadpool->freethread_count);
  226. {
  227. strand_task_entry *task_entry = dequeue_workitem(threadpool);
  228. //threadpool_log(threadpool, "get a task entry and exec! id(%d)", GetCurrentThreadId());
  229. sp_strand_work(threadpool, task_entry);
  230. }
  231. InterlockedIncrement(&threadpool->freethread_count);
  232. }
  233. else if (ret == WAIT_TIMEOUT)
  234. {
  235. threadpool_log(threadpool, "tmp thread ttl detected, begin exit!");
  236. break;
  237. }
  238. else
  239. {
  240. assert(0);
  241. }
  242. }
  243. InterlockedDecrement(&threadpool->freethread_count);
  244. InterlockedDecrement((LONG*)&threadpool->curr_tmp_threads);
  245. if (threadpool->decorator_term_cb)
  246. threadpool->decorator_term_cb(threadpool, threadpool->decorator_cb_user_data);
  247. threadpool_log(threadpool, "tmp_thread_proc leave! id(%d)", GetCurrentThreadId());
  248. return 0;
  249. }
  250. TOOLKIT_API int threadpool_create(threadpool_t **p_threadpool)
  251. {
  252. threadpool_t *threadpool = ZALLOC_T(threadpool_t);
  253. InitializeCriticalSection(&threadpool->lock);
  254. *p_threadpool = threadpool;
  255. return 0;
  256. }
  257. TOOLKIT_API int threadpool_destroy(threadpool_t *threadpool)
  258. {
  259. DeleteCriticalSection(&threadpool->lock);
  260. free(threadpool);
  261. return 0;
  262. }
  263. TOOLKIT_API int threadpool_set_thread_decorator(threadpool_t *threadpool,
  264. threadpool_decorator_callback dc_init,
  265. threadpool_decorator_callback dc_term,
  266. void *user_data)
  267. {
  268. threadpool->decorator_init_cb = dc_init;
  269. threadpool->decorator_term_cb = dc_term;
  270. threadpool->decorator_cb_user_data = user_data;
  271. return 0;
  272. }
  273. TOOLKIT_API int threadpool_start(threadpool_t *threadpool,
  274. int num_fix_thread,
  275. int max_tmp_thread,
  276. int tmp_thread_ttl,
  277. int stack_size)
  278. {
  279. //assert(threadpool->freethread_sem == NULL);
  280. assert(threadpool->workitem_sem == NULL);
  281. threadpool->lstop = 0;
  282. threadpool->num_fix_thread = num_fix_thread;
  283. threadpool->max_tmp_thread = max_tmp_thread;
  284. threadpool->stack_size = stack_size;
  285. if (num_fix_thread == 0 && max_tmp_thread == 0)
  286. threadpool->max_tmp_thread = 1;
  287. threadpool->tmp_thread_ttl = tmp_thread_ttl;
  288. INIT_LIST_HEAD(&threadpool->workitem_list);
  289. threadpool->curr_fix_threads = 0;
  290. threadpool->curr_tmp_threads = 0;
  291. /*threadpool->freethread_sem = CreateSemaphoreA(NULL, 0, 0x7fffffff, NULL);
  292. if (!threadpool->freethread_sem)
  293. return -1;*/
  294. threadpool->workitem_sem = CreateSemaphoreA(NULL, 0, 0x7fffffff, NULL);
  295. for (threadpool->curr_fix_threads = 0; threadpool->curr_fix_threads < threadpool->num_fix_thread; ++threadpool->curr_fix_threads) {
  296. DWORD dwCreationFlags = threadpool->stack_size ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0;
  297. HANDLE hThread = NULL;
  298. toolkit_setAssign(threadpool);
  299. hThread = (HANDLE)_beginthreadex(NULL, threadpool->stack_size, &fix_thread_proc, threadpool, dwCreationFlags, NULL);
  300. if (hThread) {
  301. CloseHandle(hThread);
  302. } else {
  303. return -1;
  304. }
  305. }
  306. return 0;
  307. }
  308. TOOLKIT_API int threadpool_stop(threadpool_t *threadpool)
  309. {
  310. DWORD i, t;
  311. InterlockedExchange((LONG*)&threadpool->lstop, 1);
  312. t = threadpool->curr_tmp_threads + threadpool->curr_fix_threads;
  313. for (i = 0; i < t; ++i) {
  314. strand_task_entry *task_entry = make_task_entry(NULL, NULL, NULL, NULL, 0, 0); // use empty item to awake blocked thread to exit
  315. enqueue_workitem(threadpool, task_entry, 1);
  316. ReleaseSemaphore(threadpool->workitem_sem, 1, NULL);
  317. }
  318. while (threadpool->curr_tmp_threads + threadpool->curr_fix_threads)
  319. Sleep(1);
  320. /* to see that any pending pWorkItem, so need to delete them(these items all are empty item. why?) */
  321. while (WaitForSingleObject(threadpool->workitem_sem, 0) == WAIT_OBJECT_0) { //destory all the workItem
  322. strand_task_entry *task_entry = dequeue_workitem(threadpool);
  323. destroy_task_entry(task_entry);
  324. }
  325. return 0;
  326. }
  327. static int __threadpool_queue_workitem(threadpool_t *threadpool,
  328. strand_t *strand,
  329. threadpool_workitem_proc workitem,
  330. threadpool_workitem_proc2 workitem2,
  331. void *arg,
  332. int param1,
  333. int param2,
  334. int fifo)
  335. {
  336. strand_task_entry *task_entry = NULL;
  337. DWORD ret;
  338. if (!workitem && !workitem2)
  339. return -1;
  340. if (threadpool->lstop)
  341. return -1;
  342. //threadpool_log(threadpool, "__threadpool_queue_workitem fifo:%d", fifo);
  343. task_entry = make_task_entry(strand, workitem, workitem2, arg, param1, param2);
  344. enqueue_workitem(threadpool, task_entry, fifo);
  345. ReleaseSemaphore(threadpool->workitem_sem, 1, NULL);
  346. if (threadpool->freethread_count >0)
  347. {
  348. //threadpool_log(threadpool, "get free thread to exec");
  349. }
  350. else if (threadpool->curr_tmp_threads < threadpool->max_tmp_thread)
  351. {
  352. HANDLE hThread = NULL;
  353. toolkit_setAssign(threadpool);
  354. hThread = (HANDLE)_beginthreadex(NULL, 0, &tmp_thread_proc, threadpool, 0, NULL);
  355. if (hThread)
  356. {
  357. //threadpool_log(threadpool, "alloc a new thread ok!");
  358. CloseHandle(hThread);
  359. InterlockedIncrement(&threadpool->curr_tmp_threads);
  360. }
  361. else
  362. {
  363. threadpool_log(threadpool, "alloc a new thread failed, errno: %d, last error: %d", errno, GetLastError());
  364. }
  365. }
  366. return 0;
  367. }
  368. TOOLKIT_API int threadpool_queue_workitem(threadpool_t *threadpool,
  369. strand_t *strand,
  370. threadpool_workitem_proc workitem,
  371. void *arg)
  372. {
  373. return __threadpool_queue_workitem(threadpool, strand, workitem, NULL, arg, 0, 0, 1);
  374. }
  375. TOOLKIT_API int threadpool_queue_workitem2(threadpool_t *threadpool,
  376. strand_t *strand,
  377. threadpool_workitem_proc2 workitem,
  378. void *arg,
  379. int param1,
  380. int param2)
  381. {
  382. return __threadpool_queue_workitem(threadpool, strand, NULL, workitem, arg, param1, param2, 1);
  383. }
  384. TOOLKIT_API int threadpool_post_workitem_lifo(threadpool_t *threadpool,
  385. strand_t *strand,
  386. threadpool_workitem_proc workitem,
  387. void *arg)
  388. {
  389. return __threadpool_queue_workitem(threadpool, strand, workitem, NULL, arg, 0, 0, 0);
  390. }
  391. TOOLKIT_API int threadpool_post_workitem_lifo2(threadpool_t *threadpool,
  392. strand_t *strand,
  393. threadpool_workitem_proc2 workitem,
  394. void *arg,
  395. int param1,
  396. int param2)
  397. {
  398. return __threadpool_queue_workitem(threadpool, strand, NULL, workitem, arg, param1, param2, 0);
  399. }
  400. TOOLKIT_API void threadpool_set_user_data(threadpool_t *threadpool, void *user_data)
  401. {
  402. threadpool->user_data = user_data;
  403. }
  404. TOOLKIT_API void *threadpool_get_user_data(threadpool_t *threadpool)
  405. {
  406. return threadpool->user_data;
  407. }
  408. TOOLKIT_API void threadpool_set_log(threadpool_t *threadpool, log_func func)
  409. {
  410. threadpool->log = func;
  411. }