You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Henry Jen <he...@ztune.net> on 2005/07/21 03:26:55 UTC

Recursive mutex

Hi,

I need to have recursive(NESTED) mutex support in our project.

Currently apr is relying on pthread lib to provide 
pthread_mutexattr_settype to provide recursive support, unfortunately, 
that does not seem to be in POSIX standard and is not available on the 
requested linux platform(see 
http://www.lynuxworks.com/products/posix/function-calls.php3).

I plan to implement that in apr by extend current apr_thread_mutex_t to 
include a pthread_t and an integer for count, and modify the lock/unlock 
accordingly. So here are couple questions:

1. Is this a reasonable solution or I overlook something? Or maybe 
someone had done similar?

2. How fast can this modification can be rolled into a formal apr 
release? It is going to be API compatible but not ABI(as the structure 
changed, only in the case of underlying pthread lib does not support 
recursive mutex, so should be OK)

Cheers,
Henry




Re: Recursive mutex

Posted by Joe Orton <jo...@redhat.com>.
On Wed, Jul 20, 2005 at 06:26:55PM -0700, Henry Jen wrote:
> I need to have recursive(NESTED) mutex support in our project.
> 
> Currently apr is relying on pthread lib to provide 
> pthread_mutexattr_settype to provide recursive support, unfortunately, 
> that does not seem to be in POSIX standard and is not available on the 
> requested linux platform(see 
> http://www.lynuxworks.com/products/posix/function-calls.php3).

mutexattr_settype is in a later POSIX standard (2001 I think), FWIW.

> I plan to implement that in apr by extend current apr_thread_mutex_t to 
> include a pthread_t and an integer for count, and modify the lock/unlock 
> accordingly. So here are couple questions:

Well, I'm not sure it can actually be done correctly using simply a 
pthread_t and an counter; that was what the previous code did before 
recursive mutexes where used, and it didn't work.

> 1. Is this a reasonable solution or I overlook something? Or maybe 
> someone had done similar?

e.g. look at the APR 0.9.4-level implementation

joe

Re: Recursive mutex

Posted by Henry Jen <he...@ztune.net>.
Joe Orton wrote:
> On Thu, Jul 21, 2005 at 11:37:29AM -0700, Henry Jen wrote:
> 
>>I just looked into SVN at 0.9.4(thanks Joe for pointing me there), 
>>that's pretty much what I planned to do. The trylock does not seem to be 
>>right though.
>>
>>However, I would like to know what's wrong with that implementation and 
>>try to figure out how it can be fixed.
> 
> 
> http://mail-archives.apache.org/mod_mbox/apr-dev/200307.mbox/%3c20030730230729.GA31874@manyfish.co.uk%3e
> 
> and the rest of that thread probably help, I think those comments apply 
> to the code as it was before it was removed.
> 
> I was finally motivated to throw out the old code and replace it with 
> use of POSIX recursive mutexes since doing so fixed segfaults in httpd 
> worker MPM regression tests on x86_64 with pool-debug enabled (the 
> pool-debug code is the only place inside apr/httpd where the NESTED flag 
> is used).  I don't know if those segfaults were directly attributable to 
> any of the issues I highlighted in the old code, however, just to keep 
> you on your toes ;)
> 

Thanks, that thread helps a lot.

Our user decided to ask the Linux vendor to support recursive mutex, so 
I don't need to worry about that now. :-)

Thanks again for all the pointers.

Cheers,
Henry

Re: Recursive mutex

Posted by Joe Orton <jo...@redhat.com>.
On Thu, Jul 21, 2005 at 11:37:29AM -0700, Henry Jen wrote:
> I just looked into SVN at 0.9.4(thanks Joe for pointing me there), 
> that's pretty much what I planned to do. The trylock does not seem to be 
> right though.
> 
> However, I would like to know what's wrong with that implementation and 
> try to figure out how it can be fixed.

http://mail-archives.apache.org/mod_mbox/apr-dev/200307.mbox/%3c20030730230729.GA31874@manyfish.co.uk%3e

and the rest of that thread probably help, I think those comments apply 
to the code as it was before it was removed.

I was finally motivated to throw out the old code and replace it with 
use of POSIX recursive mutexes since doing so fixed segfaults in httpd 
worker MPM regression tests on x86_64 with pool-debug enabled (the 
pool-debug code is the only place inside apr/httpd where the NESTED flag 
is used).  I don't know if those segfaults were directly attributable to 
any of the issues I highlighted in the old code, however, just to keep 
you on your toes ;)

joe

Re: Recursive mutex

