source: trunk/include/sh_pthread.h@ 145

Last change on this file since 145 was 143, checked in by rainer, 17 years ago

Bugfixes and threaded process check.

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