summaryrefslogtreecommitdiffstats
path: root/src/server/locks.c
blob: 71a18450fdbedeef5efbe74e9af5de7c53a3e805 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * locks.c
 *
 *  Created on: 16.07.2013
 *      Author: sr
 */

#include "locks.h"
#include "helper.h"
#include "../shared/timing.h"

#ifdef _DEBUG
#define MAXLOCKS (SERVER_MAX_CLIENTS * 2 + SERVER_MAX_ALTS + 200 + SERVER_MAX_IMAGES)
#define MAXTHREADS (SERVER_MAX_CLIENTS + 100)
#define LOCKLEN 60
typedef struct
{
	void *lock;
	ticks locktime;
	char locked;
	pthread_t thread;
	int lockId;
	char name[LOCKLEN];
	char where[LOCKLEN];
} debug_lock_t;

typedef struct
{
	pthread_t tid;
	ticks time;
	char name[LOCKLEN];
	char where[LOCKLEN];

} debug_thread_t;

int debugThreadCount = 0;

static debug_lock_t locks[MAXLOCKS];
static debug_thread_t threads[MAXTHREADS];
static int init_done = 0;
static pthread_spinlock_t initdestory;
static int lockId = 0;
static pthread_t watchdog = 0;
static dnbd3_signal_t* watchdogSignal = NULL;

static void *debug_thread_watchdog(void *something);

int debug_spin_init(const char *name, const char *file, int line, pthread_spinlock_t *lock, int shared)
{
	if ( !init_done ) {
		memset( locks, 0, MAXLOCKS * sizeof(debug_lock_t) );
		memset( threads, 0, MAXTHREADS * sizeof(debug_thread_t) );
		pthread_spin_init( &initdestory, PTHREAD_PROCESS_PRIVATE );
		init_done = 1;
	}
	int first = -1;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == lock ) {
			logadd( LOG_ERROR, "Lock %p (%s) already initialized (%s:%d)\n", (void*)lock, name, file, line );
			exit( 4 );
		}
		if ( first == -1 && locks[i].lock == NULL ) first = i;
	}
	if ( first == -1 ) {
		logadd( LOG_ERROR, "No more free debug locks (%s:%d)\n", file, line );
		pthread_spin_unlock( &initdestory );
		debug_dump_lock_stats();
		exit( 4 );
	}
	locks[first].lock = (void*)lock;
	locks[first].locked = 0;
	snprintf( locks[first].name, LOCKLEN, "%s", name );
	snprintf( locks[first].where, LOCKLEN, "I %s:%d", file, line );
	pthread_spin_unlock( &initdestory );
	return pthread_spin_init( lock, shared );
}

int debug_spin_lock(const char *name, const char *file, int line, pthread_spinlock_t *lock)
{
	debug_lock_t *l = NULL;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == lock ) {
			l = &locks[i];
			break;
		}
	}
	pthread_spin_unlock( &initdestory );
	if ( l == NULL ) {
		logadd( LOG_ERROR, "Tried to lock uninitialized lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
		debug_dump_lock_stats();
		exit( 4 );
	}
	debug_thread_t *t = NULL;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXTHREADS; ++i) {
		if ( threads[i].tid != 0 ) continue;
		threads[i].tid = pthread_self();
		timing_get( &threads[i].time );
		snprintf( threads[i].name, LOCKLEN, "%s", name );
		snprintf( threads[i].where, LOCKLEN, "%s:%d", file, line );
		t = &threads[i];
		break;
	}
	pthread_spin_unlock( &initdestory );
	if ( t == NULL ) {
		logadd( LOG_ERROR, "Lock sanity check: Too many waiting threads for lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
		exit( 4 );
	}
	const int retval = pthread_spin_lock( lock );
	pthread_spin_lock( &initdestory );
	t->tid = 0;
	pthread_spin_unlock( &initdestory );
	if ( l->locked ) {
		logadd( LOG_ERROR, "Lock sanity check: lock %p (%s) already locked at %s:%d\n", (void*)lock, name, file, line );
		exit( 4 );
	}
	l->locked = 1;
	timing_get( &l->locktime );
	l->thread = pthread_self();
	snprintf( l->where, LOCKLEN, "L %s:%d", file, line );
	pthread_spin_lock( &initdestory );
	l->lockId = ++lockId;
	pthread_spin_unlock( &initdestory );
	return retval;
}

int debug_spin_trylock(const char *name, const char *file, int line, pthread_spinlock_t *lock)
{
	debug_lock_t *l = NULL;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == lock ) {
			l = &locks[i];
			break;
		}
	}
	pthread_spin_unlock( &initdestory );
	if ( l == NULL ) {
		logadd( LOG_ERROR, "Tried to lock uninitialized lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
		debug_dump_lock_stats();
		exit( 4 );
	}
	debug_thread_t *t = NULL;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXTHREADS; ++i) {
		if ( threads[i].tid != 0 ) continue;
		threads[i].tid = pthread_self();
		timing_get( &threads[i].time );
		snprintf( threads[i].name, LOCKLEN, "%s", name );
		snprintf( threads[i].where, LOCKLEN, "%s:%d", file, line );
		t = &threads[i];
		break;
	}
	pthread_spin_unlock( &initdestory );
	if ( t == NULL ) {
		logadd( LOG_ERROR, "Lock sanity check: Too many waiting threads for %p (%s) at %s:%d\n", (void*)lock, name, file, line );
		exit( 4 );
	}
	const int retval = pthread_spin_trylock( lock );
	pthread_spin_lock( &initdestory );
	t->tid = 0;
	pthread_spin_unlock( &initdestory );
	if ( retval == 0 ) {
		if ( l->locked ) {
			logadd( LOG_ERROR, "Lock sanity check: lock %p (%s) already locked at %s:%d\n", (void*)lock, name, file, line );
			exit( 4 );
		}
		l->locked = 1;
		timing_get( &l->locktime );
		l->thread = pthread_self();
		snprintf( l->where, LOCKLEN, "L %s:%d", file, line );
		pthread_spin_lock( &initdestory );
		l->lockId = ++lockId;
		pthread_spin_unlock( &initdestory );
	}
	return retval;
}