Posted by Henry Jen <he...@ztune.net>.
Cliff Woolley wrote:
> On Thu, 21 Jul 2005, Nick Kew wrote:
> 
>>On Wed, 20 Jul 2005, Henry Jen wrote:
>>
>>
>>>I need to have recursive(NESTED) mutex support in our project.
>>>
>>>Currently apr is relying on pthread lib to provide
>>>pthread_mutexattr_settype to provide recursive support, unfortunately,
>>>that does not seem to be in POSIX standard and is not available on the
>>>requested linux platform(see
>>>http://www.lynuxworks.com/products/posix/function-calls.php3).
>>>
>>>I plan to implement that in apr by extend current apr_thread_mutex_t to
>>>include a pthread_t and an integer for count, and modify the lock/unlock
>>>accordingly. So here are couple questions:
>>>
>>>1. Is this a reasonable solution or I overlook something? Or maybe
>>>someone had done similar?
>>
>>Looks reasonable to me at first glance.  Bear in mind that anything
>>APR exports should be cross-platform, so you'll want to implement it
>>in the platform-independent layer.  Unless I'm misunderstanding the
>>scope of what you propose.
>>
>>
>>>2. How fast can this modification can be rolled into a formal apr
>>>release? It is going to be API compatible but not ABI(as the structure
>>>changed, only in the case of underlying pthread lib does not support
>>>recursive mutex, so should be OK)
>>
>>That's likely to be a while: it'll need to get reviewed, agreed on,
>>and then used in a test version for a while, then await sufficient
>>general agreement for a release incorporating it.
> 
> 
> I have this vague recollection that we specifically excluded recursive
> mutexes from the API at some point in the past because there were deadlock
> problems that made it impossible to implement this portably.  Perhaps
> Justin or Aaron remembers more details?
> 

I just looked into SVN at 0.9.4(thanks Joe for pointing me there), 
that's pretty much what I planned to do. The trylock does not seem to be 
right though.

However, I would like to know what's wrong with that implementation and 
try to figure out how it can be fixed.

Cheers,
Henry

Re: Recursive mutex

Posted by Cliff Woolley <jw...@virginia.edu>.
On Thu, 21 Jul 2005, Nick Kew wrote:
> On Wed, 20 Jul 2005, Henry Jen wrote:
>
> > I need to have recursive(NESTED) mutex support in our project.
> >
> > Currently apr is relying on pthread lib to provide
> > pthread_mutexattr_settype to provide recursive support, unfortunately,
> > that does not seem to be in POSIX standard and is not available on the
> > requested linux platform(see
> > http://www.lynuxworks.com/products/posix/function-calls.php3).
> >
> > I plan to implement that in apr by extend current apr_thread_mutex_t to
> > include a pthread_t and an integer for count, and modify the lock/unlock
> > accordingly. So here are couple questions:
> >
> > 1. Is this a reasonable solution or I overlook something? Or maybe
> > someone had done similar?
>
> Looks reasonable to me at first glance.  Bear in mind that anything
> APR exports should be cross-platform, so you'll want to implement it
> in the platform-independent layer.  Unless I'm misunderstanding the
> scope of what you propose.
>
> > 2. How fast can this modification can be rolled into a formal apr
> > release? It is going to be API compatible but not ABI(as the structure
> > changed, only in the case of underlying pthread lib does not support
> > recursive mutex, so should be OK)
>
> That's likely to be a while: it'll need to get reviewed, agreed on,
> and then used in a test version for a while, then await sufficient
> general agreement for a release incorporating it.

I have this vague recollection that we specifically excluded recursive
mutexes from the API at some point in the past because there were deadlock
problems that made it impossible to implement this portably.  Perhaps
Justin or Aaron remembers more details?

--Cliff

Re: Recursive mutex

Posted by Nick Kew <ni...@webthing.com>.

On Wed, 20 Jul 2005, Henry Jen wrote:

> I need to have recursive(NESTED) mutex support in our project.
>
> Currently apr is relying on pthread lib to provide
> pthread_mutexattr_settype to provide recursive support, unfortunately,
> that does not seem to be in POSIX standard and is not available on the
> requested linux platform(see
> http://www.lynuxworks.com/products/posix/function-calls.php3).
>
> I plan to implement that in apr by extend current apr_thread_mutex_t to
> include a pthread_t and an integer for count, and modify the lock/unlock
> accordingly. So here are couple questions:
>
> 1. Is this a reasonable solution or I overlook something? Or maybe
> someone had done similar?

Looks reasonable to me at first glance.  Bear in mind that anything
APR exports should be cross-platform, so you'll want to implement it
in the platform-independent layer.  Unless I'm misunderstanding the
scope of what you propose.

> 2. How fast can this modification can be rolled into a formal apr
> release? It is going to be API compatible but not ABI(as the structure
> changed, only in the case of underlying pthread lib does not support
> recursive mutex, so should be OK)

That's likely to be a while: it'll need to get reviewed, agreed on,
and then used in a test version for a while, then await sufficient
general agreement for a release incorporating it.

-- 
Nick Kew