Skip to content

port #852 (add primitive to enable/disable tick thread) #2054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions ocaml/otherlibs/systhreads/st_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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 @@ -142,6 +143,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 @@ -490,15 +494,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 @@ -577,6 +584,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 @@ -624,10 +654,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 @@ -670,10 +699,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