Asterisk - The Open Source Telephony Project  GIT-master-93d0901
Data Structures | Macros | Enumerations | Functions
taskprocessor.h File Reference

An API for managing task processing threads that can be shared across modules. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_taskprocessor_listener_callbacks
 
struct  ast_taskprocessor_local
 Local data parameter. More...
 

Macros

#define AST_TASKPROCESSOR_HIGH_WATER_LEVEL   500
 
#define AST_TASKPROCESSOR_MAX_NAME   70
 Suggested maximum taskprocessor name length (less null terminator). More...
 

Enumerations

enum  ast_tps_options { TPS_REF_DEFAULT = 0 , TPS_REF_IF_EXISTS = (1 << 0) }
 ast_tps_options for specification of taskprocessor options More...
 

Functions

unsigned int ast_taskprocessor_alert_get (void)
 Get the current taskprocessor high water alert count. More...
 
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. More...
 
void ast_taskprocessor_build_name (char *buf, unsigned int size, const char *format,...)
 Build a taskprocessor name with a sequence number on the end. More...
 
struct ast_taskprocessorast_taskprocessor_create_with_listener (const char *name, struct ast_taskprocessor_listener *listener)
 Create a taskprocessor with a custom listener. More...
 
int ast_taskprocessor_execute (struct ast_taskprocessor *tps)
 Pop a task off the taskprocessor and execute it. More...
 
struct ast_taskprocessorast_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. More...
 
unsigned int ast_taskprocessor_get_subsystem_alert (const char *subsystem)
 Get the current taskprocessor high water alert count by subsystem. More...
 
int ast_taskprocessor_is_suspended (struct ast_taskprocessor *tps)
 Get the task processor suspend status. More...
 
int ast_taskprocessor_is_task (struct ast_taskprocessor *tps)
 Am I the given taskprocessor's current task. More...
 
