apr_thread_pool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed
  4. * with this work for additional information regarding copyright
  5. * ownership. The ASF licenses this file to you under the Apache
  6. * License, Version 2.0 (the "License"); you may not use this file
  7. * except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  15. * implied. See the License for the specific language governing
  16. * permissions and limitations under the License.
  17. */
  18. #ifndef APU_THREAD_POOL_H
  19. #define APU_THREAD_POOL_H
  20. #include "apu.h"
  21. #include "apr_thread_proc.h"
  22. /**
  23. * @file apr_thread_pool.h
  24. * @brief APR Thread Pool Library
  25. * @remarks This library implements a thread pool using apr_thread_t. A thread
  26. * pool is a set of threads that can be created in advance or on demand until a
  27. * maximum number. When a task is scheduled, the thread pool will find an idle
  28. * thread to handle the task. In case all existing threads are busy and the
  29. * number of tasks in the queue is higher than the adjustable threshold, the
  30. * pool will try to create a new thread to serve the task if the maximum number
  31. * has not been reached. Otherwise, the task will be put into a queue based on
  32. * priority, which can be valued from 0 to 255, with higher values being served
  33. * first. If there are tasks with the same priority, the new task might be put at
  34. * the top or at the bottom - it depends on which function is used to put the task.
  35. *
  36. * @remarks There may be the case where the thread pool can use up to the maximum
  37. * number of threads at peak load, but having those threads idle afterwards. A
  38. * maximum number of idle threads can be set so that the extra idling threads will
  39. * be terminated to save system resources.
  40. */
  41. #if APR_HAS_THREADS
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif /* __cplusplus */
  45. /**
  46. * @defgroup APR_Util_TP Thread Pool routines
  47. * @ingroup APR_Util
  48. * @{
  49. */
  50. /** Opaque Thread Pool structure. */
  51. typedef struct apr_thread_pool apr_thread_pool_t;
  52. #define APR_THREAD_TASK_PRIORITY_LOWEST 0
  53. #define APR_THREAD_TASK_PRIORITY_LOW 63
  54. #define APR_THREAD_TASK_PRIORITY_NORMAL 127
  55. #define APR_THREAD_TASK_PRIORITY_HIGH 191
  56. #define APR_THREAD_TASK_PRIORITY_HIGHEST 255
  57. /**
  58. * Create a thread pool
  59. * @param me The pointer in which to return the newly created apr_thread_pool
  60. * object, or NULL if thread pool creation fails.
  61. * @param init_threads The number of threads to be created initially, this number
  62. * will also be used as the initial value for the maximum number of idle threads.
  63. * @param max_threads The maximum number of threads that can be created
  64. * @param pool The pool to use
  65. * @return APR_SUCCESS if the thread pool was created successfully. Otherwise,
  66. * the error code.
  67. */
  68. APU_DECLARE(apr_status_t) apr_thread_pool_create(apr_thread_pool_t **me,
  69. apr_size_t init_threads,
  70. apr_size_t max_threads,
  71. apr_pool_t *pool);
  72. /**
  73. * Destroy the thread pool and stop all the threads
  74. * @return APR_SUCCESS if all threads are stopped.
  75. */
  76. APU_DECLARE(apr_status_t) apr_thread_pool_destroy(apr_thread_pool_t *me);
  77. /**
  78. * Schedule a task to the bottom of the tasks of same priority.
  79. * @param me The thread pool
  80. * @param func The task function
  81. * @param param The parameter for the task function
  82. * @param priority The priority of the task.
  83. * @param owner Owner of this task.
  84. * @return APR_SUCCESS if the task had been scheduled successfully
  85. */
  86. APU_DECLARE(apr_status_t) apr_thread_pool_push(apr_thread_pool_t *me,
  87. apr_thread_start_t func,
  88. void *param,
  89. apr_byte_t priority,
  90. void *owner);
  91. /**
  92. * Schedule a task to be run after a delay
  93. * @param me The thread pool
  94. * @param func The task function
  95. * @param param The parameter for the task function
  96. * @param time Time in microseconds
  97. * @param owner Owner of this task.
  98. * @return APR_SUCCESS if the task had been scheduled successfully
  99. */
  100. APU_DECLARE(apr_status_t) apr_thread_pool_schedule(apr_thread_pool_t *me,
  101. apr_thread_start_t func,
  102. void *param,
  103. apr_interval_time_t time,
  104. void *owner);
  105. /**
  106. * Schedule a task to the top of the tasks of same priority.
  107. * @param me The thread pool
  108. * @param func The task function
  109. * @param param The parameter for the task function
  110. * @param priority The priority of the task.
  111. * @param owner Owner of this task.
  112. * @return APR_SUCCESS if the task had been scheduled successfully
  113. */
  114. APU_DECLARE(apr_status_t) apr_thread_pool_top(apr_thread_pool_t *me,
  115. apr_thread_start_t func,
  116. void *param,
  117. apr_byte_t priority,
  118. void *owner);
  119. /**
  120. * Cancel tasks submitted by the owner. If there is any task from the owner that
  121. * is currently running, the function will spin until the task finished.
  122. * @param me The thread pool
  123. * @param owner Owner of the task
  124. * @return APR_SUCCESS if the task has been cancelled successfully
  125. * @note The task function should not be calling cancel, otherwise the function
  126. * may get stuck forever. The function assert if it detect such a case.
  127. */
  128. APU_DECLARE(apr_status_t) apr_thread_pool_tasks_cancel(apr_thread_pool_t *me,
  129. void *owner);
  130. /**
  131. * Get the current number of tasks waiting in the queue
  132. * @param me The thread pool
  133. * @return Number of tasks in the queue
  134. */
  135. APU_DECLARE(apr_size_t) apr_thread_pool_tasks_count(apr_thread_pool_t *me);
  136. /**
  137. * Get the current number of scheduled tasks waiting in the queue
  138. * @param me The thread pool
  139. * @return Number of scheduled tasks in the queue
  140. */
  141. APU_DECLARE(apr_size_t) apr_thread_pool_scheduled_tasks_count(apr_thread_pool_t *me);
  142. /**
  143. * Get the current number of threads
  144. * @param me The thread pool
  145. * @return Total number of threads
  146. */
  147. APU_DECLARE(apr_size_t) apr_thread_pool_threads_count(apr_thread_pool_t *me);
  148. /**
  149. * Get the current number of busy threads
  150. * @param me The thread pool
  151. * @return Number of busy threads
  152. */
  153. APU_DECLARE(apr_size_t) apr_thread_pool_busy_count(apr_thread_pool_t *me);
  154. /**
  155. * Get the current number of idle threads
  156. * @param me The thread pool
  157. * @return Number of idle threads
  158. */
  159. APU_DECLARE(apr_size_t) apr_thread_pool_idle_count(apr_thread_pool_t *me);
  160. /**
  161. * Access function for the maximum number of idle threads. Number of current
  162. * idle threads will be reduced to the new limit.
  163. * @param me The thread pool
  164. * @param cnt The number
  165. * @return The number of threads that were stopped.
  166. */
  167. APU_DECLARE(apr_size_t) apr_thread_pool_idle_max_set(apr_thread_pool_t *me,
  168. apr_size_t cnt);
  169. /**
  170. * Get number of tasks that have run
  171. * @param me The thread pool
  172. * @return Number of tasks that have run
  173. */
  174. APU_DECLARE(apr_size_t)
  175. apr_thread_pool_tasks_run_count(apr_thread_pool_t * me);
  176. /**
  177. * Get high water mark of the number of tasks waiting to run
  178. * @param me The thread pool
  179. * @return High water mark of tasks waiting to run
  180. */
  181. APU_DECLARE(apr_size_t)
  182. apr_thread_pool_tasks_high_count(apr_thread_pool_t * me);
  183. /**
  184. * Get high water mark of the number of threads
  185. * @param me The thread pool
  186. * @return High water mark of threads in thread pool
  187. */
  188. APU_DECLARE(apr_size_t)
  189. apr_thread_pool_threads_high_count(apr_thread_pool_t * me);
  190. /**
  191. * Get the number of idle threads that were destroyed after timing out
  192. * @param me The thread pool
  193. * @return Number of idle threads that timed out
  194. */
  195. APU_DECLARE(apr_size_t)
  196. apr_thread_pool_threads_idle_timeout_count(apr_thread_pool_t * me);
  197. /**
  198. * Access function for the maximum number of idle threads
  199. * @param me The thread pool
  200. * @return The current maximum number
  201. */
  202. APU_DECLARE(apr_size_t) apr_thread_pool_idle_max_get(apr_thread_pool_t *me);
  203. /**
  204. * Access function for the maximum number of threads.
  205. * @param me The thread pool
  206. * @param cnt Number of threads
  207. * @return The original maximum number of threads
  208. */
  209. APU_DECLARE(apr_size_t) apr_thread_pool_thread_max_set(apr_thread_pool_t *me,
  210. apr_size_t cnt);
  211. /**
  212. * Access function for the maximum wait time (in microseconds) of an
  213. * idling thread that exceeds the maximum number of idling threads.
  214. * A non-zero value allows for the reaping of idling threads to shrink
  215. * over time. Which helps reduce thrashing.
  216. * @param me The thread pool
  217. * @param timeout The number of microseconds an idle thread should wait
  218. * till it reaps itself
  219. * @return The original maximum wait time
  220. */
  221. APU_DECLARE(apr_interval_time_t)
  222. apr_thread_pool_idle_wait_set(apr_thread_pool_t * me,
  223. apr_interval_time_t timeout);
  224. /**
  225. * Access function for the maximum wait time (in microseconds) of an
  226. * idling thread that exceeds the maximum number of idling threads
  227. * @param me The thread pool
  228. * @return The current maximum wait time
  229. */
  230. APU_DECLARE(apr_interval_time_t)
  231. apr_thread_pool_idle_wait_get(apr_thread_pool_t * me);
  232. /**
  233. * Access function for the maximum number of threads
  234. * @param me The thread pool
  235. * @return The current maximum number
  236. */
  237. APU_DECLARE(apr_size_t) apr_thread_pool_thread_max_get(apr_thread_pool_t *me);
  238. /**
  239. * Access function for the threshold of tasks in queue to trigger a new thread.
  240. * @param me The thread pool
  241. * @param cnt The new threshold
  242. * @return The original threshold
  243. */
  244. APU_DECLARE(apr_size_t) apr_thread_pool_threshold_set(apr_thread_pool_t *me,
  245. apr_size_t val);
  246. /**
  247. * Access function for the threshold of tasks in queue to trigger a new thread.
  248. * @param me The thread pool
  249. * @return The current threshold
  250. */
  251. APU_DECLARE(apr_size_t) apr_thread_pool_threshold_get(apr_thread_pool_t * me);
  252. /**
  253. * Get owner of the task currently been executed by the thread.
  254. * @param thd The thread is executing a task
  255. * @param owner Pointer to receive owner of the task.
  256. * @return APR_SUCCESS if the owner is retrieved successfully
  257. */
  258. APU_DECLARE(apr_status_t) apr_thread_pool_task_owner_get(apr_thread_t *thd,
  259. void **owner);
  260. /** @} */
  261. #ifdef __cplusplus
  262. }
  263. #endif
  264. #endif /* APR_HAS_THREADS */
  265. #endif /* !APR_THREAD_POOL_H */