threadpool.c 14 KB

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