Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Enumerations | Functions
taskprocessor.h File Reference

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

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).
 

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

#define AST_TASKPROCESSOR_HIGH_WATER_LEVEL   500

Default taskprocessor high water level alert trigger

Definition at line 64 of file taskprocessor.h.

Referenced by create_routes(), and manager_subscriptions_init().

Enumeration Type Documentation

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 };
return a reference to a taskprocessor, create one if it does not exist
Definition: taskprocessor.h:76
return a reference to a taskprocessor ONLY if it already exists
Definition: taskprocessor.h:78

Function Documentation

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 884 of file taskprocessor.c.

References tps_alert_count, and tps_alert_lock.

Referenced by AST_TEST_DEFINE().

885 {
886  unsigned int count;
887 
888  ast_rwlock_rdlock(&tps_alert_lock);
889  count = tps_alert_count;
890  ast_rwlock_unlock(&tps_alert_lock);
891 
892  return count;
893 }
static ast_rwlock_t tps_alert_lock
static unsigned int tps_alert_count
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 895 of file taskprocessor.c.

References ast_taskprocessor::high_water_alert, ast_taskprocessor::tps_queue_high, ast_taskprocessor::tps_queue_low, and ast_taskprocessor::tps_queue_size.

Referenced by ast_sorcery_object_set_congestion_levels(), AST_TEST_DEFINE(), and stasis_subscription_set_congestion_limits().

896 {
897  if (!tps || high_water < 0 || high_water < low_water) {
898  return -1;
899  }
900 
901  if (low_water < 0) {
902  /* Set low water level to 90% of high water level */
903  low_water = (high_water * 9) / 10;
904  }
905 
906  ao2_lock(tps);
907 
908  tps->tps_queue_low = low_water;
909  tps->tps_queue_high = high_water;
910 
911  if (tps->high_water_alert) {
912  if (!tps->tps_queue_size || tps->tps_queue_size < low_water) {
913  /* Update water mark alert immediately */
914  tps->high_water_alert = 0;
915  tps_alert_add(tps, -1);
916  }
917  } else {
918  if (high_water < tps->tps_queue_size) {
919  /* Update water mark alert immediately */
920  tps->high_water_alert = 1;
921  tps_alert_add(tps, +1);
922  }
923  }
924 
925  ao2_unlock(tps);
926 
927  return 0;
928 }
unsigned int high_water_alert
Definition: taskprocessor.c:89
long tps_queue_high
Taskprocessor high water alert trigger level.
Definition: taskprocessor.c:78
long tps_queue_low
Taskprocessor low water clear alert level.
Definition: taskprocessor.c:76
long tps_queue_size
Taskprocessor current queue size.
Definition: taskprocessor.c:74
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 1360 of file taskprocessor.c.

References ast_taskprocessor_seq_num().

Referenced by internal_stasis_subscribe(), and sorcery_object_type_alloc().

1361 {
1362  va_list ap;
1363  int user_size;
1364 
1365  ast_assert(buf != NULL);
1366  ast_assert(SEQ_STR_SIZE <= size);
1367 
1368  va_start(ap, format);
1369  user_size = vsnprintf(buf, size - (SEQ_STR_SIZE - 1), format, ap);
1370  va_end(ap);
1371  if (user_size < 0) {
1372  /*
1373  * Wow! We got an output error to a memory buffer.
1374  * Assume no user part of name written.
1375  */
1376  user_size = 0;
1377  } else if (size < user_size + SEQ_STR_SIZE) {
1378  /* Truncate user part of name to make sequence number fit. */
1379  user_size = size - SEQ_STR_SIZE;
1380  }
1381 
1382  /* Append sequence number to end of user name. */
1383  snprintf(buf + user_size, SEQ_STR_SIZE, "-%08x", ast_taskprocessor_seq_num());
1384 }
unsigned int ast_taskprocessor_seq_num(void)
Get the next sequence number to create a human friendly taskprocessor name.
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 1148 of file taskprocessor.c.

References ast_taskprocessor_unreference(), OBJ_KEY, and OBJ_NOLOCK.

Referenced by AST_TEST_DEFINE().

