Skip to content

Commit db9bdfe

Browse files
authored
port #852 (add primitive to enable/disable tick thread) (#2054)
1 parent a1c77d5 commit db9bdfe

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

ocaml/otherlibs/systhreads/st_stubs.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct caml_thread_table {
116116
caml_thread_t active_thread;
117117
st_masterlock thread_lock;
118118
int tick_thread_running;
119+
int tick_thread_enabled;
119120
st_thread_id tick_thread_id;
120121
};
121122

@@ -145,6 +146,9 @@ static void thread_lock_release(int dom_id)
145146
/* Whether the "tick" thread is already running for this domain */
146147
#define Tick_thread_running thread_table[Caml_state->id].tick_thread_running
147148

149+
/* Whether the "tick" thread is enabled for this domain */
150+
#define Tick_thread_enabled thread_table[Caml_state->id].tick_thread_enabled
151+
148152
/* The thread identifier of the "tick" thread for this domain */
149153
#define Tick_thread_id thread_table[Caml_state->id].tick_thread_id
150154

@@ -507,15 +511,18 @@ CAMLprim value caml_thread_initialize(value unit)
507511
return Val_unit;
508512
}
509513

510-
CAMLprim value caml_thread_cleanup(value unit)
514+
static void stop_tick_thread(void)
511515
{
512-
if (Tick_thread_running){
513-
atomic_store_release(&Tick_thread_stop, 1);
514-
st_thread_join(Tick_thread_id);
515-
atomic_store_release(&Tick_thread_stop, 0);
516-
Tick_thread_running = 0;
517-
}
516+
if (!Tick_thread_running) return;
517+
atomic_store_release(&Tick_thread_stop, 1);
518+
st_thread_join(Tick_thread_id);
519+
atomic_store_release(&Tick_thread_stop, 0);
520+
Tick_thread_running = 0;
521+
}
518522

523+
CAMLprim value caml_thread_cleanup(value unit)
524+
{
525+
stop_tick_thread();
519526
return Val_unit;
520527
}
521528

@@ -594,6 +601,29 @@ static int create_tick_thread(void)
594601
return err;
595602
}
596603

604+
static st_retcode start_tick_thread(void)
605+
{
606+
if (Tick_thread_running) return 0;
607+
st_retcode err = create_tick_thread();
608+
if (err == 0) Tick_thread_running = 1;
609+
return err;
610+
}
611+
612+
CAMLprim value caml_enable_tick_thread(value v_enable)
613+
{
614+
int enable = Long_val(v_enable) ? 1 : 0;
615+
616+
if (enable) {
617+
st_retcode err = start_tick_thread();
618+
sync_check_error(err, "caml_enable_tick_thread");
619+
} else {
620+
stop_tick_thread();
621+
}
622+
623+
Tick_thread_enabled = enable;
624+
return Val_unit;
625+
}
626+
597627
CAMLprim value caml_thread_new(value clos)
598628
{
599629
CAMLparam1(clos);
@@ -641,10 +671,9 @@ CAMLprim value caml_thread_new(value clos)
641671
sync_check_error(err, "Thread.create");
642672
}
643673

644-
if (! Tick_thread_running) {
645-
err = create_tick_thread();
674+
if (Tick_thread_enabled) {
675+
err = start_tick_thread();
646676
sync_check_error(err, "Thread.create");
647-
Tick_thread_running = 1;
648677
}
649678
CAMLreturn(th->descr);
650679
}
@@ -687,10 +716,9 @@ CAMLexport int caml_c_thread_register(void)
687716
/* Allocate the thread descriptor on the heap */
688717
th->descr = caml_thread_new_descriptor(Val_unit); /* no closure */
689718

690-
if (! Tick_thread_running) {
691-
st_retcode err = create_tick_thread();
719+
if (Tick_thread_enabled) {
720+
st_retcode err = start_tick_thread();
692721
sync_check_error(err, "caml_register_c_thread");
693-
Tick_thread_running = 1;
694722
}
695723

696724
/* Release the master lock */

0 commit comments

Comments
 (0)