Skip to content

Commit

Permalink
flambda-backend: port #852 (add primitive to enable/disable tick thre…
Browse files Browse the repository at this point in the history
…ad) (#2054)
  • Loading branch information
riaqn authored Dec 5, 2023
1 parent f85d724 commit d300550
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions otherlibs/systhreads/st_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ struct caml_thread_table {
caml_thread_t active_thread;
st_masterlock thread_lock;
int tick_thread_running;
int tick_thread_enabled;
st_thread_id tick_thread_id;
};

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

/* Whether the "tick" thread is enabled for this domain */
#define Tick_thread_enabled thread_table[Caml_state->id].tick_thread_enabled

/* The thread identifier of the "tick" thread for this domain */
#define Tick_thread_id thread_table[Caml_state->id].tick_thread_id

Expand Down Expand Up @@ -507,15 +511,18 @@ CAMLprim value caml_thread_initialize(value unit)
return Val_unit;
}

CAMLprim value caml_thread_cleanup(value unit)
static void stop_tick_thread(void)
{
if (Tick_thread_running){
atomic_store_release(&Tick_thread_stop, 1);
st_thread_join(Tick_thread_id);
atomic_store_release(&Tick_thread_stop, 0);
Tick_thread_running = 0;
}
if (!Tick_thread_running) return;
atomic_store_release(&Tick_thread_stop, 1);
st_thread_join(Tick_thread_id);
atomic_store_release(&Tick_thread_stop, 0);
Tick_thread_running = 0;
}

CAMLprim value caml_thread_cleanup(value unit)
{
stop_tick_thread();
return Val_unit;
}

Expand Down Expand Up @@ -594,6 +601,29 @@ static int create_tick_thread(void)
return err;
}

static st_retcode start_tick_thread(void)
{
if (Tick_thread_running) return 0;
st_retcode err = create_tick_thread();
if (err == 0) Tick_thread_running = 1;
return err;
}

CAMLprim value caml_enable_tick_thread(value v_enable)
{
int enable = Long_val(v_enable) ? 1 : 0;

if (enable) {
st_retcode err = start_tick_thread();
sync_check_error(err, "caml_enable_tick_thread");
} else {
stop_tick_thread();
}

Tick_thread_enabled = enable;
return Val_unit;
}

CAMLprim value caml_thread_new(value clos)
{
CAMLparam1(clos);
Expand Down Expand Up @@ -641,10 +671,9 @@ CAMLprim value caml_thread_new(value clos)
sync_check_error(err, "Thread.create");
}

if (! Tick_thread_running) {
err = create_tick_thread();
if (Tick_thread_enabled) {
err = start_tick_thread();
sync_check_error(err, "Thread.create");
Tick_thread_running = 1;
}
CAMLreturn(th->descr);
}
Expand Down Expand Up @@ -687,10 +716,9 @@ CAMLexport int caml_c_thread_register(void)
/* Allocate the thread descriptor on the heap */
th->descr = caml_thread_new_descriptor(Val_unit); /* no closure */

if (! Tick_thread_running) {
st_retcode err = create_tick_thread();
if (Tick_thread_enabled) {
st_retcode err = start_tick_thread();
sync_check_error(err, "caml_register_c_thread");
Tick_thread_running = 1;
}

/* Release the master lock */
Expand Down

0 comments on commit d300550

Please sign in to comment.