@@ -5,51 +5,84 @@ use std::sync::mpsc::channel;
5
5
use std:: time:: Duration ;
6
6
use std:: { panic, thread} ;
7
7
8
- #[ test]
9
- fn smoke_once ( ) {
10
- static O : Once = Once :: new ( ) ;
11
- let mut a = 0 ;
12
- O . call_once ( || a += 1 ) ;
13
- assert_eq ! ( a, 1 ) ;
14
- O . call_once ( || a += 1 ) ;
15
- assert_eq ! ( a, 1 ) ;
16
- }
17
-
18
- #[ test]
19
- fn stampede_once ( ) {
20
- static O : Once = Once :: new ( ) ;
21
- static mut RUN : bool = false ;
22
-
23
- let ( tx, rx) = channel ( ) ;
24
- for _ in 0 ..10 {
25
- let tx = tx. clone ( ) ;
26
- thread:: spawn ( move || {
27
- for _ in 0 ..4 {
28
- thread:: yield_now ( )
29
- }
30
- unsafe {
31
- O . call_once ( || {
32
- assert ! ( !RUN ) ;
33
- RUN = true ;
34
- } ) ;
35
- assert ! ( RUN ) ;
36
- }
37
- tx. send ( ( ) ) . unwrap ( ) ;
38
- } ) ;
8
+ use super :: nonpoison_and_poison_unwrap_test;
9
+
10
+ nonpoison_and_poison_unwrap_test ! (
11
+ name: smoke_once,
12
+ test_body: {
13
+ use locks:: Once ;
14
+
15
+ static O : Once = Once :: new( ) ;
16
+ let mut a = 0 ;
17
+ O . call_once( || a += 1 ) ;
18
+ assert_eq!( a, 1 ) ;
19
+ O . call_once( || a += 1 ) ;
20
+ assert_eq!( a, 1 ) ;
39
21
}
40
-
41
- unsafe {
42
- O . call_once ( || {
43
- assert ! ( !RUN ) ;
44
- RUN = true ;
45
- } ) ;
46
- assert ! ( RUN ) ;
22
+ ) ;
23
+
24
+ nonpoison_and_poison_unwrap_test ! (
25
+ name: stampede_once,
26
+ test_body: {
27
+ use locks:: Once ;
28
+
29
+ static O : Once = Once :: new( ) ;
30
+ static mut RUN : bool = false ;
31
+
32
+ let ( tx, rx) = channel( ) ;
33
+ for _ in 0 ..10 {
34
+ let tx = tx. clone( ) ;
35
+ thread:: spawn( move || {
36
+ for _ in 0 ..4 {
37
+ thread:: yield_now( )
38
+ }
39
+ unsafe {
40
+ O . call_once( || {
41
+ assert!( !RUN ) ;
42
+ RUN = true ;
43
+ } ) ;
44
+ assert!( RUN ) ;
45
+ }
46
+ tx. send( ( ) ) . unwrap( ) ;
47
+ } ) ;
48
+ }
49
+
50
+ unsafe {
51
+ O . call_once( || {
52
+ assert!( !RUN ) ;
53
+ RUN = true ;
54
+ } ) ;
55
+ assert!( RUN ) ;
56
+ }
57
+
58
+ for _ in 0 ..10 {
59
+ rx. recv( ) . unwrap( ) ;
60
+ }
47
61
}
48
-
49
- for _ in 0 ..10 {
50
- rx. recv ( ) . unwrap ( ) ;
62
+ ) ;
63
+
64
+ nonpoison_and_poison_unwrap_test ! (
65
+ name: wait,
66
+ test_body: {
67
+ use locks:: Once ;
68
+
69
+ for _ in 0 ..50 {
70
+ let val = AtomicBool :: new( false ) ;
71
+ let once = Once :: new( ) ;
72
+
73
+ thread:: scope( |s| {
74
+ for _ in 0 ..4 {
75
+ s. spawn( || {
76
+ once. wait( ) ;
77
+ assert!( val. load( Relaxed ) ) ;
78
+ } ) ;
79
+ }
80
+
81
+ once. call_once( || val. store( true , Relaxed ) ) ;
82
+ } ) ;
83
+ }
51
84
}
52
- }
85
+ ) ;
53
86
54
87
#[ test]
55
88
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
@@ -119,25 +152,6 @@ fn wait_for_force_to_finish() {
119
152
assert ! ( t2. join( ) . is_ok( ) ) ;
120
153
}
121
154
122
- #[ test]
123
- fn wait ( ) {
124
- for _ in 0 ..50 {
125
- let val = AtomicBool :: new ( false ) ;
126
- let once = Once :: new ( ) ;
127
-
128
- thread:: scope ( |s| {
129
- for _ in 0 ..4 {
130
- s. spawn ( || {
131
- once. wait ( ) ;
132
- assert ! ( val. load( Relaxed ) ) ;
133
- } ) ;
134
- }
135
-
136
- once. call_once ( || val. store ( true , Relaxed ) ) ;
137
- } ) ;
138
- }
139
- }
140
-
141
155
#[ test]
142
156
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
143
157
fn wait_on_poisoned ( ) {
0 commit comments