1149 {
1150  struct ast_taskprocessor *p;
1151 
1152  ao2_lock(tps_singletons);
1153  p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);
1154  if (p) {
1155  ao2_unlock(tps_singletons);
1157  return NULL;
1158  }
1159 
1160  p = __allocate_taskprocessor(name, listener);
1161  ao2_unlock(tps_singletons);
1162 
1163  return __start_taskprocessor(p);
1164 }
char name[0]
Friendly name of the taskprocessor. Subsystem is appended after the name's NULL terminator.
Definition: taskprocessor.c:97
#define OBJ_KEY
Definition: astobj2.h:1151
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
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 1277 of file taskprocessor.c.

References tps_taskprocessor_stats::_tasks_processed_count, ast_taskprocessor_size(), tps_task::callback, ast_taskprocessor_listener::callbacks, ast_taskprocessor_local::data, tps_task::datap, ast_taskprocessor_listener_callbacks::emptied, ast_taskprocessor::executing, ast_taskprocessor_local::local_data, tps_taskprocessor_stats::max_qsize, ast_taskprocessor::stats, and ast_taskprocessor::thread.

Referenced by AST_TEST_DEFINE(), and default_tps_processing_function().

1278 {
1279  struct ast_taskprocessor_local local;
1280  struct tps_task *t;
1281  long size;
1282 
1283  ao2_lock(tps);
1284  t = tps_taskprocessor_pop(tps);
1285  if (!t) {
1286  ao2_unlock(tps);
1287  return 0;
1288  }
1289 
1290  tps->thread = pthread_self();
1291  tps->executing = 1;
1292 
1293  if (t->wants_local) {
1294  local.local_data = tps->local_data;
1295  local.data = t->datap;
1296  }
1297  ao2_unlock(tps);
1298 
1299  if (t->wants_local) {
1300  t->callback.execute_local(&local);
1301  } else {
1302  t->callback.execute(t->datap);
1303  }
1304  tps_task_free(t);
1305 
1306  ao2_lock(tps);
1307  tps->thread = AST_PTHREADT_NULL;
1308  /* We need to check size in the same critical section where we reset the
1309  * executing bit. Avoids a race condition where a task is pushed right
1310  * after we pop an empty stack.
1311  */
1312  tps->executing = 0;
1313  size = ast_taskprocessor_size(tps);
1314 
1315  /* Update the stats */
1317 
1318  /* Include the task we just executed as part of the queue size. */
1319  if (size >= tps->stats.max_qsize) {
1320  tps->stats.max_qsize = size + 1;
1321  }
1322  ao2_unlock(tps);
1323 
1324  /* If we executed a task, check for the transition to empty */
1325  if (size == 0 && tps->listener->callbacks->emptied) {
1326  tps->listener->callbacks->emptied(tps->listener);
1327  }
1328  return size > 0;
1329 }
const struct ast_taskprocessor_listener_callbacks * callbacks
void(* emptied)(struct ast_taskprocessor_listener *listener)
Indicates the task processor has become empty.
void * datap
The data pointer for the task execute() function.
Definition: taskprocessor.c:54
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.
unsigned int executing
Definition: taskprocessor.c:85
union tps_task::@412 callback
The execute() task callback function pointer.
Local data parameter.
tps_task structure is queued to a taskprocessor
Definition: taskprocessor.c:47
unsigned long max_qsize
This is the maximum number of tasks queued at any one time.
Definition: taskprocessor.c:63
struct tps_taskprocessor_stats stats
Taskprocessor statistics.
Definition: taskprocessor.c:71
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 1109 of file taskprocessor.c.

References ao2_ref, ast_taskprocessor_listener_alloc(), OBJ_KEY, OBJ_NOLOCK, and TPS_REF_IF_EXISTS.

Referenced by ast_dns_system_resolver_init(), ast_msg_init(), AST_TEST_DEFINE(), internal_stasis_subscribe(), and load_module().