struct ast_taskprocessor_listenerast_taskprocessor_listener_alloc (const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
 Allocate a taskprocessor listener. More...
 
struct ast_taskprocessorast_taskprocessor_listener_get_tps (const struct ast_taskprocessor_listener *listener)
 Get a reference to the listener's taskprocessor. More...
 
void * ast_taskprocessor_listener_get_user_data (const struct ast_taskprocessor_listener *listener)
 Get the user data from the listener. More...
 
const char * ast_taskprocessor_name (struct ast_taskprocessor *tps)
 Return the name of the taskprocessor singleton. More...
 
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. More...
 
int ast_taskprocessor_push (struct ast_taskprocessor *tps, int(*task_exe)(void *datap), void *datap) attribute_warn_unused_result
 Push a task into the specified taskprocessor queue and signal the taskprocessor thread. More...
 
int ast_taskprocessor_push_local (struct ast_taskprocessor *tps, int(*task_exe)(struct ast_taskprocessor_local *local), void *datap) attribute_warn_unused_result
 Push a task into the specified taskprocessor queue and signal the taskprocessor thread. More...
 
unsigned int ast_taskprocessor_seq_num (void)
 Get the next sequence number to create a human friendly taskprocessor name. More...
 
void ast_taskprocessor_set_local (struct ast_taskprocessor *tps, void *local_data)
 Sets the local data associated with a taskprocessor. More...
 
long ast_taskprocessor_size (struct ast_taskprocessor *tps)
 Return the current size of the taskprocessor queue. More...
 
int ast_taskprocessor_suspend (struct ast_taskprocessor *tps)
 Indicate the taskprocessor is suspended. More...
 
void * ast_taskprocessor_unreference (struct ast_taskprocessor *tps)
 Unreference the specified taskprocessor and its reference count will decrement. More...
 
int ast_taskprocessor_unsuspend (struct ast_taskprocessor *tps)
 Indicate the taskprocessor is unsuspended. More...
 

Detailed Description

An API for managing task processing threads that can be shared across modules.

Author
Dwayne M. Hubbard dhubb.nosp@m.ard@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Note
A taskprocessor is a named object containing a task queue that serializes tasks pushed into it by [a] module(s) that reference the taskprocessor. A taskprocessor is created the first time its name is requested via the ast_taskprocessor_get() function or the ast_taskprocessor_create_with_listener() function and destroyed when the taskprocessor reference count reaches zero. A taskprocessor also contains an accompanying listener that is notified when changes in the task queue occur.

A task is a wrapper around a task-handling function pointer and a data pointer. A task is pushed into a taskprocessor queue using the ast_taskprocessor_push(taskprocessor, taskhandler, taskdata) function and freed by the taskprocessor after the task handling function returns. A module releases its reference to a taskprocessor using the ast_taskprocessor_unreference() function which may result in the destruction of the taskprocessor if the taskprocessor's reference count reaches zero. When the taskprocessor's reference count reaches zero, its listener's shutdown() callback will be called. Any further attempts to execute tasks will be denied.

The taskprocessor listener has the flexibility of doling out tasks to best fit the module's needs. For instance, a taskprocessor listener may have a single dispatch thread that handles all tasks, or it may dispatch tasks to a thread pool.

There is a default taskprocessor listener that will be used if a taskprocessor is created without any explicit listener. This default listener runs tasks sequentially in a single thread. The listener will execute tasks as long as there are tasks to be processed. When the taskprocessor is shut down, the default listener will stop processing tasks and join its execution thread.

Definition in file taskprocessor.h.

Macro Definition Documentation

◆ AST_TASKPROCESSOR_HIGH_WATER_LEVEL

#define AST_TASKPROCESSOR_HIGH_WATER_LEVEL   500

Default taskprocessor high water level alert trigger

Definition at line 64 of file taskprocessor.h.

◆ AST_TASKPROCESSOR_MAX_NAME

#define AST_TASKPROCESSOR_MAX_NAME   70

Suggested maximum taskprocessor name length (less null terminator).

Definition at line 61 of file taskprocessor.h.

Enumeration Type Documentation

◆ ast_tps_options

ast_tps_options for specification of taskprocessor options

Specify whether a taskprocessor should be created via ast_taskprocessor_get() if the taskprocessor does not already exist. The default behavior is to create a taskprocessor if it does not already exist and provide its reference to the calling function. To only return a reference to a taskprocessor if and only if it exists, use the TPS_REF_IF_EXISTS option in ast_taskprocessor_get().

Enumerator
TPS_REF_DEFAULT 

return a reference to a taskprocessor, create one if it does not exist

TPS_REF_IF_EXISTS 

return a reference to a taskprocessor ONLY if it already exists

Definition at line 74 of file taskprocessor.h.

74  {
75  /*! \brief return a reference to a taskprocessor, create one if it does not exist */
76  TPS_REF_DEFAULT = 0,
77  /*! \brief return a reference to a taskprocessor ONLY if it already exists */
78  TPS_REF_IF_EXISTS = (1 << 0),
79 };
@ TPS_REF_IF_EXISTS
return a reference to a taskprocessor ONLY if it already exists
Definition: taskprocessor.h:78
@ TPS_REF_DEFAULT
return a reference to a taskprocessor, create one if it does not exist
Definition: taskprocessor.h:76

Function Documentation

◆ ast_taskprocessor_alert_get()

unsigned int ast_taskprocessor_alert_get ( void  )

Get the current taskprocessor high water alert count.

Since
13.10.0
Return values
0if no taskprocessors are in high water alert.
non-zeroif some task processors are in high water alert.

Definition at line 817 of file taskprocessor.c.

818 {
819  unsigned int count;
820 
822  count = tps_alert_count;
824 
825  return count;
826 }
#define ast_rwlock_rdlock(a)
Definition: lock.h:233
#define ast_rwlock_unlock(a)
Definition: lock.h:232
static unsigned int tps_alert_count
static ast_rwlock_t tps_alert_lock

References ast_rwlock_rdlock, ast_rwlock_unlock, tps_alert_count, and tps_alert_lock.

Referenced by AST_TEST_DEFINE(), and distributor().

◆ ast_taskprocessor_alert_set_levels()

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.

Since
13.10.0
Parameters
tpsTaskprocessor to update queue water marks.
low_waterNew queue low water mark. (-1 to set as 90% of high_water)
high_waterNew queue high water mark.
Return values
0on success.
-1on error (water marks not changed).

Definition at line 828 of file taskprocessor.c.

829 {
830  if (!tps || high_water < 0 || high_water < low_water) {
831  return -1;
832  }
833 
834  if (low_water < 0) {
835  /* Set low water level to 90% of high water level */
836  low_water = (high_water * 9) / 10;
837  }
838 
839  ao2_lock(tps);
840 
841  tps->tps_queue_low = low_water;
842  tps->tps_queue_high = high_water;
843 
844  if (tps->high_water_alert) {
845  if (!tps->tps_queue_size || tps->tps_queue_size < low_water) {
846  /* Update water mark alert immediately */
847  tps->high_water_alert = 0;
848  tps_alert_add(tps, -1);
849  }
850  } else {
851  if (high_water < tps->tps_queue_size) {
852  /* Update water mark alert immediately */
853  tps->high_water_alert = 1;
854  tps_alert_add(tps, +1);
855  }
856  }
857 
858  ao2_unlock(tps);
859 
860  return 0;
861 }
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
long tps_queue_low
Taskprocessor low water clear alert level.
Definition: taskprocessor.c:76
long tps_queue_high
Taskprocessor high water alert trigger level.
Definition: taskprocessor.c:78
unsigned int high_water_alert
Definition: taskprocessor.c:89
long tps_queue_size
Taskprocessor current queue size.
Definition: taskprocessor.c:74
static void tps_alert_add(struct ast_taskprocessor *tps, int delta)

References ao2_lock, ao2_unlock, ast_taskprocessor::high_water_alert, tps_alert_add(), ast_taskprocessor::tps_queue_high, ast_taskprocessor::tps_queue_low, and ast_taskprocessor::tps_queue_size.

Referenced by actual_load_config(), ast_res_pjsip_init_options_handling(), ast_serializer_pool_set_alerts(), ast_sorcery_object_set_congestion_levels(), AST_TEST_DEFINE(), and stasis_subscription_set_congestion_limits().

◆ ast_taskprocessor_build_name()

void ast_taskprocessor_build_name ( char *  buf,
unsigned int  size,
const char *  format,
  ... 
)

Build a taskprocessor name with a sequence number on the end.

Since
13.8.0
Parameters
bufWhere to put the built taskprocessor name.
sizeHow large is buf including null terminator.
formatprintf format to create the non-sequenced part of the name.
Note
The user supplied part of the taskprocessor name is truncated to allow the full sequence number to be appended within the supplied buffer size.

Definition at line 1293 of file taskprocessor.c.

1294 {
1295  va_list ap;
1296  int user_size;
1297 
1298  ast_assert(buf != NULL);
1299  ast_assert(SEQ_STR_SIZE <= size);
1300 
1301  va_start(ap, format);
1302  user_size = vsnprintf(buf, size - (SEQ_STR_SIZE - 1), format, ap);
1303  va_end(ap);
1304  if (user_size < 0) {
1305  /*
1306  * Wow! We got an output error to a memory buffer.
1307  * Assume no user part of name written.
1308  */
1309  user_size = 0;
1310  } else if (size < user_size + SEQ_STR_SIZE) {
1311  /* Truncate user part of name to make sequence number fit. */
1312  user_size = size - SEQ_STR_SIZE;
1313  }
1314 
1315  /* Append sequence number to end of user name. */
1316  snprintf(buf + user_size, SEQ_STR_SIZE, "-%08x", ast_taskprocessor_seq_num());
1317 }
static snd_pcm_format_t format
Definition: chan_alsa.c:106
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
unsigned int ast_taskprocessor_seq_num(void)
Get the next sequence number to create a human friendly taskprocessor name.
#define SEQ_STR_SIZE
#define ast_assert(a)
Definition: utils.h:734

References ast_assert, ast_taskprocessor_seq_num(), buf, format, NULL, and SEQ_STR_SIZE.

Referenced by alloc_playback_chan(), allocate_subscription_tree(), ast_sip_session_alloc(), create_websocket_serializer(), distributor_pool_setup(), internal_stasis_subscribe(), refer_progress_alloc(), sip_options_aor_alloc(), sip_outbound_publisher_alloc(), sip_outbound_registration_state_alloc(), and sorcery_object_type_alloc().

◆ ast_taskprocessor_create_with_listener()

struct ast_taskprocessor* ast_taskprocessor_create_with_listener ( const char *  name,
struct ast_taskprocessor_listener listener 
)

Create a taskprocessor with a custom listener.

Since
12.0.0

Note that when a taskprocessor is created in this way, it does not create any threads to execute the tasks. This job is left up to the listener. The listener's start() callback will be called during this function.

Parameters
nameThe name of the taskprocessor to create
listenerThe listener for operations on this taskprocessor
Return values
NULLFailure
non-NULLsuccess

Definition at line 1081 of file taskprocessor.c.

1082 {
1083  struct ast_taskprocessor *p;
1084 
1085  ao2_lock(tps_singletons);
1086  p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);
1087  if (p) {
1088  ao2_unlock(tps_singletons);
1090  return NULL;
1091  }
1092 
1094  ao2_unlock(tps_singletons);
1095 
1096  return __start_taskprocessor(p);
1097 }
static void * listener(void *unused)
Definition: asterisk.c:1476
#define OBJ_KEY
Definition: astobj2.h:1151
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
static const char name[]
Definition: format_mp3.c:68
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
static struct ast_taskprocessor * __allocate_taskprocessor(const char *name, struct ast_taskprocessor_listener *listener)
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
static struct ast_taskprocessor * __start_taskprocessor(struct ast_taskprocessor *p)

