Skip to content

Commit

Permalink
Ensure "can_work" flag gets set/unset from inside a mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-hughes committed Aug 22, 2024
1 parent 7e6000f commit d281df7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion reclaim.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#ifdef GC_PTHREADS
static pthread_mutex_t flzr_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t flzr_t_has_work = PTHREAD_COND_INITIALIZER;
static int flzr_can_work = 0;
#elif
#error "This fork of BDWGC only supports POSIX threads"
#endif
Expand Down Expand Up @@ -888,11 +889,15 @@ static void* init_finalize_thread(void *arg)
{
while(1) {
pthread_mutex_lock(&flzr_mtx);
while (GC_should_invoke_finalizers() == 0) {
flzr_can_work = GC_should_invoke_finalizers();
while (flzr_can_work == 0) {
pthread_cond_wait(&flzr_t_has_work, &flzr_mtx);
}
pthread_mutex_unlock(&flzr_mtx);
GC_invoke_finalizers();
pthread_mutex_lock(&flzr_mtx);
flzr_can_work = 0;
pthread_mutex_unlock(&flzr_mtx);
}
return arg;
}
Expand All @@ -910,6 +915,7 @@ GC_INNER void GC_maybe_wake_finalizer_thread()
return;

pthread_mutex_lock(&flzr_mtx);
flzr_can_work = 1;
pthread_cond_signal(&flzr_t_has_work);
pthread_mutex_unlock(&flzr_mtx);
}

0 comments on commit d281df7

Please sign in to comment.