source: trunk/include/sh_pthread.h@ 326

Last change on this file since 326 was 320, checked in by katerina, 14 years ago

Fix for ticket #237: Potential deadlock in sh_hash_hashdelete()

File size: 6.6 KB
RevLine 
[130]1#ifndef SH_PTHREAD_H
2#define SH_PTHREAD_H
3
4#ifdef HAVE_PTHREAD
5
6#include <pthread.h>
[138]7
[130]8#define SH_MUTEX(M) pthread_mutex_t M
9#define SH_MUTEX_INIT(M,I) pthread_mutex_t M = I
10#define SH_MUTEX_STATIC(M,I) static pthread_mutex_t M = I
11#define SH_MUTEX_EXTERN(M) extern pthread_mutex_t M
12
[315]13#define SH_SETSIGMASK(A, B, C) sh_pthread_setsigmask(A,B,C)
14
15int sh_pthread_setsigmask(int how, const void *set, void *oldset);
16
[138]17/* pthread_mutex_unlock() has the wrong type (returns int), so
18 * we need to wrap it in this function.
19 */
20extern void sh_pthread_mutex_unlock (void *arg);
21
[130]22#define SH_MUTEX_LOCK(M) \
23 do { \
24 int oldtype; \
[320]25 int executeStack = 1; \
[130]26 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
[138]27 pthread_cleanup_push(sh_pthread_mutex_unlock, (void*)&(M));\
[130]28 pthread_mutex_lock(&(M))
29
[320]30#define SH_MUTEX_TRYLOCK(M) \
31 do { \
32 int oldtype; \
33 int executeStack = 0; \
34 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
35 pthread_cleanup_push(sh_pthread_mutex_unlock, (void*)&(M));\
36 pthread_mutex_trylock(&(M)); \
37 executeStack = 1
[130]38
[138]39#define SH_MUTEX_UNLOCK(M) \
[320]40 pthread_cleanup_pop(executeStack); \
[130]41 pthread_setcanceltype(oldtype, NULL); \
42 } while (0)
43
[132]44#define SH_MUTEX_LOCK_UNSAFE(M) pthread_mutex_lock(&(M))
[214]45#define SH_MUTEX_TRYLOCK_UNSAFE(M) pthread_mutex_trylock(&(M))
[132]46#define SH_MUTEX_UNLOCK_UNSAFE(M) pthread_mutex_unlock(&(M))
[130]47
[138]48
49/*
50 * ---- Recursive mutex ----
51 */
[139]52#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
[138]53
54#define SH_MUTEX_RECURSIVE(M) \
55static pthread_mutex_t M; \
56static void M ## _init (void) \
57{ \
58 pthread_mutexattr_t mta; \
[143]59 pthread_mutexattr_init(&mta); \
[138]60 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE); \
61 pthread_mutex_init(&(M), &mta); \
62 pthread_mutexattr_destroy(&mta); \
63 return; \
64} \
65static pthread_once_t M ## _initialized = PTHREAD_ONCE_INIT
66
67#define SH_MUTEX_RECURSIVE_INIT(M) \
68(void) pthread_once(&(M ## _initialized), (M ## _init))
69
70#define SH_MUTEX_RECURSIVE_LOCK(M) \
71 do { \
72 int oldtype; \
73 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
74 pthread_cleanup_push(sh_pthread_mutex_unlock, (void*)&(M));\
75 pthread_mutex_lock(&(M))
76
77#define SH_MUTEX_RECURSIVE_UNLOCK(M) \
78 pthread_cleanup_pop(1); \
79 pthread_setcanceltype(oldtype, NULL); \
80 } while (0)
81
82#else
83/* !defined(PTHREAD_MUTEX_RECURSIVE) */
84 struct sh_RMutex {
85
86 pthread_mutex_t lock;
87 unsigned int held;
88 unsigned int waiters;
89 pthread_t tid;
90 pthread_cond_t cv;
91};
92
93void sh_RMutexLock(struct sh_RMutex * tok);
94void sh_RMutexUnlock(void * arg);
95void sh_InitRMutex(struct sh_RMutex * tok);
96
97#define SH_MUTEX_RECURSIVE(M) \
98static struct sh_RMutex M; \
99static void M ## _init (void) \
100{ \
101 sh_InitRMutex(&(M)); \
102 return; \
103} \
104static pthread_once_t M ## _initialized = PTHREAD_ONCE_INIT
105
106#define SH_MUTEX_RECURSIVE_INIT(M) \
107(void) pthread_once(&(M ## _initialized), (M ## _init))
108
109#define SH_MUTEX_RECURSIVE_LOCK(M) \
110 do { \
111 int oldtype; \
112 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
113 pthread_cleanup_push(sh_RMutexUnlock, (void*)&(M)); \
114 sh_RMutexLock(&(M))
115
116#define SH_MUTEX_RECURSIVE_UNLOCK(M) \
117 pthread_cleanup_pop(1); \
118 pthread_setcanceltype(oldtype, NULL); \
119 } while (0)
120
121#endif
[133]122/*
123 * ---- Global mutexes ----
124 */
125SH_MUTEX_EXTERN(mutex_skey);
[134]126SH_MUTEX_EXTERN(mutex_resolv);
127SH_MUTEX_EXTERN(mutex_pwent);
[138]128SH_MUTEX_EXTERN(mutex_readdir);
[143]129/* Prevent threads from logging while we are in suspend */
130SH_MUTEX_EXTERN(mutex_thread_nolog);
[133]131
[134]132/*
133 * ---- Initialize thread-specific conversion area ----
134 */
[170]135extern int sh_g_thread(void);
[134]136
[143]137
138/*
139 * ---- Functions for threaded modules ----
140 */
141int sh_pthread_create(void *(*start_routine)(void*), void *arg);
142int sh_pthread_cancel_all(void);
143void sh_threaded_module_reconf(void *arg);
144void * sh_threaded_module_run(void *arg);
145
[130]146#else
147
[315]148#define SH_SETSIGMASK(A, B, C) sh_pthread_setsigmask(A,B,C)
149
150int sh_pthread_setsigmask(int how, const void *set, void *oldset);
151
[140]152#define PTHREAD_MUTEX_INITIALIZER NULL
[130]153#define SH_MUTEX(M) void *SH_MUTEX_DUMMY_ ## M
154#define SH_MUTEX_INIT(M,I) extern void *SH_MUTEX_DUMMY_ ## M
155#define SH_MUTEX_STATIC(M,I) extern void *SH_MUTEX_DUMMY_ ## M
156#define SH_MUTEX_EXTERN(M) extern void *SH_MUTEX_DUMMY_ ## M
157#define SH_MUTEX_LOCK(M) ((void)0)
[320]158#define SH_MUTEX_TRYLOCK(M) ((void)0)
[130]159#define SH_MUTEX_UNLOCK(M) ((void)0)
[132]160#define SH_MUTEX_LOCK_UNSAFE(M) ((void)0)
[214]161#define SH_MUTEX_TRYLOCK_UNSAFE(M) (0)
[132]162#define SH_MUTEX_UNLOCK_UNSAFE(M) ((void)0)
[130]163
[138]164#define SH_MUTEX_RECURSIVE(M) extern void *SH_MUTEX_DUMMY_ ## M
165#define SH_MUTEX_RECURSIVE_INIT(M) ((void)0)
166#define SH_MUTEX_RECURSIVE_LOCK(M) ((void)0)
167#define SH_MUTEX_RECURSIVE_UNLOCK(M) ((void)0)
168
[130]169/* #ifdef HAVE_PTHREAD */
170#endif
171
172/* #ifndef SH_PTHREAD_H */
173#endif
Note: See TracBrowser for help on using the repository browser.