Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions
transaction.c File Reference
#include "asterisk.h"
#include "asterisk/astobj2.h"
#include "asterisk/sched.h"
#include "asterisk/utils.h"
#include "asterisk/res_aeap.h"
#include "asterisk/res_aeap_message.h"
#include "general.h"
#include "logger.h"
#include "transaction.h"
Include dependency graph for transaction.c:

Go to the source code of this file.

Data Structures

struct  aeap_transaction
 

Macros

#define AEAP_TRANSACTION_BUCKETS   11
 Number of transaction buckets. More...
 

Functions

int aeap_transaction_cancel_timer (struct aeap_transaction *tsx)
 Cancel the transaction timer. More...
 
struct aeap_transactionaeap_transaction_create_and_add (struct ao2_container *transactions, const char *id, struct ast_aeap_tsx_params *params, struct ast_aeap *aeap)
 Create a transaction object, and add it to the given container. More...
 
void aeap_transaction_end (struct aeap_transaction *tsx, int result)
 End a transaction, and remove it from the given container. More...
 
struct aeap_transactionaeap_transaction_get (struct ao2_container *transactions, const char *id)
 Retrieve a transaction for the id from the container. More...
 
void aeap_transaction_params_cleanup (struct ast_aeap_tsx_params *params)
 Clean up parameter references, and possibly call optional user object cleanup. More...
 
int aeap_transaction_result (struct aeap_transaction *tsx)
 Get a transaction's result. More...
 
int aeap_transaction_start (struct aeap_transaction *tsx)
 Start the transaction. More...
 
void * aeap_transaction_user_obj (struct aeap_transaction *tsx)
 Retrieve the user object associated with the transaction. More...
 
struct ao2_containeraeap_transactions_create (void)
 Create an Asterisk external application transactions container. More...
 
 AO2_STRING_FIELD_CMP_FN (aeap_transaction, id)
 
 AO2_STRING_FIELD_HASH_FN (aeap_transaction, id)
 
static struct aeap_transactiontransaction_create (const char *id, struct ast_aeap_tsx_params *params, struct ast_aeap *aeap)
 
static void transaction_destructor (void *obj)
 
static void transaction_end (struct aeap_transaction *tsx, int timed_out, int result)
 
static int transaction_raise_timeout (const void *data)
 
static int transaction_sched_timer (struct aeap_transaction *tsx)
 
static void transaction_wait (struct aeap_transaction *tsx)
 

Macro Definition Documentation

◆ AEAP_TRANSACTION_BUCKETS

#define AEAP_TRANSACTION_BUCKETS   11

Number of transaction buckets.

Definition at line 52 of file transaction.c.

Function Documentation

◆ aeap_transaction_cancel_timer()

int aeap_transaction_cancel_timer ( struct aeap_transaction tsx)

Cancel the transaction timer.

Stops the transaction timer, but does not end/stop the transaction itself

Parameters
tsxA transaction to cancel the timer on
Returns
0 if canceled, non zero otherwise

Definition at line 57 of file transaction.c.

58{
59 if (tsx && tsx->sched_id != -1) {
61 return tsx->sched_id != -1;
62 }
63
64 return 0;
65}
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
struct ast_sched_context * aeap_sched_context(void)
Retrieve the scheduling context.
Definition: general.c:29
#define AST_SCHED_DEL_UNREF(sched, id, refcall)
schedule task to get deleted and call unref function
Definition: sched.h:82

References aeap_sched_context(), ao2_ref, AST_SCHED_DEL_UNREF, and aeap_transaction::sched_id.

Referenced by raise_msg(), transaction_destructor(), and transaction_end().

◆ aeap_transaction_create_and_add()

struct aeap_transaction * aeap_transaction_create_and_add ( struct ao2_container transactions,
const char *  id,
struct ast_aeap_tsx_params params,
struct ast_aeap aeap 
)

Create a transaction object, and add it to the given container.

Parameters
transactionsA transactions container
idAn id to use for the transaction
paramsTransaction parameters
aeapThe aeap object that "owns" this transaction
Returns
0 if successfully create and added, -1 on error

Definition at line 232 of file transaction.c.

