/* Locking in multithreaded situations.
- Copyright (C) 2005 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
# include <pthread.h>
# include <stdlib.h>
+# ifdef __cplusplus
+extern "C" {
+# endif
+
# if PTHREAD_IN_USE_DETECTION_HARD
/* The pthread_in_use() detection needs to be done at runtime. */
# define gl_lock_initializer \
PTHREAD_MUTEX_INITIALIZER
# define gl_lock_init(NAME) \
- if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_init (&NAME, NULL) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_lock(NAME) \
- if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_unlock(NAME) \
- if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_destroy(NAME) \
- if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
/* ------------------------- gl_rwlock_t datatype ------------------------- */
# define gl_rwlock_initializer \
PTHREAD_RWLOCK_INITIALIZER
# define gl_rwlock_init(NAME) \
- if (pthread_in_use () && pthread_rwlock_init (&NAME, NULL) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_rwlock_init (&NAME, NULL) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_rdlock(NAME) \
- if (pthread_in_use () && pthread_rwlock_rdlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_rwlock_rdlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_wrlock(NAME) \
- if (pthread_in_use () && pthread_rwlock_wrlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_rwlock_wrlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_unlock(NAME) \
- if (pthread_in_use () && pthread_rwlock_unlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_rwlock_unlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_destroy(NAME) \
- if (pthread_in_use () && pthread_rwlock_destroy (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_rwlock_destroy (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# else
# define gl_rwlock_initializer \
{ 0, PTHREAD_MUTEX_INITIALIZER }
# define gl_rwlock_init(NAME) \
- if (pthread_in_use ()) glthread_rwlock_init (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_init (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_rdlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_rdlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_rdlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_wrlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_wrlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_wrlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_unlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_unlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_unlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_destroy(NAME) \
- if (pthread_in_use ()) glthread_rwlock_destroy (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_destroy (&NAME); \
+ } \
+ while (0)
extern void glthread_rwlock_init (gl_rwlock_t *lock);
extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);
extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);
# 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)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_init (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_rdlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_rdlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_rdlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_wrlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_wrlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_wrlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_unlock(NAME) \
- if (pthread_in_use ()) glthread_rwlock_unlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_unlock (&NAME); \
+ } \
+ while (0)
# define gl_rwlock_destroy(NAME) \
- if (pthread_in_use ()) glthread_rwlock_destroy (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_rwlock_destroy (&NAME); \
+ } \
+ while (0)
extern void glthread_rwlock_init (gl_rwlock_t *lock);
extern void glthread_rwlock_rdlock (gl_rwlock_t *lock);
extern void glthread_rwlock_wrlock (gl_rwlock_t *lock);
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
# endif
# define gl_recursive_lock_init(NAME) \
- 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) \
- if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_lock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_recursive_lock_unlock(NAME) \
- if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_unlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_recursive_lock_destroy(NAME) \
- if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) abort ()
+ do \
+ { \
+ if (pthread_in_use () && pthread_mutex_destroy (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
+extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
# else
# 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)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_init (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_lock(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_lock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_lock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_unlock(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_unlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_unlock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_destroy(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_destroy (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_destroy (&NAME); \
+ } \
+ while (0)
extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
# 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)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_init (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_lock(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_lock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_lock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_unlock(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_unlock (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_unlock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_destroy(NAME) \
- if (pthread_in_use ()) glthread_recursive_lock_destroy (&NAME)
+ do \
+ { \
+ if (pthread_in_use ()) \
+ glthread_recursive_lock_destroy (&NAME); \
+ } \
+ while (0)
extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
while (0)
extern int glthread_once_singlethreaded (pthread_once_t *once_control);
+# ifdef __cplusplus
+}
+# endif
+
#endif
/* ========================================================================= */
# include <pth.h>
# include <stdlib.h>
+# ifdef __cplusplus
+extern "C" {
+# endif
+
# if USE_PTH_THREADS_WEAK
/* Use weak references to the GNU Pth threads library. */
# define gl_lock_initializer \
PTH_MUTEX_INIT
# define gl_lock_init(NAME) \
- if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_init (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_lock(NAME) \
- if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_unlock(NAME) \
- if (pth_in_use() && !pth_mutex_release (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_release (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_destroy(NAME) \
(void)(&NAME)
# define gl_rwlock_initializer \
PTH_RWLOCK_INIT
# define gl_rwlock_init(NAME) \
- if (pth_in_use() && !pth_rwlock_init (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_rwlock_init (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_rdlock(NAME) \
- if (pth_in_use() && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RD, 0, NULL)) abort ()
+ do \
+ { \
+ if (pth_in_use() \
+ && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RD, 0, NULL)) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_wrlock(NAME) \
- if (pth_in_use() && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RW, 0, NULL)) abort ()
+ do \
+ { \
+ if (pth_in_use() \
+ && !pth_rwlock_acquire (&NAME, PTH_RWLOCK_RW, 0, NULL)) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_unlock(NAME) \
- if (pth_in_use() && !pth_rwlock_release (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_rwlock_release (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_destroy(NAME) \
(void)(&NAME)
# define gl_recursive_lock_initializer \
PTH_MUTEX_INIT
# define gl_recursive_lock_init(NAME) \
- if (pth_in_use() && !pth_mutex_init (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_init (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_recursive_lock_lock(NAME) \
- if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_acquire (&NAME, 0, NULL)) \
+ abort (); \
+ } \
+ while (0)
# define gl_recursive_lock_unlock(NAME) \
- if (pth_in_use() && !pth_mutex_release (&NAME)) abort ()
+ do \
+ { \
+ if (pth_in_use() && !pth_mutex_release (&NAME)) \
+ abort (); \
+ } \
+ while (0)
# define gl_recursive_lock_destroy(NAME) \
(void)(&NAME)
extern void glthread_once_call (void *arg);
extern int glthread_once_singlethreaded (pth_once_t *once_control);
+# ifdef __cplusplus
+}
+# endif
+
#endif
/* ========================================================================= */
# include <synch.h>
# include <stdlib.h>
+# ifdef __cplusplus
+extern "C" {
+# endif
+
# if USE_SOLARIS_THREADS_WEAK
/* Use weak references to the old Solaris threads library. */
# define gl_lock_initializer \
DEFAULTMUTEX
# define gl_lock_init(NAME) \
- if (thread_in_use () && mutex_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && mutex_init (&NAME, USYNC_THREAD, NULL) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_lock(NAME) \
- if (thread_in_use () && mutex_lock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && mutex_lock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_unlock(NAME) \
- if (thread_in_use () && mutex_unlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && mutex_unlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_lock_destroy(NAME) \
- if (thread_in_use () && mutex_destroy (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && mutex_destroy (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
/* ------------------------- gl_rwlock_t datatype ------------------------- */
# define gl_rwlock_initializer \
DEFAULTRWLOCK
# define gl_rwlock_init(NAME) \
- if (thread_in_use () && rwlock_init (&NAME, USYNC_THREAD, NULL) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && rwlock_init (&NAME, USYNC_THREAD, NULL) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_rdlock(NAME) \
- if (thread_in_use () && rw_rdlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && rw_rdlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_wrlock(NAME) \
- if (thread_in_use () && rw_wrlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && rw_wrlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_unlock(NAME) \
- if (thread_in_use () && rw_unlock (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && rw_unlock (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
# define gl_rwlock_destroy(NAME) \
- if (thread_in_use () && rwlock_destroy (&NAME) != 0) abort ()
+ do \
+ { \
+ if (thread_in_use () && rwlock_destroy (&NAME) != 0) \
+ abort (); \
+ } \
+ while (0)
/* --------------------- gl_recursive_lock_t datatype --------------------- */
# 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)
+ do \
+ { \
+ if (thread_in_use ()) \
+ glthread_recursive_lock_init (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_lock(NAME) \
- if (thread_in_use ()) glthread_recursive_lock_lock (&NAME)
+ do \
+ { \
+ if (thread_in_use ()) \
+ glthread_recursive_lock_lock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_unlock(NAME) \
- if (thread_in_use ()) glthread_recursive_lock_unlock (&NAME)
+ do \
+ { \
+ if (thread_in_use ()) \
+ glthread_recursive_lock_unlock (&NAME); \
+ } \
+ while (0)
# define gl_recursive_lock_destroy(NAME) \
- if (thread_in_use ()) glthread_recursive_lock_destroy (&NAME)
+ do \
+ { \
+ if (thread_in_use ()) \
+ glthread_recursive_lock_destroy (&NAME); \
+ } \
+ while (0)
extern void glthread_recursive_lock_init (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_lock (gl_recursive_lock_t *lock);
extern void glthread_recursive_lock_unlock (gl_recursive_lock_t *lock);
extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));
extern int glthread_once_singlethreaded (gl_once_t *once_control);
+# ifdef __cplusplus
+}
+# endif
+
#endif
/* ========================================================================= */
# include <windows.h>
+# ifdef __cplusplus
+extern "C" {
+# endif
+
/* We can use CRITICAL_SECTION directly, rather than the Win32 Event, Mutex,
Semaphore types, because
- we need only to synchronize inside a single process (address space),
glthread_once (&NAME, INITFUNCTION)
extern void glthread_once (gl_once_t *once_control, void (*initfunction) (void));
+# ifdef __cplusplus
+}
+# endif
+
#endif
/* ========================================================================= */