Skip to content

Commit b676838

Browse files
authored
flambda-backend: Revert "Replace thread save/restore API with switch" (#1984)
Revert "Replace thread save/restore API with switch (#1869)" This reverts commit 3d748da.
1 parent 33c8b8a commit b676838

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

otherlibs/systhreads/st_stubs.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ static void memprof_ctx_iter(th_ctx_action f, void* data)
251251
} while (th != curr_thread);
252252
}
253253

254-
static void thread_save_runtime_state(void)
254+
/* Saving and restoring runtime state in curr_thread */
255+
256+
CAMLexport void caml_thread_save_runtime_state(void)
255257
{
256258
if (Caml_state->_in_minor_collection)
257259
caml_fatal_error("Thread switch from inside minor GC");
@@ -279,8 +281,12 @@ static void thread_save_runtime_state(void)
279281
caml_memprof_leave_thread();
280282
}
281283

282-
static void thread_restore_runtime_state(void)
284+
CAMLexport void caml_thread_restore_runtime_state(void)
283285
{
286+
/* Update curr_thread to point to the thread descriptor corresponding
287+
to the thread currently executing */
288+
curr_thread = st_tls_get(thread_descriptor_key);
289+
284290
#ifdef NATIVE_CODE
285291
Caml_state->_top_of_stack = curr_thread->top_of_stack;
286292
Caml_state->_bottom_of_stack= curr_thread->bottom_of_stack;
@@ -305,24 +311,16 @@ static void thread_restore_runtime_state(void)
305311
caml_memprof_enter_thread(curr_thread->memprof_ctx);
306312
}
307313

308-
CAMLexport void caml_thread_switch_runtime_state(void)
309-
{
310-
caml_thread_t new_thread = st_tls_get(thread_descriptor_key);
311-
if (new_thread == curr_thread) return;
312-
thread_save_runtime_state();
313-
curr_thread = new_thread;
314-
thread_restore_runtime_state();
315-
}
316-
317314
CAMLexport void caml_switch_runtime_locking_scheme(struct caml_locking_scheme* new)
318315
{
319316
struct caml_locking_scheme* old;
320317

318+
caml_thread_save_runtime_state();
321319
old = atomic_exchange(&caml_locking_scheme, new);
322320
/* We hold 'old', but it is no longer the runtime lock */
323321
old->unlock(old->context);
324322
acquire_runtime_lock();
325-
caml_thread_switch_runtime_state();
323+
caml_thread_restore_runtime_state();
326324
}
327325

328326

@@ -331,6 +329,9 @@ CAMLexport void caml_switch_runtime_locking_scheme(struct caml_locking_scheme* n
331329

332330
static void caml_thread_enter_blocking_section(void)
333331
{
332+
/* Save the current runtime state in the thread descriptor
333+
of the current thread */
334+
caml_thread_save_runtime_state();
334335
/* Tell other threads that the runtime is free */
335336
release_runtime_lock();
336337
}
@@ -345,7 +346,7 @@ static void caml_thread_leave_blocking_section(void)
345346
#endif
346347
/* Wait until the runtime is free */
347348
acquire_runtime_lock();
348-
caml_thread_switch_runtime_state();
349+
caml_thread_restore_runtime_state();
349350
#ifdef _WIN32
350351
SetLastError(error);
351352
#endif
@@ -637,7 +638,7 @@ static void caml_thread_stop(void)
637638
changed as the thread was running, so we save it in the
638639
curr_thread data to make sure that the cleanup logic
639640
below uses accurate information. */
640-
thread_save_runtime_state();
641+
caml_thread_save_runtime_state();
641642
/* Tell memprof that this thread is terminating. */
642643
caml_memprof_delete_th_ctx(curr_thread->memprof_ctx);
643644
/* Signal that the thread has terminated */
@@ -888,6 +889,7 @@ CAMLprim value caml_thread_yield(value unit) /* ML */
888889
*/
889890
caml_raise_async_if_exception(caml_process_pending_signals_exn(),
890891
"signal handler");
892+
caml_thread_save_runtime_state();
891893
/* caml_locking_scheme may have changed in caml_process_pending_signals_exn */
892894
s = atomic_load(&caml_locking_scheme);
893895
s->yield(s->context);
@@ -896,7 +898,7 @@ CAMLprim value caml_thread_yield(value unit) /* ML */
896898
s->unlock(s->context);
897899
acquire_runtime_lock();
898900
}
899-
caml_thread_switch_runtime_state();
901+
caml_thread_restore_runtime_state();
900902
caml_raise_async_if_exception(caml_process_pending_signals_exn(),
901903
"signal handler");
902904

otherlibs/systhreads/threads.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,11 @@ CAMLextern_libthreads
102102
void caml_switch_runtime_locking_scheme(struct caml_locking_scheme*);
103103

104104
CAMLextern_libthreads
105-
void caml_thread_switch_runtime_state(void);
105+
void caml_thread_save_runtime_state(void);
106+
107+
CAMLextern_libthreads
108+
void caml_thread_restore_runtime_state(void);
106109

107-
/* A prior version of this API used save/restore rather than a single switch.
108-
For compatibility, aliases are defined for the old API.
109-
(These will be removed when the lone user of this API is updated) */
110-
#define caml_thread_save_runtime_state()
111-
#define caml_thread_restore_runtime_state caml_thread_switch_runtime_state
112110

113111
#ifdef __cplusplus
114112
}

0 commit comments

Comments
 (0)