7a20abdcbea76cb4ffce450cf00dfb64c2046e3c
[asterisk/asterisk.git] / include / asterisk / threadpool.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012, 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
23 struct ast_threadpool;
24 struct ast_taskprocessor;
25 struct ast_threadpool_listener;
26
27 struct ast_threadpool_listener_callbacks {
28         /*!
29          * \brief Indicates that the state of threads in the pool has changed
30          *
31          * \param listener The threadpool listener
32          * \param active_threads The number of active threads in the pool
33          * \param idle_threads The number of idle threads in the pool
34          * \param zombie_threads The number of zombie threads in the pool
35          */
36         void (*state_changed)(struct ast_threadpool_listener *listener,
37                         int active_threads,
38                         int idle_threads,
39                         int zombie_threads);
40         /*!
41          * \brief Indicates that a task was pushed to the threadpool's taskprocessor
42          *
43          * \param listener The threadpool listener
44          * \param was_empty Indicates whether the taskprocessor was empty prior to adding the task
45          */
46         void (*tps_task_pushed)(struct ast_threadpool_listener *listener,
47                         int was_empty);
48         /*!
49          * \brief Indicates the threadpoo's taskprocessor has become empty
50          * 
51          * \param listener The threadpool's listener
52          */
53         void (*emptied)(struct ast_threadpool_listener *listener);
54 };
55
56 /*!
57  * \brief listener for a threadpool
58  *
59  * The listener is notified of changes in a threadpool. It can
60  * react by doing things like increasing the number of threads
61  * in the pool
62  */
63 struct ast_threadpool_listener {
64         /*! Callbacks called by the threadpool */
65         struct ast_threadpool_listener_callbacks *callbacks;
66         /*! Handle to the threadpool */
67         struct ast_threadpool *threadpool;
68         /*! User data for the listener */
69         void *private_data;
70 };
71
72 /*!
73  * \brief Create a new threadpool
74  *
75  * This function creates a threadpool and returns a taskprocessor. Tasks pushed
76  * to this taskprocessor will be handled by the threadpool and will be reported
77  * on the threadpool's listener.
78  *
79  * \param listener The listener the threadpool will notify of changes
80  * \retval NULL Failed to create the threadpool
81  * \retval non-NULL The associated taskprocessor
82  */
83 struct ast_threadpool *ast_threadpool_create(struct ast_threadpool_listener *listener);
84
85 /*!
86  * \brief Set the number of threads for the thread pool
87  *
88  * This number may be more or less than the current number of
89  * threads in the threadpool.
90  * 
91  * \param threadpool The threadpool to adjust
92  * \param size The new desired size of the threadpool
93  */
94 void ast_threadpool_set_size(struct ast_threadpool *threadpool, unsigned int size);
95
96 #endif /* ASTERISK_THREADPOOL_H */