source: trunk/include/sh_pthread.h@ 480

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

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

File size: 6.9 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; \
[383]33 volatile int executeStack = 0; \
[320]34 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype); \
35 pthread_cleanup_push(sh_pthread_mutex_unlock, (void*)&(M));\
[371]36 if (0 == pthread_mutex_trylock(&(M))) { \
37 executeStack = 1
[130]38
[371]39#define SH_MUTEX_TRYLOCK_UNLOCK(M) \
40 } \
41 pthread_cleanup_pop(executeStack); \
42 pthread_setcanceltype(oldtype, NULL); \
43 } while (0)
44
[138]45#define SH_MUTEX_UNLOCK(M) \
[320]46 pthread_cleanup_pop(executeStack); \
[130]47 pthread_setcanceltype(oldtype, NULL); \
48 } while (0)
49
[132]50#define SH_MUTEX_LOCK_UNSAFE(M) pthread_mutex_lock(&(M))
[214]51#define SH_MUTEX_TRYLOCK_UNSAFE(M) pthread_mutex_trylock(&(M))
[132]52#define SH_MUTEX_UNLOCK_UNSAFE(M) pthread_mutex_unlock(&(M))
[130]53
[138]54
55/*
56 * ---- Recursive mutex ----
57 */
[139]58#if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
[138]59
60#define SH_MUTEX_RECURSIVE(M) \
61static pthread_mutex_t M; \
62static void M ## _init (void) \
63{ \
64 pthread_mutexattr_t mta; \
[143]65 pthread_mutexattr_init(&mta); \
[138]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
[133]128/*
129 * ---- Global mutexes ----
130 */
131SH_MUTEX_EXTERN(mutex_skey);
[134]132SH_MUTEX_EXTERN(mutex_resolv);
133SH_MUTEX_EXTERN(mutex_pwent);
[138]134SH_MUTEX_EXTERN(mutex_readdir);
[143]135/* Prevent threads from logging while we are in suspend */
136SH_MUTEX_EXTERN(mutex_thread_nolog);
[133]137
[134]138/*
139 * ---- Initialize thread-specific conversion area ----
140 */
[170]141extern int sh_g_thread(void);
[134]142
[143]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
[130]152#else
153
[315]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
[140]158#define PTHREAD_MUTEX_INITIALIZER NULL
[130]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)
[320]164#define SH_MUTEX_TRYLOCK(M) ((void)0)
[130]165#define SH_MUTEX_UNLOCK(M) ((void)0)
[378]166#define SH_MUTEX_TRYLOCK_UNLOCK(M) ((void)0)
[132]167#define SH_MUTEX_LOCK_UNSAFE(M) ((void)0)
[214]168#define SH_MUTEX_TRYLOCK_UNSAFE(M) (0)
[132]169#define SH_MUTEX_UNLOCK_UNSAFE(M) ((void)0)
[130]170
[138]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
[130]176/* #ifdef HAVE_PTHREAD */
177#endif
178
179/* #ifndef SH_PTHREAD_H */
180#endif
Note: See TracBrowser for help on using the repository browser.