36 #ifndef TESTCODE_CHECK_LOCKS_H
37 #define TESTCODE_CHECK_LOCKS_H
66 #ifdef USE_THREAD_DEBUG
69 #error "Need pthreads for checked locks"
75 #define THRDEBUG_MAX_THREADS 32
77 extern int check_locking_order;
84 struct protected_area {
92 struct protected_area* next;
102 void* (*func)(
void*);
116 struct checked_lock *holding_first, *holding_last;
124 struct checked_lock {
126 pthread_mutex_t lock;
128 struct protected_area* prot;
130 const char* create_func, *create_file;
134 int create_thread, create_instance;
136 size_t contention_count;
138 size_t history_count;
144 const char* holder_func, *holder_file;
148 struct thr_check* holder;
150 struct thr_check* writeholder;
153 struct checked_lock* next_held_lock[THRDEBUG_MAX_THREADS];
155 struct checked_lock* prev_held_lock[THRDEBUG_MAX_THREADS];
158 enum check_lock_type {
169 pthread_mutex_t mutex;
171 pthread_spinlock_t spinlock;
173 pthread_rwlock_t rwlock;
225 void checklock_init(
enum check_lock_type type,
struct checked_lock** lock,
226 const char* func,
const char* file,
int line);
236 void checklock_destroy(
enum check_lock_type type,
struct checked_lock** lock,
237 const char* func,
const char* file,
int line);
247 void checklock_rdlock(
enum check_lock_type type,
struct checked_lock* lock,
248 const char* func,
const char* file,
int line);
258 void checklock_wrlock(
enum check_lock_type type,
struct checked_lock* lock,
259 const char* func,
const char* file,
int line);
269 void checklock_lock(
enum check_lock_type type,
struct checked_lock* lock,
270 const char* func,
const char* file,
int line);
280 void checklock_unlock(
enum check_lock_type type,
struct checked_lock* lock,
281 const char* func,
const char* file,
int line);
289 void checklock_thrcreate(pthread_t* thr,
void* (*func)(
void*),
void* arg);
295 void checklock_thrjoin(pthread_t thread);
303 struct checked_lock_rw {
struct checked_lock* c_rw; };
305 struct checked_lock_mutex {
struct checked_lock* c_m; };
307 struct checked_lock_spl {
struct checked_lock* c_spl; };
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__)
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__)
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__)
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)
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)
#define lock_unprotect(lock, area)
#define lock_get_mem(lock)
void * ub_thread_key_type
#define checklock_start()
#define lock_protect(lock, area, size)