filed: Fix lost wakeups between IO-threads
authorStratos Psomadakis <psomas@grnet.gr>
Fri, 24 Feb 2012 09:27:09 +0000 (11:27 +0200)
committerStratos Psomadakis <psomas@grnet.gr>
Fri, 24 Feb 2012 09:27:09 +0000 (11:27 +0200)
Race condition with free_io() / wait_cond() and alloc_io() /
cond_signal(), results in lost-wakeups between IO threads
and causes threads and eventually filed to deadlock.

Thread 1        Thread 2
free_io x          -
   -            alloc_io x
   -            cond_signal x
lock_cond x        -
wait_cond x        -

Move free_io() inside the mutex locked section before
sleeping, and lock the mutex in wake_iothread before
signalling the thread, to avoid the deadlock.

xseg/peers/filed.c

index dc1b895..ca2deea 100644 (file)
@@ -433,8 +433,11 @@ static void handle_accepted(struct store *store, struct io *io)
 static struct io* wake_up_next_iothread(struct store *store)
 {
        struct io *io = alloc_io(store);
+
        if (io){        
+               pthread_mutex_lock(&io->lock);
                pthread_cond_signal(&io->cond);
+               pthread_mutex_unlock(&io->lock);
        }
        return io;
 }
@@ -456,8 +459,8 @@ void *io_loop(void *arg)
                        handle_accepted(store, io);
                }
                else {
-                       free_io(store, io);
                        pthread_mutex_lock(&io->lock);
+                       free_io(store, io);
                        pthread_cond_wait(&io->cond, &io->lock);
                        pthread_mutex_unlock(&io->lock);
                }