Don't include <config.h> twice; this doesn't work in some cases,
[gnulib.git] / lib / lock.h
index cdddf6c..7f2cfc4 100644 (file)
@@ -1,20 +1,19 @@
 /* Locking in multithreaded situations.
    Copyright (C) 2005 Free Software Foundation, Inc.
 
-   This program is free software; you can redistribute it and/or modify it
-   under the terms of the GNU Library General Public License as published
-   by the Free Software Foundation; either version 2, or (at your option)
+   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
+   the Free Software Foundation; either version 2, or (at your option)
    any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-   USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* Written by Bruno Haible <bruno@clisp.org>, 2005.
    Based on GCC's gthr-posix.h, gthr-posix95.h, gthr-solaris.h,
@@ -59,6 +58,9 @@
 */
 
 
+#ifndef _LOCK_H
+#define _LOCK_H
+
 /* ========================================================================= */
 
 #if USE_POSIX_THREADS
 # include <pthread.h>
 # include <stdlib.h>
 
+# if PTHREAD_IN_USE_DETECTION_HARD
+
+/* The pthread_in_use() detection needs to be done at runtime.  */
+#  define pthread_in_use() \
+     glthread_in_use ()
+extern int glthread_in_use (void);
+
+# endif
+
 # if USE_POSIX_THREADS_WEAK
 
 /* Use weak references to the POSIX threads library.  */
 #   pragma weak pthread_self
 #  endif
 
-#  pragma weak pthread_cancel
-#  define pthread_in_use() (pthread_cancel != NULL)
+#  if !PTHREAD_IN_USE_DETECTION_HARD
+#   pragma weak pthread_cancel
+#   define pthread_in_use() (pthread_cancel != NULL)
+#  endif
 
 # else
 
-#  define pthread_in_use() 1
+#  if !PTHREAD_IN_USE_DETECTION_HARD
+#   define pthread_in_use() 1
+#  endif
 
 # endif
 
@@ -124,7 +139,9 @@ typedef pthread_mutex_t gl_lock_t;
 # define gl_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS pthread_mutex_t NAME;
 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS pthread_mutex_t NAME = PTHREAD_MUTEX_INITIALIZER;
+    STORAGECLASS pthread_mutex_t NAME = gl_lock_initializer;
+# define gl_lock_initializer \
+    PTHREAD_MUTEX_INITIALIZER
 # define gl_lock_init(NAME) \
     if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) abort ()
 # define gl_lock_lock(NAME) \
@@ -144,7 +161,9 @@ typedef pthread_rwlock_t gl_rwlock_t;
 #   define gl_rwlock_define(STORAGECLASS, NAME) \
       STORAGECLASS pthread_rwlock_t NAME;
 #   define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-      STORAGECLASS pthread_rwlock_t NAME = PTHREAD_RWLOCK_INITIALIZER;
+      STORAGECLASS pthread_rwlock_t NAME = gl_rwlock_initializer;
+#   define gl_rwlock_initializer \
+      PTHREAD_RWLOCK_INITIALIZER
 #   define gl_rwlock_init(NAME) \
       if (pthread_in_use () && pthread_rwlock_init (&NAME, NULL) != 0) abort ()
 #   define gl_rwlock_rdlock(NAME) \
@@ -168,7 +187,9 @@ typedef struct
 #   define gl_rwlock_define(STORAGECLASS, NAME) \
       STORAGECLASS gl_rwlock_t NAME;
 #   define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-      STORAGECLASS gl_rwlock_t NAME = { 0, PTHREAD_MUTEX_INITIALIZER };
+      STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
+#   define gl_rwlock_initializer \
+      { 0, PTHREAD_MUTEX_INITIALIZER }
 #   define gl_rwlock_init(NAME) \
       if (pthread_in_use ()) glthread_rwlock_init (&NAME)
 #   define gl_rwlock_rdlock(NAME) \