References __allocate_taskprocessor(), __start_taskprocessor(), ao2_find, ao2_lock, ao2_unlock, ast_taskprocessor_unreference(), listener(), name, NULL, OBJ_KEY, and OBJ_NOLOCK.

Referenced by AST_TEST_DEFINE(), ast_threadpool_create(), and ast_threadpool_serializer_group().

◆ ast_taskprocessor_execute()

int ast_taskprocessor_execute ( struct ast_taskprocessor tps)

Pop a task off the taskprocessor and execute it.

Since
12.0.0
Parameters
tpsThe taskprocessor from which to execute.
Return values
0There is no further work to be done.
1Tasks still remain in the taskprocessor queue.

Definition at line 1210 of file taskprocessor.c.

1211 {
1212  struct ast_taskprocessor_local local;
1213  struct tps_task *t;
1214  long size;
1215 
1216  ao2_lock(tps);
1217  t = tps_taskprocessor_pop(tps);
1218  if (!t) {
1219  ao2_unlock(tps);
1220  return 0;
1221  }
1222 
1223  tps->thread = pthread_self();
1224  tps->executing = 1;
1225 
1226  if (t->wants_local) {
1227  local.local_data = tps->local_data;
1228  local.data = t->datap;
1229  }
1230  ao2_unlock(tps);
1231 
1232  if (t->wants_local) {
1233  t->callback.execute_local(&local);
1234  } else {
1235  t->callback.execute(t->datap);
1236  }
1237  tps_task_free(t);
1238 
1239  ao2_lock(tps);
1240  tps->thread = AST_PTHREADT_NULL;
1241  /* We need to check size in the same critical section where we reset the
1242  * executing bit. Avoids a race condition where a task is pushed right
1243  * after we pop an empty stack.
1244  */
1245  tps->executing = 0;
1246  size = ast_taskprocessor_size(tps);
1247 
1248  /* Update the stats */
1250 
1251  /* Include the task we just executed as part of the queue size. */
1252  if (size >= tps->stats.max_qsize) {
1253  tps->stats.max_qsize = size + 1;
1254  }
1255  ao2_unlock(tps);
1256 
1257  /* If we executed a task, check for the transition to empty */
1258  if (size == 0 && tps->listener->callbacks->emptied) {
1259  tps->listener->callbacks->emptied(tps->listener);
1260  }
1261  return size > 0;
1262 }
#define AST_PTHREADT_NULL
Definition: lock.h:66
void(* emptied)(struct ast_taskprocessor_listener *listener)
Indicates the task processor has become empty.
const struct ast_taskprocessor_listener_callbacks * callbacks
Local data parameter.
unsigned int executing
Definition: taskprocessor.c:85
struct ast_taskprocessor_listener * listener
Definition: taskprocessor.c:81
struct tps_taskprocessor_stats stats
Taskprocessor statistics.
Definition: taskprocessor.c:71
tps_task structure is queued to a taskprocessor
Definition: taskprocessor.c:47
int(* execute)(void *datap)
Definition: taskprocessor.c:50
int(* execute_local)(struct ast_taskprocessor_local *local)
Definition: taskprocessor.c:51
unsigned int wants_local
Definition: taskprocessor.c:57
union tps_task::@432 callback
The execute() task callback function pointer.
void * datap
The data pointer for the task execute() function.
Definition: taskprocessor.c:54
unsigned long max_qsize
This is the maximum number of tasks queued at any one time.
Definition: taskprocessor.c:63
unsigned long _tasks_processed_count
This is the current number of tasks processed.
Definition: taskprocessor.c:65
long ast_taskprocessor_size(struct ast_taskprocessor *tps)
Return the current size of the taskprocessor queue.
static void * tps_task_free(struct tps_task *task)
static struct tps_task * tps_taskprocessor_pop(struct ast_taskprocessor *tps)

