From 1c1310eed7376e2d89d3a853bcb29ca22b429eb6 Mon Sep 17 00:00:00 2001 From: Mikolaj Golub Date: Thu, 19 Sep 2013 20:15:24 +0000 Subject: [PATCH] Use cv_broadcast() instead of cv_signal() when waking up threads waiting on an empty queue as the queue may have several consumers. Before the fix the following scenario was possible: 2 threads are waiting on empty queue, 2 threads are inserting simultaneously. The first inserting thread detects that the queue is empty and is going to send the signal, but before it sends the second thread inserts too. When the first sends the signal only one of the waiting threads receive it while the other one may wait forever. The scenario above is is believed to be the cause of the observed cases, when ggate_recv_thread() was getting stuck on taking free request, while the free queue was not empty. Reviewed by: pjd Tested by: Yamagi Burmeister yamagi.org Approved by: re (marius) MFC after: 2 weeks --- sbin/hastd/primary.c | 4 ++-- sbin/hastd/secondary.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sbin/hastd/primary.c b/sbin/hastd/primary.c index 92d1d9ec31a..5758f419948 100644 --- a/sbin/hastd/primary.c +++ b/sbin/hastd/primary.c @@ -172,7 +172,7 @@ static pthread_mutex_t metadata_lock; hio_next[(ncomp)]); \ mtx_unlock(&hio_##name##_list_lock[ncomp]); \ if (_wakeup) \ - cv_signal(&hio_##name##_list_cond[(ncomp)]); \ + cv_broadcast(&hio_##name##_list_cond[(ncomp)]); \ } while (0) #define QUEUE_INSERT2(hio, name) do { \ bool _wakeup; \ @@ -182,7 +182,7 @@ static pthread_mutex_t metadata_lock; TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_##name##_next);\ mtx_unlock(&hio_##name##_list_lock); \ if (_wakeup) \ - cv_signal(&hio_##name##_list_cond); \ + cv_broadcast(&hio_##name##_list_cond); \ } while (0) #define QUEUE_TAKE1(hio, name, ncomp, timeout) do { \ bool _last; \ diff --git a/sbin/hastd/secondary.c b/sbin/hastd/secondary.c index c0c67c6ee43..f766003ce3d 100644 --- a/sbin/hastd/secondary.c +++ b/sbin/hastd/secondary.c @@ -115,7 +115,7 @@ static void *send_thread(void *arg); TAILQ_INSERT_TAIL(&hio_##name##_list, (hio), hio_next); \ mtx_unlock(&hio_##name##_list_lock); \ if (_wakeup) \ - cv_signal(&hio_##name##_list_cond); \ + cv_broadcast(&hio_##name##_list_cond); \ } while (0) #define QUEUE_TAKE(name, hio) do { \ mtx_lock(&hio_##name##_list_lock); \