source: branches/samhain_3_1/include/sh_pthread.h@ 577

Last change on this file since 577 was 383, checked in by katerina, 13 years ago

Fix for ticket #281 (warnings from clang static analyzer).

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