source: trunk/include/sh_pthread.h@ 255

Last change on this file since 255 was 214, checked in by katerina, 16 years ago

Rewrite mail subsystem for more flexibility (closes ticket #141).

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