References tps_taskprocessor_stats::_tasks_processed_count, ao2_lock, ao2_unlock, AST_PTHREADT_NULL, ast_taskprocessor_size(), tps_task::callback, ast_taskprocessor_listener::callbacks, ast_taskprocessor_local::data, tps_task::datap, ast_taskprocessor_listener_callbacks::emptied, tps_task::execute, tps_task::execute_local, ast_taskprocessor::executing, ast_taskprocessor::listener, ast_taskprocessor_local::local_data, ast_taskprocessor::local_data, tps_taskprocessor_stats::max_qsize, ast_taskprocessor::stats, ast_taskprocessor::thread, tps_task_free(), tps_taskprocessor_pop(), and tps_task::wants_local.

Referenced by AST_TEST_DEFINE(), default_tps_processing_function(), execute_tasks(), and threadpool_execute().

◆ ast_taskprocessor_get()

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.

The default behavior of instantiating a taskprocessor if one does not already exist can be disabled by specifying the TPS_REF_IF_EXISTS ast_tps_options as the second argument to ast_taskprocessor_get().

Parameters
nameThe name of the taskprocessor
createUse 0 by default or specify TPS_REF_IF_EXISTS to return NULL if the taskprocessor does not already exist return A pointer to a reference counted taskprocessor under normal conditions, or NULL if the TPS_REF_IF_EXISTS reference type is specified and the taskprocessor does not exist
Since
1.6.1

