Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use atomics for system_working global #13333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions misc/tsan_suppressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ race_top:RUBY_VM_INTERRUPTED_ANY
race_top:unblock_function_set
race_top:threadptr_get_interrupts

# system_working needs to be converted to atomic
race:system_working

# It's already crashing. We're doing our best
signal:rb_vm_bugreport
race:check_reserved_signal_
Expand Down
2 changes: 1 addition & 1 deletion thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static int hrtime_update_expire(rb_hrtime_t *, const rb_hrtime_t);
NORETURN(static void async_bug_fd(const char *mesg, int errno_arg, int fd));
MAYBE_UNUSED(static int consume_communication_pipe(int fd));

static volatile int system_working = 1;
static rb_atomic_t system_working = 1;
static rb_internal_thread_specific_key_t specific_key_count;

/********************************************************************************/
Expand Down
22 changes: 10 additions & 12 deletions thread_pthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,7 @@ rb_thread_wakeup_timer_thread(int sig)
timer_thread_wakeup_force();

// interrupt main thread if main thread is available
if (system_working) {
if (RUBY_ATOMIC_LOAD(system_working)) {
rb_vm_t *vm = GET_VM();
rb_thread_t *main_th = vm->ractor.main_thread;

Expand Down Expand Up @@ -3005,12 +3005,12 @@ timer_thread_func(void *ptr)

RUBY_DEBUG_LOG("started%s", "");

while (system_working) {
while (RUBY_ATOMIC_LOAD(system_working)) {
timer_thread_check_signal(vm);
timer_thread_check_timeout(vm);
ubf_wakeup_all_threads();

RUBY_DEBUG_LOG("system_working:%d", system_working);
RUBY_DEBUG_LOG("system_working:%d", RUBY_ATOMIC_LOAD(system_working));
timer_thread_polling(vm);
}

Expand Down Expand Up @@ -3124,18 +3124,16 @@ rb_thread_create_timer_thread(void)
static int
native_stop_timer_thread(void)
{
int stopped;
stopped = --system_working <= 0;
RUBY_ATOMIC_SET(system_working, 0);

if (stopped) {
RUBY_DEBUG_LOG("wakeup send %d", timer_th.comm_fds[1]);
timer_thread_wakeup_force();
RUBY_DEBUG_LOG("wakeup sent");
pthread_join(timer_th.pthread_id, NULL);
}
RUBY_DEBUG_LOG("wakeup send %d", timer_th.comm_fds[1]);
timer_thread_wakeup_force();
RUBY_DEBUG_LOG("wakeup sent");
pthread_join(timer_th.pthread_id, NULL);

if (TT_DEBUG) fprintf(stderr, "stop timer thread\n");
return stopped;

return 1;
}

static void
Expand Down
16 changes: 8 additions & 8 deletions thread_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,14 +798,14 @@ rb_thread_create_timer_thread(void)
static int
native_stop_timer_thread(void)
{
int stopped = --system_working <= 0;
if (stopped) {
SetEvent(timer_thread.lock);
native_thread_join(timer_thread.id);
CloseHandle(timer_thread.lock);
timer_thread.lock = 0;
}
return stopped;
RUBY_ATOMIC_SET(system_working, 0);

SetEvent(timer_thread.lock);
native_thread_join(timer_thread.id);
CloseHandle(timer_thread.lock);
timer_thread.lock = 0;

return 1;
}

static void
Expand Down
Loading