[asterisk-dev] Re: deadlock in ast_custom_function_register?

Tony Mountifield tony at softins.clara.co.uk
Tue Oct 24 05:17:42 MST 2006


In article <6cf319390610240448v1b327ee1w6792468a06ca8fb0 at mail.gmail.com>,
Yuan Qin <yuan007qin at gmail.com> wrote:
> 
> Hi, all:
> 
>      Asterisk1.2.10,  in pbx.c, the ast_custom_function_register is
> 
> int ast_custom_function_register(struct ast_custom_function *acf)
> {
>     if (!acf)
>         return -1;
> 
>     /* try to lock functions list ... */
>     if (ast_mutex_lock(&acflock)) {
>         ast_log(LOG_ERROR, "Unable to lock function list. Failed registering
> function %s\n", acf->name);
>         return -1;
>     }
> 
>     if (ast_custom_function_find(acf->name)) {
>         ast_log(LOG_ERROR, "Function %s already registered.\n", acf->name);
>         ast_mutex_unlock(&acflock);
>         return -1;
>    }
>    /* ......... */
> }
> 
> struct ast_custom_function* ast_custom_function_find(char *name)
> {
>     struct ast_custom_function *acfptr;
> 
>     /* try to lock functions list ... */
>     if (ast_mutex_lock(&acflock)) {
>         ast_log(LOG_ERROR, "Unable to lock function list\n");
>         return NULL;
>     }
>    /* ....... */
> }
> 
> The mutex acflock is locked twice,  so, the process will be deadlock here,
> but it's not in fact.
> Why? Is there something that I missed?

Asterisk relies on using recursive mutexes. If a process tries to lock
a mutex that it has already locked, it will succeed, and the "lock count"
of the mutex will be incremented. When it unlocks the mutex, the lock
count will be decremented. When this count reaches zero, the mutex will
really be unlocked and available for another process to lock.

Cheers
Tony
-- 
Tony Mountifield
Work: tony at softins.co.uk - http://www.softins.co.uk
Play: tony at mountifield.org - http://tony.mountifield.org


More information about the asterisk-dev mailing list