@@ -116,6 +116,7 @@ struct caml_thread_table {
116
116
caml_thread_t active_thread ;
117
117
st_masterlock thread_lock ;
118
118
int tick_thread_running ;
119
+ int tick_thread_enabled ;
119
120
st_thread_id tick_thread_id ;
120
121
};
121
122
@@ -145,6 +146,9 @@ static void thread_lock_release(int dom_id)
145
146
/* Whether the "tick" thread is already running for this domain */
146
147
#define Tick_thread_running thread_table[Caml_state->id].tick_thread_running
147
148
149
+ /* Whether the "tick" thread is enabled for this domain */
150
+ #define Tick_thread_enabled thread_table[Caml_state->id].tick_thread_enabled
151
+
148
152
/* The thread identifier of the "tick" thread for this domain */
149
153
#define Tick_thread_id thread_table[Caml_state->id].tick_thread_id
150
154
@@ -507,15 +511,18 @@ CAMLprim value caml_thread_initialize(value unit)
507
511
return Val_unit ;
508
512
}
509
513
510
- CAMLprim value caml_thread_cleanup ( value unit )
514
+ static void stop_tick_thread ( void )
511
515
{
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
+ }
518
522
523
+ CAMLprim value caml_thread_cleanup (value unit )
524
+ {
525
+ stop_tick_thread ();
519
526
return Val_unit ;
520
527
}
521
528
@@ -594,6 +601,29 @@ static int create_tick_thread(void)
594
601
return err ;
595
602
}
596
603
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
+
597
627
CAMLprim value caml_thread_new (value clos )
598
628
{
599
629
CAMLparam1 (clos );
@@ -641,10 +671,9 @@ CAMLprim value caml_thread_new(value clos)
641
671
sync_check_error (err , "Thread.create" );
642
672
}
643
673
644
- if (! Tick_thread_running ) {
645
- err = create_tick_thread ();
674
+ if (Tick_thread_enabled ) {
675
+ err = start_tick_thread ();
646
676
sync_check_error (err , "Thread.create" );
647
- Tick_thread_running = 1 ;
648
677
}
649
678
CAMLreturn (th -> descr );
650
679
}
@@ -687,10 +716,9 @@ CAMLexport int caml_c_thread_register(void)
687
716
/* Allocate the thread descriptor on the heap */
688
717
th -> descr = caml_thread_new_descriptor (Val_unit ); /* no closure */
689
718
690
- if (! Tick_thread_running ) {
691
- st_retcode err = create_tick_thread ();
719
+ if (Tick_thread_enabled ) {
720
+ st_retcode err = start_tick_thread ();
692
721
sync_check_error (err , "caml_register_c_thread" );
693
- Tick_thread_running = 1 ;
694
722
}
695
723
696
724
/* Release the master lock */
0 commit comments