@@ -201,8 +222,9 @@ typedef struct
 # define gl_rwlock_define(STORAGECLASS, NAME) \
     STORAGECLASS gl_rwlock_t NAME;
 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS gl_rwlock_t NAME = \
-      { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 };
+    STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
+# define gl_rwlock_initializer \
+    { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER, 0, 0 }
 # define gl_rwlock_init(NAME) \
     if (pthread_in_use ()) glthread_rwlock_init (&NAME)
 # define gl_rwlock_rdlock(NAME) \
@@ -230,12 +252,14 @@ extern void glthread_rwlock_destroy (gl_rwlock_t *lock);
 typedef pthread_mutex_t gl_recursive_lock_t;
 #   define gl_recursive_lock_define(STORAGECLASS, NAME) \
       STORAGECLASS pthread_mutex_t NAME;
+#   define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
+      STORAGECLASS pthread_mutex_t NAME = gl_recursive_lock_initializer;
 #   ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER
-#    define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-       STORAGECLASS pthread_mutex_t NAME = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
+#    define gl_recursive_lock_initializer \
+       PTHREAD_RECURSIVE_MUTEX_INITIALIZER
 #   else
-#    define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-       STORAGECLASS pthread_mutex_t NAME = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#    define gl_recursive_lock_initializer \
+       PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
 #   endif
 #   define gl_recursive_lock_init(NAME) \
       if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) abort ()
@@ -258,8 +282,9 @@ typedef struct
 #   define gl_recursive_lock_define(STORAGECLASS, NAME) \
       STORAGECLASS gl_recursive_lock_t NAME;
 #   define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-      STORAGECLASS gl_recursive_lock_t NAME = \
-        { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 };
+      STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
+#   define gl_recursive_lock_initializer \
+      { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, 0 }
 #   define gl_recursive_lock_init(NAME) \
       if (pthread_in_use ()) glthread_recursive_lock_init (&NAME)
 #   define gl_recursive_lock_lock(NAME) \
@@ -290,8 +315,9 @@ typedef struct
 #  define gl_recursive_lock_define(STORAGECLASS, NAME) \
      STORAGECLASS gl_recursive_lock_t NAME;
 #  define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-     STORAGECLASS gl_recursive_lock_t NAME = \
-       { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 };
+     STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
+#  define gl_recursive_lock_initializer \
+     { PTHREAD_MUTEX_INITIALIZER, (pthread_t) 0, 0 }
 #  define gl_recursive_lock_init(NAME) \
      if (pthread_in_use ()) glthread_recursive_lock_init (&NAME)
 #  define gl_recursive_lock_lock(NAME) \
@@ -367,7 +393,9 @@ typedef pth_mutex_t gl_lock_t;
 # define gl_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS pth_mutex_t NAME;
 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS pth_mutex_t NAME = PTH_MUTEX_INIT;
+    STORAGECLASS pth_mutex_t NAME = gl_lock_initializer;
+# define gl_lock_initializer \
+    PTH_MUTEX_INIT
 # define gl_lock_init(NAME) \
     if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
 # define gl_lock_lock(NAME) \
@@ -383,7 +411,9 @@ typedef pth_rwlock_t gl_rwlock_t;
 #  define gl_rwlock_define(STORAGECLASS, NAME) \
      STORAGECLASS pth_rwlock_t NAME;
 #  define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-     STORAGECLASS pth_rwlock_t NAME = PTH_RWLOCK_INIT;
+     STORAGECLASS pth_rwlock_t NAME = gl_rwlock_initializer;
+#  define gl_rwlock_initializer \
+     PTH_RWLOCK_INIT
 #  define gl_rwlock_init(NAME) \
      if (pth_in_use() && !pth_rwlock_init (&NAME)) abort ()
 #  define gl_rwlock_rdlock(NAME) \
@@ -402,7 +432,9 @@ typedef pth_mutex_t gl_recursive_lock_t;
 #  define gl_recursive_lock_define(STORAGECLASS, NAME) \
      STORAGECLASS pth_mutex_t NAME;
 #  define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-     STORAGECLASS pth_mutex_t NAME = PTH_MUTEX_INIT;
