Re: recursive mutexes in Lion?
Re: recursive mutexes in Lion?
- Subject: Re: recursive mutexes in Lion?
- From: Greg Parker <email@hidden>
- Date: Tue, 31 Jan 2012 14:27:10 -0800
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
_______________________________________________
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