Free all possibly held mutexes in error handlers

Now that we have 3 mutexes lying around, it's important that we check
and free these if necessary if error() is called in any thread that can
hold them.  To do this, we now have flexthread.c, which defines a
flexthread_mutex struct.  This is a wrapper around a pthread_mutex_t and
a pthread_t.  The idea is that in the error handler, the thread can
check whether it holds the mutex and can free it if and only if it does.
This is important because pthread fast mutexes can be freed by *any*
thread, not just the thread which holds them.

Note: it is only ever safe for a thread to check if it holds the mutex
itself.  It is *never* safe to check if another thread holds a mutex
without first locking that mutex, which makes the whole operation rather
pointless.
This commit is contained in:
Alex Young
2012-07-11 09:43:16 +01:00
parent 17fe6d3023
commit f3f017a87d
17 changed files with 351 additions and 70 deletions

View File

@@ -8,6 +8,7 @@
#include "self_pipe.h"
#include "mbox.h"
#include "control.h"
#include "flexthread.h"
/* Carries the "globals". */
struct flexnbd {
@@ -28,7 +29,7 @@ struct flexnbd {
/* switch_mutex is the lock around dereferencing the serve
* pointer.
*/
pthread_mutex_t switch_mutex;
struct flexthread_mutex * switch_mutex;
/* File descriptor for a signalfd(2) signal stream. */
int signal_fd;
@@ -60,8 +61,9 @@ struct flexnbd * flexnbd_create_listening(
void flexnbd_destroy( struct flexnbd * );
enum mirror_state;
enum mirror_state flexnbd_get_mirror_state( struct flexnbd * );
void flexnbd_switch_lock( struct flexnbd * );
void flexnbd_switch_unlock( struct flexnbd * );
void flexnbd_lock_switch( struct flexnbd * );
void flexnbd_unlock_switch( struct flexnbd * );
int flexnbd_switch_locked( struct flexnbd * );
int flexnbd_default_deny( struct flexnbd * );
void flexnbd_set_server( struct flexnbd * flexnbd, struct server * serve );
void flexnbd_switch( struct flexnbd * flexnbd, struct server *(listen_cb)(struct listen *) );