+     STORAGECLASS pth_mutex_t NAME = gl_recursive_lock_initializer;
+#  define gl_recursive_lock_initializer \
+     PTH_MUTEX_INIT
 #  define gl_recursive_lock_init(NAME) \
      if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
 #  define gl_recursive_lock_lock(NAME) \
@@ -478,7 +510,9 @@ typedef mutex_t gl_lock_t;
 # define gl_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS mutex_t NAME;
 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS mutex_t NAME = DEFAULTMUTEX;
+    STORAGECLASS mutex_t NAME = gl_lock_initializer;
+# define gl_lock_initializer \
+    DEFAULTMUTEX
 # define gl_lock_init(NAME) \
     if (thread_in_use () && mutex_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
 # define gl_lock_lock(NAME) \
@@ -494,7 +528,9 @@ typedef rwlock_t gl_rwlock_t;
 # define gl_rwlock_define(STORAGECLASS, NAME) \
     STORAGECLASS rwlock_t NAME;
 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS rwlock_t NAME = DEFAULTRWLOCK;
+    STORAGECLASS rwlock_t NAME = gl_rwlock_initializer;
+# define gl_rwlock_initializer \
+    DEFAULTRWLOCK
 # define gl_rwlock_init(NAME) \
     if (thread_in_use () && rwlock_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
 # define gl_rwlock_rdlock(NAME) \
@@ -521,7 +557,9 @@ typedef struct
 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS gl_recursive_lock_t NAME;
 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS gl_recursive_lock_t NAME = { DEFAULTMUTEX, (thread_t) 0, 0 };
+    STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
+# define gl_recursive_lock_initializer \
+    { DEFAULTMUTEX, (thread_t) 0, 0 }
 # define gl_recursive_lock_init(NAME) \
     if (thread_in_use ()) glthread_recursive_lock_init (&NAME)
 # define gl_recursive_lock_lock(NAME) \
@@ -594,7 +632,9 @@ typedef struct
 # define gl_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS gl_lock_t NAME;
 # define gl_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS gl_lock_t NAME = { { 0, -1 } };
+    STORAGECLASS gl_lock_t NAME = gl_lock_initializer;
+# define gl_lock_initializer \
+    { { 0, -1 } }
 # define gl_lock_init(NAME) \
     glthread_lock_init (&NAME)
 # define gl_lock_lock(NAME) \
@@ -634,7 +674,9 @@ typedef struct
 # define gl_rwlock_define(STORAGECLASS, NAME) \
     STORAGECLASS gl_rwlock_t NAME;
 # define gl_rwlock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS gl_rwlock_t NAME = { { 0, -1 } };
+    STORAGECLASS gl_rwlock_t NAME = gl_rwlock_initializer;
+# define gl_rwlock_initializer \
+    { { 0, -1 } }
 # define gl_rwlock_init(NAME) \
     glthread_rwlock_init (&NAME)
 # define gl_rwlock_rdlock(NAME) \
@@ -668,7 +710,9 @@ typedef struct
 # define gl_recursive_lock_define(STORAGECLASS, NAME) \
     STORAGECLASS gl_recursive_lock_t NAME;
 # define gl_recursive_lock_define_initialized(STORAGECLASS, NAME) \
-    STORAGECLASS gl_recursive_lock_t NAME = { { 0, -1 }, 0, 0 };
+    STORAGECLASS gl_recursive_lock_t NAME = gl_recursive_lock_initializer;
+# define gl_recursive_lock_initializer \
+    { { 0, -1 }, 0, 0 }
 # define gl_recursive_lock_init(NAME) \
     glthread_recursive_lock_init (&NAME)
 # define gl_recursive_lock_lock(NAME) \
@@ -750,3 +794,7 @@ typedef int gl_once_t;
     while (0)
 
 #endif
+
+/* ========================================================================= */
+
+#endif /* _LOCK_H */