Electroneum
Loading...
Searching...
No Matches
checklocks.h
Go to the documentation of this file.
1
35
36#ifndef TESTCODE_CHECK_LOCKS_H
37#define TESTCODE_CHECK_LOCKS_H
38
65
66#ifdef USE_THREAD_DEBUG
67#ifndef HAVE_PTHREAD
68/* we need the *timed*lock() routines to use for deadlock detection. */
69#error "Need pthreads for checked locks"
70#endif
71/******************* THREAD DEBUG ************************/
72#include <pthread.h>
73
75#define THRDEBUG_MAX_THREADS 32 /* threads */
77extern int check_locking_order;
78
84struct protected_area {
86 void* region;
88 size_t size;
90 void* hold;
92 struct protected_area* next;
93};
94
98struct thr_check {
100 pthread_t id;
102 void* (*func)(void*);
104 void* arg;
106 int num;
108 int locks_created;
110 FILE* order_info;
116 struct checked_lock *holding_first, *holding_last;
118 struct checked_lock* waiting;
119};
120
124struct checked_lock {
126 pthread_mutex_t lock;
128 struct protected_area* prot;
130 const char* create_func, *create_file;
132 int create_line;
134 int create_thread, create_instance;
136 size_t contention_count;
138 size_t history_count;
140 int hold_count;
142 int wait_count;
144 const char* holder_func, *holder_file;
146 int holder_line;
148 struct thr_check* holder;
150 struct thr_check* writeholder;
151
153 struct checked_lock* next_held_lock[THRDEBUG_MAX_THREADS];
155 struct checked_lock* prev_held_lock[THRDEBUG_MAX_THREADS];
156
158 enum check_lock_type {
160 check_lock_mutex,
162 check_lock_spinlock,
164 check_lock_rwlock
165 } type;
167 union {
169 pthread_mutex_t mutex;
171 pthread_spinlock_t spinlock;
173 pthread_rwlock_t rwlock;
174 } u;
175};
176
189void lock_protect(void* lock, void* area, size_t size);
190
197void lock_unprotect(void* lock, void* area);
198
204size_t lock_get_mem(void* lock);
205
209void checklock_start(void);
210
214void checklock_stop(void);
215
225void checklock_init(enum check_lock_type type, struct checked_lock** lock,
226 const char* func, const char* file, int line);
227
236void checklock_destroy(enum check_lock_type type, struct checked_lock** lock,
237 const char* func, const char* file, int line);
238
247void checklock_rdlock(enum check_lock_type type, struct checked_lock* lock,
248 const char* func, const char* file, int line);
249
258void checklock_wrlock(enum check_lock_type type, struct checked_lock* lock,
259 const char* func, const char* file, int line);
260
269void checklock_lock(enum check_lock_type type, struct checked_lock* lock,
270 const char* func, const char* file, int line);
271
280void checklock_unlock(enum check_lock_type type, struct checked_lock* lock,
281 const char* func, const char* file, int line);
282
289void checklock_thrcreate(pthread_t* thr, void* (*func)(void*), void* arg);
290
295void checklock_thrjoin(pthread_t thread);
296
303struct checked_lock_rw { struct checked_lock* c_rw; };
305struct checked_lock_mutex { struct checked_lock* c_m; };
307struct checked_lock_spl { struct checked_lock* c_spl; };
308
310typedef struct checked_lock_rw lock_rw_type;
311#define lock_rw_init(lock) checklock_init(check_lock_rwlock, &((lock)->c_rw), __func__, __FILE__, __LINE__)
312#define lock_rw_destroy(lock) checklock_destroy(check_lock_rwlock, &((lock)->c_rw), __func__, __FILE__, __LINE__)
313#define lock_rw_rdlock(lock) checklock_rdlock(check_lock_rwlock, (lock)->c_rw, __func__, __FILE__, __LINE__)
314#define lock_rw_wrlock(lock) checklock_wrlock(check_lock_rwlock, (lock)->c_rw, __func__, __FILE__, __LINE__)
315#define lock_rw_unlock(lock) checklock_unlock(check_lock_rwlock, (lock)->c_rw, __func__, __FILE__, __LINE__)
316
318typedef struct checked_lock_mutex lock_basic_type;
319#define lock_basic_init(lock) checklock_init(check_lock_mutex, &((lock)->c_m), __func__, __FILE__, __LINE__)
320#define lock_basic_destroy(lock) checklock_destroy(check_lock_mutex, &((lock)->c_m), __func__, __FILE__, __LINE__)
321#define lock_basic_lock(lock) checklock_lock(check_lock_mutex, (lock)->c_m, __func__, __FILE__, __LINE__)
322#define lock_basic_unlock(lock) checklock_unlock(check_lock_mutex, (lock)->c_m, __func__, __FILE__, __LINE__)
323
325typedef struct checked_lock_spl lock_quick_type;
326#define lock_quick_init(lock) checklock_init(check_lock_spinlock, &((lock)->c_spl), __func__, __FILE__, __LINE__)
327#define lock_quick_destroy(lock) checklock_destroy(check_lock_spinlock, &((lock)->c_spl), __func__, __FILE__, __LINE__)
328#define lock_quick_lock(lock) checklock_lock(check_lock_spinlock, (lock)->c_spl, __func__, __FILE__, __LINE__)
329#define lock_quick_unlock(lock) checklock_unlock(check_lock_spinlock, (lock)->c_spl, __func__, __FILE__, __LINE__)
330
332typedef pthread_t ub_thread_type;
333#define ub_thread_create(thr, func, arg) checklock_thrcreate(thr, func, arg)
334#define ub_thread_self() pthread_self()
335#define ub_thread_join(thread) checklock_thrjoin(thread)
336
337typedef pthread_key_t ub_thread_key_type;
338#define ub_thread_key_create(key, f) LOCKRET(pthread_key_create(key, f))
339#define ub_thread_key_set(key, v) LOCKRET(pthread_setspecific(key, v))
340#define ub_thread_key_get(key) pthread_getspecific(key)
341
342#endif /* USE_THREAD_DEBUG */
343#endif /* TESTCODE_CHECK_LOCKS_H */
pid_t ub_thread_type
Definition locks.h:283
#define lock_unprotect(lock, area)
Definition locks.h:87
int lock_rw_type
Definition locks.h:261
int lock_quick_type
Definition locks.h:276
#define lock_get_mem(lock)
Definition locks.h:88
#define checklock_stop()
Definition locks.h:90
void * ub_thread_key_type
Definition locks.h:292
#define checklock_start()
Definition locks.h:89
#define lock_protect(lock, area, size)
Definition locks.h:86
int lock_basic_type
Definition locks.h:269