fix: use _Atomic flag for sel_alloc_thread_id registration, move reg before loop, add wrong-thread guard test

This commit is contained in:
Rongsong Shen 2026-04-30 06:15:01 +00:00
parent b722e5cb3d
commit 1eb4eb1335
3 changed files with 61 additions and 13 deletions

View file

@ -23,8 +23,9 @@
/* ── optimize level ───────────────────────────────────────────────────── */
static _Atomic int g_sel_optimize_level = -1; /* -1 = unset */
static pthread_t sel_alloc_thread_id; /* 0 = unregistered — skip checks */
static _Atomic int g_sel_optimize_level = -1; /* -1 = unset */
static pthread_t sel_alloc_thread_id;
static _Atomic int sel_alloc_thread_registered = 0; /* 1 after sel_register_alloc_thread() */
void sel_optimize_set(int level)
{
@ -186,12 +187,14 @@ void sel_optimize_test_reset(void)
{
sel_node_table_fini();
atomic_store_explicit(&g_sel_optimize_level, -1, memory_order_seq_cst);
sel_alloc_thread_id = 0;
atomic_store_explicit(&sel_alloc_thread_registered, 0, memory_order_seq_cst);
/* sel_alloc_thread_id left as-is; only checked when registered flag is set */
}
void sel_register_alloc_thread(void)
{
sel_alloc_thread_id = pthread_self();
atomic_store_explicit(&sel_alloc_thread_registered, 1, memory_order_release);
}
/* ── node construction ───────────────────────────────────────────────── */
@ -651,7 +654,8 @@ int eval_select(const xce_select_node_t* node, select_eval_ctx_t* ctx)
if (sel_get_optimize_level() <= 0) {
ERROR("eval_select called with optimize_level <= 0"); abort();
}
if (sel_alloc_thread_id != 0 && !pthread_equal(pthread_self(), sel_alloc_thread_id)) {
if (atomic_load_explicit(&sel_alloc_thread_registered, memory_order_acquire)
&& !pthread_equal(pthread_self(), sel_alloc_thread_id)) {
ERROR("eval_select called from non-allocator thread");
return -1;
}
@ -875,7 +879,8 @@ void gc_sweep_select_nodes(const xce_sched_job_t** live_jobs, size_t njobs)
if (sel_node_table.table == NULL) {
return;
}
if (sel_alloc_thread_id != 0 && !pthread_equal(pthread_self(), sel_alloc_thread_id)) {
if (atomic_load_explicit(&sel_alloc_thread_registered, memory_order_acquire)
&& !pthread_equal(pthread_self(), sel_alloc_thread_id)) {
ERROR("gc_sweep_select_nodes called from non-allocator thread");
return;
}