Definition at line 1042 of file taskprocessor.c.

1043 {
1044  struct ast_taskprocessor *p;
1047 
1048  if (ast_strlen_zero(name)) {
1049  ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
1050  return NULL;
1051  }
1052  ao2_lock(tps_singletons);
1053  p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);
1054  if (p || (create & TPS_REF_IF_EXISTS)) {
1055  /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
1056  ao2_unlock(tps_singletons);
1057  return p;
1058  }
1059 
1060  /* Create a new taskprocessor. Start by creating a default listener */
1062  if (!pvt) {
1063  ao2_unlock(tps_singletons);
1064  return NULL;
1065  }
1067  if (!listener) {
1068  ao2_unlock(tps_singletons);
1070  return NULL;
1071  }
1072 
1074  ao2_unlock(tps_singletons);
1075  p = __start_taskprocessor(p);
1076  ao2_ref(listener, -1);
1077 
1078  return p;
1079 }
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define LOG_ERROR
Definition: logger.h:286
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
A listener for taskprocessors.
static void * default_listener_pvt_alloc(void)
static const struct ast_taskprocessor_listener_callbacks default_listener_callbacks
struct ast_taskprocessor_listener * ast_taskprocessor_listener_alloc(const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
Allocate a taskprocessor listener.
static void default_listener_pvt_destroy(struct default_taskprocessor_listener_pvt *pvt)

References __allocate_taskprocessor(), __start_taskprocessor(), ao2_find, ao2_lock, ao2_ref, ao2_unlock, ast_log, ast_strlen_zero(), ast_taskprocessor_listener_alloc(), default_listener_callbacks, default_listener_pvt_alloc(), default_listener_pvt_destroy(), listener(), LOG_ERROR, name, NULL, OBJ_KEY, OBJ_NOLOCK, and TPS_REF_IF_EXISTS.

Referenced by alloc_playback_chan(), ast_dns_system_resolver_init(), ast_msg_init(), AST_TEST_DEFINE(), cli_tps_ping(), cli_tps_reset_stats(), find_request_serializer(), internal_stasis_subscribe(), load_module(), load_objects(), and threadpool_alloc().

◆ ast_taskprocessor_get_subsystem_alert()

unsigned int ast_taskprocessor_get_subsystem_alert ( const char *  subsystem)

Get the current taskprocessor high water alert count by subsystem.

Since
13.26.0
16.3.0
Parameters
subsystemThe subsystem name
Return values
0if no taskprocessors are in high water alert.
non-zeroif some task processors are in high water alert.

Definition at line 637 of file taskprocessor.c.

638 {
639  struct subsystem_alert *alert;
640  unsigned int count = 0;
641  int idx;
642 
643  AST_VECTOR_RW_RDLOCK(&overloaded_subsystems);
644  idx = AST_VECTOR_GET_INDEX(&overloaded_subsystems, subsystem, subsystem_match);
645  if (idx >= 0) {
646  alert = AST_VECTOR_GET(&overloaded_subsystems, idx);
647  count = alert->alert_count;
648  }
649  AST_VECTOR_RW_UNLOCK(&overloaded_subsystems);
650 
651  return count;
652 }
unsigned int alert_count
static int subsystem_match(struct subsystem_alert *alert, const char *subsystem)
#define AST_VECTOR_GET_INDEX(vec, value, cmp)
Get the 1st index from a vector that matches the given comparison.
Definition: vector.h:719
#define AST_VECTOR_RW_UNLOCK(vec)
Unlock vector.
Definition: vector.h:897
#define AST_VECTOR_RW_RDLOCK(vec)
Obtain read lock on vector.
Definition: vector.h:877
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680

References subsystem_alert::alert_count, AST_VECTOR_GET, AST_VECTOR_GET_INDEX, AST_VECTOR_RW_RDLOCK, AST_VECTOR_RW_UNLOCK, subsystem_alert::subsystem, and subsystem_match().

Referenced by AST_TEST_DEFINE(), and distributor().

◆ ast_taskprocessor_is_suspended()

int ast_taskprocessor_is_suspended ( struct ast_taskprocessor tps)

Get the task processor suspend status.

Since
13.12.0
Parameters
tpsTask processor.
Return values
non-zeroif the task processor is suspended

Definition at line 1205 of file taskprocessor.c.

1206 {
1207  return tps ? tps->suspended : -1;
1208 }
unsigned int suspended
Definition: taskprocessor.c:91

References ast_taskprocessor::suspended.

Referenced by ast_sip_session_suspend().

◆ ast_taskprocessor_is_task()

int ast_taskprocessor_is_task ( struct ast_taskprocessor tps)

Am I the given taskprocessor's current task.

Since
12.7.0
Parameters
tpsTaskprocessor to check.
Return values
non-zeroif current thread is the taskprocessor thread.

Definition at line 1264 of file taskprocessor.c.

1265 {
1266  int is_task;
1267 
1268  ao2_lock(tps);
1269  is_task = pthread_equal(tps->thread, pthread_self());
1270  ao2_unlock(tps);
1271  return is_task;
1272 }

References ao2_lock, ao2_unlock, and ast_taskprocessor::thread.

Referenced by ast_sip_push_task_wait_serializer(), ast_sip_session_suspend(), and handle_new_invite_request().

◆ ast_taskprocessor_listener_alloc()

struct ast_taskprocessor_listener* ast_taskprocessor_listener_alloc ( const struct ast_taskprocessor_listener_callbacks callbacks,
void *  user_data 
)

Allocate a taskprocessor listener.

Since
12.0.0

This will result in the listener being allocated with the specified callbacks.

Parameters
callbacksThe callbacks to assign to the listener
user_dataThe user data for the listener
Return values
NULLFailure
non-NULLThe newly allocated taskprocessor listener

Definition at line 928 of file taskprocessor.c.

929 {
931 
933  if (!listener) {
934  return NULL;
935  }
936  listener->callbacks = callbacks;
937  listener->user_data = user_data;
938 
939  return listener;
940 }
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
struct @497 callbacks
static void taskprocessor_listener_dtor(void *obj)

References ao2_alloc, callbacks, listener(), NULL, taskprocessor_listener_dtor(), and ast_taskprocessor_listener::user_data.

Referenced by ast_taskprocessor_get(), AST_TEST_DEFINE(), ast_threadpool_create(), and ast_threadpool_serializer_group().

◆ ast_taskprocessor_listener_get_tps()

struct ast_taskprocessor* ast_taskprocessor_listener_get_tps ( const struct ast_taskprocessor_listener listener)

Get a reference to the listener's taskprocessor.

This will return the taskprocessor with its reference count increased. Release the reference to this object by using ast_taskprocessor_unreference()

Parameters
listenerThe listener that has the taskprocessor
Returns
The taskprocessor

Definition at line 942 of file taskprocessor.c.

943 {
944  ao2_ref(listener->tps, +1);
945  return listener->tps;
946 }

References ao2_ref, and listener().

Referenced by serializer_task_pushed().

◆ ast_taskprocessor_listener_get_user_data()

void* ast_taskprocessor_listener_get_user_data ( const struct ast_taskprocessor_listener listener)

Get the user data from the listener.

Parameters
listenerThe taskprocessor listener
Returns
The listener's user data

Definition at line 948 of file taskprocessor.c.

949 {
950  return listener->user_data;
951 }

References listener().

Referenced by serializer_shutdown(), serializer_task_pushed(), test_emptied(), test_task_pushed(), threadpool_tps_emptied(), and threadpool_tps_task_pushed().

◆ ast_taskprocessor_name()

const char* ast_taskprocessor_name ( struct ast_taskprocessor tps)

Return the name of the taskprocessor singleton.

Since
1.6.1

Definition at line 904 of file taskprocessor.c.

905 {
906  if (!tps) {
907  ast_log(LOG_ERROR, "no taskprocessor specified!\n");
908  return NULL;
909  }
910  return tps->name;
911 }
char name[0]
Friendly name of the taskprocessor. Subsystem is appended after the name's NULL terminator.
Definition: taskprocessor.c:97

References ast_log, LOG_ERROR, ast_taskprocessor::name, and NULL.

Referenced by ast_serializer_pool_set_alerts(), ast_sip_get_distributor_serializer(), distributor(), grow(), record_serializer(), shrink(), sip_options_apply_aor_configuration(), and sip_options_contact_add_task().

◆ ast_taskprocessor_name_append()

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.

Parameters
bufWhere to copy the appended taskprocessor name.
sizeHow large is buf including null terminator.
nameA name to append the sequence number to.

Definition at line 1283 of file taskprocessor.c.

1284 {
1285  int final_size = strlen(name) + SEQ_STR_SIZE;
1286 
1287  ast_assert(buf != NULL && name != NULL);
1288  ast_assert(final_size <= size);
1289 
1290  snprintf(buf, final_size, "%s-%08x", name, ast_taskprocessor_seq_num());
1291 }

References ast_assert, ast_taskprocessor_seq_num(), buf, name, NULL, and SEQ_STR_SIZE.

Referenced by ast_serializer_pool_create().

◆ ast_taskprocessor_push()

int ast_taskprocessor_push ( struct ast_taskprocessor tps,
int(*)(void *datap)  task_exe,
void *  datap 
)

Push a task into the specified taskprocessor queue and signal the taskprocessor thread.

Parameters
tpsThe taskprocessor structure
task_exeThe task handling function to push into the taskprocessor queue
datapThe data to be used by the task handling function
Return values
0success
-1failure
Since
1.6.1

Definition at line 1173 of file taskprocessor.c.

1174 {
1175  return taskprocessor_push(tps, tps_task_alloc(task_exe, datap));
1176 }
static struct tps_task * tps_task_alloc(int(*task_exe)(void *datap), void *datap)
static int taskprocessor_push(struct ast_taskprocessor *tps, struct tps_task *t)

References taskprocessor_push(), and tps_task_alloc().

Referenced by ast_cc_agent_status_response(), ast_cc_monitor_failed(), ast_cc_monitor_party_b_free(), ast_cc_monitor_status_request(), ast_cc_monitor_stop_ringing(), ast_msg_queue(), ast_sip_push_task(), ast_sorcery_create(), ast_sorcery_delete(), ast_sorcery_update(), AST_TEST_DEFINE(), ast_threadpool_push(), ast_threadpool_set_size(), async_delete_name_rec(), async_play_sound_helper(), cc_request_state_change(), cli_tps_ping(), destroy_conference_bridge(), dns_system_resolver_resolve(), generic_monitor_devstate_cb(), handle_cc_status(), hepv3_send_packet(), iax2_transmit(), mwi_handle_subscribe(), mwi_handle_unsubscribe(), play_sound_helper(), sorcery_object_load(), stasis_unsubscribe(), threadpool_active_thread_idle(), threadpool_idle_thread_dead(), threadpool_tps_emptied(), threadpool_tps_task_pushed(), and threadpool_zombie_thread_dead().

◆ ast_taskprocessor_push_local()

int ast_taskprocessor_push_local ( struct ast_taskprocessor tps,
int(*)(struct ast_taskprocessor_local *local)  task_exe,
void *  datap 
)

Push a task into the specified taskprocessor queue and signal the taskprocessor thread.

The callback receives a ast_taskprocessor_local struct, which contains both the provided datap pointer, and any local data set on the taskprocessor with ast_taskprocessor_set_local().

Parameters
tpsThe taskprocessor structure
task_exeThe task handling function to push into the taskprocessor queue
datapThe data to be used by the task handling function
Return values
0success
-1failure
Since
12.0.0

Referenced by AST_TEST_DEFINE(), and dispatch_message().

◆ ast_taskprocessor_seq_num()

unsigned int ast_taskprocessor_seq_num ( void  )

Get the next sequence number to create a human friendly taskprocessor name.

Since
13.8.0
Returns
Sequence number for use in creating human friendly taskprocessor names.

Definition at line 1274 of file taskprocessor.c.

1275 {
1276  static int seq_num;
1277 
1278  return (unsigned int) ast_atomic_fetchadd_int(&seq_num, +1);
1279 }
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755

References ast_atomic_fetchadd_int().

Referenced by ast_taskprocessor_build_name(), and ast_taskprocessor_name_append().

◆ ast_taskprocessor_set_local()

void ast_taskprocessor_set_local ( struct ast_taskprocessor tps,
void *  local_data 
)

Sets the local data associated with a taskprocessor.

Since
12.0.0

See ast_taskprocessor_push_local().

Parameters
tpsTask processor.
local_dataLocal data to associate with tps.

Definition at line 1099 of file taskprocessor.c.

1101 {
1102  SCOPED_AO2LOCK(lock, tps);
1103  tps->local_data = local_data;
1104 }
ast_mutex_t lock
Definition: app_meetme.c:1093
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition: lock.h:602

References ast_taskprocessor::local_data, lock, and SCOPED_AO2LOCK.

Referenced by AST_TEST_DEFINE(), and internal_stasis_subscribe().

◆ ast_taskprocessor_size()

long ast_taskprocessor_size ( struct ast_taskprocessor tps)

Return the current size of the taskprocessor queue.

Since
13.7.0

Definition at line 898 of file taskprocessor.c.

899 {
900  return (tps) ? tps->tps_queue_size : -1;
901 }

References ast_taskprocessor::tps_queue_size.

Referenced by ast_serializer_pool_get(), ast_taskprocessor_execute(), AST_TEST_DEFINE(), and ast_threadpool_queue_size().

◆ ast_taskprocessor_suspend()

int ast_taskprocessor_suspend ( struct ast_taskprocessor tps)

Indicate the taskprocessor is suspended.

Since
13.12.0
Parameters
tpsTask processor.
Return values
0success
-1failure

Definition at line 1183 of file taskprocessor.c.

1184 {
1185  if (tps) {
1186  ao2_lock(tps);
1187  tps->suspended = 1;
1188  ao2_unlock(tps);
1189  return 0;
1190  }
1191  return -1;
1192 }

References ao2_lock, ao2_unlock, and ast_taskprocessor::suspended.

Referenced by ast_sip_session_suspend(), and AST_TEST_DEFINE().

◆ ast_taskprocessor_unreference()

void* ast_taskprocessor_unreference ( struct ast_taskprocessor tps)

Unreference the specified taskprocessor and its reference count will decrement.

Taskprocessors use astobj2 and will unlink from the taskprocessor singleton container and destroy themself when the taskprocessor reference count reaches zero.

Parameters
tpstaskprocessor to unreference
Returns
NULL
Since
1.6.1

Definition at line 1107 of file taskprocessor.c.

1108 {
1109  if (!tps) {
1110  return NULL;
1111  }
1112 
1113  /* To prevent another thread from finding and getting a reference to this
1114  * taskprocessor we hold the singletons lock. If we didn't do this then
1115  * they may acquire it and find that the listener has been shut down.
1116  */
1117  ao2_lock(tps_singletons);
1118 
1119  if (ao2_ref(tps, -1) > 3) {
1120  ao2_unlock(tps_singletons);
1121  return NULL;
1122  }
1123 
1124  /* If we're down to 3 references, then those must be:
1125  * 1. The reference we just got rid of
1126  * 2. The container
1127  * 3. The listener
1128  */
1129  ao2_unlink_flags(tps_singletons, tps, OBJ_NOLOCK);
1130  ao2_unlock(tps_singletons);
1131 
1133  return NULL;
1134 }
#define ao2_unlink_flags(container, obj, flags)
Remove an object from a container.
Definition: astobj2.h:1600
static void listener_shutdown(struct ast_taskprocessor_listener *listener)

References ao2_lock, ao2_ref, ao2_unlink_flags, ao2_unlock, ast_taskprocessor::listener, listener_shutdown(), NULL, and OBJ_NOLOCK.

Referenced by __start_taskprocessor(), __unload_module(), ast_msg_shutdown(), ast_res_pjsip_cleanup_options_handling(), ast_serializer_pool_destroy(), ast_taskprocessor_create_with_listener(), AST_TEST_DEFINE(), ast_threadpool_shutdown(), cli_tps_ping(), cli_tps_reset_stats(), cli_tps_reset_stats_all(), destroy_conference_bridge(), distributor(), distributor_pool_shutdown(), dns_system_resolver_destroy(), execute_tasks(), exten_state_subscription_destructor(), refer_progress_destroy(), scheduler(), schtd_dtor(), serializer_task_pushed(), session_destructor(), sip_options_aor_dtor(), sip_outbound_publisher_destroy(), sip_outbound_registration_client_state_destroy(), sorcery_object_type_destructor(), subscription_dtor(), subscription_persistence_recreate(), subscription_tree_destructor(), tps_report_taskprocessor_list(), tps_shutdown_thread(), tps_taskprocessor_tab_complete(), unload_module(), and websocket_cb().

◆ ast_taskprocessor_unsuspend()

int ast_taskprocessor_unsuspend ( struct ast_taskprocessor tps)

Indicate the taskprocessor is unsuspended.

Since
13.12.0
Parameters
tpsTask processor.
Return values
0success
-1failure

Definition at line 1194 of file taskprocessor.c.

1195 {
1196  if (tps) {
1197  ao2_lock(tps);
1198  tps->suspended = 0;
1199  ao2_unlock(tps);
1200  return 0;
1201  }
1202  return -1;
1203 }

References ao2_lock, ao2_unlock, and ast_taskprocessor::suspended.

Referenced by ast_sip_session_unsuspend(), and AST_TEST_DEFINE().