Electroneum
Loading...
Searching...
No Matches
locks.h
Go to the documentation of this file.
1
35
36#ifndef UTIL_LOCKS_H
37#define UTIL_LOCKS_H
38
57
58/* if you define your own LOCKRET before including locks.h, you can get most
59 * locking functions without the dependency on log_err. */
60#ifndef LOCKRET
61#include "util/log.h"
67#define LOCKRET(func) do {\
68 int lockret_err; \
69 if( (lockret_err=(func)) != 0) \
70 log_err("%s at %d could not " #func ": %s", \
71 __FILE__, __LINE__, strerror(lockret_err)); \
72 } while(0)
73#endif
74
76#if defined(HAVE_PTHREAD) && defined(HAVE_PTHREAD_SPINLOCK_T) && defined(ENABLE_LOCK_CHECKS)
77# define USE_THREAD_DEBUG
78#endif
79
80#ifdef USE_THREAD_DEBUG
81/******************* THREAD DEBUG ************************/
82/* (some) checking; to detect races and deadlocks. */
83#include "testcode/checklocks.h"
84
85#else /* USE_THREAD_DEBUG */
86#define lock_protect(lock, area, size) /* nop */
87#define lock_unprotect(lock, area) /* nop */
88#define lock_get_mem(lock) (0) /* nothing */
89#define checklock_start() /* nop */
90#define checklock_stop() /* nop */
91
92#ifdef HAVE_PTHREAD
93#include <pthread.h>
94
95/******************* PTHREAD ************************/
96
98typedef pthread_mutex_t lock_basic_type;
100#define lock_basic_init(lock) LOCKRET(pthread_mutex_init(lock, NULL))
101#define lock_basic_destroy(lock) LOCKRET(pthread_mutex_destroy(lock))
102#define lock_basic_lock(lock) LOCKRET(pthread_mutex_lock(lock))
103#define lock_basic_unlock(lock) LOCKRET(pthread_mutex_unlock(lock))
104
105#ifndef HAVE_PTHREAD_RWLOCK_T
107typedef pthread_mutex_t lock_rw_type;
108#define lock_rw_init(lock) LOCKRET(pthread_mutex_init(lock, NULL))
109#define lock_rw_destroy(lock) LOCKRET(pthread_mutex_destroy(lock))
110#define lock_rw_rdlock(lock) LOCKRET(pthread_mutex_lock(lock))
111#define lock_rw_wrlock(lock) LOCKRET(pthread_mutex_lock(lock))
112#define lock_rw_unlock(lock) LOCKRET(pthread_mutex_unlock(lock))
113#else /* HAVE_PTHREAD_RWLOCK_T */
115typedef pthread_rwlock_t lock_rw_type;
117#define lock_rw_init(lock) LOCKRET(pthread_rwlock_init(lock, NULL))
118#define lock_rw_destroy(lock) LOCKRET(pthread_rwlock_destroy(lock))
119#define lock_rw_rdlock(lock) LOCKRET(pthread_rwlock_rdlock(lock))
120#define lock_rw_wrlock(lock) LOCKRET(pthread_rwlock_wrlock(lock))
121#define lock_rw_unlock(lock) LOCKRET(pthread_rwlock_unlock(lock))
122#endif /* HAVE_PTHREAD_RWLOCK_T */
123
124#ifndef HAVE_PTHREAD_SPINLOCK_T
126typedef pthread_mutex_t lock_quick_type;
128#define lock_quick_init(lock) LOCKRET(pthread_mutex_init(lock, NULL))
129#define lock_quick_destroy(lock) LOCKRET(pthread_mutex_destroy(lock))
130#define lock_quick_lock(lock) LOCKRET(pthread_mutex_lock(lock))
131#define lock_quick_unlock(lock) LOCKRET(pthread_mutex_unlock(lock))
132
133#else /* HAVE_PTHREAD_SPINLOCK_T */
135typedef pthread_spinlock_t lock_quick_type;
143#define lock_quick_init(lock) LOCKRET(pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE))
144#define lock_quick_destroy(lock) LOCKRET(pthread_spin_destroy(lock))
145#define lock_quick_lock(lock) LOCKRET(pthread_spin_lock(lock))
146#define lock_quick_unlock(lock) LOCKRET(pthread_spin_unlock(lock))
147
148#endif /* HAVE SPINLOCK */
149
151typedef pthread_t ub_thread_type;
156#define PTHREADSTACKSIZE 2*1024*1024
157#define PTHREADCREATE(thr, stackrequired, func, arg) do {\
158 pthread_attr_t attr; \
159 size_t stacksize; \
160 LOCKRET(pthread_attr_init(&attr)); \
161 LOCKRET(pthread_attr_getstacksize(&attr, &stacksize)); \
162 if (stacksize < stackrequired) { \
163 LOCKRET(pthread_attr_setstacksize(&attr, stackrequired)); \
164 LOCKRET(pthread_create(thr, &attr, func, arg)); \
165 LOCKRET(pthread_attr_getstacksize(&attr, &stacksize)); \
166 verbose(VERB_ALGO, "Thread stack size set to %u", (unsigned)stacksize); \
167 } else {LOCKRET(pthread_create(thr, NULL, func, arg));} \
168 } while(0)
170#define ub_thread_create(thr, func, arg) PTHREADCREATE(thr, PTHREADSTACKSIZE, func, arg)
172#define ub_thread_self() pthread_self()
174#define ub_thread_join(thread) LOCKRET(pthread_join(thread, NULL))
175typedef pthread_key_t ub_thread_key_type;
176#define ub_thread_key_create(key, f) LOCKRET(pthread_key_create(key, f))
177#define ub_thread_key_set(key, v) LOCKRET(pthread_setspecific(key, v))
178#define ub_thread_key_get(key) pthread_getspecific(key)
179
180#else /* we do not HAVE_PTHREAD */
181#ifdef HAVE_SOLARIS_THREADS
182
183/******************* SOLARIS THREADS ************************/
184#include <synch.h>
185#include <thread.h>
186
187typedef rwlock_t lock_rw_type;
188#define lock_rw_init(lock) LOCKRET(rwlock_init(lock, USYNC_THREAD, NULL))
189#define lock_rw_destroy(lock) LOCKRET(rwlock_destroy(lock))
190#define lock_rw_rdlock(lock) LOCKRET(rw_rdlock(lock))
191#define lock_rw_wrlock(lock) LOCKRET(rw_wrlock(lock))
192#define lock_rw_unlock(lock) LOCKRET(rw_unlock(lock))
193
195typedef mutex_t lock_basic_type;
196#define lock_basic_init(lock) LOCKRET(mutex_init(lock, USYNC_THREAD, NULL))
197#define lock_basic_destroy(lock) LOCKRET(mutex_destroy(lock))
198#define lock_basic_lock(lock) LOCKRET(mutex_lock(lock))
199#define lock_basic_unlock(lock) LOCKRET(mutex_unlock(lock))
200
202typedef mutex_t lock_quick_type;
203#define lock_quick_init(lock) LOCKRET(mutex_init(lock, USYNC_THREAD, NULL))
204#define lock_quick_destroy(lock) LOCKRET(mutex_destroy(lock))
205#define lock_quick_lock(lock) LOCKRET(mutex_lock(lock))
206#define lock_quick_unlock(lock) LOCKRET(mutex_unlock(lock))
207
209typedef thread_t ub_thread_type;
210#define ub_thread_create(thr, func, arg) LOCKRET(thr_create(NULL, NULL, func, arg, NULL, thr))
211#define ub_thread_self() thr_self()
212#define ub_thread_join(thread) LOCKRET(thr_join(thread, NULL, NULL))
213typedef thread_key_t ub_thread_key_type;
214#define ub_thread_key_create(key, f) LOCKRET(thr_keycreate(key, f))
215#define ub_thread_key_set(key, v) LOCKRET(thr_setspecific(key, v))
217
218
219#else /* we do not HAVE_SOLARIS_THREADS and no PTHREADS */
220/******************* WINDOWS THREADS ************************/
221#ifdef HAVE_WINDOWS_THREADS
222#include <windows.h>
223
224/* Use a mutex */
225typedef LONG lock_rw_type;
226#define lock_rw_init(lock) lock_basic_init(lock)
227#define lock_rw_destroy(lock) lock_basic_destroy(lock)
228#define lock_rw_rdlock(lock) lock_basic_lock(lock)
229#define lock_rw_wrlock(lock) lock_basic_lock(lock)
230#define lock_rw_unlock(lock) lock_basic_unlock(lock)
231
233typedef LONG lock_basic_type;
238
240typedef LONG lock_quick_type;
241#define lock_quick_init(lock) lock_basic_init(lock)
242#define lock_quick_destroy(lock) lock_basic_destroy(lock)
243#define lock_quick_lock(lock) lock_basic_lock(lock)
244#define lock_quick_unlock(lock) lock_basic_unlock(lock)
245
247typedef HANDLE ub_thread_type;
248void ub_thread_create(ub_thread_type* thr, void* (*func)(void*), void* arg);
251typedef DWORD ub_thread_key_type;
255
256#else /* we do not HAVE_SOLARIS_THREADS, PTHREADS or WINDOWS_THREADS */
257
258/******************* NO THREADS ************************/
259#define THREADS_DISABLED 1
261typedef int lock_rw_type;
262#define lock_rw_init(lock) /* nop */
263#define lock_rw_destroy(lock) /* nop */
264#define lock_rw_rdlock(lock) /* nop */
265#define lock_rw_wrlock(lock) /* nop */
266#define lock_rw_unlock(lock) /* nop */
267
269typedef int lock_basic_type;
270#define lock_basic_init(lock) /* nop */
271#define lock_basic_destroy(lock) /* nop */
272#define lock_basic_lock(lock) /* nop */
273#define lock_basic_unlock(lock) /* nop */
274
276typedef int lock_quick_type;
277#define lock_quick_init(lock) /* nop */
278#define lock_quick_destroy(lock) /* nop */
279#define lock_quick_lock(lock) /* nop */
280#define lock_quick_unlock(lock) /* nop */
281
283typedef pid_t ub_thread_type;
286#define ub_thread_create(thr, func, arg) \
287 ub_thr_fork_create(thr, func, arg)
288#define ub_thread_self() getpid()
289#define ub_thread_join(thread) ub_thr_fork_wait(thread)
291void ub_thr_fork_create(ub_thread_type* thr, void* (*func)(void*), void* arg);
292typedef void* ub_thread_key_type;
293#define ub_thread_key_create(key, f) (*(key)) = NULL
294#define ub_thread_key_set(key, v) (key) = (v)
295#define ub_thread_key_get(key) (key)
296
297#endif /* HAVE_WINDOWS_THREADS */
298#endif /* HAVE_SOLARIS_THREADS */
299#endif /* HAVE_PTHREAD */
300#endif /* USE_THREAD_DEBUG */
301
307
312
313#endif /* UTIL_LOCKS_H */
const char * key
#define lock_basic_destroy(lock)
Definition locks.h:271
pid_t ub_thread_type
Definition locks.h:283
#define ub_thread_create(thr, func, arg)
Definition locks.h:286
void ub_thr_fork_create(ub_thread_type *thr, void *(*func)(void *), void *arg)
int lock_rw_type
Definition locks.h:261
int lock_quick_type
Definition locks.h:276
#define ub_thread_key_set(key, v)
Definition locks.h:294
#define ub_thread_key_create(key, f)
Definition locks.h:293
#define lock_basic_lock(lock)
Definition locks.h:272
#define ub_thread_self()
Definition locks.h:288
void ub_thr_fork_wait(ub_thread_type thread)
#define lock_basic_unlock(lock)
Definition locks.h:273
void ub_thread_blocksigs(void)
void * ub_thread_key_type
Definition locks.h:292
#define ub_thread_join(thread)
Definition locks.h:289
int lock_basic_type
Definition locks.h:269
#define lock_basic_init(lock)
Definition locks.h:270
#define ub_thread_key_get(key)
Definition locks.h:295
void ub_thread_sig_unblock(int sig)