Asterisk - The Open Source Telephony Project GIT-master-20e40a9
Loading...
Searching...
No Matches
taskprocessor.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007-2013, Digium, Inc.
5 *
6 * Dwayne M. Hubbard <dhubbard@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 * \file
21 *
22 * \brief An API for managing task processing threads that can be shared across modules
23 *
24 * \author Dwayne M. Hubbard <dhubbard@digium.com>
25 *
26 * \note A taskprocessor is a named object containing a task queue that
27 * serializes tasks pushed into it by [a] module(s) that reference the taskprocessor.
28 * A taskprocessor is created the first time its name is requested via the
29 * ast_taskprocessor_get() function or the ast_taskprocessor_create_with_listener()
30 * function and destroyed when the taskprocessor reference count reaches zero. A
31 * taskprocessor also contains an accompanying listener that is notified when changes
32 * in the task queue occur.
33 *
34 * A task is a wrapper around a task-handling function pointer and a data
35 * pointer. A task is pushed into a taskprocessor queue using the
36 * ast_taskprocessor_push(taskprocessor, taskhandler, taskdata) function and freed by the
37 * taskprocessor after the task handling function returns. A module releases its
38 * reference to a taskprocessor using the ast_taskprocessor_unreference() function which
39 * may result in the destruction of the taskprocessor if the taskprocessor's reference
40 * count reaches zero. When the taskprocessor's reference count reaches zero, its
41 * listener's shutdown() callback will be called. Any further attempts to execute tasks
42 * will be denied.
43 *
44 * The taskprocessor listener has the flexibility of doling out tasks to best fit the
45 * module's needs. For instance, a taskprocessor listener may have a single dispatch
46 * thread that handles all tasks, or it may dispatch tasks to a thread pool.
47 *
48 * There is a default taskprocessor listener that will be used if a taskprocessor is
49 * created without any explicit listener. This default listener runs tasks sequentially
50 * in a single thread. The listener will execute tasks as long as there are tasks to be
51 * processed. When the taskprocessor is shut down, the default listener will stop
52 * processing tasks and join its execution thread.
53 */
54
55#ifndef __AST_TASKPROCESSOR_H__
56#define __AST_TASKPROCESSOR_H__
57
59
60/*! \brief Suggested maximum taskprocessor name length (less null terminator). */
61#define AST_TASKPROCESSOR_MAX_NAME 70
62
63/*! Default taskprocessor high water level alert trigger */
64#define AST_TASKPROCESSOR_HIGH_WATER_LEVEL 500
65
66/*!
67 * \brief ast_tps_options for specification of taskprocessor options
68 *
69 * Specify whether a taskprocessor should be created via ast_taskprocessor_get() if the taskprocessor
70 * does not already exist. The default behavior is to create a taskprocessor if it does not already exist
71 * and provide its reference to the calling function. To only return a reference to a taskprocessor if
72 * and only if it exists, use the TPS_REF_IF_EXISTS option in ast_taskprocessor_get().
73 */
75 /*! \brief return a reference to a taskprocessor, create one if it does not exist */
77 /*! \brief return a reference to a taskprocessor ONLY if it already exists */
79};
80
82
84 /*!
85 * \brief The taskprocessor has started completely
86 *
87 * This indicates that the taskprocessor is fully set up and the listener
88 * can now start interacting with it.
89 *
90 * \param listener The listener to start
91 */
93 /*!
94 * \brief Indicates a task was pushed to the processor
95 *
96 * \param listener The listener
97 * \param was_empty If non-zero, the taskprocessor was empty prior to the task being pushed
98 */
100 /*!
101 * \brief Indicates the task processor has become empty
102 *
103 * \param listener The listener
104 */
106 /*!
107 * \brief Indicates the taskprocessor wishes to die.
108 *
109 * All operations on the task processor must to be stopped in
110 * this callback. This is an opportune time to free the listener's
111 * user data if it is not going to be used anywhere else.
112 *
113 * After this callback returns, it is NOT safe to operate on the
114 * listener's reference to the taskprocessor.
115 *
116 * \param listener The listener
117 */
120};
121
122/*!
123 * \brief Get a reference to the listener's taskprocessor
124 *
125 * This will return the taskprocessor with its reference count increased. Release
126 * the reference to this object by using ast_taskprocessor_unreference()
127 *
128 * \param listener The listener that has the taskprocessor
129 * \return The taskprocessor
130 */
132
133/*!
134 * \brief Get the user data from the listener
135 * \param listener The taskprocessor listener
136 * \return The listener's user data
137 */
139
140/*!
141 * \brief Allocate a taskprocessor listener
142 *
143 * \since 12.0.0
144 *
145 * This will result in the listener being allocated with the specified
146 * callbacks.
147 *
148 * \param callbacks The callbacks to assign to the listener
149 * \param user_data The user data for the listener
150 * \retval NULL Failure
151 * \retval non-NULL The newly allocated taskprocessor listener
152 */
154
155/*!
156 * \brief Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary
157 *
158 * The default behavior of instantiating a taskprocessor if one does not already exist can be
159 * disabled by specifying the TPS_REF_IF_EXISTS ast_tps_options as the second argument to ast_taskprocessor_get().
160 * \param name The name of the taskprocessor
161 * \param create Use 0 by default or specify TPS_REF_IF_EXISTS to return NULL if the taskprocessor does
162 * not already exist
163 * return A pointer to a reference counted taskprocessor under normal conditions, or NULL if the
164 * TPS_REF_IF_EXISTS reference type is specified and the taskprocessor does not exist
165 * \since 1.6.1
166 */
167struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_options create);
168
169/*!
170 * \brief Create a taskprocessor with a custom listener
171 *
172 * \since 12.0.0
173 *
174 * Note that when a taskprocessor is created in this way, it does not create
175 * any threads to execute the tasks. This job is left up to the listener.
176 * The listener's start() callback will be called during this function.
177 *
178 * \param name The name of the taskprocessor to create
179 * \param listener The listener for operations on this taskprocessor
180 * \retval NULL Failure
181 * \retval non-NULL success
182 */
184
185/*!
186 * \brief Sets the local data associated with a taskprocessor.
187 *
188 * \since 12.0.0
189 *
190 * See ast_taskprocessor_push_local().
191 *
192 * \param tps Task processor.
193 * \param local_data Local data to associate with \a tps.
194 */
196
197/*!
198 * \brief Unreference the specified taskprocessor and its reference count will decrement.
199 *
200 * Taskprocessors use astobj2 and will unlink from the taskprocessor singleton container and destroy
201 * themself when the taskprocessor reference count reaches zero.
202 * \param tps taskprocessor to unreference
203 * \return NULL
204 * \since 1.6.1
205 */
207
208/*!
209 * \brief Push a task into the specified taskprocessor queue and signal the taskprocessor thread
210 * \param tps The taskprocessor structure
211 * \param task_exe The task handling function to push into the taskprocessor queue
212 * \param datap The data to be used by the task handling function
213 * \retval 0 success
214 * \retval -1 failure
215 * \since 1.6.1
216 */
217int __ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap,
218 const char *file, int line, const char *function) attribute_warn_unused_result;
219#define ast_taskprocessor_push(tps, task_exe, datap) \
220 __ast_taskprocessor_push(tps, task_exe, datap, __FILE__, __LINE__, __PRETTY_FUNCTION__)
221
222/*! \brief Local data parameter */
224 /*! Local data, associated with the taskprocessor. */
226 /*! Data pointer passed with this task. */
227 void *data;
228};
229
230/*!
231 * \brief Push a task into the specified taskprocessor queue and signal the
232 * taskprocessor thread.
233 *
234 * The callback receives a \ref ast_taskprocessor_local struct, which contains
235 * both the provided \a datap pointer, and any local data set on the
236 * taskprocessor with ast_taskprocessor_set_local().
237 *
238 * \param tps The taskprocessor structure
239 * \param task_exe The task handling function to push into the taskprocessor queue
240 * \param datap The data to be used by the task handling function
241 * \retval 0 success
242 * \retval -1 failure
243 * \since 12.0.0
244 */
246 int (*task_exe)(struct ast_taskprocessor_local *local), void *datap,
247 const char *file, int line, const char *function) attribute_warn_unused_result;
248#define ast_taskprocessor_push_local(tps, task_exe, datap) \
249 __ast_taskprocessor_push_local(tps, task_exe, datap, __FILE__, __LINE__, __PRETTY_FUNCTION__)
250
251/*!
252 * \brief Indicate the taskprocessor is suspended.
253 *
254 * \since 13.12.0
255 *
256 * \param tps Task processor.
257 * \retval 0 success
258 * \retval -1 failure
259 */
261
262/*!
263 * \brief Indicate the taskprocessor is unsuspended.
264 *
265 * \since 13.12.0
266 *
267 * \param tps Task processor.
268 * \retval 0 success
269 * \retval -1 failure
270 */
272
273/*!
274 * \brief Get the task processor suspend status
275 *
276 * \since 13.12.0
277 *
278 * \param tps Task processor.
279 * \retval non-zero if the task processor is suspended
280 */
282
283/*!
284 * \brief Pop a task off the taskprocessor and execute it.
285 *
286 * \since 12.0.0
287 *
288 * \param tps The taskprocessor from which to execute.
289 * \retval 0 There is no further work to be done.
290 * \retval 1 Tasks still remain in the taskprocessor queue.
291 */
293
294/*!
295 * \brief Am I the given taskprocessor's current task.
296 * \since 12.7.0
297 *
298 * \param tps Taskprocessor to check.
299 *
300 * \retval non-zero if current thread is the taskprocessor thread.
301 */
303
304/*!
305 * \brief Get the next sequence number to create a human friendly taskprocessor name.
306 * \since 13.8.0
307 *
308 * \return Sequence number for use in creating human friendly taskprocessor names.
309 */
310unsigned int ast_taskprocessor_seq_num(void);
311
312/*!
313 * \brief Append the next sequence number to the given string, and copy into the buffer.
314 *
315 * \param buf Where to copy the appended taskprocessor name.
316 * \param size How large is buf including null terminator.
317 * \param name A name to append the sequence number to.
318 */
319void ast_taskprocessor_name_append(char *buf, unsigned int size, const char *name);
320
321/*!
322 * \brief Build a taskprocessor name with a sequence number on the end.
323 * \since 13.8.0
324 *
325 * \param buf Where to put the built taskprocessor name.
326 * \param size How large is buf including null terminator.
327 * \param format printf format to create the non-sequenced part of the name.
328 *
329 * \note The user supplied part of the taskprocessor name is truncated
330 * to allow the full sequence number to be appended within the supplied
331 * buffer size.
332 */
333void __attribute__((format(printf, 3, 4))) ast_taskprocessor_build_name(char *buf, unsigned int size, const char *format, ...);
334
335/*!
336 * \brief Return the name of the taskprocessor singleton
337 * \since 1.6.1
338 */
339const char *ast_taskprocessor_name(struct ast_taskprocessor *tps);
340
341/*!
342 * \brief Return the current size of the taskprocessor queue
343 * \since 13.7.0
344 */
346
347/*!
348 * \brief Return the listener associated with the taskprocessor
349 */
351
352/*!
353 * \brief Get the current taskprocessor high water alert count.
354 * \since 13.10.0
355 *
356 * \retval 0 if no taskprocessors are in high water alert.
357 * \retval non-zero if some task processors are in high water alert.
358 */
359unsigned int ast_taskprocessor_alert_get(void);
360
361
362/*!
363 * \brief Get the current taskprocessor high water alert count by subsystem.
364 * \since 13.26.0
365 * \since 16.3.0
366 *
367 * \param subsystem The subsystem name
368 *
369 * \retval 0 if no taskprocessors are in high water alert.
370 * \retval non-zero if some task processors are in high water alert.
371 */
372unsigned int ast_taskprocessor_get_subsystem_alert(const char *subsystem);
373
374/*!
375 * \brief Set the high and low alert water marks of the given taskprocessor queue.
376 * \since 13.10.0
377 *
378 * \param tps Taskprocessor to update queue water marks.
379 * \param low_water New queue low water mark. (-1 to set as 90% of high_water)
380 * \param high_water New queue high water mark.
381 *
382 * \retval 0 on success.
383 * \retval -1 on error (water marks not changed).
384 */
385int ast_taskprocessor_alert_set_levels(struct ast_taskprocessor *tps, long low_water, long high_water);
386
387#endif /* __AST_TASKPROCESSOR_H__ */
static void * listener(void *unused)
Definition asterisk.c:1530
#define attribute_warn_unused_result
Definition compiler.h:71
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static const char name[]
Definition format_mp3.c:68
struct @506 callbacks
void(* task_pushed)(struct ast_taskprocessor_listener *listener, int was_empty)
Indicates a task was pushed to the processor.
int(* start)(struct ast_taskprocessor_listener *listener)
The taskprocessor has started completely.
void(* emptied)(struct ast_taskprocessor_listener *listener)
Indicates the task processor has become empty.
void(* shutdown)(struct ast_taskprocessor_listener *listener)
Indicates the taskprocessor wishes to die.
void(* dtor)(struct ast_taskprocessor_listener *listener)
A listener for taskprocessors.
Local data parameter.
A ast_taskprocessor structure is a singleton by name.
struct ast_taskprocessor * ast_taskprocessor_get(const char *name, enum ast_tps_options create)
Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary.
struct ast_taskprocessor_listener * ast_taskprocessor_listener_alloc(const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
Allocate a taskprocessor listener.
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
void ast_taskprocessor_set_local(struct ast_taskprocessor *tps, void *local_data)
Sets the local data associated with a taskprocessor.
int ast_taskprocessor_is_suspended(struct ast_taskprocessor *tps)
Get the task processor suspend status.
ast_tps_options
ast_tps_options for specification of taskprocessor options
@ TPS_REF_IF_EXISTS
return a reference to a taskprocessor ONLY if it already exists
@ TPS_REF_DEFAULT
return a reference to a taskprocessor, create one if it does not exist
unsigned int ast_taskprocessor_alert_get(void)
Get the current taskprocessor high water alert count.
int ast_taskprocessor_is_task(struct ast_taskprocessor *tps)
Am I the given taskprocessor's current task.
unsigned int ast_taskprocessor_seq_num(void)
Get the next sequence number to create a human friendly taskprocessor name.
void ast_taskprocessor_name_append(char *buf, unsigned int size, const char *name)
Append the next sequence number to the given string, and copy into the buffer.
void * ast_taskprocessor_listener_get_user_data(const struct ast_taskprocessor_listener *listener)
Get the user data from the listener.
long ast_taskprocessor_size(struct ast_taskprocessor *tps)
Return the current size of the taskprocessor queue.
int ast_taskprocessor_unsuspend(struct ast_taskprocessor *tps)
Indicate the taskprocessor is unsuspended.
int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
Pop a task off the taskprocessor and execute it.
unsigned int ast_taskprocessor_get_subsystem_alert(const char *subsystem)
Get the current taskprocessor high water alert count by subsystem.
void ast_taskprocessor_build_name(char *buf, unsigned int size, const char *format,...)
Build a taskprocessor name with a sequence number on the end.
struct ast_taskprocessor * ast_taskprocessor_create_with_listener(const char *name, struct ast_taskprocessor_listener *listener)
Create a taskprocessor with a custom listener.
struct ast_taskprocessor * ast_taskprocessor_listener_get_tps(const struct ast_taskprocessor_listener *listener)
Get a reference to the listener's taskprocessor.
int __ast_taskprocessor_push_local(struct ast_taskprocessor *tps, int(*task_exe)(struct ast_taskprocessor_local *local), void *datap, const char *file, int line, const char *function) attribute_warn_unused_result
Push a task into the specified taskprocessor queue and signal the taskprocessor thread.
const char * ast_taskprocessor_name(struct ast_taskprocessor *tps)
Return the name of the taskprocessor singleton.
int ast_taskprocessor_alert_set_levels(struct ast_taskprocessor *tps, long low_water, long high_water)
Set the high and low alert water marks of the given taskprocessor queue.
int __ast_taskprocessor_push(struct ast_taskprocessor *tps, int(*task_exe)(void *datap), void *datap, const char *file, int line, const char *function) attribute_warn_unused_result
Push a task into the specified taskprocessor queue and signal the taskprocessor thread.
int ast_taskprocessor_suspend(struct ast_taskprocessor *tps)
Indicate the taskprocessor is suspended.