[asterisk-dev] ast_mutex_t usage

Tilghman Lesher tlesher at digium.com
Sat Feb 27 12:57:42 CST 2010


On Friday 26 February 2010 15:19:41 Alex Massover wrote:
> Hello,
>
> Let me rephrase the question, maybe someone will help me :)
>
> Is there any chance that ast_mutex in module will “miss”, for example
> because of multiple processes (fork() in core maybe)?
>
> From: asterisk-dev-bounces at lists.digium.com
> [mailto:asterisk-dev-bounces at lists.digium.com] On Behalf Of Alex Massover
> Sent: יום ה 25 פברואר 2010 19:58
> To: asterisk-dev at lists.digium.com
> Subject: [asterisk-dev] ast_mutex_t usage
>
> Hello,
>
> I have a custom module, that exports foo(), the problem is that foo() is
> not safe-thread, i.e. calls to it should be serialized.
>
> I tried to do it with ast_mutex like following:
>
> AST_MUTEX_DEFINE_STATIC(mp);
>
> static int foo(…..)
> {
> ….
> ast_mutex_lock(&mp);
> [non-reentrant code here]
> ast_mutex_unlock(&mp);
> …..
> }
>
>
> But I still see  that [non-reentrant] part is entered more than once
> concurrently. Each channel can call foo() only once in dialplan, so it
> looks like other threads/channels are entering it.
>
> Do I miss something please? Maybe there’re a better ways to serialize a
> calls to module function?

No, that's the correct way.  What's your evidence that the critical section is
being entered concurrently?

-- 
Tilghman Lesher
Digium, Inc. | Senior Software Developer
twitter: Corydon76 | IRC: Corydon76-dig (Freenode)
Check us out at: www.digium.com & www.asterisk.org



More information about the asterisk-dev mailing list