summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/compat.c4
-rw-r--r--common/compat.h18
-rw-r--r--common/library.c14
3 files changed, 30 insertions, 6 deletions
diff --git a/common/compat.c b/common/compat.c
index 1153f95..f0e31eb 100644
--- a/common/compat.c
+++ b/common/compat.c
@@ -169,8 +169,9 @@ getprogname (void)
#include <fcntl.h>
#include <unistd.h>
+#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
void
-p11_mutex_init (p11_mutex_t *mutex)
+p11_recursive_mutex_init (p11_mutex_t *mutex)
{
pthread_mutexattr_t attr;
int ret;
@@ -181,6 +182,7 @@ p11_mutex_init (p11_mutex_t *mutex)
assert (ret == 0);
pthread_mutexattr_destroy (&attr);
}
+#endif
char *
p11_dl_error (void)
diff --git a/common/compat.h b/common/compat.h
index 1b36673..0062fae 100644
--- a/common/compat.h
+++ b/common/compat.h
@@ -121,6 +121,8 @@ typedef HANDLE p11_thread_t;
typedef DWORD p11_thread_id_t;
+#define P11_RECURSIVE_MUTEX_INIT(m) \
+ (InitializeCriticalSection (&m))
#define p11_mutex_init(m) \
(InitializeCriticalSection (m))
#define p11_mutex_lock(m) \
@@ -180,8 +182,22 @@ void p11_mmap_close (p11_mmap *map);
typedef pthread_mutex_t p11_mutex_t;
-void p11_mutex_init (p11_mutex_t *mutex);
+#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+/* No implementation, because done by static initializer */
+#define P11_RECURSIVE_MUTEX_INIT(mutex)
+
+#else
+
+#define P11_RECURSIVE_MUTEX_INIT(mutex) \
+ (p11_recursive_mutex_init (&(mutex)))
+
+void p11_recursive_mutex_init (p11_mutex_t *mutex);
+
+#endif
+
+#define p11_mutex_init(m) \
+ (pthread_mutex_init (m, NULL))
#define p11_mutex_lock(m) \
(pthread_mutex_lock (m))
#define p11_mutex_unlock(m) \
diff --git a/common/library.c b/common/library.c
index 1f0ba41..465b35d 100644
--- a/common/library.c
+++ b/common/library.c
@@ -60,9 +60,15 @@ typedef struct {
static p11_local * _p11_library_get_thread_local (void);
+#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
+p11_mutex_t p11_library_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+
+p11_mutex_t p11_virtual_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
+#else
p11_mutex_t p11_library_mutex;
p11_mutex_t p11_virtual_mutex;
+#endif
#ifdef OS_UNIX
pthread_once_t p11_library_once = PTHREAD_ONCE_INIT;
@@ -126,8 +132,8 @@ p11_library_init_impl (void)
{
p11_debug_init ();
p11_debug ("initializing library");
- p11_mutex_init (&p11_library_mutex);
- p11_mutex_init (&p11_virtual_mutex);
+ P11_RECURSIVE_MUTEX_INIT (p11_library_mutex);
+ P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex);
pthread_key_create (&thread_local, free);
p11_message_storage = thread_local_message;
#ifdef HAVE_STRERROR_L
@@ -191,8 +197,8 @@ p11_library_init (void)
{
p11_debug_init ();
p11_debug ("initializing library");
- p11_mutex_init (&p11_library_mutex);
- p11_mutex_init (&p11_virtual_mutex);
+ P11_RECURSIVE_MUTEX_INIT (p11_library_mutex);
+ P11_RECURSIVE_MUTEX_INIT (p11_virtual_mutex);
thread_local = TlsAlloc ();
if (thread_local == TLS_OUT_OF_INDEXES)
p11_debug ("couldn't setup tls");