Skip to content

Commit 622c8dc

Browse files
authored
flambda-backend: Port #852 (add a primitive to enable/disable tick thread) (#2130)
1 parent 4ebef65 commit 622c8dc

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

otherlibs/systhreads/st_stubs.c

+41-13
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ struct caml_thread_table {
134134
st_masterlock default_lock;
135135
struct caml_locking_scheme default_locking_scheme;
136136
int tick_thread_running;
137+
int tick_thread_disabled;
137138
st_thread_id tick_thread_id;
138139
};
139140

@@ -183,6 +184,9 @@ static void thread_lock_release(int dom_id)
183184
/* Whether the "tick" thread is already running for this domain */
184185
#define Tick_thread_running thread_table[Caml_state->id].tick_thread_running
185186

187+
/* Whether the "tick" thread is disabled for this domain */
188+
#define Tick_thread_disabled thread_table[Caml_state->id].tick_thread_disabled
189+
186190
/* The thread identifier of the "tick" thread for this domain */
187191
#define Tick_thread_id thread_table[Caml_state->id].tick_thread_id
188192

@@ -598,15 +602,18 @@ CAMLprim value caml_thread_initialize(value unit)
598602
return Val_unit;
599603
}
600604

601-
CAMLprim value caml_thread_cleanup(value unit)
605+
static void stop_tick_thread(void)
602606
{
603-
if (Tick_thread_running){
604-
atomic_store_release(&Tick_thread_stop, 1);
605-
st_thread_join(Tick_thread_id);
606-
atomic_store_release(&Tick_thread_stop, 0);
607-
Tick_thread_running = 0;
608-
}
607+
if (!Tick_thread_running) return;
608+
atomic_store_release(&Tick_thread_stop, 1);
609+
st_thread_join(Tick_thread_id);
610+
atomic_store_release(&Tick_thread_stop, 0);
611+
Tick_thread_running = 0;
612+
}
609613

614+
CAMLprim value caml_thread_cleanup(value unit)
615+
{
616+
stop_tick_thread();
610617
return Val_unit;
611618
}
612619

@@ -691,6 +698,29 @@ static int create_tick_thread(void)
691698
return err;
692699
}
693700

701+
static st_retcode start_tick_thread(void)
702+
{
703+
if (Tick_thread_running) return 0;
704+
st_retcode err = create_tick_thread();
705+
if (err == 0) Tick_thread_running = 1;
706+
return err;
707+
}
708+
709+
CAMLprim value caml_enable_tick_thread(value v_enable)
710+
{
711+
int enable = Long_val(v_enable) ? 1 : 0;
712+
713+
if (enable) {
714+
st_retcode err = start_tick_thread();
715+
sync_check_error(err, "caml_enable_tick_thread");
716+
} else {
717+
stop_tick_thread();
718+
}
719+
720+
Tick_thread_disabled = !enable;
721+
return Val_unit;
722+
}
723+
694724
CAMLprim value caml_thread_new(value clos)
695725
{
696726
CAMLparam1(clos);
@@ -738,10 +768,9 @@ CAMLprim value caml_thread_new(value clos)
738768
sync_check_error(err, "Thread.create");
739769
}
740770

741-
if (! Tick_thread_running) {
742-
err = create_tick_thread();
771+
if (!Tick_thread_disabled) {
772+
err = start_tick_thread();
743773
sync_check_error(err, "Thread.create");
744-
Tick_thread_running = 1;
745774
}
746775
CAMLreturn(th->descr);
747776
}
@@ -788,10 +817,9 @@ CAMLexport int caml_c_thread_register(void)
788817
/* Allocate the thread descriptor on the heap */
789818
th->descr = caml_thread_new_descriptor(Val_unit); /* no closure */
790819

791-
if (! Tick_thread_running) {
792-
st_retcode err = create_tick_thread();
820+
if (!Tick_thread_disabled) {
821+
st_retcode err = start_tick_thread();
793822
sync_check_error(err, "caml_register_c_thread");
794-
Tick_thread_running = 1;
795823
}
796824

797825
/* Release the master lock */

0 commit comments

Comments
 (0)