234{
235 struct aeap_transaction *tsx;
236
237 tsx = transaction_create(id, params, aeap);
238 if (!tsx) {
239 return NULL;
240 }
241
242 if (!ao2_link(transactions, tsx)) {
243 aeap_error(tsx->aeap, "transaction", "unable to add '%s' to container", id);
244 ao2_ref(tsx, -1);
245 return NULL;
246 }
247
248 /*
249 * Yes, this creates a circular reference. This reference is removed though
250 * upon transaction end. It's assumed here that the given transactions container
251 * takes "ownership", and ultimate responsibility of its contained transactions.
252 * Thus when the given container needs to be unref'ed/freed it must call
253 * aeap_transaction_end for each transaction prior to doing so.
254 */
255 /* tsx->container = ao2_bump(transactions); */
256
257 /*
258 * The transaction needs to know what container manages it, so it can remove
259 * itself from the given container under certain conditions (e.g. transaction
260 * timeout).
261 *
262 * It's expected that the given container will out live any contained transaction
263 * (i.e. the container will not itself be destroyed before ensuring all contained
264 * transactions are ended, and removed). Thus there is no reason to bump the given
265 * container's reference here.
266 */
267 tsx->container = transactions;
268
269 return tsx;
270}
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
#define aeap_error(obj, name, fmt,...)
Log an Asterisk external application error.
#define NULL
Definition: resample.c:96
struct ao2_container * container
Definition: transaction.c:36
struct ast_aeap * aeap
Definition: transaction.c:34
struct ast_aeap_tsx_params params
Definition: transaction.c:46
static struct aeap_transaction * transaction_create(const char *id, struct ast_aeap_tsx_params *params, struct ast_aeap *aeap)
Definition: transaction.c:88

References aeap_transaction::aeap, aeap_error, ao2_link, ao2_ref, aeap_transaction::container, NULL, aeap_transaction::params, and transaction_create().

Referenced by ast_aeap_send_msg_tsx(), and exec().

◆ aeap_transaction_end()

void aeap_transaction_end ( struct aeap_transaction tsx,
int  result 
)

End a transaction, and remove it from the given container.

The "result" parameter is a value representing the state (success/failure, perhaps even something else) of transactional processing upon ending.

Parameters
tsxA transaction to end
resultA result to give to the transaction

Definition at line 217 of file transaction.c.

218{
219 transaction_end(tsx, 0, result);
220}
static PGresult * result
Definition: cel_pgsql.c:84
static void transaction_end(struct aeap_transaction *tsx, int timed_out, int result)
Definition: transaction.c:122

References result, and transaction_end().

Referenced by ast_aeap_send_msg_tsx(), end_transaction(), exec(), raise_msg(), and tsx_end().

◆ aeap_transaction_get()

struct aeap_transaction * aeap_transaction_get ( struct ao2_container transactions,
const char *  id 
)

Retrieve a transaction for the id from the container.

Parameters
transactionsA transactions container
idA transaction id
Returns
an AEAP transaction object, NULL if no transaction is found

Definition at line 212 of file transaction.c.

213{
214 return ao2_find(transactions, id, OBJ_SEARCH_KEY);
215}
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
@ OBJ_SEARCH_KEY
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101

References ao2_find, and OBJ_SEARCH_KEY.

Referenced by raise_msg().

◆ aeap_transaction_params_cleanup()

void aeap_transaction_params_cleanup ( struct ast_aeap_tsx_params params)

Clean up parameter references, and possibly call optional user object cleanup.

Parameters
paramsTransaction parameters

Definition at line 67 of file transaction.c.

68{
69 ao2_cleanup(params->msg);
70
71 if (params->obj_cleanup) {
72 params->obj_cleanup(params->obj);
73 }
74}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
struct ast_aeap_message * msg
Definition: res_aeap.h:333
ast_aeap_user_obj_cleanup obj_cleanup
Definition: res_aeap.h:350

References ao2_cleanup, ast_aeap_tsx_params::msg, ast_aeap_tsx_params::obj, and ast_aeap_tsx_params::obj_cleanup.

Referenced by ast_aeap_send_msg_tsx(), transaction_create(), and transaction_destructor().

◆ aeap_transaction_result()

int aeap_transaction_result ( struct aeap_transaction tsx)

Get a transaction's result.

A transaction's result is a value that represents the relative success (0), or failure (-1) of a transaction. For example, a timeout is considered a failure and will elicit a -1.

This value though is also dependent upon the result of the message handler associated with the transaction. Meaning if an associated message is handled, then its result is stored as the transaction result and returned here.

Parameters
tsxA transaction object
Returns
The transaction result

Definition at line 222 of file transaction.c.

223{
224 return tsx->result;
225}

References aeap_transaction::result.

Referenced by ast_aeap_send_msg_tsx().

◆ aeap_transaction_start()

