File: apr_thread_pool.h

package info (click to toggle)
apr-util 1.6.1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,484 kB
  • sloc: ansic: 31,678; sh: 3,347; makefile: 222; perl: 154; xml: 36
file content (299 lines) | stat: -rw-r--r-- 11,104 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed
 * with this work for additional information regarding copyright
 * ownership.  The ASF licenses this file to you under the Apache
 * License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */

#ifndef APU_THREAD_POOL_H
#define APU_THREAD_POOL_H

#include "apu.h"
#include "apr_thread_proc.h"

/**
 * @file apr_thread_pool.h
 * @brief APR Thread Pool Library

 * @remarks This library implements a thread pool using apr_thread_t. A thread
 * pool is a set of threads that can be created in advance or on demand until a
 * maximum number. When a task is scheduled, the thread pool will find an idle
 * thread to handle the task. In case all existing threads are busy and the
 * number of tasks in the queue is higher than the adjustable threshold, the
 * pool will try to create a new thread to serve the task if the maximum number
 * has not been reached. Otherwise, the task will be put into a queue based on
 * priority, which can be valued from 0 to 255, with higher values being served
 * first. If there are tasks with the same priority, the new task might be put at
 * the top or at the bottom - it depends on which function is used to put the task.
 *
 * @remarks There may be the case where the thread pool can use up to the maximum
 * number of threads at peak load, but having those threads idle afterwards. A
 * maximum number of idle threads can be set so that the extra idling threads will
 * be terminated to save system resources.
 */