int debug_spin_unlock(const char *name, const char *file, int line, pthread_spinlock_t *lock)
{
	debug_lock_t *l = NULL;
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == lock ) {
			l = &locks[i];
			break;
		}
	}
	pthread_spin_unlock( &initdestory );
	if ( l == NULL ) {
		logadd( LOG_ERROR, "Tried to unlock uninitialized lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
		exit( 4 );
	}
	if ( !l->locked ) {
		logadd( LOG_ERROR, "Unlock sanity check: lock %p (%s) not locked at %s:%d\n", (void*)lock, name, file, line );
		exit( 4 );
	}
	l->locked = 0;
	l->thread = 0;
	snprintf( l->where, LOCKLEN, "U %s:%d", file, line );
	int retval = pthread_spin_unlock( lock );
	return retval;
}

int debug_spin_destroy(const char *name, const char *file, int line, pthread_spinlock_t *lock)
{
	pthread_spin_lock( &initdestory );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == lock ) {
			if ( locks[i].locked ) {
				logadd( LOG_ERROR, "Tried to destroy lock %p (%s) at %s:%d when it is still locked\n", (void*)lock, name, file, line );
				exit( 4 );
			}
			locks[i].lock = NULL;
			snprintf( locks[i].where, LOCKLEN, "D %s:%d", file, line );
			pthread_spin_unlock( &initdestory );
			return pthread_spin_destroy( lock );
		}
	}
	logadd( LOG_ERROR, "Tried to destroy non-existent lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
	exit( 4 );
}

void debug_dump_lock_stats()
{
	declare_now;
	pthread_spin_lock( &initdestory );
	printf( "\n **** LOCKS ****\n\n" );
	for (int i = 0; i < MAXLOCKS; ++i) {
		if ( locks[i].lock == NULL ) continue;
		if ( locks[i].locked ) {
			printf( "* *** %s ***\n"
					"* Where: %s\n"
					"* When: %d secs ago\n"
					"* Locked: %d\n"
					"* Serial: %d\n"
					"* Thread: %d\n", locks[i].name, locks[i].where, (int)timing_diff( &locks[i].locktime, &now ), (int)locks[i].locked, locks[i].lockId,
					(int)locks[i].thread );
		} else {
			printf( "* *** %s ***\n"
					"* Where: %s\n"
					"* Locked: %d\n", locks[i].name, locks[i].where, (int)locks[i].locked );
		}
	}
	printf( "\n **** WAITING THREADS ****\n\n" );
	for (int i = 0; i < MAXTHREADS; ++i) {
		if ( threads[i].tid == 0 ) continue;
		printf( "* *** Thread %d ***\n"
				"* Lock: %s\n"
				"* Where: %s\n"
				"* How long: %d secs\n", (int)threads[i].tid, threads[i].name, threads[i].where, (int)timing_diff( &threads[i].time, &now ) );
	}
	pthread_spin_unlock( &initdestory );
}

static void *debug_thread_watchdog(void *something UNUSED)
{
	setThreadName( "debug-watchdog" );
	while ( !_shutdown ) {
		if ( init_done ) {
			declare_now;
			pthread_spin_lock( &initdestory );
			for (int i = 0; i < MAXTHREADS; ++i) {
				if ( threads[i].tid == 0 ) continue;
				const uint32_t diff = timing_diff( &threads[i].time, &now );
				if ( diff > 6 && diff < 100000 ) {
					printf( "\n\n +++++++++ DEADLOCK ++++++++++++\n\n" );
					pthread_spin_unlock( &initdestory );
					debug_dump_lock_stats();
					exit( 99 );
				}
			}
			pthread_spin_unlock( &initdestory );
		}
		if ( watchdogSignal == NULL || signal_wait( watchdogSignal, 5000 ) == SIGNAL_ERROR ) sleep( 5 );
	}
	return NULL ;
}

#endif

void debug_locks_start_watchdog()
{
#ifdef _DEBUG
	watchdogSignal = signal_new();
	if ( 0 != thread_create( &watchdog, NULL, &debug_thread_watchdog, (void *)NULL ) ) {
		logadd( LOG_ERROR, "Could not start debug-lock watchdog." );
		return;
	}
#endif
}

void debug_locks_stop_watchdog()
{
#ifdef _DEBUG
	_shutdown = true;
	printf( "Killing debug watchdog...\n" );
	pthread_spin_lock( &initdestory );
	signal_call( watchdogSignal );
	pthread_spin_unlock( &initdestory );
	thread_join( watchdog, NULL );
	signal_close( watchdogSignal );
#endif
}