threadpool.c 13 KB

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