workqueue: Fix race conditions in the workqueue mechanism
authorBart Van Assche <bvanassche@acm.org>
Sat, 4 Jul 2020 16:44:56 +0000 (09:44 -0700)
committerBart Van Assche <bvanassche@acm.org>
Sat, 4 Jul 2020 16:49:54 +0000 (09:49 -0700)
commit3d86aaaa12eb95934a1f9e9303008ff958da66c5
treeca3f989b31c919b059cfcbfddfddff5388b388fd
parent2dddd728bfdc51f08355e13f7f21b224d6b032fd
workqueue: Fix race conditions in the workqueue mechanism

From https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_wait.html:
"The application shall ensure that these functions are called with mutex
locked by the calling thread; otherwise, an error (for
PTHREAD_MUTEX_ERRORCHECK and robust mutexes) or undefined behavior (for
other mutexes) results.

From https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html:
"however, if predictable scheduling behavior is required, then that mutex
shall be locked by the thread calling pthread_cond_broadcast() or
pthread_cond_signal()."

Hence always hold the associated mutex around pthread_cond_wait() and
pthread_signal() calls.

This patch fixes the hangs reported by Travis and Appveyor for test case
t0013.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
workqueue.c