Thread (57 messages) flat view 57 messages, 7 authors, 2016-10-27

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

From: Simon Ruderich <hidden>
Date: 2016-10-26 09:41:10

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

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help