Skip to content

Commit 0e3da93

Browse files
author
Volodymyr Samokhatko
committed
threading: recursive mutex
1 parent c8e7bd2 commit 0e3da93

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

include/rfb/threading.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@
2626

2727
#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
2828
#include <pthread.h>
29+
#define RFB_THREADING_GLUE_DETAIL(x, y) x ## y
30+
#define RFB_THREADING_GLUE(x, y) RFB_THREADING_GLUE_DETAIL(x, y)
2931
#if 0 /* debugging */
3032
#define LOCK(mutex) (rfbLog("%s:%d LOCK(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_lock(&(mutex)))
3133
#define UNLOCK(mutex) (rfbLog("%s:%d UNLOCK(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_unlock(&(mutex)))
3234
#define MUTEX(mutex) pthread_mutex_t (mutex)
3335
#define INIT_MUTEX(mutex) (rfbLog("%s:%d INIT_MUTEX(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex)), pthread_mutex_init(&(mutex),NULL))
36+
#define INIT_MUTEX_RECURSIVE(mutex) do { \
37+
rfbLog("%s:%d INIT_MUTEX_RECURSIVE(%s,0x%x)\n",__FILE__,__LINE__,#mutex,&(mutex); \
38+
pthread_mutexattr_t RFB_THREADING_GLUE(recattr, __LINE__); \
39+
pthread_mutexattr_settype(&RFB_THREADING_GLUE(recattr, __LINE__), PTHREAD_MUTEX_RECURSIVE); \
40+
pthread_mutex_init(&(mutex), &RFB_THREADING_GLUE(recattr, __LINE__)); \
41+
pthread_mutexattr_destroy(&RFB_THREADING_GLUE(recattr, __LINE__)); \
42+
} while (0)
3443
#define TINI_MUTEX(mutex) (rfbLog("%s:%d TINI_MUTEX(%s)\n",__FILE__,__LINE__,#mutex), pthread_mutex_destroy(&(mutex)))
3544
#define TSIGNAL(cond) (rfbLog("%s:%d TSIGNAL(%s)\n",__FILE__,__LINE__,#cond), pthread_cond_signal(&(cond)))
3645
#define WAIT(cond,mutex) (rfbLog("%s:%d WAIT(%s,%s)\n",__FILE__,__LINE__,#cond,#mutex), pthread_cond_wait(&(cond),&(mutex)))
@@ -46,6 +55,12 @@
4655
#define MUTEX(mutex) pthread_mutex_t (mutex)
4756
#define MUTEX_SIZE (sizeof(pthread_mutex_t))
4857
#define INIT_MUTEX(mutex) pthread_mutex_init(&(mutex),NULL)
58+
#define INIT_MUTEX_RECURSIVE(mutex) do { \
59+
pthread_mutexattr_t RFB_THREADING_GLUE(recattr, __LINE__); \
60+
pthread_mutexattr_settype(&RFB_THREADING_GLUE(recattr, __LINE__), PTHREAD_MUTEX_RECURSIVE); \
61+
pthread_mutex_init(&(mutex), &RFB_THREADING_GLUE(recattr, __LINE__)); \
62+
pthread_mutexattr_destroy(&RFB_THREADING_GLUE(recattr, __LINE__)); \
63+
} while (0)
4964
#define TINI_MUTEX(mutex) pthread_mutex_destroy(&(mutex))
5065
#define TSIGNAL(cond) pthread_cond_signal(&(cond))
5166
#define WAIT(cond,mutex) pthread_cond_wait(&(cond),&(mutex))
@@ -66,6 +81,7 @@
6681
#define MUTEX(mutex) CRITICAL_SECTION (mutex)
6782
#define MUTEX_SIZE (sizeof(CRITICAL_SECTION))
6883
#define INIT_MUTEX(mutex) InitializeCriticalSection(&(mutex))
84+
#define INIT_MUTEX_RECURSIVE(mutex) INIT_MUTEX(mutex)
6985
#define TINI_MUTEX(mutex) DeleteCriticalSection(&(mutex))
7086
#define TSIGNAL(cond) WakeAllConditionVariable(&(cond))
7187
#define WAIT(cond,mutex) SleepConditionVariableCS(&(cond),&(mutex),INFINITE);
@@ -83,6 +99,7 @@
8399
#define UNLOCK(mutex)
84100
#define MUTEX(mutex)
85101
#define INIT_MUTEX(mutex)
102+
#define INIT_MUTEX_RECURSIVE(mutex)
86103
#define TINI_MUTEX(mutex)
87104
#define TSIGNAL(cond)
88105
#define WAIT(cond,mutex) this_is_unsupported

0 commit comments

Comments
 (0)