[Asterisk-Dev] Variables - Race Condition
brian k. west
brian at bkw.org
Wed May 26 20:39:22 MST 2004
Please post this to bugs.digium.com
Thanks,
Brian
----- Original Message -----
From: "Daniel Bichara" <daniel at bichara.com.br>
To: <asterisk-dev at lists.digium.com>
Sent: Wednesday, May 26, 2004 6:25 AM
Subject: [Asterisk-Dev] Variables - Race Condition
> Hi all,
>
> I am testing * running with multiple channels. In my tests, using lot of
> variables (global and locals) with extensions and contexts, I found a
> potencial race condition while updating variables values and/or getting
it.
>
> I produced the attached patch that eliminated segmentation faults during
> my tests. Running it with 30 channels there was no performance penalties.
>
> Daniel
>
----------------------------------------------------------------------------
----
> --- asterisk-0.7.1/pbx.c 2004-05-19 20:07:44.000000000 +0000
> +++ asterisk-0.7.1.new/pbx.c 2004-05-11 16:02:21.000000000 +0000
> @@ -348,6 +348,8 @@
>
> };
>
> +static ast_mutex_t varlock = AST_MUTEX_INITIALIZER;
> +
> /* Lock for the application list */
> static ast_mutex_t applock = AST_MUTEX_INITIALIZER;
> static struct ast_context *contexts = NULL;
> @@ -889,6 +891,7 @@
> *ret = workspace;
> } else {
> if (c) {
> + ast_mutex_lock(&varlock);
> AST_LIST_TRAVERSE(headp,variables,entries) {
> #if 0
> ast_log(LOG_WARNING,"Comparing variable '%s' with
'%s'\n",var,ast_var_name(variables));
> @@ -901,9 +904,11 @@
> }
> }
> }
> + ast_mutex_unlock(&varlock);
> }
> if (!(*ret)) {
> /* Try globals */
> + ast_mutex_lock(&varlock);
> AST_LIST_TRAVERSE(&globals,variables,entries) {
> #if 0
> ast_log(LOG_WARNING,"Comparing variable '%s' with
'%s'\n",var,ast_var_name(variables));
> @@ -916,6 +921,7 @@
> }
> }
> }
> + ast_mutex_unlock(&varlock);
> }
> if (!(*ret)) {
> int len=strlen(var);
> @@ -4384,6 +4390,7 @@
> char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name) {
> struct ast_var_t *variables;
> struct varshead *headp;
> + char *ret;
>
> if (chan)
> headp=&chan->varshead;
> @@ -4391,18 +4398,26 @@
> headp=&globals;
>
> if (name) {
> + ast_mutex_lock(&varlock);
> AST_LIST_TRAVERSE(headp,variables,entries) {
> - if (!strcmp(name, ast_var_name(variables)))
> - return ast_var_value(variables);
> + if (!strcmp(name, ast_var_name(variables))){
> + ret=ast_var_value(variables);
> + ast_mutex_unlock(&varlock);
> + return ret;
> + }
> }
> if (headp != &globals) {
> /* Check global variables if we haven't already */
> headp = &globals;
> AST_LIST_TRAVERSE(headp,variables,entries) {
> - if (!strcmp(name, ast_var_name(variables)))
> - return ast_var_value(variables);
> + if (!strcmp(name, ast_var_name(variables))) {
> + ret=ast_var_value(variables);
> + ast_mutex_unlock(&varlock);
> + return ret;
> + }
> }
> }
> + ast_mutex_unlock(&varlock);
> }
> return NULL;
> }
> @@ -4414,6 +4429,8 @@
> headp=&chan->varshead;
> else
> headp=&globals;
> +
> + ast_mutex_lock(&varlock);
>
> AST_LIST_TRAVERSE (headp,newvariable,entries) {
> if (strcasecmp(ast_var_name(newvariable),name)==0) {
> @@ -4430,6 +4447,7 @@
> newvariable=ast_var_assign(name,value);
> AST_LIST_INSERT_HEAD(headp,newvariable,entries);
> }
> + ast_mutex_unlock(&varlock);
> }
>
> int pbx_builtin_setvar(struct ast_channel *chan, void *data)
>
More information about the asterisk-dev
mailing list