summaryrefslogtreecommitdiffstats
path: root/src/server/locks.h
blob: 3b04caa587d6bfdb1a81bfd7f0ea450e04031feb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef _LOCKS_H_
#define _LOCKS_H_

#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Lock priority

#define LOCK_RELOAD 90
#define LOCK_LOAD_CONFIG 100
#define LOCK_REMOTE_CLONE 110
#define LOCK_CLIENT_LIST 120
#define LOCK_CLIENT 130
#define LOCK_INTEGRITY_QUEUE 140
#define LOCK_IMAGE_LIST 150
#define LOCK_IMAGE 160
#define LOCK_UPLINK_QUEUE 170
#define LOCK_ALT_SERVER_LIST 180
#define LOCK_CLIENT_SEND 190
#define LOCK_UPLINK_RTT 200
#define LOCK_UPLINK_SEND 210
#define LOCK_RPC_ACL 220
#define LOCK_FUSE_INIT 300
#define LOCK_FUSE_DIR 310

//

#ifdef DNBD3_SERVER_DEBUG_LOCKS

#define mutex_init( lock, prio ) debug_mutex_init( #lock, __FILE__, __LINE__, lock, prio)
#define mutex_lock( lock ) debug_mutex_lock( #lock, __FILE__, __LINE__, lock, false)
#define mutex_trylock( lock ) debug_mutex_lock( #lock, __FILE__, __LINE__, lock, true)
#define mutex_unlock( lock ) debug_mutex_unlock( #lock, __FILE__, __LINE__, lock)
#define mutex_cond_wait( cond, lock ) debug_mutex_cond_wait( #lock, __FILE__, __LINE__, cond, lock)
#define mutex_destroy( lock ) debug_mutex_destroy( #lock, __FILE__, __LINE__, lock)

int debug_mutex_init(const char *name, const char *file, int line, pthread_mutex_t *lock, int priority);
int debug_mutex_lock(const char *name, const char *file, int line, pthread_mutex_t *lock, bool try);
int debug_mutex_unlock(const char *name, const char *file, int line, pthread_mutex_t *lock);
int debug_mutex_cond_wait(const char *name, const char *file, int line, pthread_cond_t *restrict cond, pthread_mutex_t *restrict lock);
int debug_mutex_destroy(const char *name, const char *file, int line, pthread_mutex_t *lock);

void debug_dump_lock_stats();


#else

#define mutex_init( lock, prio ) pthread_mutex_init(lock, NULL)
#define mutex_lock( lock ) pthread_mutex_lock(lock)
#define mutex_trylock( lock ) pthread_mutex_trylock(lock)
#define mutex_unlock( lock ) pthread_mutex_unlock(lock)
#define mutex_cond_wait( cond, lock ) pthread_cond_wait(cond, lock)
#define mutex_destroy( lock ) pthread_mutex_destroy(lock)

#endif

#ifdef DNBD3_SERVER_DEBUG_THREADS

#include <dnbd3/shared/log.h>

extern int debugThreadCount;
#define thread_create(thread,attr,routine,arg) (logadd( LOG_INFO, "THREAD_CREATE: %d @ %s:%d\n", debugThreadCount, __FILE__, (int)__LINE__), debug_thread_create(thread, attr, routine, arg))
static inline pthread_t debug_thread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
{
	int i;
	if (attr == NULL || pthread_attr_getdetachstate(attr, &i) != 0 || i == PTHREAD_CREATE_JOINABLE) {
		++debugThreadCount;
	}
	return pthread_create( thread, attr, start_routine, arg );
}

#define thread_detach(thread) (logadd( LOG_INFO, "THREAD_DETACH: %d @ %s:%d\n", debugThreadCount, __FILE__, __LINE__), debug_thread_detach(thread))
static inline int debug_thread_detach(pthread_t thread)
{
	const int ret = pthread_detach(thread);
	if (ret == 0) {
		--debugThreadCount;
	} else {
		logadd( LOG_INFO, "THREAD_DETACH: Tried to detach invalid thread (error %d)\n", (int)errno);
		exit(1);
	}
	return ret;
}
#define thread_join(thread,value) (logadd( LOG_INFO, "THREAD_JOIN: %d @ %s:%d\n", debugThreadCount, __FILE__, __LINE__), debug_thread_join(thread,value))
static inline int debug_thread_join(pthread_t thread, void **value_ptr)
{
	const int ret = pthread_join(thread, value_ptr);
	if (ret == 0) {
		--debugThreadCount;
	} else {
		logadd( LOG_INFO, "THREAD_JOIN: Tried to join invalid thread (error %d)\n", (int)errno);
		exit(1);
	}
	return ret;
}

#else

#define thread_create(thread,attr,routine,param)  pthread_create( thread, attr, routine, param )
#define thread_detach(thread) pthread_detach( thread )
#define thread_join(thread,value) pthread_join( thread, value )

#endif  /* DNBD3_SERVER_DEBUG_THREADS */

#endif /* LOCKS_H_ */