Re: recursive mutexes in Lion?
Re: recursive mutexes in Lion?
- Subject: Re: recursive mutexes in Lion?
- From: Jack Howarth <email@hidden>
- Date: Thu, 02 Feb 2012 21:48:25 -0500
On Tue, Jan 31, 2012 at 02:27:10PM -0800, Greg Parker wrote:
> On Jan 31, 2012, at 1:26 PM, Greg Parker <email@hidden> wrote:
> > On Jan 31, 2012, at 11:58 AM, Jack Howarth <email@hidden> wrote:
> >> The upcoming FSF gcc 4.7 release seems to have uncovered that the claimed
> >> recursive mutex support in pthread.h for Lion is broken...
> >>
> >> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51906
> >>
> >> ---- pthread_test.C ----
> >> #include <pthread.h>
> >> #include <stdio.h>
> >>
> >> /* int pthread_mutexatttr_settype(pthread_mutexattr_t *attr, int type); */
> >>
> >> int main()
> >> {
> >> /* or PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP */
> >> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
> >> pthread_mutexattr_t mta;
> >>
> >> /* pthread_mutexattr_init(&mta); */
> >> /* or PTHREAD_MUTEX_RECURSIVE_NP */
> >> printf(" returned %d\n",pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE));
> >> printf(" returned %d\n",pthread_mutex_trylock(&mutex));
> >> }
> >
> > Why is the call to pthread_mutexattr_init() commented out? Does it work when you call pthread_mutexattr_init() first?
> >
> > I would not be surprised if pthread_mutexattr_settype() fails with EINVAL when given an uninitialized attr variable.
>
> Turns out there is a bug here that the gcc tests uncovered, but the code above doesn't show it.
>
> The problem is that pthread_mutex_trylock() does not recognize PTHREAD_RECURSIVE_MUTEX_INITIALIZER. If you use that static initializer and the first operation on the mutex is pthread_mutex_trylock() then it will fail with EINVAL.
>
> A recursive mutex using pthread_mutex_init() works. A recursive mutex for which
> the first operation is pthread_mutex_lock() also works.
>
>
> % clang test.c && ./a.out
> pthread_mutex_trylock mutex1 returned 22
>
> % cat test.c
> #include <pthread.h>
> #include <stdio.h>
> int main() {
> int err;
>
> // fails: pthread_mutex_trylock of statically-initialized recursive mutex
> pthread_mutex_t mutex1 = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
> err = pthread_mutex_trylock(&mutex1);
> if (err) printf("pthread_mutex_trylock mutex1 returned %d\n", err);
>
> // succeeds: pthread_mutex_lock of statically-initialized recursive mutex
> pthread_mutex_t mutex2 = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
> err = pthread_mutex_lock(&mutex2);
> if (err) printf("pthread_mutex_lock mutex2 returned %d\n", err);
> }
>
>
> --
> Greg Parker email@hidden Runtime Wrangler
>
Greg,
Isn't there an additional bug here in pthread.h? Currently you get different code when
targeting 10.6 on Lion (use of recursive mutexes) compared to targeting 10.6 on SL? Shouldn't
you have an additional wrapper like...
#if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
#if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE)
#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {_PTHREAD_ERRORCHECK_MUTEX_SIG_init, {0}}
#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {_PTHREAD_RECURSIVE_MUTEX_SIG_init, {0}}
#endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE */
#endif /* (__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070) */
to prevent recursive mutex from being used when targeting earlier releases than Lion?
Perhaps this should be another radar?
Jack
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden