summaryrefslogtreecommitdiffstats
path: root/src/server/locks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/locks.c')
-rw-r--r--src/server/locks.c324
1 files changed, 148 insertions, 176 deletions
diff --git a/src/server/locks.c b/src/server/locks.c
index a5b7c76..3be73b3 100644
--- a/src/server/locks.c
+++ b/src/server/locks.c
@@ -7,52 +7,50 @@
#include "locks.h"
#include "helper.h"
-#include "../shared/timing.h"
+#include <dnbd3/shared/timing.h>
-#ifdef _DEBUG
+#ifdef DNBD3_SERVER_DEBUG_LOCKS
#define MAXLOCKS (SERVER_MAX_CLIENTS * 2 + SERVER_MAX_ALTS + 200 + SERVER_MAX_IMAGES)
#define MAXTHREADS (SERVER_MAX_CLIENTS + 100)
+#define MAXLPT 20
#define LOCKLEN 60
typedef struct
{
- void *lock;
+ void * _Atomic lock;
ticks locktime;
- char locked;
- pthread_t thread;
+ bool _Atomic locked;
+ pthread_t _Atomic thread;
int lockId;
+ int prio;
char name[LOCKLEN];
char where[LOCKLEN];
} debug_lock_t;
typedef struct
{
- pthread_t tid;
+ pthread_t _Atomic tid;
ticks time;
char name[LOCKLEN];
char where[LOCKLEN];
-
+ debug_lock_t *locks[MAXLPT];
} 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_mutex_t initdestory;
+static pthread_mutex_t initdestory = PTHREAD_MUTEX_INITIALIZER;
static int lockId = 0;
-static pthread_t watchdog = 0;
-static dnbd3_signal_t* watchdogSignal = NULL;
-static void *debug_thread_watchdog(void *something);
+#define ULDE(...) do { \
+ pthread_mutex_unlock( &initdestory ); \
+ logadd( LOG_ERROR, __VA_ARGS__ ); \
+ debug_dump_lock_stats(); \
+ exit( 4 ); \
+} while(0)
-int debug_mutex_init(const char *name, const char *file, int line, pthread_mutex_t *lock)
+int debug_mutex_init(const char *name, const char *file, int line, pthread_mutex_t *lock, int priority)
{
- if ( !init_done ) {
- memset( locks, 0, MAXLOCKS * sizeof(debug_lock_t) );
- memset( threads, 0, MAXTHREADS * sizeof(debug_thread_t) );
- pthread_mutex_init( &initdestory, NULL );
- init_done = 1;
- }
int first = -1;
pthread_mutex_lock( &initdestory );
for (int i = 0; i < MAXLOCKS; ++i) {
@@ -63,20 +61,18 @@ int debug_mutex_init(const char *name, const char *file, int line, pthread_mutex
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_mutex_unlock( &initdestory );
- debug_dump_lock_stats();
- exit( 4 );
+ ULDE( "No more free debug locks (%s:%d)\n", file, line );
}
locks[first].lock = (void*)lock;
- locks[first].locked = 0;
+ locks[first].locked = false;
+ locks[first].prio = priority;
snprintf( locks[first].name, LOCKLEN, "%s", name );
snprintf( locks[first].where, LOCKLEN, "I %s:%d", file, line );
pthread_mutex_unlock( &initdestory );
return pthread_mutex_init( lock, NULL );
}
-int debug_mutex_lock(const char *name, const char *file, int line, pthread_mutex_t *lock)
+int debug_mutex_lock(const char *name, const char *file, int line, pthread_mutex_t *lock, bool try)
{
debug_lock_t *l = NULL;
pthread_mutex_lock( &initdestory );
@@ -86,163 +82,180 @@ int debug_mutex_lock(const char *name, const char *file, int line, pthread_mutex
break;
}
}
- pthread_mutex_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 );
+ ULDE( "Tried to lock uninitialized lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
}
debug_thread_t *t = NULL;
- pthread_mutex_lock( &initdestory );
+ int first = -1;
+ const pthread_t self = pthread_self();
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_mutex_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_mutex_lock( lock );
- pthread_mutex_lock( &initdestory );
- t->tid = 0;
- pthread_mutex_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_mutex_lock( &initdestory );
- l->lockId = ++lockId;
- pthread_mutex_unlock( &initdestory );
- return retval;
-}
-
-int debug_mutex_trylock(const char *name, const char *file, int line, pthread_mutex_t *lock)
-{
- debug_lock_t *l = NULL;
- pthread_mutex_lock( &initdestory );
- for (int i = 0; i < MAXLOCKS; ++i) {
- if ( locks[i].lock == lock ) {
- l = &locks[i];
+ if ( threads[i].tid == self ) {
+ t = &threads[i];
break;
}
+ if ( first == -1 && threads[i].tid == 0 ) {
+ first = i;
+ }
}
- pthread_mutex_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_mutex_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_mutex_unlock( &initdestory );
+ int idx;
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 );
+ if ( first == -1 ) {
+ ULDE( "Lock sanity check: Too many waiting threads for lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
+ }
+ t = &threads[first];
+ timing_get( &t->time );
+ t->tid = self;
+ snprintf( t->name, LOCKLEN, "%s", name );
+ snprintf( t->where, LOCKLEN, "%s:%d", file, line );
+ memset( t->locks, 0, sizeof(t->locks) );
+ idx = 0;
+ } else {
+ // Thread already has locks, check for order violation
+ idx = -1;
+ for (int i = 0; i < MAXLPT; ++i) {
+ if ( t->locks[i] == NULL ) {
+ if ( idx == -1 ) {
+ idx = i;
+ }
+ continue;
+ }
+ if ( t->locks[i]->prio >= l->prio ) {
+ ULDE( "Lock priority violation: %s at %s:%d (%d) when already holding %s at %s (%d)",
+ name, file, line, l->prio,
+ t->locks[i]->name, t->locks[i]->where, t->locks[i]->prio );
+ }
+ if ( t->locks[i] == l ) {
+ ULDE( "Tried to recusively lock %s in the same thread. Tried at %s:%d, when already locked at %s",
+ name, file, line, t->locks[i]->name );
+ }
+ }
+ if ( idx == -1 ) {
+ ULDE( "Thread %d tried to lock more than %d locks.", (int)self, (int)MAXLPT );
+ }
}
- const int retval = pthread_mutex_trylock( lock );
- pthread_mutex_lock( &initdestory );
- t->tid = 0;
pthread_mutex_unlock( &initdestory );
+ const int retval = try ? pthread_mutex_trylock( lock ) : pthread_mutex_lock( lock );
if ( retval == 0 ) {
+ timing_get( &l->locktime );
+ l->thread = self;
+ snprintf( l->where, LOCKLEN, "L %s:%d", file, line );
+ pthread_mutex_lock( &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_mutex_lock( &initdestory );
+ l->locked = true;
+ t->locks[idx] = l;
l->lockId = ++lockId;
pthread_mutex_unlock( &initdestory );
+ } else if ( !try || retval != EBUSY ) {
+ logadd( LOG_ERROR, "Acquiring lock %s at %s:%d failed with error code %d", name, file, line, retval );
+ debug_dump_lock_stats();
+ exit( 4 );
}
return retval;
}
int debug_mutex_unlock(const char *name, const char *file, int line, pthread_mutex_t *lock)
{
- debug_lock_t *l = NULL;
+ debug_thread_t *t = NULL;
+ pthread_t self = pthread_self();
pthread_mutex_lock( &initdestory );
- for (int i = 0; i < MAXLOCKS; ++i) {
- if ( locks[i].lock == lock ) {
- l = &locks[i];
+ for (int i = 0; i < MAXTHREADS; ++i) {
+ if ( threads[i].tid == self ) {
+ t = &threads[i];
break;
}
}
- pthread_mutex_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 ( t == NULL ) {
+ ULDE( "Unlock called from unknown thread for %s at %s:%d", name, file, line );
}
- 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 );
+ int idx = -1;
+ int cnt = 0;
+ for (int i = 0; i < MAXLPT; ++i) {
+ if ( t->locks[i] == NULL )
+ continue;
+ cnt++;
+ if ( t->locks[i]->lock == lock ) {
+ idx = i;
+ }
}
- l->locked = 0;
+ if ( idx == -1 ) {
+ ULDE( "Unlock: Calling thread doesn't hold lock %s at %s:%d", name, file, line );
+ }
+ debug_lock_t *l = t->locks[idx];
+ if ( l->thread != self || !l->locked ) {
+ ULDE( "Unlock sanity check for lock debugger failed! Lock %s is assigned to calling thread, but lock's meta data doesn't match up at %s:%d", name, file, line );
+ }
+ l->locked = false;
l->thread = 0;
+ t->locks[idx] = NULL;
+ if ( cnt == 1 ) {
+ t->tid = 0; // No more locks held, free up slot
+ }
snprintf( l->where, LOCKLEN, "U %s:%d", file, line );
- int retval = pthread_mutex_unlock( lock );
+ pthread_mutex_unlock( &initdestory );
+ const int retval = pthread_mutex_unlock( lock );
+ if ( retval != 0 ) {
+ logadd( LOG_ERROR, "pthread_mutex_unlock returned %d for %s at %s:%d", retval, name, file, line );
+ exit( 4 );
+ }
return retval;
}
int debug_mutex_cond_wait(const char *name, const char *file, int line, pthread_cond_t *restrict cond, pthread_mutex_t *restrict lock)
{
debug_lock_t *l = NULL;
+ debug_thread_t *t = NULL;
+ pthread_t self = pthread_self();
pthread_mutex_lock( &initdestory );
- for (int i = 0; i < MAXLOCKS; ++i) {
- if ( locks[i].lock == lock ) {
- l = &locks[i];
+ for (int i = 0; i < MAXTHREADS; ++i) {
+ if ( threads[i].tid == self ) {
+ t = &threads[i];
break;
}
}
- pthread_mutex_unlock( &initdestory );
+ if ( t == NULL ) {
+ ULDE( "Unlock called from unknown thread for %s at %s:%d", name, file, line );
+ }
+ int mp = 0, mpi = -1;
+ for (int i = 0; i < MAXLPT; ++i) {
+ if ( t->locks[i] == NULL )
+ continue;
+ if ( t->locks[i]->lock == lock ) {
+ l = t->locks[i];
+ } else if ( t->locks[i]->prio > mp ) {
+ mp = t->locks[i]->prio;
+ mpi = i;
+ }
+ }
if ( l == NULL ) {
- logadd( LOG_ERROR, "Tried to cond_wait on uninitialized lock %p (%s) at %s:%d\n", (void*)lock, name, file, line );
- exit( 4 );
+ ULDE( "cond_wait: Calling thread doesn't hold lock %s at %s:%d", name, file, line );
}
- if ( !l->locked ) {
- logadd( LOG_ERROR, "Cond_wait sanity check: lock %p (%s) not locked at %s:%d\n", (void*)lock, name, file, line );
- exit( 4 );
+ if ( l->thread != self || !l->locked ) {
+ ULDE( "cond_wait: Sanity check for lock debugger failed! Lock %s is assigned to calling thread, but lock's meta data doesn't match up at %s:%d", name, file, line );
}
- pthread_t self = pthread_self();
- if ( l->thread != self ) {
- logadd( LOG_ERROR, "Cond_wait called from non-owning thread for %p (%s) at %s:%d\n", (void*)lock, name, file, line );
- exit( 4 );
+ if ( mp >= l->prio ) {
+ ULDE( "cond_wait: Yielding a mutex while holding another one with higher prio: %s at %s:%d (%d) while also holding %s at %s (%d)",
+ name, file, line, l->prio,
+ t->locks[mpi]->name, t->locks[mpi]->where, mp );
}
- l->locked = 0;
+ l->locked = false;
l->thread = 0;
- snprintf( l->where, LOCKLEN, "CW %s:%d", file, line );
+ snprintf( l->where, LOCKLEN, "CWU %s:%d", file, line );
+ pthread_mutex_unlock( &initdestory );
int retval = pthread_cond_wait( cond, lock );
if ( retval != 0 ) {
logadd( LOG_ERROR, "pthread_cond_wait returned %d for lock %p (%s) at %s:%d\n", retval, (void*)lock, name, file, line );
exit( 4 );
}
- if ( l->locked != 0 || l->thread != 0 ) {
+ if ( l->locked || l->thread != 0 ) {
logadd( LOG_ERROR, "Lock is not free after returning from pthread_cond_wait for %p (%s) at %s:%d\n", (void*)lock, name, file, line );
exit( 4 );
}
- l->locked = 1;
l->thread = self;
timing_get( &l->locktime );
+ l->locked = true;
pthread_mutex_lock( &initdestory );
l->lockId = ++lockId;
pthread_mutex_unlock( &initdestory );
@@ -256,6 +269,7 @@ int debug_mutex_destroy(const char *name, const char *file, int line, pthread_mu
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 );
+ logadd( LOG_ERROR, "Currently locked by: %s", locks[i].where );
exit( 4 );
}
locks[i].lock = NULL;
@@ -289,63 +303,21 @@ void debug_dump_lock_stats()
"* Locked: %d\n", locks[i].name, locks[i].where, (int)locks[i].locked );
}
}
- printf( "\n **** WAITING THREADS ****\n\n" );
+ printf( "\n **** ACTIVE THREADS ****\n\n" );
for (int i = 0; i < MAXTHREADS; ++i) {
- if ( threads[i].tid == 0 ) continue;
+ 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_mutex_unlock( &initdestory );
-}
-
-static void *debug_thread_watchdog(void *something UNUSED)
-{
- setThreadName( "debug-watchdog" );
- while ( !_shutdown ) {
- if ( init_done ) {
- declare_now;
- pthread_mutex_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_mutex_unlock( &initdestory );
- debug_dump_lock_stats();
- exit( 99 );
- }
- }
- pthread_mutex_unlock( &initdestory );
+ for (int j = 0; j < MAXLPT; ++j) {
+ if ( threads[i].locks[j] == NULL )
+ continue;
+ printf( " * Lock %s @ %s\n", threads[i].locks[j]->name, threads[i].locks[j]->where );
}
- 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
+ pthread_mutex_unlock( &initdestory );
}
-void debug_locks_stop_watchdog()
-{
-#ifdef _DEBUG
- _shutdown = true;
- printf( "Killing debug watchdog...\n" );
- pthread_mutex_lock( &initdestory );
- signal_call( watchdogSignal );
- pthread_mutex_unlock( &initdestory );
- thread_join( watchdog, NULL );
- signal_close( watchdogSignal );
#endif
-}