int aeap_transaction_start ( struct aeap_transaction tsx)

Start the transaction.

Parameters
tsxThe transaction to initiate
Returns
0 if successfully raised, and handled. Otherwise non zero.

Definition at line 198 of file transaction.c.

199{
200 if (transaction_sched_timer(tsx)) {
201 return -1;
202 }
203
204 if (tsx->params.wait) {
205 /* Wait until transaction completes, or times out */
206 transaction_wait(tsx);
207 }
208
209 return 0;
210}
static int transaction_sched_timer(struct aeap_transaction *tsx)
Definition: transaction.c:170
static void transaction_wait(struct aeap_transaction *tsx)
Definition: transaction.c:187

References aeap_transaction::params, transaction_sched_timer(), transaction_wait(), and ast_aeap_tsx_params::wait.

Referenced by ast_aeap_send_msg_tsx(), and exec().

◆ aeap_transaction_user_obj()

void * aeap_transaction_user_obj ( struct aeap_transaction tsx)

Retrieve the user object associated with the transaction.

Parameters
tsxA transaction object
Returns
A user object, or NULL if non associated

Definition at line 227 of file transaction.c.

228{
229 return tsx->params.obj;
230}

References ast_aeap_tsx_params::obj, and aeap_transaction::params.

Referenced by end_transaction(), and raise_msg().

◆ aeap_transactions_create()

struct ao2_container * aeap_transactions_create ( void  )

Create an Asterisk external application transactions container.

Returns
A transaction object, or NULL on error

Definition at line 272 of file transaction.c.

273{
274 struct ao2_container *transactions;
275
277 aeap_transaction_hash_fn, NULL, aeap_transaction_cmp_fn);
278 if (!transactions) {
279 ast_log(LOG_ERROR, "AEAP transaction: unable to create container\n");
280 return NULL;
281 }
282
283 return transactions;
284}
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
#define LOG_ERROR
Generic container type.
#define AEAP_TRANSACTION_BUCKETS
Number of transaction buckets.
Definition: transaction.c:52

References AEAP_TRANSACTION_BUCKETS, AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ast_log, LOG_ERROR, and NULL.

Referenced by ast_aeap_create(), and exec().

◆ AO2_STRING_FIELD_CMP_FN()

AO2_STRING_FIELD_CMP_FN ( aeap_transaction  ,
id   
)

◆ AO2_STRING_FIELD_HASH_FN()

AO2_STRING_FIELD_HASH_FN ( aeap_transaction  ,
id   
)

◆ transaction_create()

static struct aeap_transaction * transaction_create ( const char *  id,
struct ast_aeap_tsx_params params,
struct ast_aeap aeap 
)
static

Definition at line 88 of file transaction.c.

90{
91 struct aeap_transaction *tsx;
92
93 if (!id) {
94 aeap_error(aeap, "transaction", "missing transaction id");
96 return NULL;
97 }
98
99 tsx = ao2_alloc(sizeof(*tsx) + strlen(id) + 1, transaction_destructor);
100 if (!tsx) {
101 aeap_error(aeap, "transaction", "unable to create for '%s'", id);
103 return NULL;
104 }
105
106 strcpy(tsx->id, id); /* safe */
107 tsx->sched_id = -1;
108
110
111 /*
112 * Currently, transactions, and their lifetimes are fully managed by the given 'aeap'
113 * object, so do not bump its reference here as we want the 'aeap' object to stop
114 * transactions and not transactions potentially stopping the 'aeap' object.
115 */
116 tsx->aeap = aeap;
117 tsx->params = *params;
118
119 return tsx;
120}
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#define ast_cond_init(cond, attr)
Definition: lock.h:201
ast_cond_t handled_cond
Definition: transaction.c:42
static void transaction_destructor(void *obj)
Definition: transaction.c:76
void aeap_transaction_params_cleanup(struct ast_aeap_tsx_params *params)
Clean up parameter references, and possibly call optional user object cleanup.
Definition: transaction.c:67

References aeap_transaction::aeap, aeap_error, aeap_transaction_params_cleanup(), ao2_alloc, ast_cond_init, aeap_transaction::handled_cond, aeap_transaction::id, NULL, aeap_transaction::params, aeap_transaction::sched_id, and transaction_destructor().

Referenced by aeap_transaction_create_and_add().

◆ transaction_destructor()

static void transaction_destructor ( void *  obj)
static

Definition at line 76 of file transaction.c.

