From 47440615a039f03af2de8b4b3ba694abe9e99dd3 Mon Sep 17 00:00:00 2001 From: Zesen Qian Date: Mon, 20 Nov 2023 17:38:15 +0000 Subject: [PATCH 1/3] port #852 --- ocaml/otherlibs/systhreads/st_stubs.c | 52 ++++++++++++++++++++------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/ocaml/otherlibs/systhreads/st_stubs.c b/ocaml/otherlibs/systhreads/st_stubs.c index fe1df205eca..a47341b39ee 100644 --- a/ocaml/otherlibs/systhreads/st_stubs.c +++ b/ocaml/otherlibs/systhreads/st_stubs.c @@ -113,6 +113,7 @@ struct caml_thread_table { caml_thread_t active_thread; st_masterlock thread_lock; int tick_thread_running; + int caml_tick_thread_enabled; st_thread_id tick_thread_id; }; @@ -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 @@ -490,15 +494,17 @@ CAMLprim value caml_thread_initialize(value unit) return Val_unit; } +static void stop_tick_thread(void) { + 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) { - 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; - } - + stop_tick_thread(); return Val_unit; } @@ -577,6 +583,28 @@ static int create_tick_thread(void) return err; } +static st_retcode start_tick_thread(void) +{ + if (Tick_thread_running) return; + st_retcode err = create_tick_thread(); + if (err == 0) Tick_thread_running = 1; +} + +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(); + st_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); @@ -624,10 +652,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); } @@ -670,10 +697,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 */ From af7afd003c4f0e0be7dee8bea4dc757244109327 Mon Sep 17 00:00:00 2001 From: Zesen Qian Date: Mon, 20 Nov 2023 17:45:18 +0000 Subject: [PATCH 2/3] tiny cleanup --- ocaml/otherlibs/systhreads/st_stubs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ocaml/otherlibs/systhreads/st_stubs.c b/ocaml/otherlibs/systhreads/st_stubs.c index a47341b39ee..dd10ca11f31 100644 --- a/ocaml/otherlibs/systhreads/st_stubs.c +++ b/ocaml/otherlibs/systhreads/st_stubs.c @@ -113,7 +113,7 @@ struct caml_thread_table { caml_thread_t active_thread; st_masterlock thread_lock; int tick_thread_running; - int caml_tick_thread_enabled; + int tick_thread_enabled; st_thread_id tick_thread_id; }; @@ -494,7 +494,8 @@ CAMLprim value caml_thread_initialize(value unit) return Val_unit; } -static void stop_tick_thread(void) { +static void stop_tick_thread(void) +{ if (!Tick_thread_running) return; atomic_store_release(&Tick_thread_stop, 1); st_thread_join(Tick_thread_id); From 8aed62d29852457e1a7916175f4aff9234baef72 Mon Sep 17 00:00:00 2001 From: Zesen Qian Date: Mon, 20 Nov 2023 17:48:21 +0000 Subject: [PATCH 3/3] didn't compile, hehe --- ocaml/otherlibs/systhreads/st_stubs.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ocaml/otherlibs/systhreads/st_stubs.c b/ocaml/otherlibs/systhreads/st_stubs.c index dd10ca11f31..4889ed5e0bc 100644 --- a/ocaml/otherlibs/systhreads/st_stubs.c +++ b/ocaml/otherlibs/systhreads/st_stubs.c @@ -586,9 +586,10 @@ static int create_tick_thread(void) static st_retcode start_tick_thread(void) { - if (Tick_thread_running) return; + 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) @@ -597,7 +598,7 @@ CAMLprim value caml_enable_tick_thread(value v_enable) if (enable) { st_retcode err = start_tick_thread(); - st_check_error(err, "caml_enable_tick_thread"); + sync_check_error(err, "caml_enable_tick_thread"); } else { stop_tick_thread(); }