3
3
#![ cfg_attr( feature="nightly" , feature( static_condvar) ) ]
4
4
#![ cfg_attr( feature="nightly" , feature( static_mutex) ) ]
5
5
6
+ use std:: sync:: atomic:: Ordering ;
7
+
6
8
#[ cfg( not( windows) ) ]
7
9
extern crate libc;
8
10
#[ cfg( windows) ]
@@ -15,17 +17,21 @@ extern crate lazy_static;
15
17
16
18
#[ cfg( feature="nightly" ) ]
17
19
mod features {
20
+ use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT } ;
18
21
use std:: sync:: { StaticCondvar , CONDVAR_INIT , StaticMutex , MUTEX_INIT } ;
19
22
pub static CVAR : StaticCondvar = CONDVAR_INIT ;
20
23
pub static MUTEX : StaticMutex = MUTEX_INIT ;
24
+ pub static DONE : AtomicBool = ATOMIC_BOOL_INIT ;
21
25
}
22
26
#[ cfg( not( feature="nightly" ) ) ]
23
27
mod features {
28
+ use std:: sync:: atomic:: { AtomicBool , ATOMIC_BOOL_INIT } ;
24
29
use std:: sync:: { Condvar , Mutex } ;
25
30
lazy_static ! {
26
31
pub static ref CVAR : Condvar = Condvar :: new( ) ;
27
32
pub static ref MUTEX : Mutex <bool > = Mutex :: new( false ) ;
28
33
}
34
+ pub static DONE : AtomicBool = ATOMIC_BOOL_INIT ;
29
35
}
30
36
use self :: features:: * ;
31
37
@@ -35,9 +41,11 @@ mod platform {
35
41
use libc:: types:: os:: common:: posix01:: sighandler_t;
36
42
use libc:: consts:: os:: posix88:: SIGINT ;
37
43
use libc:: funcs:: posix01:: signal:: signal;
44
+ use std:: sync:: atomic:: Ordering ;
38
45
39
46
#[ repr( C ) ]
40
47
pub fn handler ( _: c_int ) {
48
+ super :: features:: DONE . store ( true , Ordering :: Relaxed ) ;
41
49
super :: features:: CVAR . notify_all ( ) ;
42
50
}
43
51
#[ inline]
@@ -49,8 +57,10 @@ mod platform {
49
57
mod platform {
50
58
use kernel32:: SetConsoleCtrlHandler ;
51
59
use winapi:: { BOOL , DWORD , TRUE } ;
60
+ use std:: sync:: atomic:: Ordering ;
52
61
53
62
pub unsafe extern "system" fn handler ( _: DWORD ) -> BOOL {
63
+ super :: features:: DONE . store ( true , Ordering :: Relaxed ) ;
54
64
super :: features:: CVAR . notify_all ( ) ;
55
65
TRUE
56
66
}
@@ -75,7 +85,9 @@ impl CtrlC {
75
85
}
76
86
:: std:: thread:: spawn ( move || {
77
87
loop {
78
- let _ = CVAR . wait ( MUTEX . lock ( ) . unwrap ( ) ) ;
88
+ if !DONE . load ( Ordering :: Relaxed ) {
89
+ let _ = CVAR . wait ( MUTEX . lock ( ) . unwrap ( ) ) ;
90
+ }
79
91
user_handler ( ) ;
80
92
}
81
93
} ) ;
0 commit comments