Re: [PATCH v2] Make xmalloc and xrealloc thread-safe
- From
Fredrik Kuivinen <frekui@gmail.com>
- Date
- Apr 7, 2010, 16:13 UTC
- Message-ID
- <v2t4c8ef71004070913r2de3c8car31f39a2ab7aa6d15@mail.gmail.com>
- In-Reply-To
- <alpine.LFD.2.00.1004071103341.7232@xanadu.home>
On Wed, Apr 7, 2010 at 17:08, Nicolas Pitre <nico@fluxnic.net> wrote:
Show 10 quoted lines
> On Wed, 7 Apr 2010, Fredrik Kuivinen wrote: >> diff --git a/thread-utils.c b/thread-utils.c >> index 4f9c829..3c8d817 100644 >> --- a/thread-utils.c >> +++ b/thread-utils.c >> @@ -1,4 +1,5 @@ >> #include "cache.h" >> +#include <pthread.h> > > This will fail compilation on Windows surely?
I think it will work. We use "#include <pthread.h>" in builtin/grep.c, builtin/pack-objects.c, and preload-index.c already.
Show 29 quoted lines
>> #if defined(hpux) || defined(__hpux) || defined(_hpux)
>> # include <sys/pstat.h>
>> @@ -43,3 +44,24 @@ int online_cpus(void)
>>
>> return 1;
>> }
>> +
>> +int init_recursive_mutex(pthread_mutex_t *m)
>> +{
>> +#ifdef _WIN32
>> + /* The mutexes in the WIN32 pthreads emulation layer are
>> + * recursive, so we don't have to do anything extra here. */
>> + return pthread_mutex_init(m, NULL);
>> +#else
>> + pthread_mutexattr_t a;
>> + int ret;
>> + if (pthread_mutexattr_init(&a))
>> + die("pthread_mutexattr_init failed: %s", strerror(errno));
>> +
>> + if (pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE))
>> + die("pthread_mutexattr_settype failed: %s", strerror(errno));
>> +
>> + ret = pthread_mutex_init(m, &a);
>> + pthread_mutexattr_destroy(&a);
>> + return ret;
>
> Are you sure the pthread_mutexattr_t object can be destroyed even if the
> mutex is still in use? Is the attribute object "attached" to the mutex
> or merely used as a template?It is safe. See http://www.opengroup.org/onlinepubs/009695399/functions/pthread_mutexattr_init.html
- Fredrik