Skip to content

Commit 3a0fd03

Browse files
committed
profile: drop nested timer callbacks
1 parent 284e824 commit 3a0fd03

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

‎notes/profile-lifecycle-mt.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ active callback to leave, then clears callback data and the shared stack-dump
1313
buffer. Stale profile hooks check that the active profiler still belongs to the
1414
current VM before loading callback state.
1515

16+
Callback delivery is single-entry across both profiler backends. The TG-local
17+
signal path and the non-TG-local timer-thread path CAS the callback count from
18+
zero to one before invoking user code; nested or concurrent samples are dropped
19+
instead of recursively entering callback state.
20+
1621
The Lua `jit.profile.start()` wrapper first stops any old same-VM profiler
1722
session, clears old registry anchors, anchors the new hidden callback coroutine
1823
and Lua function in the registry, and only then starts the C profiler. The timer

‎src/lj_profile.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,24 @@ static LJ_AINLINE int profile_state_active_g(ProfileState *ps, global_State *g)
146146
profile_g_load_acq(ps) == g;
147147
}
148148

149+
static void profile_callback_leave(ProfileState *ps);
150+
149151
#if !LJ_PROFILE_TGLOCAL
150152
static int profile_callback_enter(ProfileState *ps, global_State *g)
151153
{
154+
uint32_t old = 0;
152155
if (!profile_state_active_g(ps, g))
153156
return 0;
157+
if (!la_cas32(&ps->callbacks, &old, 1, LA_ACQ_REL, LA_ACQ))
158+
return 0;
154159
la_store32_rel(&ps->callback_tid, lj_thr_current_id(g));
155-
(void)la_add32_rlx(&ps->callbacks, 1);
156160
if (profile_state_active_g(ps, g))
157161
return 1;
158-
if (la_sub32_acqrel(&ps->callbacks, 1) == 1)
159-
la_futex_wake(&ps->callbacks, INT_MAX);
160-
la_store32_rel(&ps->callback_tid, 0);
162+
profile_callback_leave(ps);
161163
return 0;
162164
}
163165
#endif
164166

165-
static void profile_callback_leave(ProfileState *ps);
166-
167167
#if LJ_PROFILE_TGLOCAL
168168
static int profile_callback_tryenter(ProfileState *ps, global_State *g)
169169
{

0 commit comments

Comments
 (0)