@@ -9,14 +9,18 @@ mod needs_feature {
9
9
static V1 : AtomicUsize = AtomicUsize :: new ( 0 ) ;
10
10
static V2 : AtomicBool = AtomicBool :: new ( false ) ;
11
11
12
- let reg1 = gix:: interrupt:: init_handler ( 3 , || {
13
- V1 . fetch_add ( 1 , Ordering :: SeqCst ) ;
14
- } )
12
+ // SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
13
+ let reg1 = unsafe {
14
+ gix:: interrupt:: init_handler ( 3 , || {
15
+ V1 . fetch_add ( 1 , Ordering :: SeqCst ) ;
16
+ } )
17
+ }
15
18
. expect ( "succeeds" ) ;
16
19
assert ! ( !gix:: interrupt:: is_triggered( ) ) ;
17
20
assert_eq ! ( V1 . load( Ordering :: Relaxed ) , 0 ) ;
18
- let reg2 =
19
- gix:: interrupt:: init_handler ( 2 , || V2 . store ( true , Ordering :: SeqCst ) ) . expect ( "multi-initialization is OK" ) ;
21
+ // SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
22
+ let reg2 = unsafe { gix:: interrupt:: init_handler ( 2 , || V2 . store ( true , Ordering :: SeqCst ) ) }
23
+ . expect ( "multi-initialization is OK" ) ;
20
24
assert ! ( !V2 . load( Ordering :: Relaxed ) ) ;
21
25
22
26
signal_hook:: low_level:: raise ( SIGTERM ) . expect ( "signal can be raised" ) ;
@@ -36,13 +40,17 @@ mod needs_feature {
36
40
"the deregistration succeeded and this is an optional side-effect"
37
41
) ;
38
42
39
- let reg1 = gix:: interrupt:: init_handler ( 3 , || {
40
- V1 . fetch_add ( 1 , Ordering :: SeqCst ) ;
41
- } )
43
+ // SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
44
+ let reg1 = unsafe {
45
+ gix:: interrupt:: init_handler ( 3 , || {
46
+ V1 . fetch_add ( 1 , Ordering :: SeqCst ) ;
47
+ } )
48
+ }
42
49
. expect ( "succeeds" ) ;
43
50
assert_eq ! ( V1 . load( Ordering :: Relaxed ) , 2 , "nothing changed yet" ) ;
44
- let reg2 =
45
- gix:: interrupt:: init_handler ( 2 , || V2 . store ( true , Ordering :: SeqCst ) ) . expect ( "multi-initialization is OK" ) ;
51
+ // SAFETY: The closure doesn't use mutexes or memory allocation, so it should be safe to call from a signal handler.
52
+ let reg2 = unsafe { gix:: interrupt:: init_handler ( 2 , || V2 . store ( true , Ordering :: SeqCst ) ) }
53
+ . expect ( "multi-initialization is OK" ) ;
46
54
assert ! ( !V2 . load( Ordering :: Relaxed ) ) ;
47
55
48
56
signal_hook:: low_level:: raise ( SIGTERM ) . expect ( "signal can be raised" ) ;
0 commit comments