Skip to content

Commit

Permalink
always operate on running_count atomically when multithreaded
Browse files Browse the repository at this point in the history
In upcoming changes we'll need to read running_count within error() to determine
whether we're single threaded or not. To do this while avoiding torn reads, we
need to use atomic reads and writes with running_count.

Github: related to #131 "minimal trace mode"
  • Loading branch information
Smattr committed Jun 16, 2019
1 parent 009b5a9 commit 1c8cb06
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions rumur/resources/header.c
Original file line number Diff line number Diff line change
Expand Up @@ -2378,7 +2378,7 @@ static void rendezvous_depart(bool leader) {
/* Reset the counter for the next rendezvous. */
assert(rendezvous_pending == 0 && "a rendezvous point is being exited "
"while some participating threads have yet to arrive");
rendezvous_pending = running_count;
rendezvous_pending = __atomic_load_n(&running_count, __ATOMIC_SEQ_CST);

/* Wake up the 'followers'. */
r = pthread_cond_broadcast(&rendezvous_cond);
Expand Down Expand Up @@ -2415,7 +2415,7 @@ retry:;
*/
bool leader = rendezvous_arrive();

if (leader && running_count > 1) {
if (leader && __atomic_load_n(&running_count, __ATOMIC_SEQ_CST) > 1) {
/* We unfortunately opted out of this rendezvous while the remaining threads
* were arriving at one and we were the last to arrive. Let's pretend we are
* participating in the rendezvous and unblock them.
Expand All @@ -2427,8 +2427,8 @@ retry:;
}

/* Remove ourselves from the known threads. */
assert(running_count > 0);
running_count--;
assert(__atomic_load_n(&running_count, __ATOMIC_SEQ_CST) > 0);
(void)__atomic_fetch_sub(&running_count, 1, __ATOMIC_SEQ_CST);

int r __attribute__((unused)) = pthread_mutex_unlock(&rendezvous_lock);
assert(r == 0);
Expand Down

0 comments on commit 1c8cb06

Please sign in to comment.