Asterisk - The Open Source Telephony Project GIT-master-77d630f
|
#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/taskpool.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/astobj2.h"
#include "asterisk/serializer_shutdown_group.h"
#include "asterisk/utils.h"
#include "asterisk/time.h"
#include "asterisk/sched.h"
Go to the source code of this file.
Data Structures | |
struct | ast_taskpool |
An opaque taskpool structure. More... | |
struct | serializer |
struct | taskpool_sync_task |
struct | taskpool_taskprocessor |
A taskpool taskprocessor. More... | |
struct | taskpool_taskprocessors |
A container of taskprocessors. More... | |
Macros | |
#define | TASKPOOL_GROW_THRESHOLD (AST_TASKPROCESSOR_HIGH_WATER_LEVEL * 5) / 10 |
The threshold for a taskprocessor at which we consider the pool needing to grow (50% of high water threshold) More... | |
#define | TASKPOOL_QUEUE_SIZE_ADD(tps, size) (size += ast_taskprocessor_size(tps->taskprocessor)) |
#define | TASKPROCESSOR_IS_IDLE(tps, timeout) (ast_tvdiff_ms(ast_tvnow(), tps->last_pushed) > (timeout)) |
Typedefs | |
typedef void(* | taskpool_selector) (struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
Functions | |
struct ast_taskpool * | ast_taskpool_create (const char *name, const struct ast_taskpool_options *options) |
Create a new taskpool. More... | |
static struct ast_taskpool * | ast_taskpool_get_current (void) |
int | ast_taskpool_init (void) |
int | ast_taskpool_push (struct ast_taskpool *pool, int(*task)(void *data), void *data) |
Push a task to the taskpool. More... | |
int | ast_taskpool_push_wait (struct ast_taskpool *pool, int(*task)(void *data), void *data) |
Push a task to the taskpool, and wait for completion. More... | |
long | ast_taskpool_queue_size (struct ast_taskpool *pool) |
Get the current number of queued tasks in the taskpool. More... | |
struct ast_taskprocessor * | ast_taskpool_serializer (const char *name, struct ast_taskpool *pool) |
Serialized execution of tasks within a ast_taskpool. More... | |
struct ast_taskprocessor * | ast_taskpool_serializer_get_current (void) |
Get the taskpool serializer currently associated with this thread. More... | |
struct ast_taskprocessor * | ast_taskpool_serializer_group (const char *name, struct ast_taskpool *pool, struct ast_serializer_shutdown_group *shutdown_group) |
Serialized execution of tasks within a ast_taskpool. More... | |
int | ast_taskpool_serializer_push_wait (struct ast_taskprocessor *serializer, int(*task)(void *data), void *data) |
Push a task to a serializer, and wait for completion. More... | |
void | ast_taskpool_shutdown (struct ast_taskpool *pool) |
Shut down a taskpool and remove the underlying taskprocessors. More... | |
size_t | ast_taskpool_taskprocessors_count (struct ast_taskpool *pool) |
Get the current number of taskprocessors in the taskpool. More... | |
AST_THREADSTORAGE_RAW (current_taskpool_pool) | |
Thread storage for the current taskpool. More... | |
AST_THREADSTORAGE_RAW (current_taskpool_serializer) | |
static int | execute_tasks (void *data) |
static struct serializer * | serializer_create (struct ast_taskpool *pool, struct ast_serializer_shutdown_group *shutdown_group) |
static void | serializer_dtor (void *obj) |
static void | serializer_shutdown (struct ast_taskprocessor_listener *listener) |
static int | serializer_start (struct ast_taskprocessor_listener *listener) |
static void | serializer_task_pushed (struct ast_taskprocessor_listener *listener, int was_empty) |
static void | taskpool_dynamic_pool_grow (struct ast_taskpool *pool, struct taskpool_taskprocessor **taskprocessor) |
static int | taskpool_dynamic_pool_shrink (const void *data) |
static void | taskpool_least_full_selector (struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
Least full taskprocessor selector. More... | |
static void | taskpool_sequential_selector (struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
static int | taskpool_serializer_empty_task (void *data) |
static void | taskpool_shutdown (void) |
static int | taskpool_sync_task (void *data) |
static void | taskpool_sync_task_cleanup (struct taskpool_sync_task *sync_task) |
static int | taskpool_sync_task_init (struct taskpool_sync_task *sync_task, int(*task)(void *), void *data) |
static struct taskpool_taskprocessor * | taskpool_taskprocessor_alloc (struct ast_taskpool *pool, char type) |
static void | taskpool_taskprocessor_dtor (void *obj) |
static int | taskpool_taskprocessor_start (void *data) |
static int | taskpool_taskprocessor_stop (void *data) |
static void | taskpool_taskprocessors_cleanup (struct taskpool_taskprocessors *taskprocessors) |
static int | taskpool_taskprocessors_init (struct taskpool_taskprocessors *taskprocessors, unsigned int size) |
Variables | |
static struct ast_sched_context * | sched |
Scheduler used for dynamic pool shrinking. More... | |
static struct ast_taskprocessor_listener_callbacks | serializer_tps_listener_callbacks |
#define TASKPOOL_GROW_THRESHOLD (AST_TASKPROCESSOR_HIGH_WATER_LEVEL * 5) / 10 |
The threshold for a taskprocessor at which we consider the pool needing to grow (50% of high water threshold)
Definition at line 80 of file taskpool.c.
#define TASKPOOL_QUEUE_SIZE_ADD | ( | tps, | |
size | |||
) | (size += ast_taskprocessor_size(tps->taskprocessor)) |
Definition at line 462 of file taskpool.c.
#define TASKPROCESSOR_IS_IDLE | ( | tps, | |
timeout | |||
) | (ast_tvdiff_ms(ast_tvnow(), tps->last_pushed) > (timeout)) |
Definition at line 221 of file taskpool.c.
typedef void(* taskpool_selector) (struct ast_taskpool *pool, struct taskpool_taskprocessors *taskprocessors, struct taskpool_taskprocessor **taskprocessor, unsigned int *growth_threshold_reached) |
Definition at line 51 of file taskpool.c.
struct ast_taskpool * ast_taskpool_create | ( | const char * | name, |
const struct ast_taskpool_options * | options | ||
) |
Create a new taskpool.
This function creates a taskpool. Tasks may be pushed onto this task pool and will be automatically acted upon by taskprocessors within the pool.
Only a single taskpool with a given name may exist. This function will fail if a taskpool with the given name already exists.
name | The unique name for the taskpool |
options | The behavioral options for this taskpool |
NULL | Failed to create the taskpool |
non-NULL | The newly-created taskpool |
Definition at line 324 of file taskpool.c.
References ao2_alloc, ao2_bump, ao2_ref, ast_log, ast_sched_add(), AST_TASKPOOL_OPTIONS_VERSION, AST_TASKPOOL_SELECTOR_DEFAULT, AST_TASKPOOL_SELECTOR_LEAST_FULL, AST_TASKPOOL_SELECTOR_SEQUENTIAL, ast_taskpool_shutdown(), AST_VECTOR_APPEND, ast_taskpool::dynamic_taskprocessors, ast_taskpool_options::growth_threshold, ast_taskpool_options::initial_size, LOG_WARNING, ast_taskpool_options::max_size, ast_taskpool_options::minimum_size, ast_taskpool::name, name, NULL, ast_taskpool::options, options, ast_taskpool::selector, ast_taskpool::shrink_sched_id, ast_taskpool::static_taskprocessors, taskpool_dynamic_pool_shrink(), TASKPOOL_GROW_THRESHOLD, taskpool_least_full_selector(), taskpool_sequential_selector(), taskpool_taskprocessor_alloc(), taskpool_taskprocessors_init(), taskpool_taskprocessor::taskprocessor, and taskpool_taskprocessors::taskprocessors.
Referenced by AST_TEST_DEFINE(), handle_cli_taskpool_push_efficiency(), handle_cli_taskpool_push_serializer_efficiency(), and stasis_init().
|
static |
Definition at line 92 of file taskpool.c.
References ast_threadstorage_get_ptr().
Referenced by ast_taskpool_push_wait(), ast_taskpool_serializer_push_wait(), execute_tasks(), and taskpool_taskprocessor_stop().
int ast_taskpool_init | ( | void | ) |
Provided by taskpool.c
Definition at line 931 of file taskpool.c.
References ast_register_cleanup(), ast_sched_context_create(), ast_sched_start_thread(), and taskpool_shutdown().
Referenced by asterisk_daemon().
int ast_taskpool_push | ( | struct ast_taskpool * | pool, |
int(*)(void *data) | task, | ||
void * | data | ||
) |
Push a task to the taskpool.
Tasks pushed into the taskpool will be automatically taken by one of the taskprocessors within
pool | The taskpool to add the task to |
task | The task to add |
data | The parameter for the task |
0 | success |
-1 | failure |
Definition at line 522 of file taskpool.c.
References ao2_bump, ao2_cleanup, ao2_lock, ao2_unlock, ast_taskprocessor_push(), ast_tvnow(), ast_taskpool_options::auto_increment, ast_taskpool::dynamic_taskprocessors, NULL, ast_taskpool::options, RAII_VAR, ast_taskpool::selector, ast_taskpool::shutting_down, ast_taskpool::static_taskprocessors, task(), taskpool_dynamic_pool_grow(), and taskpool_taskprocessor::taskprocessor.
Referenced by ast_taskpool_push_wait(), AST_TEST_DEFINE(), efficiency_task(), execute_tasks(), handle_cli_taskpool_push_efficiency(), and serializer_task_pushed().
int ast_taskpool_push_wait | ( | struct ast_taskpool * | pool, |
int(*)(void *data) | task, | ||
void * | data | ||
) |
Push a task to the taskpool, and wait for completion.
Tasks pushed into the taskpool will be automatically taken by one of the taskprocessors within
pool | The taskpool to add the task to |
task | The task to add |
data | The parameter for the task |
0 | success |
-1 | failure |
Definition at line 623 of file taskpool.c.
References ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, ast_taskpool_get_current(), ast_taskpool_push(), sync_task(), task(), taskpool_sync_task_cleanup(), and taskpool_sync_task_init().
Referenced by AST_TEST_DEFINE().
long ast_taskpool_queue_size | ( | struct ast_taskpool * | pool | ) |
Get the current number of queued tasks in the taskpool.
pool | The taskpool to query |
The | number of queued tasks in the taskpool |
Definition at line 464 of file taskpool.c.
References ao2_lock, ao2_unlock, AST_VECTOR_CALLBACK_VOID, ast_taskpool::dynamic_taskprocessors, ast_taskpool::static_taskprocessors, TASKPOOL_QUEUE_SIZE_ADD, and taskpool_taskprocessors::taskprocessors.
struct ast_taskprocessor * ast_taskpool_serializer | ( | const char * | name, |
struct ast_taskpool * | pool | ||
) |
Serialized execution of tasks within a ast_taskpool.
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 taskprocessor from a ast_taskpool.
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 relies 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_taskpool for execution. |
NULL | on error. |
Definition at line 819 of file taskpool.c.
References ast_taskpool_serializer_group(), name, and NULL.
Referenced by AST_TEST_DEFINE(), handle_cli_taskpool_push_serializer_efficiency(), and internal_stasis_subscribe().
struct ast_taskprocessor * ast_taskpool_serializer_get_current | ( | void | ) |
Get the taskpool serializer currently associated with this thread.
serializer | on success. |
NULL | on error or no serializer associated with the thread. |
Definition at line 784 of file taskpool.c.
References ast_threadstorage_get_ptr().
Referenced by ast_taskpool_serializer_push_wait(), requeue_task(), serializer_efficiency_task(), and simple_task().
struct ast_taskprocessor * ast_taskpool_serializer_group | ( | const char * | name, |
struct ast_taskpool * | pool, | ||
struct ast_serializer_shutdown_group * | shutdown_group | ||
) |
Serialized execution of tasks within a ast_taskpool.
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 taskprocessor from a ast_taskpool.
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 relies 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_taskpool for execution. |
shutdown_group | Group shutdown controller. (NULL if no group association) |
NULL | on error. |
Definition at line 789 of file taskpool.c.
References ao2_ref, ast_serializer_shutdown_group_inc(), ast_taskprocessor_create_with_listener(), ast_taskprocessor_listener_alloc(), listener(), name, NULL, serializer_create(), serializer_tps_listener_callbacks, and shutdown_group.
Referenced by ast_serializer_taskpool_create(), and ast_taskpool_serializer().
int ast_taskpool_serializer_push_wait | ( | struct ast_taskprocessor * | serializer, |
int(*)(void *data) | task, | ||
void * | data | ||
) |
Push a task to a serializer, and wait for completion.
serializer | The serializer to add the task to |
task | The task to add |
data | The parameter for the task |
0 | success |
-1 | failure |
Definition at line 832 of file taskpool.c.
References ao2_lock, ao2_unlock, ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, ast_taskpool_get_current(), ast_taskpool_serializer_get_current(), ast_taskprocessor_execute(), ast_taskprocessor_listener(), ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_push(), ast_taskprocessor_size(), ast_threadstorage_set_ptr(), listener(), NULL, sync_task(), task(), taskpool_serializer_empty_task(), taskpool_sync_task_cleanup(), and taskpool_sync_task_init().
Referenced by AST_TEST_DEFINE(), and requeue_task().
void ast_taskpool_shutdown | ( | struct ast_taskpool * | pool | ) |
Shut down a taskpool and remove the underlying taskprocessors.
pool | The pool to shut down |
Definition at line 653 of file taskpool.c.
References ao2_lock, ao2_ref, ao2_unlock, AST_SCHED_DEL_UNREF, ast_taskpool::dynamic_taskprocessors, ast_taskpool::shrink_sched_id, ast_taskpool::shutting_down, ast_taskpool::static_taskprocessors, and taskpool_taskprocessors_cleanup().
Referenced by ast_taskpool_create(), AST_TEST_DEFINE(), handle_cli_taskpool_push_efficiency(), handle_cli_taskpool_push_serializer_efficiency(), and stasis_cleanup().
size_t ast_taskpool_taskprocessors_count | ( | struct ast_taskpool * | pool | ) |
Get the current number of taskprocessors in the taskpool.
pool | The taskpool to query |
The | number of taskprocessors in the taskpool |
Definition at line 451 of file taskpool.c.
References ao2_lock, ao2_unlock, AST_VECTOR_SIZE, ast_taskpool::dynamic_taskprocessors, ast_taskpool::static_taskprocessors, and taskpool_taskprocessors::taskprocessors.
Referenced by AST_TEST_DEFINE().
AST_THREADSTORAGE_RAW | ( | current_taskpool_pool | ) |
Thread storage for the current taskpool.
AST_THREADSTORAGE_RAW | ( | current_taskpool_serializer | ) |
|
static |
Definition at line 710 of file taskpool.c.
References ao2_lock, ao2_unlock, ast_taskpool_get_current(), ast_taskpool_push(), ast_taskprocessor_execute(), ast_taskprocessor_listener(), ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_size(), ast_taskprocessor_unreference(), ast_threadstorage_set_ptr(), execute_tasks(), listener(), NULL, serializer::pool, and ast_taskprocessor_listener::tps.
Referenced by execute_tasks(), and serializer_task_pushed().
|
static |
Definition at line 691 of file taskpool.c.
References ao2_alloc, ao2_bump, NULL, serializer::pool, serializer_dtor(), serializer::shutdown_group, and shutdown_group.
Referenced by ast_taskpool_serializer_group().
|
static |
Definition at line 681 of file taskpool.c.
References ao2_cleanup, NULL, serializer::pool, and serializer::shutdown_group.
Referenced by serializer_create().
|
static |
Definition at line 768 of file taskpool.c.
References ao2_cleanup, ast_serializer_shutdown_group_dec(), ast_taskprocessor_listener_get_user_data(), listener(), and serializer::shutdown_group.
|
static |
Definition at line 762 of file taskpool.c.
|
static |
Definition at line 750 of file taskpool.c.
References ast_taskpool_push(), ast_taskprocessor_listener_get_tps(), ast_taskprocessor_listener_get_user_data(), ast_taskprocessor_unreference(), execute_tasks(), listener(), and serializer::pool.
|
static |
Definition at line 479 of file taskpool.c.
References ao2_ref, AST_VECTOR_APPEND, AST_VECTOR_SIZE, ast_taskpool_options::auto_increment, ast_taskpool::dynamic_taskprocessors, ast_taskpool_options::max_size, ast_taskpool::options, ast_taskpool::static_taskprocessors, taskpool_taskprocessor_alloc(), taskpool_taskprocessor::taskprocessor, taskpool_taskprocessors::taskprocessor_num, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_push().
|
static |
Definition at line 226 of file taskpool.c.
References ao2_cleanup, ao2_lock, ao2_ref, ao2_unlock, AST_VECTOR_REMOVE_ALL_CMP_UNORDERED, AST_VECTOR_SIZE, ast_taskpool::dynamic_taskprocessors, ast_taskpool_options::idle_timeout, ast_taskpool::options, ast_taskpool::shutting_down, TASKPROCESSOR_IS_IDLE, taskpool_taskprocessors::taskprocessor_num, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_create().
|
static |
Least full taskprocessor selector.
\interal
Definition at line 287 of file taskpool.c.
References ast_taskprocessor_size(), AST_VECTOR_GET, AST_VECTOR_SIZE, ast_taskpool_options::growth_threshold, NULL, ast_taskpool::options, taskpool_taskprocessor::taskprocessor, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_create().
|
static |
Definition at line 262 of file taskpool.c.
References ast_taskprocessor_size(), AST_VECTOR_GET, AST_VECTOR_SIZE, ast_taskpool_options::growth_threshold, ast_taskpool::options, taskpool_taskprocessors::taskprocessor_num, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_create().
|
static |
Definition at line 827 of file taskpool.c.
Referenced by ast_taskpool_serializer_push_wait().
|
static |
Definition at line 923 of file taskpool.c.
References ast_sched_context_destroy(), and NULL.
Referenced by ast_taskpool_init().
|
static |
Definition at line 603 of file taskpool.c.
References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, and sync_task().
|
static |
Definition at line 594 of file taskpool.c.
References ast_cond_destroy, ast_mutex_destroy, and sync_task().
Referenced by ast_taskpool_push_wait(), and ast_taskpool_serializer_push_wait().
|
static |
Definition at line 580 of file taskpool.c.
References ast_cond_init, ast_mutex_init, NULL, sync_task(), and task().
Referenced by ast_taskpool_push_wait(), and ast_taskpool_serializer_push_wait().
|
static |
Definition at line 152 of file taskpool.c.
References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ao2_bump, ao2_ref, ast_taskprocessor_build_name(), ast_taskprocessor_get(), AST_TASKPROCESSOR_MAX_NAME, ast_taskprocessor_push(), ast_taskprocessor_unreference(), ast_tvnow(), ast_taskpool::name, NULL, taskpool_taskprocessor_dtor(), taskpool_taskprocessor_start(), taskpool_taskprocessor::taskprocessor, TPS_REF_DEFAULT, and type.
Referenced by ast_taskpool_create(), and taskpool_dynamic_pool_grow().
|
static |
Definition at line 116 of file taskpool.c.
References ast_taskprocessor_push(), ast_taskprocessor_unreference(), NULL, taskpool_taskprocessor_stop(), and taskpool_taskprocessor::taskprocessor.
Referenced by taskpool_taskprocessor_alloc().
|
static |
Definition at line 131 of file taskpool.c.
References ast_threadstorage_set_ptr(), ast_taskpool::options, and ast_taskpool_options::thread_start.
Referenced by taskpool_taskprocessor_alloc().
|
static |
Definition at line 101 of file taskpool.c.
References ao2_cleanup, ast_taskpool_get_current(), ast_taskpool::options, and ast_taskpool_options::thread_end.
Referenced by taskpool_taskprocessor_dtor().
|
static |
Definition at line 206 of file taskpool.c.
References ao2_cleanup, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_FREE, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_shutdown().
|
static |
Definition at line 193 of file taskpool.c.
References AST_VECTOR_INIT, and taskpool_taskprocessors::taskprocessors.
Referenced by ast_taskpool_create().
|
static |
Scheduler used for dynamic pool shrinking.
Definition at line 83 of file taskpool.c.
|
static |
Definition at line 778 of file taskpool.c.
Referenced by ast_taskpool_serializer_group().