Re: [PATCH 27/36] attr: convert to new threadsafe API
- From
Simon Ruderich <simon@ruderich.org>
- Date
- Oct 26, 2016, 09:35 UTC
- Message-ID
- <20161026093558.drsdwn2hm7bbcktt@ruderich.org>
- In-Reply-To
- <alpine.DEB.2.20.1610261049470.3264@virtualbox>
On Wed, Oct 26, 2016 at 10:52:23AM +0200, Johannes Schindelin wrote:
Show 19 quoted lines
> diff --git a/attr.c b/attr.c
> index d5a6aa9..6933504 100644
> --- a/attr.c
> +++ b/attr.c
> @@ -50,7 +50,16 @@ static struct git_attr *(git_attr_hash[HASHSIZE]);
> #ifndef NO_PTHREADS
>
> static pthread_mutex_t attr_mutex;
> -#define attr_lock()pthread_mutex_lock(&attr_mutex)
> +static inline void attr_lock(void)
> +{
> + static int initialized;
> +
> + if (!initialized) {
> + pthread_mutex_init(&attr_mutex, NULL);
> + initialized = 1;
> + }
> + pthread_mutex_lock(&attr_mutex);
> +}This may initialize the mutex multiple times during the first lock (which may happen in parallel).
pthread provides static initializers. To quote the man page:
Variables of type pthread_mutex_t can also be initialized
statically, using the constants PTHREAD_MUTEX_INITIALIZER
(for fast mutexes), PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
(for recursive mutexes), and
PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP (for error checking
mutexes).Regards Simon
-- + Privatsphäre ist notwendig + Ich verwende GnuPG http://gnupg.org + Öffentlicher Schlüssel: 0x92FEFDB7E44C32F9