Asterisk - The Open Source Telephony Project GIT-master-ff80666
threadpool.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012-2013, Digium, Inc.
5 *
6 * Mark Michelson <mmmichelson@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19
20#ifndef _ASTERISK_THREADPOOL_H
21#define _ASTERISK_THREADPOOL_H
22
23struct ast_threadpool;
26
28
30 /*!
31 * \brief Indicates that the state of threads in the pool has changed
32 *
33 * \param pool The pool whose state has changed
34 * \param listener The threadpool listener
35 * \param active_threads The number of active threads in the pool
36 * \param idle_threads The number of idle threads in the pool
37 */
38 void (*state_changed)(struct ast_threadpool *pool,
40 int active_threads,
41 int idle_threads);
42 /*!
43 * \brief Indicates that a task was pushed to the threadpool
44 *
45 * \param pool The pool that had a task pushed
46 * \param listener The threadpool listener
47 * \param was_empty Indicates whether there were any tasks prior to adding the new one.
48 */
49 void (*task_pushed)(struct ast_threadpool *pool,
51 int was_empty);
52 /*!
53 * \brief Indicates the threadpool's taskprocessor has become empty
54 *
55 * \param pool The pool that has become empty
56 * \param listener The threadpool's listener
57 */
59
60 /*!
61 * \brief The threadpool is shutting down
62 *
63 * This would be an opportune time to free the listener's user data
64 * if one wishes. However, it is acceptable to not do so if the user data
65 * should persist beyond the lifetime of the pool.
66 *
67 * \param listener The threadpool's listener
68 */
70};
71
73#define AST_THREADPOOL_OPTIONS_VERSION 1
74 /*! Version of threadpool options in use */
76 /*!
77 * \brief Time limit in seconds for idle threads
78 *
79 * A time of 0 or less will mean no timeout.
80 */
82 /*!
83 * \brief Number of threads to increment pool by
84 *
85 * If a task is added into a pool and no idle thread is
86 * available to activate, then the pool can automatically
87 * grow by the given amount.
88 *
89 * Zero is a perfectly valid value to give here if you want
90 * to control threadpool growth yourself via your listener.
91 */
93 /*!
94 * \brief Number of threads the pool will start with
95 *
96 * When the threadpool is allocated, it will immediately size
97 * itself to have this number of threads in it.
98 *
99 * Zero is a valid value if the threadpool should start
100 * without any threads allocated.
101 */
103 /*!
104 * \brief Maximum number of threads a pool may have
105 *
106 * When the threadpool's size increases, it can never increase
107 * beyond this number of threads.
108 *
109 * Zero is a valid value if the threadpool does not have a
110 * maximum size.
111 */
113 /*!
114 * \brief Function to call when a thread starts
115 *
116 * This is useful if there is something common that all threads
117 * in a threadpool need to do when they start.
118 */
119 void (*thread_start)(void);
120 /*!
121 * \brief Function to call when a thread ends
122 *
123 * This is useful if there is common cleanup to execute when
124 * a thread completes
125 */
126 void (*thread_end)(void);
127};
128
129/*!
130 * \brief Allocate a threadpool listener
131 *
132 * This function will call back into the alloc callback for the
133 * listener.
134 *
135 * \param callbacks Listener callbacks to assign to the listener
136 * \param user_data User data to be stored in the threadpool listener
137 * \retval NULL Failed to allocate the listener
138 * \retval non-NULL The newly-created threadpool listener
139 */
142
143/*!
144 * \brief Get the threadpool listener's user data
145 * \param listener The threadpool listener
146 * \return The user data
147 */
149
150/*!
151 * \brief Create a new threadpool
152 *
153 * This function creates a threadpool. Tasks may be pushed onto this thread pool
154 * and will be automatically acted upon by threads within the pool.
155 *
156 * Only a single threadpool with a given name may exist. This function will fail
157 * if a threadpool with the given name already exists.
158 *
159 * \param name The unique name for the threadpool
160 * \param listener The listener the threadpool will notify of changes. Can be NULL.
161 * \param options The behavioral options for this threadpool
162 * \retval NULL Failed to create the threadpool
163 * \retval non-NULL The newly-created threadpool
164 */
165struct ast_threadpool *ast_threadpool_create(const char *name,
167 const struct ast_threadpool_options *options);
168
169/*!
170 * \brief Set the number of threads for the thread pool
171 *
172 * This number may be more or less than the current number of
173 * threads in the threadpool.
174 *
175 * \param threadpool The threadpool to adjust
176 * \param size The new desired size of the threadpool
177 */
178void ast_threadpool_set_size(struct ast_threadpool *threadpool, unsigned int size);
179
180/*!
181 * \brief Push a task to the threadpool
182 *
183 * Tasks pushed into the threadpool will be automatically taken by
184 * one of the threads within
185 * \param pool The threadpool to add the task to
186 * \param task The task to add
187 * \param data The parameter for the task
188 * \retval 0 success
189 * \retval -1 failure
190 */
191int ast_threadpool_push(struct ast_threadpool *pool, int (*task)(void *data), void *data)
193
194/*!
195 * \brief Shut down a threadpool and destroy it
196 *
197 * \param pool The pool to shut down
198 */
199void ast_threadpool_shutdown(struct ast_threadpool *pool);
200
201/*!
202 * \brief Get the threadpool serializer currently associated with this thread.
203 * \since 14.0.0
204 *
205 * \note The returned pointer is valid while the serializer
206 * thread is running.
207 *
208 * \note Use ao2_ref() on serializer if you are going to keep it
209 * for another thread. To unref it you must then use
210 * ast_taskprocessor_unreference().
211 *
212 * \retval serializer on success.
213 * \retval NULL on error or no serializer associated with the thread.
214 */
216
217/*!
218 * \brief Serialized execution of tasks within a \ref ast_threadpool.
219 *
220 * \since 12.0.0
221 *
222 * A \ref ast_taskprocessor with the same contract as a default taskprocessor
223 * (tasks execute serially) except instead of executing out of a dedicated
224 * thread, execution occurs in a thread from a \ref ast_threadpool. Think of it
225 * as a lightweight thread.
226 *
227 * While it guarantees that each task will complete before executing the next,
228 * there is no guarantee as to which thread from the \c pool individual tasks
229 * will execute. This normally only matters if your code relies on thread
230 * specific information, such as thread locals.
231 *
232 * Use ast_taskprocessor_unreference() to dispose of the returned \ref
233 * ast_taskprocessor.
234 *
235 * Only a single taskprocessor with a given name may exist. This function will fail
236 * if a taskprocessor with the given name already exists.
237 *
238 * \param name Name of the serializer. (must be unique)
239 * \param pool \ref ast_threadpool for execution.
240 *
241 * \return \ref ast_taskprocessor for enqueuing work.
242 * \retval NULL on error.
243 */
244struct ast_taskprocessor *ast_threadpool_serializer(const char *name, struct ast_threadpool *pool);
245
246/*!
247 * \brief Serialized execution of tasks within a \ref ast_threadpool.
248 * \since 13.5.0
249 *
250 * A \ref ast_taskprocessor with the same contract as a default taskprocessor
251 * (tasks execute serially) except instead of executing out of a dedicated
252 * thread, execution occurs in a thread from a \ref ast_threadpool. Think of it
253 * as a lightweight thread.
254 *
255 * While it guarantees that each task will complete before executing the next,
256 * there is no guarantee as to which thread from the \c pool individual tasks
257 * will execute. This normally only matters if your code relies on thread
258 * specific information, such as thread locals.
259 *
260 * Use ast_taskprocessor_unreference() to dispose of the returned \ref
261 * ast_taskprocessor.
262 *
263 * Only a single taskprocessor with a given name may exist. This function will fail
264 * if a taskprocessor with the given name already exists.
265 *
266 * \param name Name of the serializer. (must be unique)
267 * \param pool \ref ast_threadpool for execution.
268 * \param shutdown_group Group shutdown controller. (NULL if no group association)
269 *
270 * \return \ref ast_taskprocessor for enqueuing work.
271 * \retval NULL on error.
272 */
275
276/*!
277 * \brief Return the size of the threadpool's task queue
278 * \since 13.7.0
279 */
281
282#endif /* ASTERISK_THREADPOOL_H */
static void * listener(void *unused)
Definition: asterisk.c:1530
#define attribute_warn_unused_result
Definition: compiler.h:71
static const char name[]
Definition: format_mp3.c:68
static struct ast_serializer_shutdown_group * shutdown_group
Shutdown group for options serializers.
struct @476 callbacks
static struct ast_threadpool * threadpool
Thread pool for observers.
Definition: sorcery.c:87
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
void(* shutdown)(struct ast_threadpool_listener *listener)
The threadpool is shutting down.
Definition: threadpool.h:69
void(* emptied)(struct ast_threadpool *pool, struct ast_threadpool_listener *listener)
Indicates the threadpool's taskprocessor has become empty.
Definition: threadpool.h:58
void(* task_pushed)(struct ast_threadpool *pool, struct ast_threadpool_listener *listener, int was_empty)
Indicates that a task was pushed to the threadpool.
Definition: threadpool.h:49
void(* state_changed)(struct ast_threadpool *pool, struct ast_threadpool_listener *listener, int active_threads, int idle_threads)
Indicates that the state of threads in the pool has changed.
Definition: threadpool.h:38
listener for a threadpool
Definition: threadpool.c:110
void(* thread_start)(void)
Function to call when a thread starts.
Definition: threadpool.h:119
int idle_timeout
Time limit in seconds for idle threads.
Definition: threadpool.h:81
int max_size
Maximum number of threads a pool may have.
Definition: threadpool.h:112
void(* thread_end)(void)
Function to call when a thread ends.
Definition: threadpool.h:126
int auto_increment
Number of threads to increment pool by.
Definition: threadpool.h:92
int initial_size
Number of threads the pool will start with.
Definition: threadpool.h:102
An opaque threadpool structure.
Definition: threadpool.c:36
static struct test_options options
static int task(void *data)
Queued task for baseline test.
void ast_threadpool_shutdown(struct ast_threadpool *pool)
Shut down a threadpool and destroy it.
Definition: threadpool.c:966
struct ast_taskprocessor * ast_threadpool_serializer_get_current(void)
Get the threadpool serializer currently associated with this thread.
Definition: threadpool.c:1296
int ast_threadpool_push(struct ast_threadpool *pool, int(*task)(void *data), void *data) attribute_warn_unused_result
Push a task to the threadpool.
Definition: threadpool.c:957
long ast_threadpool_queue_size(struct ast_threadpool *pool)
Return the size of the threadpool's task queue.
Definition: threadpool.c:1336
void * ast_threadpool_listener_get_user_data(const struct ast_threadpool_listener *listener)
Get the threadpool listener's user data.
Definition: threadpool.c:906
struct ast_taskprocessor * ast_threadpool_serializer_group(const char *name, struct ast_threadpool *pool, struct ast_serializer_shutdown_group *shutdown_group)
Serialized execution of tasks within a ast_threadpool.
Definition: threadpool.c:1301
void ast_threadpool_set_size(struct ast_threadpool *threadpool, unsigned int size)
Set the number of threads for the thread pool.
Definition: threadpool.c:875
struct ast_threadpool * ast_threadpool_create(const char *name, struct ast_threadpool_listener *listener, const struct ast_threadpool_options *options)
Create a new threadpool.
Definition: threadpool.c:916
struct ast_threadpool_listener * ast_threadpool_listener_alloc(const struct ast_threadpool_listener_callbacks *callbacks, void *user_data)
Allocate a threadpool listener.
Definition: threadpool.c:894
struct ast_taskprocessor * ast_threadpool_serializer(const char *name, struct ast_threadpool *pool)
Serialized execution of tasks within a ast_threadpool.
Definition: threadpool.c:1331