#if APR_HAS_THREADS

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @defgroup APR_Util_TP Thread Pool routines
 * @ingroup APR_Util
 * @{
 */

/** Opaque Thread Pool structure. */
typedef struct apr_thread_pool apr_thread_pool_t;

#define APR_THREAD_TASK_PRIORITY_LOWEST 0
#define APR_THREAD_TASK_PRIORITY_LOW 63
#define APR_THREAD_TASK_PRIORITY_NORMAL 127
#define APR_THREAD_TASK_PRIORITY_HIGH 191
#define APR_THREAD_TASK_PRIORITY_HIGHEST 255

/**
 * Create a thread pool
 * @param me The pointer in which to return the newly created apr_thread_pool
 * object, or NULL if thread pool creation fails.
 * @param init_threads The number of threads to be created initially, this number
 * will also be used as the initial value for the maximum number of idle threads.
 * @param max_threads The maximum number of threads that can be created
 * @param pool The pool to use
 * @return APR_SUCCESS if the thread pool was created successfully. Otherwise,
 * the error code.
 */
APU_DECLARE(apr_status_t) apr_thread_pool_create(apr_thread_pool_t **me,
                                                 apr_size_t init_threads,
                                                 apr_size_t max_threads,
                                                 apr_pool_t *pool);

/**
 * Destroy the thread pool and stop all the threads
 * @return APR_SUCCESS if all threads are stopped.
 */
APU_DECLARE(apr_status_t) apr_thread_pool_destroy(apr_thread_pool_t *me);

/**
 * Schedule a task to the bottom of the tasks of same priority.
 * @param me The thread pool
 * @param func The task function
 * @param param The parameter for the task function
 * @param priority The priority of the task.
 * @param owner Owner of this task.
 * @return APR_SUCCESS if the task had been scheduled successfully
 */
APU_DECLARE(apr_status_t) apr_thread_pool_push(apr_thread_pool_t *me,
                                               apr_thread_start_t func,
                                               void *param,
                                               apr_byte_t priority,
                                               void *owner);
/**
 * Schedule a task to be run after a delay
 * @param me The thread pool
 * @param func The task function
 * @param param The parameter for the task function
 * @param time Time in microseconds
 * @param owner Owner of this task.
 * @return APR_SUCCESS if the task had been scheduled successfully
 */
APU_DECLARE(apr_status_t) apr_thread_pool_schedule(apr_thread_pool_t *me,
                                                   apr_thread_start_t func,
                                                   void *param,
                                                   apr_interval_time_t time,
                                                   void *owner);

/**
 * Schedule a task to the top of the tasks of same priority.
 * @param me The thread pool
 * @param func The task function
 * @param param The parameter for the task function
 * @param priority The priority of the task.
 * @param owner Owner of this task.
 * @return APR_SUCCESS if the task had been scheduled successfully
 */
APU_DECLARE(apr_status_t) apr_thread_pool_top(apr_thread_pool_t *me,
                                              apr_thread_start_t func,
                                              void *param,
                                              apr_byte_t priority,
                                              void *owner);

/**
 * Cancel tasks submitted by the owner. If there is any task from the owner that
 * is currently running, the function will spin until the task finished.
 * @param me The thread pool
 * @param owner Owner of the task
 * @return APR_SUCCESS if the task has been cancelled successfully
 * @note The task function should not be calling cancel, otherwise the function
 * may get stuck forever. The function assert if it detect such a case.
 */
APU_DECLARE(apr_status_t) apr_thread_pool_tasks_cancel(apr_thread_pool_t *me,
                                                       void *owner);

/**
 * Get the current number of tasks waiting in the queue
 * @param me The thread pool
 * @return Number of tasks in the queue
 */
APU_DECLARE(apr_size_t) apr_thread_pool_tasks_count(apr_thread_pool_t *me);

/**
 * Get the current number of scheduled tasks waiting in the queue
 * @param me The thread pool
 * @return Number of scheduled tasks in the queue
 */
APU_DECLARE(apr_size_t) apr_thread_pool_scheduled_tasks_count(apr_thread_pool_t *me);

/**
 * Get the current number of threads
 * @param me The thread pool
 * @return Total number of threads
 */
APU_DECLARE(apr_size_t) apr_thread_pool_threads_count(apr_thread_pool_t *me);

/**
 * Get the current number of busy threads
 * @param me The thread pool
 * @return Number of busy threads
 */
APU_DECLARE(apr_size_t) apr_thread_pool_busy_count(apr_thread_pool_t *me);

/**
 * Get the current number of idle threads
 * @param me The thread pool
 * @return Number of idle threads
 */
APU_DECLARE(apr_size_t) apr_thread_pool_idle_count(apr_thread_pool_t *me);

/**
 * Access function for the maximum number of idle threads. Number of current
 * idle threads will be reduced to the new limit.
 * @param me The thread pool
 * @param cnt The number
 * @return The number of threads that were stopped.
 */
APU_DECLARE(apr_size_t) apr_thread_pool_idle_max_set(apr_thread_pool_t *me,
                                                     apr_size_t cnt);

/**
 * Get number of tasks that have run
 * @param me The thread pool
 * @return Number of tasks that have run
 */
APU_DECLARE(apr_size_t)
    apr_thread_pool_tasks_run_count(apr_thread_pool_t * me);

/**
 * Get high water mark of the number of tasks waiting to run
 * @param me The thread pool
 * @return High water mark of tasks waiting to run
 */
APU_DECLARE(apr_size_t)
    apr_thread_pool_tasks_high_count(apr_thread_pool_t * me);

/**
 * Get high water mark of the number of threads
 * @param me The thread pool
 * @return High water mark of threads in thread pool
 */
APU_DECLARE(apr_size_t)
    apr_thread_pool_threads_high_count(apr_thread_pool_t * me);

/**
 * Get the number of idle threads that were destroyed after timing out
 * @param me The thread pool
 * @return Number of idle threads that timed out
 */
APU_DECLARE(apr_size_t)
    apr_thread_pool_threads_idle_timeout_count(apr_thread_pool_t * me);

/**
 * Access function for the maximum number of idle threads
 * @param me The thread pool
 * @return The current maximum number
 */
APU_DECLARE(apr_size_t) apr_thread_pool_idle_max_get(apr_thread_pool_t *me);

/**
 * Access function for the maximum number of threads.
 * @param me The thread pool
 * @param cnt Number of threads
 * @return The original maximum number of threads
 */
APU_DECLARE(apr_size_t) apr_thread_pool_thread_max_set(apr_thread_pool_t *me,
                                                       apr_size_t cnt);

/**
 * Access function for the maximum wait time (in microseconds) of an
 * idling thread that exceeds the maximum number of idling threads.
 * A non-zero value allows for the reaping of idling threads to shrink
 * over time.  Which helps reduce thrashing.
 * @param me The thread pool
 * @param timeout The number of microseconds an idle thread should wait
 * till it reaps itself
 * @return The original maximum wait time
 */
APU_DECLARE(apr_interval_time_t)
    apr_thread_pool_idle_wait_set(apr_thread_pool_t * me,
                                  apr_interval_time_t timeout);

/**
 * Access function for the maximum wait time (in microseconds) of an
 * idling thread that exceeds the maximum number of idling threads
 * @param me The thread pool
 * @return The current maximum wait time
 */
APU_DECLARE(apr_interval_time_t)
    apr_thread_pool_idle_wait_get(apr_thread_pool_t * me);

/**
 * Access function for the maximum number of threads
 * @param me The thread pool
 * @return The current maximum number
 */
APU_DECLARE(apr_size_t) apr_thread_pool_thread_max_get(apr_thread_pool_t *me);

/**
 * Access function for the threshold of tasks in queue to trigger a new thread.
 * @param me The thread pool
 * @param cnt The new threshold
 * @return The original threshold
 */
APU_DECLARE(apr_size_t) apr_thread_pool_threshold_set(apr_thread_pool_t *me,
                                                      apr_size_t val);

/**
 * Access function for the threshold of tasks in queue to trigger a new thread.
 * @param me The thread pool
 * @return The current threshold
 */
APU_DECLARE(apr_size_t) apr_thread_pool_threshold_get(apr_thread_pool_t * me);

/**
 * Get owner of the task currently been executed by the thread.
 * @param thd The thread is executing a task
 * @param owner Pointer to receive owner of the task.
 * @return APR_SUCCESS if the owner is retrieved successfully
 */
APU_DECLARE(apr_status_t) apr_thread_pool_task_owner_get(apr_thread_t *thd,
                                                         void **owner);

/** @} */

#ifdef __cplusplus
}
#endif

#endif /* APR_HAS_THREADS */
#endif /* !APR_THREAD_POOL_H */