darwin: fix uv_barrier race condition

Prior to this patch a race condition may occur in case a thread tries
to destroy the barrier while other awaiting threads were not notified.
Since the internal mutex and condition variables are destroyed this may
cause an undefined behavior as described by the man pages.

So in order to prevent such scenarios the detroy function will not wait
until all awaiting threads are finished before proceeding.

Fixes: https://github.com/libuv/libuv/issues/3102
PR-URL: https://github.com/libuv/libuv/pull/3162
Reviewed-By: Jameson Nash <vtjnash@gmail.com>
This commit is contained in:
Guilherme Íscaro 2021-07-09 17:07:18 -03:00 committed by GitHub
parent b12699b1ef
commit e49ef4f309
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -107,8 +107,7 @@ int uv_barrier_wait(uv_barrier_t* barrier) {
}
last = (--b->out == 0);
if (!last)
uv_cond_signal(&b->cond); /* Not needed for last thread. */
uv_cond_signal(&b->cond);
uv_mutex_unlock(&b->mutex);
return last;
@ -122,9 +121,10 @@ void uv_barrier_destroy(uv_barrier_t* barrier) {
uv_mutex_lock(&b->mutex);
assert(b->in == 0);
assert(b->out == 0);
while (b->out != 0)
uv_cond_wait(&b->cond, &b->mutex);
if (b->in != 0 || b->out != 0)
if (b->in != 0)
abort();
uv_mutex_unlock(&b->mutex);