Asterisk - The Open Source Telephony Project
18.5.0
|
#include "asterisk.h"
#include "asterisk/threadpool.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Data Structures | |
struct | ast_serializer_shutdown_group |
struct | ast_threadpool |
An opaque threadpool structure. More... | |
struct | ast_threadpool_listener |
listener for a threadpool More... | |
struct | pool_options_pair |
struct | serializer |
struct | set_size_data |
Helper struct used for queued operations that change the size of the threadpool. More... | |
struct | task_pushed_data |
helper used for queued task when tasks are pushed More... | |
struct | thread_worker_pair |
Struct used for queued operations involving worker state changes. More... | |
struct | worker_thread |
Macros | |
#define | THREAD_BUCKETS 89 |
Enumerations | |
enum | worker_state { ALIVE, ZOMBIE, DEAD } |
states for worker threads More... | |
Functions | |
static int | activate_thread (void *obj, void *arg, int flags) |
Activate idle threads. More... | |
struct ast_serializer_shutdown_group * | ast_serializer_shutdown_group_alloc (void) |
Create a serializer group shutdown control object. More... | |
int | ast_serializer_shutdown_group_join (struct ast_serializer_shutdown_group *shutdown_group, int timeout) |
Wait for the serializers in the group to shutdown with timeout. More... | |
struct ast_threadpool * | ast_threadpool_create (const char *name, struct ast_threadpool_listener *listener, const struct ast_threadpool_options *options) |
Create a new threadpool. More... | |
struct ast_threadpool_listener * | ast_threadpool_listener_alloc (const struct ast_threadpool_listener_callbacks *callbacks, void *user_data) |
Allocate a threadpool listener. More... | |
void * | ast_threadpool_listener_get_user_data (const struct ast_threadpool_listener *listener) |
Get the threadpool listener's user data. More... | |
int | ast_threadpool_push (struct ast_threadpool *pool, int(*task)(void *data), void *data) |
Push a task to the threadpool. More... | |
long | ast_threadpool_queue_size (struct ast_threadpool *pool) |
Return the size of the threadpool's task queue. More... | |
struct ast_taskprocessor * | ast_threadpool_serializer (const char *name, struct ast_threadpool *pool) |
Serialized execution of tasks within a ast_threadpool. More... | |
struct ast_taskprocessor * | ast_threadpool_serializer_get_current (void) |
Get the threadpool serializer currently associated with this thread. More... | |
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. More... | |
void | ast_threadpool_set_size (struct ast_threadpool *pool, unsigned int size) |
Set the number of threads for the thread pool. More... | |
void | ast_threadpool_shutdown (struct ast_threadpool *pool) |
Shut down a threadpool and destroy it. More... | |
AST_THREADSTORAGE_RAW (current_serializer) | |
static int | execute_tasks (void *data) |
static void | grow (struct ast_threadpool *pool, int delta) |
Add threads to the threadpool. More... | |
static int | kill_threads (void *obj, void *arg, int flags) |
ao2 callback to kill a set number of threads. More... | |
static int | queued_active_thread_idle (void *data) |
Move a worker thread from the active container to the idle container. More... | |
static int | queued_emptied (void *data) |
Queued task that handles the case where the threadpool's taskprocessor is emptied. More... | |
static int | queued_idle_thread_dead (void *data) |
static int | queued_set_size (void *data) |
Change the size of the threadpool. More... | |
static int | queued_task_pushed (void *data) |
Queued task called when tasks are pushed into the threadpool. More... | |
static int | queued_zombie_thread_dead (void *data) |
Kill a zombie thread. More... | |
static struct serializer * | serializer_create (struct ast_threadpool *pool, struct ast_serializer_shutdown_group *shutdown_group) |
static void | serializer_dtor (void *obj) |
static void | serializer_shutdown (struct ast_taskprocessor_listener *listener) |
static void | serializer_shutdown_group_dec (struct ast_serializer_shutdown_group *shutdown_group) |
static void | serializer_shutdown_group_dtor (void *vdoomed) |
static void | serializer_shutdown_group_inc (struct ast_serializer_shutdown_group *shutdown_group) |
static int | serializer_start (struct ast_taskprocessor_listener *listener) |
static void | serializer_task_pushed (struct ast_taskprocessor_listener *listener, int was_empty) |
static struct set_size_data * | set_size_data_alloc (struct ast_threadpool *pool, unsigned int size) |
Allocate and initialize a set_size_data. More... | |
static void | shrink (struct ast_threadpool *pool, int delta) |
Remove threads from the threadpool. More... | |
static struct task_pushed_data * | task_pushed_data_alloc (struct ast_threadpool *pool, int was_empty) |
Allocate and initialize a task_pushed_data. More... | |
static struct thread_worker_pair * | thread_worker_pair_alloc (struct ast_threadpool *pool, struct worker_thread *worker) |
Allocate and initialize a thread_worker_pair. More... | |
static void | thread_worker_pair_free (struct thread_worker_pair *pair) |
Destructor for thread_worker_pair. More... | |
static void | threadpool_active_thread_idle (struct ast_threadpool *pool, struct worker_thread *worker) |
Queue a task to move a thread from the active list to the idle list. More... | |
static struct ast_threadpool * | threadpool_alloc (const char *name, const struct ast_threadpool_options *options) |
static void | threadpool_destructor (void *obj) |
Destroy a threadpool's components. More... | |
static int | threadpool_execute (struct ast_threadpool *pool) |
Execute a task in the threadpool. More... | |
static void | threadpool_idle_thread_dead (struct ast_threadpool *pool, struct worker_thread *worker) |
static void | threadpool_send_state_changed (struct ast_threadpool *pool) |
Notify the threadpool listener that the state has changed. More... | |
static void | threadpool_tps_emptied (struct ast_taskprocessor_listener *listener) |
Taskprocessor listener emptied callback. More... | |
static void | threadpool_tps_shutdown (struct ast_taskprocessor_listener *listener) |
Taskprocessor listener shutdown callback. More... | |
static int | threadpool_tps_start (struct ast_taskprocessor_listener *listener) |
static void | threadpool_tps_task_pushed (struct ast_taskprocessor_listener *listener, int was_empty) |
Taskprocessor listener callback called when a task is added. More... | |
static void | threadpool_zombie_thread_dead (struct ast_threadpool *pool, struct worker_thread *worker) |
Queue a task to kill a zombie thread. More... | |
static void | worker_active (struct worker_thread *worker) |
Active loop for worker threads. More... | |
static int | worker_idle (struct worker_thread *worker) |
Idle function for worker threads. More... | |
static int | worker_set_state (struct worker_thread *worker, enum worker_state state) |
Change a worker's state. More... | |
static void | worker_shutdown (struct worker_thread *worker) |
shut a worker thread down More... | |
static void * | worker_start (void *arg) |
start point for worker threads More... | |
static struct worker_thread * | worker_thread_alloc (struct ast_threadpool *pool) |
Allocate and initialize a new worker thread. More... | |
static int | worker_thread_cmp (void *obj, void *arg, int flags) |
static void | worker_thread_destroy (void *obj) |
Worker thread destructor. More... | |
static int | worker_thread_hash (const void *obj, int flags) |
static int | worker_thread_start (struct worker_thread *worker) |
static int | zombify_threads (void *obj, void *arg, void *data, int flags) |
ao2 callback to zombify a set number of threads. More... | |
Variables | |
static struct ast_taskprocessor_listener_callbacks | serializer_tps_listener_callbacks |
static struct ast_taskprocessor_listener_callbacks | threadpool_tps_listener_callbacks |
Table of taskprocessor listener callbacks for threadpool's main taskprocessor. More... | |
static int | worker_id_counter |
#define THREAD_BUCKETS 89 |
Definition at line 28 of file threadpool.c.
Referenced by threadpool_alloc().
enum worker_state |
states for worker threads
Definition at line 120 of file threadpool.c.
|
static |
Activate idle threads.
This function always returns CMP_MATCH because all workers that this function acts on need to be seen as matches so they are unlinked from the list of idle threads.
Called as an ao2_callback in the threadpool's control taskprocessor thread.
obj | The worker to activate |
arg | The pool where the worker belongs |
CMP_MATCH |
Definition at line 491 of file threadpool.c.
References ast_threadpool::active_threads, ALIVE, ao2_link, ao2_unlink, ast_debug, ast_log, CMP_MATCH, worker_thread::id, LOG_WARNING, pool, and worker_set_state().
Referenced by queued_set_size(), and queued_task_pushed().
struct ast_serializer_shutdown_group* ast_serializer_shutdown_group_alloc | ( | void | ) |
Create a serializer group shutdown control object.
Definition at line 1229 of file threadpool.c.
References ao2_alloc, ast_cond_init, ast_serializer_shutdown_group::cond, NULL, serializer_shutdown_group_dtor(), and shutdown_group.
Referenced by ast_serializer_pool_create(), load_module(), and sip_options_init_task().
int ast_serializer_shutdown_group_join | ( | struct ast_serializer_shutdown_group * | shutdown_group, |
int | timeout | ||
) |
Wait for the serializers in the group to shutdown with timeout.
shutdown_group | Group shutdown controller. (Returns 0 immediately if NULL) |
timeout | Number of seconds to wait for the serializers in the group to shutdown. Zero if the timeout is disabled. |
Definition at line 1241 of file threadpool.c.
References ao2_lock, ao2_object_get_lockaddr(), ao2_unlock, ast_assert, ast_cond_timedwait, ast_cond_wait, ast_tvnow(), ast_serializer_shutdown_group::cond, ast_serializer_shutdown_group::count, lock, NULL, and timeout.
Referenced by ast_res_pjsip_cleanup_options_handling(), ast_serializer_pool_destroy(), and unload_module().
struct ast_threadpool* ast_threadpool_create | ( | const char * | name, |
struct ast_threadpool_listener * | listener, | ||
const struct ast_threadpool_options * | options | ||
) |
Create a new threadpool.
This function creates a threadpool. Tasks may be pushed onto this thread pool and will be automatically acted upon by threads within the pool.
Only a single threadpool with a given name may exist. This function will fail if a threadpool with the given name already exists.
name | The unique name for the threadpool |
listener | The listener the threadpool will notify of changes. Can be NULL. |
options | The behavioral options for this threadpool |
NULL | Failed to create the threadpool |
non-NULL | The newly-created threadpool |
Definition at line 915 of file threadpool.c.
References ao2_cleanup, ao2_ref, ast_alloca, ast_log, ast_taskprocessor_create_with_listener(), ast_taskprocessor_listener_alloc(), AST_THREADPOOL_OPTIONS_VERSION, ast_threadpool_set_size(), ast_threadpool::listener, LOG_WARNING, NULL, pool, RAII_VAR, threadpool_alloc(), ast_threadpool::tps, and ast_threadpool_options::version.
Referenced by ast_sorcery_init(), AST_TEST_DEFINE(), load_module(), and stasis_init().
struct ast_threadpool_listener* ast_threadpool_listener_alloc | ( | const struct ast_threadpool_listener_callbacks * | callbacks, |
void * | user_data | ||
) |
Allocate a threadpool listener.
This function will call back into the alloc callback for the listener.
callbacks | Listener callbacks to assign to the listener |
user_data | User data to be stored in the threadpool listener |
NULL | Failed to allocate the listener |
non-NULL | The newly-created threadpool listener |
Definition at line 893 of file threadpool.c.
References ao2_alloc, ast_threadpool_listener::callbacks, ast_threadpool::listener, NULL, and ast_threadpool_listener::user_data.
Referenced by AST_TEST_DEFINE().
void* ast_threadpool_listener_get_user_data | ( | const struct ast_threadpool_listener * | listener | ) |
Get the threadpool listener's user data.
listener | The threadpool listener |
Definition at line 905 of file threadpool.c.
References ast_threadpool_listener::user_data.
Referenced by listener_check(), test_emptied(), test_shutdown(), test_state_changed(), test_task_pushed(), and wait_for_task_pushed().
int ast_threadpool_push | ( | struct ast_threadpool * | pool, |
int(*)(void *data) | task, | ||
void * | data | ||
) |
Push a task to the threadpool.
Tasks pushed into the threadpool will be automatically taken by one of the threads within
pool | The threadpool to add the task to |
task | The task to add |
data | The parameter for the task |
0 | success |
-1 | failure |
Definition at line 956 of file threadpool.c.
References ast_taskprocessor_push(), lock, SCOPED_AO2LOCK, ast_threadpool::shutting_down, task(), and ast_threadpool::tps.
Referenced by AST_TEST_DEFINE(), and serializer_task_pushed().
long ast_threadpool_queue_size | ( | struct ast_threadpool * | pool | ) |
Return the size of the threadpool's task queue.
Definition at line 1437 of file threadpool.c.
References ast_taskprocessor_size(), and ast_threadpool::tps.
Referenced by ast_sip_threadpool_queue_size().
struct ast_taskprocessor* ast_threadpool_serializer | ( | const char * | name, |
struct ast_threadpool * | pool | ||
) |
Serialized execution of tasks within a ast_threadpool.
A ast_taskprocessor with the same contract as a default taskprocessor (tasks execute serially) except instead of executing out of a dedicated thread, execution occurs in a thread from a ast_threadpool. Think of it as a lightweight thread.
While it guarantees that each task will complete before executing the next, there is no guarantee as to which thread from the pool
individual tasks will execute. This normally only matters if your code relys on thread specific information, such as thread locals.
Use ast_taskprocessor_unreference() to dispose of the returned ast_taskprocessor.
Only a single taskprocessor with a given name may exist. This function will fail if a taskprocessor with the given name already exists.
name | Name of the serializer. (must be unique) |
pool | ast_threadpool for execution. |
NULL
on error. Definition at line 1432 of file threadpool.c.
References ast_threadpool_serializer_group(), and NULL.
Referenced by AST_TEST_DEFINE(), internal_stasis_subscribe(), and sorcery_object_type_alloc().
struct ast_taskprocessor* ast_threadpool_serializer_get_current | ( | void | ) |
Get the threadpool serializer currently associated with this thread.
serializer | on success. |
NULL | on error or no serializer associated with the thread. |
Definition at line 1397 of file threadpool.c.
References ast_threadstorage_get_ptr().
Referenced by record_serializer(), rfc3326_outgoing_request(), rfc3326_outgoing_response(), and sip_resolve().
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.
A ast_taskprocessor with the same contract as a default taskprocessor (tasks execute serially) except instead of executing out of a dedicated thread, execution occurs in a thread from a ast_threadpool. Think of it as a lightweight thread.
While it guarantees that each task will complete before executing the next, there is no guarantee as to which thread from the pool
individual tasks will execute. This normally only matters if your code relys on thread specific information, such as thread locals.
Use ast_taskprocessor_unreference() to dispose of the returned ast_taskprocessor.
Only a single taskprocessor with a given name may exist. This function will fail if a taskprocessor with the given name already exists.
name | Name of the serializer. (must be unique) |
pool | ast_threadpool for execution. |
shutdown_group | Group shutdown controller. (NULL if no group association) |
NULL
on error. Definition at line 1402 of file threadpool.c.
References ao2_ref, ast_taskprocessor_create_with_listener(), ast_taskprocessor_listener_alloc(), ast_threadpool::listener, NULL, serializer_create(), serializer_shutdown_group_inc(), and ast_threadpool::tps.
Referenced by ast_serializer_pool_create(), ast_sip_create_serializer_group(), and ast_threadpool_serializer().
void ast_threadpool_set_size | ( | struct ast_threadpool * | threadpool, |
unsigned int | size | ||
) |
Set the number of threads for the thread pool.
This number may be more or less than the current number of threads in the threadpool.
threadpool | The threadpool to adjust |
size | The new desired size of the threadpool |
Definition at line 874 of file threadpool.c.
References ast_free, ast_taskprocessor_push(), ast_threadpool::control_tps, lock, queued_set_size(), SCOPED_AO2LOCK, set_size_data_alloc(), and ast_threadpool::shutting_down.
Referenced by AST_TEST_DEFINE(), and ast_threadpool_create().
void ast_threadpool_shutdown | ( | struct ast_threadpool * | pool | ) |
Shut down a threadpool and destroy it.
pool | The pool to shut down |
Definition at line 965 of file threadpool.c.
References ao2_lock, ao2_unlock, ast_taskprocessor_unreference(), ast_threadpool::control_tps, ast_threadpool::shutting_down, and ast_threadpool::tps.
Referenced by AST_TEST_DEFINE(), load_module(), sorcery_cleanup(), stasis_cleanup(), and unload_module().
AST_THREADSTORAGE_RAW | ( | current_serializer | ) |
Referenced by serializer_create().
|
static |
Definition at line 1349 of file threadpool.c.
References ast_taskprocessor_execute(), ast_taskprocessor_unreference(), ast_threadstorage_set_ptr(), NULL, and ast_threadpool::tps.
Referenced by serializer_task_pushed().
|
static |
Add threads to the threadpool.
This function is called from the threadpool's control taskprocessor thread.
pool | The pool that is expanding The number of threads to add to the pool |
Definition at line 524 of file threadpool.c.
References ast_threadpool::active_threads, ao2_container_count(), ao2_link, ao2_ref, ao2_unlink, ast_debug, ast_log, ast_taskprocessor_name(), worker_thread::id, ast_threadpool::idle_threads, LOG_ERROR, LOG_WARNING, ast_threadpool_options::max_size, ast_threadpool::options, ast_threadpool::tps, worker_thread_alloc(), and worker_thread_start().
Referenced by __ast_string_field_ptr_build_va(), __ast_string_field_ptr_grow(), queued_set_size(), and queued_task_pushed().
|
static |
ao2 callback to kill a set number of threads.
Threads will be unlinked from the container as long as the counter has not reached zero. The counter is decremented with each thread that is removed.
obj | The worker thread up for possible destruction |
arg | The counter |
flags | Unused |
CMP_MATCH | The counter has not reached zero, so this flag should be removed. |
CMP_STOP | The counter has reached zero so no more threads should be removed. |
Definition at line 714 of file threadpool.c.
References CMP_MATCH, and CMP_STOP.
Referenced by shrink().
|
static |
Move a worker thread from the active container to the idle container.
This function is called from the threadpool's control taskprocessor thread.
data | A thread_worker_pair containing the threadpool and the worker to move. |
Definition at line 235 of file threadpool.c.
References ast_threadpool::active_threads, ao2_link, ao2_unlink, ast_threadpool::idle_threads, thread_worker_pair::pool, thread_worker_pair_free(), threadpool_send_state_changed(), and thread_worker_pair::worker.
Referenced by threadpool_active_thread_idle().
|
static |
Queued task that handles the case where the threadpool's taskprocessor is emptied.
This simply lets the threadpool's listener know that the threadpool is devoid of tasks
data | The pool that has become empty |
Definition at line 638 of file threadpool.c.
References ast_threadpool_listener::callbacks, ast_threadpool_listener_callbacks::emptied, ast_threadpool::listener, and pool.
Referenced by threadpool_tps_emptied().
|
static |
Definition at line 321 of file threadpool.c.
References ao2_unlink, ast_threadpool::idle_threads, thread_worker_pair::pool, thread_worker_pair_free(), threadpool_send_state_changed(), and thread_worker_pair::worker.
Referenced by threadpool_idle_thread_dead().
|
static |
Change the size of the threadpool.
This can either result in shrinking or growing the threadpool depending on the new desired size and the current size.
This function is run from the threadpool control taskprocessor thread
data | A set_size_data used for determining how to act |
Definition at line 838 of file threadpool.c.
References activate_thread(), ast_threadpool::active_threads, ao2_callback, ao2_container_count(), ast_debug, ast_free, grow(), ast_threadpool::idle_threads, OBJ_MULTIPLE, OBJ_NODATA, OBJ_NOLOCK, OBJ_UNLINK, pool, set_size_data::pool, shrink(), set_size_data::size, and threadpool_send_state_changed().
Referenced by ast_threadpool_set_size().
|
static |
Queued task called when tasks are pushed into the threadpool.
This function first calls into the threadpool's listener to let it know that a task has been pushed. It then wakes up all idle threads and moves them into the active thread container.
data | A task_pushed_data |
Definition at line 564 of file threadpool.c.
References activate_thread(), ast_threadpool::active_threads, ao2_callback, ao2_container_count(), ast_free, ast_threadpool_options::auto_increment, ast_threadpool_listener::callbacks, grow(), ast_threadpool::idle_threads, ast_threadpool::listener, OBJ_NODATA, OBJ_NOLOCK, OBJ_UNLINK, ast_threadpool::options, pool, task_pushed_data::pool, ast_threadpool_listener_callbacks::task_pushed, threadpool_send_state_changed(), and task_pushed_data::was_empty.
Referenced by threadpool_tps_task_pushed().
|
static |
Kill a zombie thread.
This runs from the threadpool's control taskprocessor thread.
data | A thread_worker_pair containing the threadpool and the zombie thread |
Definition at line 284 of file threadpool.c.
References ao2_unlink, thread_worker_pair::pool, thread_worker_pair_free(), threadpool_send_state_changed(), thread_worker_pair::worker, and ast_threadpool::zombie_threads.
Referenced by threadpool_zombie_thread_dead().
|
static |
Definition at line 1332 of file threadpool.c.
References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_bump, ao2_ref, AST_THREADSTORAGE_RAW(), NULL, pool, serializer::pool, serializer_dtor(), and serializer::shutdown_group.
Referenced by ast_threadpool_serializer_group().
|
static |
Definition at line 1322 of file threadpool.c.
References ao2_cleanup, NULL, serializer::pool, and serializer::shutdown_group.
Referenced by serializer_create().
|
static |
Definition at line 1381 of file threadpool.c.
References ao2_cleanup, ast_taskprocessor_listener_get_user_data(), serializer_shutdown_group_dec(), and serializer::shutdown_group.
|
static |
Definition at line 1305 of file threadpool.c.
References ao2_lock, ao2_unlock, ast_cond_signal, ast_serializer_shutdown_group::cond, and ast_serializer_shutdown_group::count.
Referenced by serializer_shutdown().
|
static |
Definition at line 1222 of file threadpool.c.
References ast_cond_destroy, and ast_serializer_shutdown_group::cond.
Referenced by ast_serializer_shutdown_group_alloc().
|
static |
Definition at line 1289 of file threadpool.c.
References ao2_lock, ao2_unlock, and ast_serializer_shutdown_group::count.
Referenced by ast_threadpool_serializer_group().
|
static |
Definition at line 1375 of file threadpool.c.
|
static |
Definition at line 1363 of file threadpool.c.
References ast_taskprocessor_listener_get_tps(), ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_unreference(), ast_threadpool_push(), execute_tasks(), serializer::pool, and ast_threadpool::tps.
|
static |
Allocate and initialize a set_size_data.
pool | The pool for the set_size_data |
size | The size to store in the set_size_data |
Definition at line 814 of file threadpool.c.
References ast_malloc, NULL, pool, set_size_data::pool, and set_size_data::size.
Referenced by ast_threadpool_set_size().
|
static |
Remove threads from the threadpool.
The preference is to kill idle threads. However, if there are more threads to remove than there are idle threads, then active threads will be zombified instead.
This function is called from the threadpool control taskprocessor thread.
pool | The threadpool to remove threads from |
delta | The number of threads to remove |
Definition at line 775 of file threadpool.c.
References ast_threadpool::active_threads, ao2_callback, ao2_callback_data, ao2_container_count(), ast_debug, ast_taskprocessor_name(), ast_threadpool::idle_threads, kill_threads(), MIN, OBJ_MULTIPLE, OBJ_NODATA, OBJ_NOLOCK, OBJ_UNLINK, ast_threadpool::tps, and zombify_threads().
Referenced by queued_set_size().
|
static |
Allocate and initialize a task_pushed_data.
pool | The threadpool to set in the task_pushed_data |
was_empty | The was_empty value to set in the task_pushed_data |
NULL | Unable to allocate task_pushed_data |
non-NULL | The newly-allocated task_pushed_data |
Definition at line 466 of file threadpool.c.
References ast_malloc, NULL, pool, task_pushed_data::pool, and task_pushed_data::was_empty.
Referenced by threadpool_tps_task_pushed().
|
static |
Allocate and initialize a thread_worker_pair.
pool | Threadpool to assign to the thread_worker_pair |
worker | Worker thread to assign to the thread_worker_pair |
Definition at line 214 of file threadpool.c.
References ao2_ref, ast_malloc, NULL, thread_worker_pair::pool, pool, and thread_worker_pair::worker.
Referenced by threadpool_active_thread_idle(), threadpool_idle_thread_dead(), and threadpool_zombie_thread_dead().
|
static |
Destructor for thread_worker_pair.
Definition at line 203 of file threadpool.c.
References ao2_ref, ast_free, and thread_worker_pair::worker.
Referenced by queued_active_thread_idle(), queued_idle_thread_dead(), queued_zombie_thread_dead(), threadpool_active_thread_idle(), threadpool_idle_thread_dead(), and threadpool_zombie_thread_dead().
|
static |
Queue a task to move a thread from the active list to the idle list.
This is called by a worker thread when it runs out of tasks to perform and goes idle.
pool | The threadpool to which the worker belongs |
worker | The worker thread that has gone idle |
Definition at line 256 of file threadpool.c.
References ast_taskprocessor_push(), ast_threadpool::control_tps, lock, queued_active_thread_idle(), SCOPED_AO2LOCK, ast_threadpool::shutting_down, thread_worker_pair_alloc(), and thread_worker_pair_free().
Referenced by worker_start().
|
static |
Definition at line 404 of file threadpool.c.
References ao2_alloc, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_cleanup, ao2_container_alloc_hash, ao2_ref, ast_free, ast_str_buffer(), ast_str_create, ast_str_set(), ast_taskprocessor_get(), NULL, ast_threadpool::options, pool, RAII_VAR, THREAD_BUCKETS, threadpool_destructor(), TPS_REF_DEFAULT, worker_thread_cmp(), and worker_thread_hash().
Referenced by ast_threadpool_create().
|
static |
Destroy a threadpool's components.
This is the destructor called automatically when the threadpool's reference count reaches zero. This is not to be confused with threadpool_destroy.
By the time this actually gets called, most of the cleanup has already been done in the pool. The only thing left to do is to release the final reference to the threadpool listener.
obj | The pool to destroy |
Definition at line 386 of file threadpool.c.
References ao2_cleanup, ast_threadpool::listener, and pool.
Referenced by threadpool_alloc().
|
static |
Execute a task in the threadpool.
This is the function that worker threads call in order to execute tasks in the threadpool
pool | The pool to which the tasks belong. |
0 | Either the pool has been shut down or there are no tasks. |
1 | There are still tasks remaining in the pool. |
Definition at line 362 of file threadpool.c.
References ao2_lock, ao2_unlock, ast_taskprocessor_execute(), ast_threadpool::shutting_down, and ast_threadpool::tps.
Referenced by worker_active().
|
static |
Definition at line 332 of file threadpool.c.
References ast_taskprocessor_push(), ast_threadpool::control_tps, lock, queued_idle_thread_dead(), SCOPED_AO2LOCK, ast_threadpool::shutting_down, thread_worker_pair_alloc(), and thread_worker_pair_free().
Referenced by worker_idle().
|
static |
Notify the threadpool listener that the state has changed.
This notifies the threadpool listener via its state_changed callback.
pool | The threadpool whose state has changed |
Definition at line 180 of file threadpool.c.
References ast_threadpool::active_threads, ao2_container_count(), ast_threadpool_listener::callbacks, ast_threadpool::idle_threads, ast_threadpool::listener, and ast_threadpool_listener_callbacks::state_changed.
Referenced by queued_active_thread_idle(), queued_idle_thread_dead(), queued_set_size(), queued_task_pushed(), and queued_zombie_thread_dead().
|
static |
Taskprocessor listener emptied callback.
The threadpool queues a task to let the threadpool listener know that the threadpool no longer contains any tasks.
listener | The taskprocessor listener. The threadpool is the listener's private data. |
Definition at line 654 of file threadpool.c.
References ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_push(), ast_threadpool_listener::callbacks, ast_threadpool::control_tps, ast_threadpool_listener_callbacks::emptied, ast_threadpool::listener, lock, pool, queued_emptied(), SCOPED_AO2LOCK, and ast_threadpool::shutting_down.
|
static |
Taskprocessor listener shutdown callback.
The threadpool will shut down and destroy all of its worker threads when this is called back. By the time this gets called, the taskprocessor's control taskprocessor has already been destroyed. Therefore there is no risk in outright destroying the worker threads here.
listener | The taskprocessor listener. The threadpool is the listener's private data. |
Definition at line 679 of file threadpool.c.
References ast_threadpool::active_threads, ao2_cleanup, ast_taskprocessor_listener_get_user_data(), ast_threadpool_listener::callbacks, ast_threadpool::idle_threads, ast_threadpool::listener, pool, ast_threadpool_listener_callbacks::shutdown, and ast_threadpool::zombie_threads.
|
static |
Definition at line 444 of file threadpool.c.
|
static |
Taskprocessor listener callback called when a task is added.
The threadpool uses this opportunity to queue a task on its control taskprocessor in order to activate idle threads and notify the threadpool listener that the task has been pushed.
listener | The taskprocessor listener. The threadpool is the listener's private data |
was_empty | True if the taskprocessor was empty prior to the task being pushed |
Definition at line 610 of file threadpool.c.
References ast_free, ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_push(), ast_threadpool::control_tps, lock, pool, queued_task_pushed(), SCOPED_AO2LOCK, ast_threadpool::shutting_down, and task_pushed_data_alloc().
|
static |
Queue a task to kill a zombie thread.
This is called by a worker thread when it acknowledges that it is time for it to die.
Definition at line 301 of file threadpool.c.
References ast_taskprocessor_push(), ast_threadpool::control_tps, lock, queued_zombie_thread_dead(), SCOPED_AO2LOCK, ast_threadpool::shutting_down, thread_worker_pair_alloc(), and thread_worker_pair_free().
Referenced by worker_start().
|
static |
Active loop for worker threads.
The worker will stay in this loop for its lifetime, executing tasks as they become available. If there are no tasks currently available, then the thread will go idle.
worker | The worker thread executing tasks. |
Definition at line 1124 of file threadpool.c.
References worker_thread::pool, and threadpool_execute().
Referenced by worker_start().
|
static |
Idle function for worker threads.
The worker waits here until it gets told by the threadpool to wake up.
worker is locked before entering this function.
worker | The idle worker |
0 | The thread is being woken up so that it can conclude. |
non-zero | The thread is being woken up to do more work. |
Definition at line 1153 of file threadpool.c.
References ALIVE, ast_cond_timedwait, ast_cond_wait, ast_debug, ast_tvnow(), worker_thread::cond, DEAD, ast_threadpool_options::idle_timeout, worker_thread::lock, worker_thread::options, worker_thread::pool, worker_thread::state, threadpool_idle_thread_dead(), and worker_thread::wake_up.
Referenced by worker_start().
|
static |
Change a worker's state.
The threadpool calls into this function in order to let a worker know how it should proceed.
-1 | failure (state transition not permitted) |
0 | success |
Definition at line 1186 of file threadpool.c.
References ALIVE, ast_assert, ast_cond_signal, worker_thread::cond, DEAD, worker_thread::lock, lock, SCOPED_MUTEX, worker_thread::state, state, worker_thread::wake_up, and ZOMBIE.
Referenced by activate_thread(), worker_shutdown(), and zombify_threads().
|
static |
shut a worker thread down
Set the worker dead and then wait for its thread to finish executing.
worker | The worker thread to shut down |
Definition at line 1009 of file threadpool.c.
References AST_PTHREADT_NULL, DEAD, NULL, worker_thread::thread, and worker_set_state().
Referenced by worker_thread_destroy().
|
static |
start point for worker threads
Worker threads start in the active state but may immediately go idle if there is no work to be done
arg | The worker thread |
NULL |
Definition at line 1044 of file threadpool.c.
References ALIVE, ast_mutex_lock, ast_mutex_unlock, worker_thread::lock, NULL, worker_thread::options, worker_thread::pool, worker_thread::state, ast_threadpool_options::thread_end, ast_threadpool_options::thread_start, threadpool_active_thread_idle(), threadpool_zombie_thread_dead(), worker_active(), worker_idle(), and ZOMBIE.
Referenced by worker_thread_start().
|
static |
Allocate and initialize a new worker thread.
This will create, initialize, and start the thread.
pool | The threadpool to which the worker will be added |
NULL | Failed to allocate or start the worker thread |
non-NULL | The newly-created worker thread |
Definition at line 1093 of file threadpool.c.
References ALIVE, ao2_alloc, ast_atomic_fetchadd_int(), ast_cond_init, ast_mutex_init, AST_PTHREADT_NULL, worker_thread::cond, worker_thread::id, worker_thread::lock, NULL, ast_threadpool::options, worker_thread::options, worker_thread::pool, pool, worker_thread::state, worker_thread::thread, and worker_thread_destroy().
Referenced by grow().
|
static |
Definition at line 993 of file threadpool.c.
References CMP_MATCH, and worker_thread::id.
Referenced by threadpool_alloc().
|
static |
Worker thread destructor.
Called automatically when refcount reaches 0. Shuts down the worker thread and destroys its component parts
Definition at line 1025 of file threadpool.c.
References ast_cond_destroy, ast_debug, ast_mutex_destroy, worker_thread::cond, worker_thread::id, worker_thread::lock, and worker_shutdown().
Referenced by worker_thread_alloc().
|
static |
Definition at line 986 of file threadpool.c.
References worker_thread::id.
Referenced by threadpool_alloc().
|
static |
Definition at line 1109 of file threadpool.c.
References ast_pthread_create, NULL, worker_thread::thread, and worker_start().
Referenced by grow().
|
static |
ao2 callback to zombify a set number of threads.
Threads will be zombified as long as the counter has not reached zero. The counter is decremented with each thread that is zombified.
Zombifying a thread involves removing it from its current container, adding it to the zombie container, and changing the state of the worker to a zombie
This callback is called from the threadpool control taskprocessor thread.
obj | The worker thread that may be zombified |
arg | The pool to which the worker belongs |
data | The counter |
flags | Unused |
CMP_MATCH | The zombified thread should be removed from its current container |
CMP_STOP | Stop attempting to zombify threads |
Definition at line 745 of file threadpool.c.
References ao2_link, ast_log, CMP_MATCH, CMP_STOP, worker_thread::id, LOG_WARNING, pool, worker_set_state(), ZOMBIE, and ast_threadpool::zombie_threads.
Referenced by shrink().
|
static |
Definition at line 1391 of file threadpool.c.
|
static |
Table of taskprocessor listener callbacks for threadpool's main taskprocessor.
Definition at line 695 of file threadpool.c.
|
static |
A monotonically increasing integer used for worker thread identification.
Definition at line 984 of file threadpool.c.