[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 | */
|
---|
| 16 | extern 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))
|
---|
[214] | 32 | #define SH_MUTEX_TRYLOCK_UNSAFE(M) pthread_mutex_trylock(&(M))
|
---|
[132] | 33 | #define SH_MUTEX_UNLOCK_UNSAFE(M) pthread_mutex_unlock(&(M))
|
---|
[130] | 34 |
|
---|
[138] | 35 |
|
---|
| 36 | /*
|
---|
| 37 | * ---- Recursive mutex ----
|
---|
| 38 | */
|
---|
[139] | 39 | #if defined(HAVE_PTHREAD_MUTEX_RECURSIVE)
|
---|
[138] | 40 |
|
---|
| 41 | #define SH_MUTEX_RECURSIVE(M) \
|
---|
| 42 | static pthread_mutex_t M; \
|
---|
| 43 | static void M ## _init (void) \
|
---|
| 44 | { \
|
---|
| 45 | pthread_mutexattr_t mta; \
|
---|
[143] | 46 | pthread_mutexattr_init(&mta); \
|
---|
[138] | 47 | pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE); \
|
---|
| 48 | pthread_mutex_init(&(M), &mta); \
|
---|
| 49 | pthread_mutexattr_destroy(&mta); \
|
---|
| 50 | return; \
|
---|
| 51 | } \
|
---|
| 52 | static 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 |
|
---|
| 80 | void sh_RMutexLock(struct sh_RMutex * tok);
|
---|
| 81 | void sh_RMutexUnlock(void * arg);
|
---|
| 82 | void sh_InitRMutex(struct sh_RMutex * tok);
|
---|
| 83 |
|
---|
| 84 | #define SH_MUTEX_RECURSIVE(M) \
|
---|
| 85 | static struct sh_RMutex M; \
|
---|
| 86 | static void M ## _init (void) \
|
---|
| 87 | { \
|
---|
| 88 | sh_InitRMutex(&(M)); \
|
---|
| 89 | return; \
|
---|
| 90 | } \
|
---|
| 91 | static 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
|
---|
[133] | 109 | /*
|
---|
| 110 | * ---- Global mutexes ----
|
---|
| 111 | */
|
---|
| 112 | SH_MUTEX_EXTERN(mutex_skey);
|
---|
[134] | 113 | SH_MUTEX_EXTERN(mutex_resolv);
|
---|
| 114 | SH_MUTEX_EXTERN(mutex_pwent);
|
---|
[138] | 115 | SH_MUTEX_EXTERN(mutex_readdir);
|
---|
[143] | 116 | /* Prevent threads from logging while we are in suspend */
|
---|
| 117 | SH_MUTEX_EXTERN(mutex_thread_nolog);
|
---|
[133] | 118 |
|
---|
[134] | 119 | /*
|
---|
| 120 | * ---- Initialize thread-specific conversion area ----
|
---|
| 121 | */
|
---|
[170] | 122 | extern int sh_g_thread(void);
|
---|
[134] | 123 |
|
---|
[143] | 124 |
|
---|
| 125 | /*
|
---|
| 126 | * ---- Functions for threaded modules ----
|
---|
| 127 | */
|
---|
| 128 | int sh_pthread_create(void *(*start_routine)(void*), void *arg);
|
---|
| 129 | int sh_pthread_cancel_all(void);
|
---|
| 130 | void sh_threaded_module_reconf(void *arg);
|
---|
| 131 | void * sh_threaded_module_run(void *arg);
|
---|
| 132 |
|
---|
[130] | 133 | #else
|
---|
| 134 |
|
---|
[140] | 135 | #define PTHREAD_MUTEX_INITIALIZER NULL
|
---|
[130] | 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)
|
---|
[132] | 142 | #define SH_MUTEX_LOCK_UNSAFE(M) ((void)0)
|
---|
[214] | 143 | #define SH_MUTEX_TRYLOCK_UNSAFE(M) (0)
|
---|
[132] | 144 | #define SH_MUTEX_UNLOCK_UNSAFE(M) ((void)0)
|
---|
[130] | 145 |
|
---|
[138] | 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 |
|
---|
[130] | 151 | /* #ifdef HAVE_PTHREAD */
|
---|
| 152 | #endif
|
---|
| 153 |
|
---|
| 154 | /* #ifndef SH_PTHREAD_H */
|
---|
| 155 | #endif
|
---|