Re: [PATCH 27/36] attr: convert to new threadsafe API

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Oct 26, 2016 at 10:52:23AM +0200, Johannes Schindelin wrote:
> 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

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]