@@ -134,6 +134,7 @@ struct caml_thread_table {
134
134
st_masterlock default_lock ;
135
135
struct caml_locking_scheme default_locking_scheme ;
136
136
int tick_thread_running ;
137
+ int tick_thread_disabled ;
137
138
st_thread_id tick_thread_id ;
138
139
};
139
140
@@ -183,6 +184,9 @@ static void thread_lock_release(int dom_id)
183
184
/* Whether the "tick" thread is already running for this domain */
184
185
#define Tick_thread_running thread_table[Caml_state->id].tick_thread_running
185
186
187
+ /* Whether the "tick" thread is disabled for this domain */
188
+ #define Tick_thread_disabled thread_table[Caml_state->id].tick_thread_disabled
189
+
186
190
/* The thread identifier of the "tick" thread for this domain */
187
191
#define Tick_thread_id thread_table[Caml_state->id].tick_thread_id
188
192
@@ -598,15 +602,18 @@ CAMLprim value caml_thread_initialize(value unit)
598
602
return Val_unit ;
599
603
}
600
604
601
- CAMLprim value caml_thread_cleanup ( value unit )
605
+ static void stop_tick_thread ( void )
602
606
{
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
+ }
609
613
614
+ CAMLprim value caml_thread_cleanup (value unit )
615
+ {
616
+ stop_tick_thread ();
610
617
return Val_unit ;
611
618
}
612
619
@@ -691,6 +698,29 @@ static int create_tick_thread(void)
691
698
return err ;
692
699
}
693
700
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
+
694
724
CAMLprim value caml_thread_new (value clos )
695
725
{
696
726
CAMLparam1 (clos );
@@ -738,10 +768,9 @@ CAMLprim value caml_thread_new(value clos)
738
768
sync_check_error (err , "Thread.create" );
739
769
}
740
770
741
- if (! Tick_thread_running ) {
742
- err = create_tick_thread ();
771
+ if (!Tick_thread_disabled ) {
772
+ err = start_tick_thread ();
743
773
sync_check_error (err , "Thread.create" );
744
- Tick_thread_running = 1 ;
745
774
}
746
775
CAMLreturn (th -> descr );
747
776
}
@@ -788,10 +817,9 @@ CAMLexport int caml_c_thread_register(void)
788
817
/* Allocate the thread descriptor on the heap */
789
818
th -> descr = caml_thread_new_descriptor (Val_unit ); /* no closure */
790
819
791
- if (! Tick_thread_running ) {
792
- st_retcode err = create_tick_thread ();
820
+ if (!Tick_thread_disabled ) {
821
+ st_retcode err = start_tick_thread ();
793
822
sync_check_error (err , "caml_register_c_thread" );
794
- Tick_thread_running = 1 ;
795
823
}
796
824
797
825
/* Release the master lock */
0 commit comments