@@ -3,8 +3,9 @@ use std::fs::File;
33use std:: panic:: { catch_unwind, AssertUnwindSafe } ;
44use std:: path:: Path ;
55use std:: sync:: atomic:: { AtomicBool , Ordering } ;
6+ use std:: sync:: Mutex ;
67
7- use windows_sys:: Win32 :: Foundation :: GetLastError ;
8+ use windows_sys:: Win32 :: Foundation :: { GetLastError , HANDLE } ;
89use windows_sys:: Win32 :: System :: LibraryLoader :: { GetModuleHandleA , GetProcAddress , LoadLibraryA } ;
910use windows_sys:: Win32 :: System :: Threading :: Sleep ;
1011
@@ -24,6 +25,11 @@ static MONITOR_STARTED: AtomicBool = AtomicBool::new(false);
2425static MONITOR_RUNNING : AtomicBool = AtomicBool :: new ( false ) ;
2526static RELOAD_IN_PROGRESS : AtomicBool = AtomicBool :: new ( false ) ;
2627
28+ struct SendHandle ( HANDLE ) ;
29+ unsafe impl Send for SendHandle { }
30+
31+ static MONITOR_THREAD_HANDLE : Mutex < Option < SendHandle > > = Mutex :: new ( None ) ;
32+
2733extern "system" {
2834 fn CreateDirectoryA ( path : * const u8 , security : * const std:: ffi:: c_void ) -> i32 ;
2935 fn CreateThread (
@@ -33,8 +39,10 @@ extern "system" {
3339 param : * mut std:: ffi:: c_void ,
3440 flags : u32 ,
3541 id : * mut u32 ,
36- ) -> * mut std :: ffi :: c_void ;
42+ ) -> HANDLE ;
3743 fn FreeLibrary ( module : * mut std:: ffi:: c_void ) -> i32 ;
44+ fn WaitForSingleObject ( handle : HANDLE , milliseconds : u32 ) -> u32 ;
45+ fn CloseHandle ( handle : HANDLE ) -> i32 ;
3846}
3947
4048pub fn is_reloading ( ) -> bool {
@@ -75,12 +83,26 @@ pub fn start_monitor() {
7583 log_warn ( "failed to start reload.flag monitor thread" ) ;
7684 return ;
7785 }
86+ if let Ok ( mut h) = MONITOR_THREAD_HANDLE . lock ( ) {
87+ * h = Some ( SendHandle ( handle) ) ;
88+ }
7889 }
7990 log_info ( "reload.flag monitor thread started" ) ;
8091}
8192
8293pub fn stop_monitor ( ) {
94+ if !MONITOR_STARTED . load ( Ordering :: SeqCst ) {
95+ return ;
96+ }
8397 MONITOR_RUNNING . store ( false , Ordering :: SeqCst ) ;
98+ unsafe {
99+ if let Ok ( mut h) = MONITOR_THREAD_HANDLE . lock ( ) {
100+ if let Some ( SendHandle ( handle) ) = h. take ( ) {
101+ WaitForSingleObject ( handle, 2000 ) ;
102+ CloseHandle ( handle) ;
103+ }
104+ }
105+ }
84106 MONITOR_STARTED . store ( false , Ordering :: SeqCst ) ;
85107}
86108
0 commit comments