ks_thread_pool.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2018 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #define TP_MAX_QLEN 1024
  24. typedef enum {
  25. TP_STATE_DOWN = 0,
  26. TP_STATE_RUNNING = 1
  27. } ks_thread_pool_state_t;
  28. struct ks_thread_pool_s {
  29. uint32_t min;
  30. uint32_t max;
  31. uint32_t idle_sec;
  32. size_t stack_size;
  33. ks_thread_priority_t priority;
  34. ks_q_t *q;
  35. uint32_t thread_count;
  36. uint32_t busy_thread_count;
  37. uint32_t running_thread_count;
  38. uint32_t dying_thread_count;
  39. ks_hash_t *thread_hash;
  40. ks_hash_t *thread_die_hash;
  41. ks_thread_pool_state_t state;
  42. ks_mutex_t *state_mutex;
  43. ks_mutex_t *mutex;
  44. };
  45. typedef struct ks_thread_job_s {
  46. ks_thread_function_t func;
  47. void *data;
  48. } ks_thread_job_t;
  49. static void *worker_thread(ks_thread_t *thread, void *data);
  50. static void cleanup_threads(ks_thread_pool_t *tp)
  51. {
  52. ks_hash_iterator_t *itt;
  53. ks_hash_write_lock(tp->thread_die_hash);
  54. for (itt = ks_hash_first(tp->thread_die_hash, KS_UNLOCKED); itt; ) {
  55. void *key;
  56. ks_hash_this(itt, (const void **)&key, NULL, NULL);
  57. ks_thread_join((ks_thread_t*)key);
  58. itt = ks_hash_next(&itt);
  59. ks_hash_remove(tp->thread_die_hash, key);
  60. ks_hash_write_lock(tp->thread_hash);
  61. ks_hash_remove(tp->thread_hash, key);
  62. ks_hash_write_unlock(tp->thread_hash);
  63. ks_thread_destroy((ks_thread_t**)&key);
  64. }
  65. ks_hash_write_unlock(tp->thread_die_hash);
  66. }
  67. static int check_queue(ks_thread_pool_t *tp, ks_bool_t adding)
  68. {
  69. ks_thread_t *thread;
  70. int need = 0;
  71. ks_mutex_lock(tp->mutex);
  72. if (tp->state != TP_STATE_RUNNING) {
  73. ks_mutex_unlock(tp->mutex);
  74. return 1;
  75. }
  76. cleanup_threads(tp);
  77. if (tp->thread_count < tp->min) {
  78. need = tp->min - tp->thread_count;
  79. }
  80. if (adding) {
  81. if (!need && tp->busy_thread_count + ks_q_size(tp->q) >= tp->running_thread_count - tp->dying_thread_count &&
  82. (tp->thread_count - tp->dying_thread_count + 1 <= tp->max)) {
  83. need++;
  84. }
  85. }
  86. tp->thread_count += need;
  87. ks_mutex_unlock(tp->mutex);
  88. while(need > 0) {
  89. /* To avoid a deadlock we protect thread_hash from being locked when state is changed to DOWN */
  90. ks_mutex_lock(tp->state_mutex);
  91. if (tp->state != TP_STATE_RUNNING) {
  92. /* Not going to spin-up the rest of the threads */
  93. ks_mutex_lock(tp->mutex);
  94. tp->thread_count -= need;
  95. ks_mutex_unlock(tp->mutex);
  96. ks_mutex_unlock(tp->state_mutex);
  97. return 0;
  98. }
  99. if (ks_thread_create_ex(&thread, worker_thread, tp, KS_THREAD_FLAG_DEFAULT, tp->stack_size, tp->priority, NULL) != KS_STATUS_SUCCESS) {
  100. ks_mutex_lock(tp->mutex);
  101. tp->thread_count--;
  102. ks_mutex_unlock(tp->mutex);
  103. } else {
  104. ks_hash_insert(tp->thread_hash, thread, NULL);
  105. }
  106. ks_mutex_unlock(tp->state_mutex);
  107. need--;
  108. }
  109. /*
  110. ks_log(KS_LOG_DEBUG, "WORKER check: adding %d need %d running %d dying %d total %d max %d\n",
  111. adding, need, tp->running_thread_count, tp->dying_thread_count, tp->thread_count, tp->max);
  112. */
  113. return need;
  114. }
  115. static uint32_t TID = 0;
  116. static void *worker_thread(ks_thread_t *thread, void *data)
  117. {
  118. ks_thread_pool_t *tp = (ks_thread_pool_t *) data;
  119. uint32_t idle_sec = 0;
  120. uint32_t my_id = 0;
  121. int die = 0;
  122. ks_mutex_lock(tp->mutex);
  123. tp->running_thread_count++;
  124. my_id = ++TID;
  125. ks_mutex_unlock(tp->mutex);
  126. while(tp->state == TP_STATE_RUNNING) {
  127. ks_thread_job_t *job;
  128. void *pop = NULL;
  129. ks_status_t status;
  130. status = ks_q_pop_timeout(tp->q, &pop, 100);
  131. if (status == KS_STATUS_BREAK) {
  132. if (tp->state != TP_STATE_RUNNING) {
  133. break;
  134. }
  135. continue;
  136. }
  137. /*
  138. ks_log(KS_LOG_DEBUG, "WORKER %d idle_sec %d/%d running %d dying %d total %d max %d\n",
  139. my_id, idle_sec, tp->idle_sec, tp->running_thread_count, tp->dying_thread_count, tp->thread_count, tp->max);
  140. */
  141. check_queue(tp, KS_FALSE);
  142. if (status == KS_STATUS_TIMEOUT) { // || status == KS_STATUS_BREAK) {
  143. idle_sec++;
  144. //printf("WTF %d/%d %d,%d,%d %d/%d\n", idle_sec / 10, tp->idle_sec,
  145. // tp->running_thread_count , tp->dying_thread_count , tp->busy_thread_count,
  146. // tp->running_thread_count - tp->dying_thread_count - tp->busy_thread_count, tp->min);
  147. if (idle_sec / 10 >= tp->idle_sec) {
  148. ks_mutex_lock(tp->mutex);
  149. if (tp->running_thread_count - tp->dying_thread_count - tp->busy_thread_count > 0 && tp->running_thread_count > tp->min) {
  150. tp->dying_thread_count++;
  151. die = 1;
  152. }
  153. ks_mutex_unlock(tp->mutex);
  154. if (die) {
  155. break;
  156. }
  157. }
  158. continue;
  159. }
  160. if ((status != KS_STATUS_SUCCESS && status != KS_STATUS_BREAK)) {
  161. ks_log(KS_LOG_ERROR, "WORKER %d POP FAIL %d %p\n", my_id, status, (void *)pop);
  162. break;
  163. }
  164. job = (ks_thread_job_t *) pop;
  165. ks_mutex_lock(tp->mutex);
  166. tp->busy_thread_count++;
  167. ks_mutex_unlock(tp->mutex);
  168. idle_sec = 0;
  169. job->func(thread, job->data);
  170. ks_pool_free(&job);
  171. ks_mutex_lock(tp->mutex);
  172. tp->busy_thread_count--;
  173. ks_mutex_unlock(tp->mutex);
  174. }
  175. ks_mutex_lock(tp->mutex);
  176. tp->running_thread_count--;
  177. tp->thread_count--;
  178. if (die) {
  179. tp->dying_thread_count--;
  180. }
  181. ks_hash_insert(tp->thread_die_hash, thread, NULL);
  182. ks_mutex_unlock(tp->mutex);
  183. return NULL;
  184. }
  185. KS_DECLARE(ks_status_t) ks_thread_pool_create(ks_thread_pool_t **tp, uint32_t min, uint32_t max, size_t stack_size,
  186. ks_thread_priority_t priority, uint32_t idle_sec)
  187. {
  188. ks_pool_t *pool = NULL;
  189. ks_pool_open(&pool);
  190. *tp = (ks_thread_pool_t *) ks_pool_alloc(pool, sizeof(ks_thread_pool_t));
  191. (*tp)->min = min;
  192. (*tp)->max = max;
  193. (*tp)->stack_size = stack_size;
  194. (*tp)->priority = priority;
  195. (*tp)->state = TP_STATE_RUNNING;
  196. (*tp)->idle_sec = idle_sec;
  197. ks_mutex_create(&(*tp)->mutex, KS_MUTEX_FLAG_DEFAULT, pool);
  198. ks_mutex_create(&(*tp)->state_mutex, KS_MUTEX_FLAG_DEFAULT, pool);
  199. ks_q_create(&(*tp)->q, pool, TP_MAX_QLEN);
  200. ks_hash_create(&(*tp)->thread_hash, KS_HASH_MODE_PTR, KS_HASH_FLAG_NONE, pool);
  201. ks_hash_create(&(*tp)->thread_die_hash, KS_HASH_MODE_PTR, KS_HASH_FLAG_NONE, pool);
  202. check_queue(*tp, KS_FALSE);
  203. return KS_STATUS_SUCCESS;
  204. }
  205. KS_DECLARE(ks_status_t) ks_thread_pool_destroy(ks_thread_pool_t **tp)
  206. {
  207. ks_pool_t *pool = NULL;
  208. ks_hash_iterator_t *itt;
  209. ks_assert(tp);
  210. /* To avoid a deadlock we do not allow check_queue() to lock thread_hash while state is changed to DOWN */
  211. ks_mutex_lock((*tp)->state_mutex);
  212. (*tp)->state = TP_STATE_DOWN;
  213. ks_mutex_unlock((*tp)->state_mutex);
  214. ks_hash_write_lock((*tp)->thread_hash);
  215. for (itt = ks_hash_first((*tp)->thread_hash, KS_UNLOCKED); itt; ) {
  216. void *key;
  217. ks_hash_this(itt, (const void **)&key, NULL, NULL);
  218. ks_thread_join((ks_thread_t*)key);
  219. itt = ks_hash_next(&itt);
  220. ks_hash_remove((*tp)->thread_hash, key);
  221. ks_thread_destroy((ks_thread_t**)&key);
  222. }
  223. ks_hash_write_unlock((*tp)->thread_hash);
  224. ks_hash_destroy(&(*tp)->thread_hash);
  225. ks_hash_destroy(&(*tp)->thread_die_hash);
  226. pool = ks_pool_get(*tp);
  227. ks_pool_close(&pool);
  228. return KS_STATUS_SUCCESS;
  229. }
  230. KS_DECLARE(ks_status_t) ks_thread_pool_add_job(ks_thread_pool_t *tp, ks_thread_function_t func, void *data)
  231. {
  232. ks_thread_job_t *job = (ks_thread_job_t *) ks_pool_alloc(ks_pool_get(tp), sizeof(*job));
  233. job->func = func;
  234. job->data = data;
  235. ks_q_push(tp->q, job);
  236. check_queue(tp, KS_TRUE);
  237. return KS_STATUS_SUCCESS;
  238. }
  239. KS_DECLARE(ks_size_t) ks_thread_pool_backlog(ks_thread_pool_t *tp)
  240. {
  241. return ks_q_size(tp->q);
  242. }