77{
78 struct aeap_transaction *tsx = obj;
79
80 /* Ensure timer is canceled */
82
84
86}
#define ast_cond_destroy(cond)
Definition: lock.h:202
int aeap_transaction_cancel_timer(struct aeap_transaction *tsx)
Cancel the transaction timer.
Definition: transaction.c:57

References aeap_transaction_cancel_timer(), aeap_transaction_params_cleanup(), ast_cond_destroy, aeap_transaction::handled_cond, and aeap_transaction::params.

Referenced by transaction_create().

◆ transaction_end()

static void transaction_end ( struct aeap_transaction tsx,
int  timed_out,
int  result 
)
static

Definition at line 122 of file transaction.c.

123{
124 if (!tsx) {
125 return;
126 }
127
128 ao2_lock(tsx);
129
130 tsx->result = result;
131
132 if (tsx->container) {
133 ao2_unlink(tsx->container, tsx);
134 tsx->container = NULL;
135 }
136
137 if (!timed_out) {
139 } else if (tsx->sched_id != -1) {
140 tsx->sched_id = -1;
141 }
142
143 if (!tsx->handled) {
144 if (timed_out) {
145 if (tsx->params.on_timeout) {
146 tsx->params.on_timeout(tsx->aeap, tsx->params.msg, tsx->params.obj);
147 } else {
148 aeap_error(tsx->aeap, "transaction", "message '%s' timed out",
150 }
151 }
152
153 tsx->handled = 1;
155 }
156
157 ao2_unlock(tsx);
158
159 ao2_ref(tsx, -1);
160}
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
#define ast_cond_signal(cond)
Definition: lock.h:203
const char * ast_aeap_message_name(const struct ast_aeap_message *message)
Retrieve a message name.
ast_aeap_on_timeout on_timeout
Definition: res_aeap.h:337

References aeap_transaction::aeap, aeap_error, aeap_transaction_cancel_timer(), ao2_lock, ao2_ref, ao2_unlink, ao2_unlock, ast_aeap_message_name(), ast_cond_signal, aeap_transaction::container, aeap_transaction::handled, aeap_transaction::handled_cond, ast_aeap_tsx_params::msg, NULL, ast_aeap_tsx_params::obj, ast_aeap_tsx_params::on_timeout, aeap_transaction::params, result, aeap_transaction::result, and aeap_transaction::sched_id.

Referenced by aeap_transaction_end(), and transaction_raise_timeout().

◆ transaction_raise_timeout()

static int transaction_raise_timeout ( const void *  data)
static

Definition at line 162 of file transaction.c.

163{
164 /* Ref added added at timer creation removed in end call */
165 transaction_end((struct aeap_transaction *)data, 1, -1);
166
167 return 0;
168}

References transaction_end().

Referenced by transaction_sched_timer().

◆ transaction_sched_timer()

static int transaction_sched_timer ( struct aeap_transaction tsx)
static

Definition at line 170 of file transaction.c.

171{
172 if (tsx->params.timeout <= 0 || tsx->sched_id != -1) {
173 return 0;
174 }
175
178 if (tsx->sched_id == -1) {
179 aeap_error(tsx->aeap, "transaction", "unable to schedule timeout for '%s'", tsx->id);
180 ao2_ref(tsx, -1);
181 return -1;
182 }
183
184 return 0;
185}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
int ast_sched_add(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data) attribute_warn_unused_result
Adds a scheduled event.
Definition: sched.c:567
static int transaction_raise_timeout(const void *data)
Definition: transaction.c:162

References aeap_transaction::aeap, aeap_error, aeap_sched_context(), ao2_bump, ao2_ref, ast_sched_add(), aeap_transaction::id, aeap_transaction::params, aeap_transaction::sched_id, ast_aeap_tsx_params::timeout, and transaction_raise_timeout().

Referenced by aeap_transaction_start().

◆ transaction_wait()

static void transaction_wait ( struct aeap_transaction tsx)
static

Definition at line 187 of file transaction.c.

188{
189 ao2_lock(tsx);
190
191 while (!tsx->handled) {
193 }
194
195 ao2_unlock(tsx);
196}
void * ao2_object_get_lockaddr(void *obj)
Return the mutex lock address of an object.
Definition: astobj2.c:476
#define ast_cond_wait(cond, mutex)
Definition: lock.h:205

References ao2_lock, ao2_object_get_lockaddr(), ao2_unlock, ast_cond_wait, aeap_transaction::handled, and aeap_transaction::handled_cond.

Referenced by aeap_transaction_start().