changeset 9493:ea7514ad4ec0

Make recursive locks really recursive on POSIX systems.
author Bruno Haible <bruno@clisp.org>
date Fri, 30 Nov 2007 16:39:12 +0100
parents 5561da1401f1
children 7cd817e07a16
files ChangeLog lib/lock.c lib/lock.h
diffstat 3 files changed, 34 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-11-30  Bruno Haible  <bruno@clisp.org>
+
+	* lib/lock.h (gl_recursive_lock_init) [PTHREAD &&
+	PTHREAD_RECURSIVE_MUTEX_INITIALIZER]: Call
+	glthread_recursive_lock_init.
+	* lib/lock.c (glthread_recursive_lock_init)
+	[PTHREAD_RECURSIVE_MUTEX_INITIALIZER]: New function.
+	Reported by Yoann Vandoorselaere <yoann.v@prelude-ids.com>.
+
 2007-11-28  Paul Eggert  <eggert@cs.ucla.edu>
 
 	New function qset_acl, like set_acl but with syscall semantics.
--- a/lib/lock.c
+++ b/lib/lock.c
@@ -1,5 +1,5 @@
 /* Locking in multithreaded situations.
-   Copyright (C) 2005-2006 Free Software Foundation, Inc.
+   Copyright (C) 2005-2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -250,7 +250,24 @@
 
 # if HAVE_PTHREAD_MUTEX_RECURSIVE
 
-#  if !(defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
+#  if defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER || defined PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+
+void
+glthread_recursive_lock_init (gl_recursive_lock_t *lock)
+{
+  pthread_mutexattr_t attributes;
+
+  if (pthread_mutexattr_init (&attributes) != 0)
+    abort ();
+  if (pthread_mutexattr_settype (&attributes, PTHREAD_MUTEX_RECURSIVE) != 0)
+    abort ();
+  if (pthread_mutex_init (lock, &attributes) != 0)
+    abort ();
+  if (pthread_mutexattr_destroy (&attributes) != 0)
+    abort ();
+}
+
+#  else
 
 void
 glthread_recursive_lock_init (gl_recursive_lock_t *lock)
--- a/lib/lock.h
+++ b/lib/lock.h
@@ -361,11 +361,11 @@
        PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
 #   endif
 #   define gl_recursive_lock_init(NAME) \
-      do                                                                  \
-        {                                                                 \
-          if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) \
-            abort ();                                                     \
-        }                                                                 \
+      do                                          \
+        {                                         \
+          if (pthread_in_use ())                  \
+            glthread_recursive_lock_init (&NAME); \
+        }                                         \
       while (0)
 #   define gl_recursive_lock_lock(NAME) \
       do                                                            \
@@ -388,6 +388,7 @@
             abort ();                                                  \
         }                                                              \
       while (0)
+extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
 
 #  else