1110 {
1111  struct ast_taskprocessor *p;
1112  struct ast_taskprocessor_listener *listener;
1114 
1115  if (ast_strlen_zero(name)) {
1116  ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
1117  return NULL;
1118  }
1119  ao2_lock(tps_singletons);
1120  p = ao2_find(tps_singletons, name, OBJ_KEY | OBJ_NOLOCK);
1121  if (p || (create & TPS_REF_IF_EXISTS)) {
1122  /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
1123  ao2_unlock(tps_singletons);
1124  return p;
1125  }
1126 
1127  /* Create a new taskprocessor. Start by creating a default listener */
1128  pvt = default_listener_pvt_alloc();
1129  if (!pvt) {
1130  ao2_unlock(tps_singletons);
1131  return NULL;
1132  }
1133  listener = ast_taskprocessor_listener_alloc(&default_listener_callbacks, pvt);
1134  if (!listener) {
1135  ao2_unlock(tps_singletons);
1136  default_listener_pvt_destroy(pvt);
1137  return NULL;
1138  }
1139 
1140  p = __allocate_taskprocessor(name, listener);
1141  ao2_unlock(tps_singletons);
1142  p = __start_taskprocessor(p);
1143  ao2_ref(listener, -1);
1144 
1145  return p;
1146 }
A listener for taskprocessors.
#define OBJ_KEY
Definition: astobj2.h:1151
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
struct ast_taskprocessor_listener * ast_taskprocessor_listener_alloc(const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
Allocate a taskprocessor listener.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
return a reference to a taskprocessor ONLY if it already exists
Definition: taskprocessor.h:78
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
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 704 of file taskprocessor.c.

References AST_VECTOR_GET, AST_VECTOR_GET_INDEX, AST_VECTOR_RW_RDLOCK, and AST_VECTOR_RW_UNLOCK.

Referenced by AST_TEST_DEFINE().

705 {
706  struct subsystem_alert *alert;
707  unsigned int count = 0;
708  int idx;
709 
710  AST_VECTOR_RW_RDLOCK(&overloaded_subsystems);
711  idx = AST_VECTOR_GET_INDEX(&overloaded_subsystems, subsystem, subsystem_match);
712  if (idx >= 0) {
713  alert = AST_VECTOR_GET(&overloaded_subsystems, idx);
714  count = alert->alert_count;
715  }
716  AST_VECTOR_RW_UNLOCK(&overloaded_subsystems);
717 
718  return count;
719 }
#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
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 1272 of file taskprocessor.c.

References ast_taskprocessor::suspended.

1273 {
1274  return tps ? tps->suspended : -1;
1275 }
unsigned int suspended
Definition: taskprocessor.c:91
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 1331 of file taskprocessor.c.

References ast_taskprocessor::thread.

Referenced by ast_sip_push_task_wait_serializer().

1332 {
1333  int is_task;
1334 
1335  ao2_lock(tps);
1336  is_task = pthread_equal(tps->thread, pthread_self());
1337  ao2_unlock(tps);
1338  return is_task;
1339 }
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 995 of file taskprocessor.c.

References ast_taskprocessor_listener::callbacks, and ast_taskprocessor_listener::user_data.

Referenced by ast_taskprocessor_get(), and AST_TEST_DEFINE().

996 {
997  struct ast_taskprocessor_listener *listener;
998 
999  listener = ao2_alloc(sizeof(*listener), taskprocessor_listener_dtor);
1000  if (!listener) {
1001  return NULL;
1002  }
1003  listener->callbacks = callbacks;
1004  listener->user_data = user_data;
1005 
1006  return listener;
1007 }
A listener for taskprocessors.
const struct ast_taskprocessor_listener_callbacks * callbacks
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 1009 of file taskprocessor.c.

References ao2_ref, and ast_taskprocessor_listener::tps.

1010 {
1011  ao2_ref(listener->tps, +1);
1012  return listener->tps;
1013 }
struct ast_taskprocessor * tps
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
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 1015 of file taskprocessor.c.

References ast_taskprocessor_listener::user_data.

Referenced by test_emptied(), test_shutdown(), and test_task_pushed().

1016 {
1017  return listener->user_data;
1018 }
const char* ast_taskprocessor_name ( struct ast_taskprocessor tps)

Return the name of the taskprocessor singleton.

Since
1.6.1

Definition at line 971 of file taskprocessor.c.

References ast_taskprocessor::name.

Referenced by ast_sip_get_distributor_serializer().

972 {
973  if (!tps) {
974  ast_log(LOG_ERROR, "no taskprocessor specified!\n");
975  return NULL;
976  }
977  return tps->name;
978 }
char name[0]
Friendly name of the taskprocessor. Subsystem is appended after the name's NULL terminator.
Definition: taskprocessor.c:97
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 1350 of file taskprocessor.c.

References ast_taskprocessor_seq_num().

1351 {
1352  int final_size = strlen(name) + SEQ_STR_SIZE;
1353 
1354  ast_assert(buf != NULL && name != NULL);
1355  ast_assert(final_size <= size);
1356 
1357  snprintf(buf, final_size, "%s-%08x", name, ast_taskprocessor_seq_num());
1358 }
unsigned int ast_taskprocessor_seq_num(void)
Get the next sequence number to create a human friendly taskprocessor name.
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 1240 of file taskprocessor.c.

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(), destroy_conference_bridge(), dns_system_resolver_resolve(), hepv3_send_packet(), and stasis_unsubscribe().

1241 {
1242  return taskprocessor_push(tps, tps_task_alloc(task_exe, datap));
1243 }
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
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 1341 of file taskprocessor.c.

References ast_atomic_fetchadd_int().

Referenced by ast_taskprocessor_build_name(), and ast_taskprocessor_name_append().

1342 {
1343  static int seq_num;
1344 
1345  return (unsigned int) ast_atomic_fetchadd_int(&seq_num, +1);
1346 }
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:757
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 1166 of file taskprocessor.c.

References lock, and SCOPED_AO2LOCK.

Referenced by internal_stasis_subscribe().

1168 {
1169  SCOPED_AO2LOCK(lock, tps);
1170  tps->local_data = local_data;
1171 }
ast_mutex_t lock
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition: lock.h:604
long ast_taskprocessor_size ( struct ast_taskprocessor tps)

Return the current size of the taskprocessor queue.

Since
13.7.0

Definition at line 965 of file taskprocessor.c.

References ast_taskprocessor::tps_queue_size.

Referenced by ast_taskprocessor_execute(), and AST_TEST_DEFINE().

966 {
967  return (tps) ? tps->tps_queue_size : -1;
968 }
long tps_queue_size
Taskprocessor current queue size.
Definition: taskprocessor.c:74
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 1250 of file taskprocessor.c.

References ast_taskprocessor::suspended.

Referenced by AST_TEST_DEFINE().

1251 {
1252  if (tps) {
1253  ao2_lock(tps);
1254  tps->suspended = 1;
1255  ao2_unlock(tps);
1256  return 0;
1257  }
1258  return -1;
1259 }
unsigned int suspended
Definition: taskprocessor.c:91
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 1174 of file taskprocessor.c.

References ao2_ref, ao2_unlink_flags, and OBJ_NOLOCK.

Referenced by ast_msg_shutdown(), ast_taskprocessor_create_with_listener(), AST_TEST_DEFINE(), destroy_conference_bridge(), dns_system_resolver_destroy(), sorcery_object_type_destructor(), and unload_module().

1175 {
1176  if (!tps) {
1177  return NULL;
1178  }
1179 
1180  /* To prevent another thread from finding and getting a reference to this
1181  * taskprocessor we hold the singletons lock. If we didn't do this then
1182  * they may acquire it and find that the listener has been shut down.
1183  */
1184  ao2_lock(tps_singletons);
1185 
1186  if (ao2_ref(tps, -1) > 3) {
1187  ao2_unlock(tps_singletons);
1188  return NULL;
1189  }
1190 
1191  /* If we're down to 3 references, then those must be:
1192  * 1. The reference we just got rid of
1193  * 2. The container
1194  * 3. The listener
1195  */
1196  ao2_unlink_flags(tps_singletons, tps, OBJ_NOLOCK);
1197  ao2_unlock(tps_singletons);
1198 
1199  listener_shutdown(tps->listener);
1200  return NULL;
1201 }
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_unlink_flags(container, obj, flags)
Remove an object from a container.
Definition: astobj2.h:1600
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 1261 of file taskprocessor.c.

References ast_taskprocessor::suspended.

Referenced by AST_TEST_DEFINE().

1262 {
1263  if (tps) {
1264  ao2_lock(tps);
1265  tps->suspended = 0;
1266  ao2_unlock(tps);
1267  return 0;
1268  }
1269  return -1;
1270 }
unsigned int suspended
Definition: taskprocessor.c:91