- convert the lists of switches to use the linked list macros

- remove some checks of the result of ast_mutex_lock, since it is not necessary
   (this would be a good project to add to the janitor projects list).


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@25443 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Russell Bryant 20 years ago
parent 05ea5482d0
commit 0fd612930a

@ -25,6 +25,7 @@
#include "asterisk/sched.h" #include "asterisk/sched.h"
#include "asterisk/channel.h" #include "asterisk/channel.h"
#include "asterisk/linkedlists.h"
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
extern "C" { extern "C" {
@ -80,7 +81,7 @@ typedef int (ast_switch_f)(struct ast_channel *chan, const char *context,
/*!< Data structure associated with an Asterisk switch */ /*!< Data structure associated with an Asterisk switch */
struct ast_switch { struct ast_switch {
struct ast_switch *next; AST_LIST_ENTRY(ast_switch) list;
const char *name; /*!< Name of the switch */ const char *name; /*!< Name of the switch */
const char *description; /*!< Description of the switch */ const char *description; /*!< Description of the switch */

210
pbx.c

@ -144,7 +144,7 @@ struct ast_sw {
const char *registrar; /*!< Registrar */ const char *registrar; /*!< Registrar */
char *data; /*!< Data load */ char *data; /*!< Data load */
int eval; int eval;
struct ast_sw *next; /*!< Link them together */ AST_LIST_ENTRY(ast_sw) list;
char *tmpdata; char *tmpdata;
char stuff[0]; char stuff[0];
}; };
@ -164,7 +164,7 @@ struct ast_context {
struct ast_include *includes; /*!< Include other contexts */ struct ast_include *includes; /*!< Include other contexts */
struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */ struct ast_ignorepat *ignorepats; /*!< Patterns for which to continue playing dialtone */
const char *registrar; /*!< Registrar */ const char *registrar; /*!< Registrar */
struct ast_sw *alts; /*!< Alternative switches */ AST_LIST_HEAD_NOLOCK(, ast_sw) alts; /*!< Alternative switches */
char name[0]; /*!< Name of the context */ char name[0]; /*!< Name of the context */
}; };
@ -466,8 +466,7 @@ AST_MUTEX_DEFINE_STATIC(conlock); /*!< Lock for the ast_context list */
static AST_LIST_HEAD_STATIC(apps, ast_app); static AST_LIST_HEAD_STATIC(apps, ast_app);
struct ast_switch *switches = NULL; static AST_LIST_HEAD_STATIC(switches, ast_switch);
AST_MUTEX_DEFINE_STATIC(switchlock); /*!< Lock for switches */
static int stateid = 1; static int stateid = 1;
/* WARNING: /* WARNING:
@ -544,15 +543,13 @@ static struct ast_switch *pbx_findswitch(const char *sw)
{ {
struct ast_switch *asw; struct ast_switch *asw;
if (ast_mutex_lock(&switchlock)) { AST_LIST_LOCK(&switches);
ast_log(LOG_WARNING, "Unable to obtain application lock\n"); AST_LIST_TRAVERSE(&switches, asw, list) {
return NULL;
}
for (asw = switches; asw; asw = asw->next) {
if (!strcasecmp(asw->name, sw)) if (!strcasecmp(asw->name, sw))
break; break;
} }
ast_mutex_unlock(&switchlock); AST_LIST_UNLOCK(&switches);
return asw; return asw;
} }
@ -797,7 +794,7 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
return NULL; return NULL;
} }
/* Check alternative switches */ /* Check alternative switches */
for (sw = tmp->alts; sw; sw = sw->next) { AST_LIST_TRAVERSE(&tmp->alts, sw, list) {
if ((asw = pbx_findswitch(sw->name))) { if ((asw = pbx_findswitch(sw->name))) {
/* Substitute variables now */ /* Substitute variables now */
if (sw->eval) if (sw->eval)
@ -1466,13 +1463,8 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
char atmp[80]; char atmp[80];
char atmp2[EXT_DATA_SIZE+100]; char atmp2[EXT_DATA_SIZE+100];
if (ast_mutex_lock(&conlock)) { ast_mutex_lock(&conlock);
ast_log(LOG_WARNING, "Unable to obtain lock\n");
if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH) || (action == HELPER_MATCHMORE))
return 0;
else
return -1;
}
e = pbx_find_extension(c, con, context, exten, priority, label, callerid, action, incstack, &stacklen, &status, &sw, &data, &foundcontext); e = pbx_find_extension(c, con, context, exten, priority, label, callerid, action, incstack, &stacklen, &status, &sw, &data, &foundcontext);
if (e) { if (e) {
switch(action) { switch(action) {
@ -1599,12 +1591,10 @@ static struct ast_exten *ast_hint_extension(struct ast_channel *c, const char *c
char *incstack[AST_PBX_MAX_STACK]; char *incstack[AST_PBX_MAX_STACK];
int stacklen = 0; int stacklen = 0;
if (ast_mutex_lock(&conlock)) { ast_mutex_lock(&conlock);
ast_log(LOG_WARNING, "Unable to obtain lock\n");
return NULL;
}
e = pbx_find_extension(c, NULL, context, exten, PRIORITY_HINT, NULL, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data, &foundcontext); e = pbx_find_extension(c, NULL, context, exten, PRIORITY_HINT, NULL, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data, &foundcontext);
ast_mutex_unlock(&conlock); ast_mutex_unlock(&conlock);
return e; return e;
} }
@ -2357,16 +2347,14 @@ int pbx_set_autofallthrough(int newval)
static struct ast_context *find_context_locked(const char *context) static struct ast_context *find_context_locked(const char *context)
{ {
struct ast_context *c = NULL; struct ast_context *c = NULL;
if (ast_lock_contexts()) {
errno = EBUSY; ast_lock_contexts();
return NULL;
}
while ( (c = ast_walk_contexts(c)) ) { while ( (c = ast_walk_contexts(c)) ) {
if (!strcmp(ast_get_context_name(c), context)) if (!strcmp(ast_get_context_name(c), context))
return c; return c;
} }
ast_unlock_contexts(); ast_unlock_contexts();
errno = ENOENT;
return NULL; return NULL;
} }
@ -2453,27 +2441,26 @@ int ast_context_remove_switch(const char *context, const char *sw, const char *d
*/ */
int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar) int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar)
{ {
struct ast_sw *i, *pi = NULL; struct ast_sw *i;
int ret = -1; int ret = -1;
if (ast_mutex_lock(&con->lock)) ast_mutex_lock(&con->lock);
return -1;
/* walk switchs */ /* walk switches */
for (i = con->alts; i; pi = i, i = i->next) { AST_LIST_TRAVERSE_SAFE_BEGIN(&con->alts, i, list) {
if (!strcmp(i->name, sw) && !strcmp(i->data, data) && if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
(!registrar || !strcmp(i->registrar, registrar))) { (!registrar || !strcmp(i->registrar, registrar))) {
/* found, remove from list */ /* found, remove from list */
if (pi) AST_LIST_REMOVE_CURRENT(&con->alts, list);
pi->next = i->next;
else
con->alts = i->next;
free(i); /* free switch and return */ free(i); /* free switch and return */
ret = 0; ret = 0;
break; break;
} }
} }
AST_LIST_TRAVERSE_SAFE_END
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
return ret; return ret;
} }
@ -2508,8 +2495,7 @@ int ast_context_remove_extension2(struct ast_context *con, const char *extension
{ {
struct ast_exten *exten, *prev_exten = NULL; struct ast_exten *exten, *prev_exten = NULL;
if (ast_mutex_lock(&con->lock)) ast_mutex_lock(&con->lock);
return -1;
/* go through all extensions in context and search the right one ... */ /* go through all extensions in context and search the right one ... */
exten = con->root; exten = con->root;
@ -2662,47 +2648,27 @@ int ast_register_application(const char *app, int (*execute)(struct ast_channel
*/ */
int ast_register_switch(struct ast_switch *sw) int ast_register_switch(struct ast_switch *sw)
{ {
struct ast_switch *tmp, *prev=NULL; struct ast_switch *tmp;
if (ast_mutex_lock(&switchlock)) {
ast_log(LOG_ERROR, "Unable to lock switch lock\n"); AST_LIST_LOCK(&switches);
return -1; AST_LIST_TRAVERSE(&switches, tmp, list) {
} if (!strcasecmp(tmp->name, sw->name)) {
for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) { AST_LIST_UNLOCK(&switches);
if (!strcasecmp(tmp->name, sw->name))
break;
}
if (tmp) {
ast_mutex_unlock(&switchlock);
ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name); ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
return -1; return -1;
} }
sw->next = NULL; }
if (prev) AST_LIST_INSERT_TAIL(&switches, sw, list);
prev->next = sw; AST_LIST_UNLOCK(&switches);
else
switches = sw;
ast_mutex_unlock(&switchlock);
return 0; return 0;
} }
void ast_unregister_switch(struct ast_switch *sw) void ast_unregister_switch(struct ast_switch *sw)
{ {
struct ast_switch *tmp, *prev=NULL; AST_LIST_LOCK(&switches);
if (ast_mutex_lock(&switchlock)) { AST_LIST_REMOVE(&switches, sw, list);
ast_log(LOG_ERROR, "Unable to lock switch lock\n"); AST_LIST_UNLOCK(&switches);
return;
}
for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) {
if (tmp == sw) {
if (prev)
prev->next = tmp->next;
else
switches = tmp->next;
tmp->next = NULL;
break;
}
}
ast_mutex_unlock(&switchlock);
} }
/* /*
@ -2863,10 +2829,7 @@ static int handle_show_hints(int fd, int argc, char *argv[])
} }
/* ... we have hints ... */ /* ... we have hints ... */
ast_cli(fd, "\n -= Registered Asterisk Dial Plan Hints =-\n"); ast_cli(fd, "\n -= Registered Asterisk Dial Plan Hints =-\n");
if (AST_LIST_LOCK(&hints)) { AST_LIST_LOCK(&hints);
ast_log(LOG_ERROR, "Unable to lock hints\n");
return -1;
}
AST_LIST_TRAVERSE(&hints, hint, list) { AST_LIST_TRAVERSE(&hints, hint, list) {
watchers = 0; watchers = 0;
for (watcher = hint->callbacks; watcher; watcher = watcher->next) for (watcher = hint->callbacks; watcher; watcher = watcher->next)
@ -2888,19 +2851,21 @@ static int handle_show_hints(int fd, int argc, char *argv[])
static int handle_show_switches(int fd, int argc, char *argv[]) static int handle_show_switches(int fd, int argc, char *argv[])
{ {
struct ast_switch *sw; struct ast_switch *sw;
if (!switches) {
AST_LIST_LOCK(&switches);
if (AST_LIST_EMPTY(&switches)) {
AST_LIST_UNLOCK(&switches);
ast_cli(fd, "There are no registered alternative switches\n"); ast_cli(fd, "There are no registered alternative switches\n");
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
/* ... we have applications ... */
ast_cli(fd, "\n -= Registered Asterisk Alternative Switches =-\n"); ast_cli(fd, "\n -= Registered Asterisk Alternative Switches =-\n");
if (ast_mutex_lock(&switchlock)) { AST_LIST_TRAVERSE(&switches, sw, list)
ast_log(LOG_ERROR, "Unable to lock switches\n");
return -1;
}
for (sw = switches; sw; sw = sw->next)
ast_cli(fd, "%s: %s\n", sw->name, sw->description); ast_cli(fd, "%s: %s\n", sw->name, sw->description);
ast_mutex_unlock(&switchlock);
AST_LIST_UNLOCK(&switches);
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
@ -2998,11 +2963,7 @@ static char *complete_show_dialplan_context(const char *line, const char *word,
if (pos != 2) if (pos != 2)
return NULL; return NULL;
/* try to lock contexts list ... */ ast_lock_contexts();
if (ast_lock_contexts()) {
ast_log(LOG_ERROR, "Unable to lock context list\n");
return NULL;
}
wordlen = strlen(word); wordlen = strlen(word);
@ -3014,8 +2975,8 @@ static char *complete_show_dialplan_context(const char *line, const char *word,
} }
} }
/* ... unlock and return */
ast_unlock_contexts(); ast_unlock_contexts();
return ret; return ret;
} }
@ -3032,11 +2993,7 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
struct ast_context *c = NULL; struct ast_context *c = NULL;
int res = 0, old_total_exten = dpc->total_exten; int res = 0, old_total_exten = dpc->total_exten;
/* try to lock contexts */ ast_lock_contexts();
if (ast_lock_contexts()) {
ast_log(LOG_WARNING, "Failed to lock contexts list\n");
return -1;
}
/* walk all contexts ... */ /* walk all contexts ... */
while ( (c = ast_walk_contexts(c)) ) { while ( (c = ast_walk_contexts(c)) ) {
@ -3045,8 +3002,8 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
!strcmp(ast_get_context_name(c), context)) { !strcmp(ast_get_context_name(c), context)) {
dpc->context_existence = 1; dpc->context_existence = 1;
/* try to lock context before walking in ... */ ast_lock_context(c);
if (!ast_lock_context(c)) { /* XXX re-indent this block */
struct ast_exten *e; struct ast_exten *e;
struct ast_include *i; struct ast_include *i;
struct ast_ignorepat *ip; struct ast_ignorepat *ip;
@ -3203,7 +3160,6 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
if (context_info_printed) ast_cli(fd, "\r\n"); if (context_info_printed) ast_cli(fd, "\r\n");
} }
} }
}
ast_unlock_contexts(); ast_unlock_contexts();
return (dpc->total_exten == old_total_exten) ? -1 : res; return (dpc->total_exten == old_total_exten) ? -1 : res;
@ -3800,12 +3756,7 @@ int ast_context_add_include2(struct ast_context *con, const char *value,
new_include->next = NULL; new_include->next = NULL;
new_include->registrar = registrar; new_include->registrar = registrar;
/* ... try to lock this context ... */ ast_mutex_lock(&con->lock);
if (ast_mutex_lock(&con->lock)) {
free(new_include);
errno = EBUSY;
return -1;
}
/* ... go to last include and check if context is already included too... */ /* ... go to last include and check if context is already included too... */
for (i = con->includes; i; i = i->next) { for (i = con->includes; i; i = i->next) {
@ -3858,7 +3809,7 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
const char *data, int eval, const char *registrar) const char *data, int eval, const char *registrar)
{ {
struct ast_sw *new_sw; struct ast_sw *new_sw;
struct ast_sw *i, *il = NULL; /* sw, sw_last */ struct ast_sw *i;
int length; int length;
char *p; char *p;
@ -3892,35 +3843,28 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
} }
if (eval) if (eval)
new_sw->tmpdata = p; new_sw->tmpdata = p;
new_sw->next = NULL;
new_sw->eval = eval; new_sw->eval = eval;
new_sw->registrar = registrar; new_sw->registrar = registrar;
/* ... try to lock this context ... */ /* ... try to lock this context ... */
if (ast_mutex_lock(&con->lock)) { ast_mutex_lock(&con->lock);
free(new_sw);
errno = EBUSY;
return -1;
}
/* ... go to last sw and check if context is already swd too... */ /* ... go to last sw and check if context is already swd too... */
for (i = con->alts; i; i = i->next) { AST_LIST_TRAVERSE(&con->alts, i, list) {
if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) { if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
free(new_sw); free(new_sw);
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
errno = EEXIST; errno = EEXIST;
return -1; return -1;
} }
il = i;
} }
/* ... sw new context into context list, unlock, return */ /* ... sw new context into context list, unlock, return */
if (il) AST_LIST_INSERT_TAIL(&con->alts, new_sw, list);
il->next = new_sw;
else
con->alts = new_sw;
if (option_verbose > 2) if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_3 "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con)); ast_verbose(VERBOSE_PREFIX_3 "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con));
ast_mutex_unlock(&con->lock); ast_mutex_unlock(&con->lock);
return 0; return 0;
@ -3946,10 +3890,7 @@ int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat
{ {
struct ast_ignorepat *ip, *ipl = NULL; struct ast_ignorepat *ip, *ipl = NULL;
if (ast_mutex_lock(&con->lock)) { ast_mutex_lock(&con->lock);
errno = EBUSY;
return -1;
}
for (ip = con->ignorepats; ip; ip = ip->next) { for (ip = con->ignorepats; ip; ip = ip->next) {
if (!strcmp(ip->pattern, ignorepat) && if (!strcmp(ip->pattern, ignorepat) &&
@ -4252,14 +4193,7 @@ int ast_add_extension2(struct ast_context *con,
tmp->datad = datad; tmp->datad = datad;
tmp->registrar = registrar; tmp->registrar = registrar;
if (ast_mutex_lock(&con->lock)) { ast_mutex_lock(&con->lock);
free(tmp);
/* And properly destroy the data */
datad(data);
ast_log(LOG_WARNING, "Failed to lock context '%s'\n", con->name);
errno = EBUSY;
return -1;
}
for (e = con->root; e; e = e->next) { for (e = con->root; e; e = e->next) {
/* Make sure patterns are always last! */ /* Make sure patterns are always last! */
if ((e->exten[0] != '_') && (extension[0] == '_')) if ((e->exten[0] != '_') && (extension[0] == '_'))
@ -4819,7 +4753,7 @@ void __ast_context_destroy(struct ast_context *con, const char *registrar)
{ {
struct ast_context *tmp, *tmpl=NULL; struct ast_context *tmp, *tmpl=NULL;
struct ast_include *tmpi, *tmpil= NULL; struct ast_include *tmpi, *tmpil= NULL;
struct ast_sw *sw, *swl= NULL; struct ast_sw *sw;
struct ast_exten *e, *el, *en; struct ast_exten *e, *el, *en;
struct ast_ignorepat *ipi, *ipl = NULL; struct ast_ignorepat *ipi, *ipl = NULL;
@ -4830,10 +4764,7 @@ void __ast_context_destroy(struct ast_context *con, const char *registrar)
(!registrar || !strcasecmp(registrar, tmp->registrar))) { (!registrar || !strcasecmp(registrar, tmp->registrar))) {
/* Okay, let's lock the structure to be sure nobody else /* Okay, let's lock the structure to be sure nobody else
is searching through it. */ is searching through it. */
if (ast_mutex_lock(&tmp->lock)) { ast_mutex_lock(&tmp->lock);
ast_log(LOG_WARNING, "Unable to lock context lock\n");
break;
}
if (tmpl) if (tmpl)
tmpl->next = tmp->next; tmpl->next = tmp->next;
else else
@ -4853,13 +4784,8 @@ void __ast_context_destroy(struct ast_context *con, const char *registrar)
ipi = ipi->next; ipi = ipi->next;
free(ipl); free(ipl);
} }
for (sw = tmp->alts; sw; ) { while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
/* Free switches */ free(sw);
swl = sw;
sw = sw->next;
free(swl);
swl = sw;
}
for (e = tmp->root; e;) { for (e = tmp->root; e;) {
for (en = e->peer; en;) { for (en = e->peer; en;) {
el = en; el = en;
@ -5734,9 +5660,9 @@ struct ast_sw *ast_walk_context_switches(struct ast_context *con,
struct ast_sw *sw) struct ast_sw *sw)
{ {
if (!sw) if (!sw)
return con ? con->alts : NULL; return con ? AST_LIST_FIRST(&con->alts) : NULL;
else else
return sw->next; return AST_LIST_NEXT(sw, list);
} }
struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten, struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,

Loading…
Cancel
Save