Skip to content

Commit

Permalink
fix: make the cond var safe and document it
Browse files Browse the repository at this point in the history
  • Loading branch information
hargoniX committed Jan 13, 2025
1 parent 94ac027 commit 13529c7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/runtime/uv/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,27 @@ void event_loop_lock(event_loop_t * event_loop) {

// Unlock event loop
void event_loop_unlock(event_loop_t * event_loop) {
uv_mutex_unlock(&event_loop->mutex);
if (event_loop->n_waiters == 0) {
uv_cond_signal(&event_loop->cond_var);
}
uv_mutex_unlock(&event_loop->mutex);
}

// Runs the loop and stops when it needs to register new requests.
void event_loop_run_loop(event_loop_t * event_loop) {
while (uv_loop_alive(event_loop->loop)) {
uv_mutex_lock(&event_loop->mutex);

if (event_loop->n_waiters != 0) {
while (event_loop->n_waiters != 0) {
uv_cond_wait(&event_loop->cond_var, &event_loop->mutex);
}

if (event_loop->n_waiters == 0) {
uv_run(event_loop->loop, UV_RUN_ONCE);
}
uv_run(event_loop->loop, UV_RUN_ONCE);
/*
* We leave `uv_run` only when `uv_stop` is called as there is always the `uv_async_t` so
* we can never run out of things to wait on. `uv_stop` is only called from `async_callback`
* when another thread wants to work with the event loop so we need to give up the mutex.
*/

uv_mutex_unlock(&event_loop->mutex);
}
Expand Down

0 comments on commit 13529c7

Please sign in to comment.