@@ -9,7 +9,6 @@ use std::{hint, mem, thread};
9
9
////////////////////////////////////////////////////////////////////////////////////////////////////
10
10
// Nonpoison & Poison Tests
11
11
////////////////////////////////////////////////////////////////////////////////////////////////////
12
-
13
12
use super :: nonpoison_and_poison_unwrap_test;
14
13
15
14
nonpoison_and_poison_unwrap_test ! (
@@ -112,6 +111,7 @@ nonpoison_and_poison_unwrap_test!(
112
111
self . 0 . fetch_add( 1 , Ordering :: SeqCst ) ;
113
112
}
114
113
}
114
+
115
115
let num_drops = Arc :: new( AtomicUsize :: new( 0 ) ) ;
116
116
let m = Mutex :: new( Foo ( num_drops. clone( ) ) ) ;
117
117
assert_eq!( num_drops. load( Ordering :: SeqCst ) , 0 ) ;
@@ -169,6 +169,28 @@ nonpoison_and_poison_unwrap_test!(
169
169
}
170
170
) ;
171
171
172
+ nonpoison_and_poison_unwrap_test ! (
173
+ name: test_set_drop,
174
+ test_body: {
175
+ use locks:: Mutex ;
176
+
177
+ struct Foo ( Arc <AtomicUsize >) ;
178
+ impl Drop for Foo {
179
+ fn drop( & mut self ) {
180
+ self . 0 . fetch_add( 1 , Ordering :: SeqCst ) ;
181
+ }
182
+ }
183
+
184
+ let num_drops = Arc :: new( AtomicUsize :: new( 0 ) ) ;
185
+ let m = Mutex :: new( Foo ( num_drops. clone( ) ) ) ;
186
+ assert_eq!( num_drops. load( Ordering :: SeqCst ) , 0 ) ;
187
+
188
+ let different = Foo ( Arc :: new( AtomicUsize :: new( 42 ) ) ) ;
189
+ maybe_unwrap!( m. set( different) ) ;
190
+ assert_eq!( num_drops. load( Ordering :: SeqCst ) , 1 ) ;
191
+ }
192
+ ) ;
193
+
172
194
nonpoison_and_poison_unwrap_test ! (
173
195
name: test_replace,
174
196
test_body: {
@@ -277,6 +299,63 @@ nonpoison_and_poison_unwrap_test!(
277
299
}
278
300
) ;
279
301
302
+ #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
303
+ nonpoison_and_poison_unwrap_test ! (
304
+ name: test_panics,
305
+ test_body: {
306
+ use locks:: Mutex ;
307
+
308
+ let mutex = Mutex :: new( 42 ) ;
309
+
310
+ let catch_unwind_result1 = panic:: catch_unwind( AssertUnwindSafe ( || {
311
+ let _guard1 = maybe_unwrap!( mutex. lock( ) ) ;
312
+
313
+ panic!( "test panic with mutex once" ) ;
314
+ } ) ) ;
315
+ assert!( catch_unwind_result1. is_err( ) ) ;
316
+
317
+ let catch_unwind_result2 = panic:: catch_unwind( AssertUnwindSafe ( || {
318
+ let _guard2 = maybe_unwrap!( mutex. lock( ) ) ;
319
+
320
+ panic!( "test panic with mutex twice" ) ;
321
+ } ) ) ;
322
+ assert!( catch_unwind_result2. is_err( ) ) ;
323
+
324
+ let catch_unwind_result3 = panic:: catch_unwind( AssertUnwindSafe ( || {
325
+ let _guard3 = maybe_unwrap!( mutex. lock( ) ) ;
326
+
327
+ panic!( "test panic with mutex thrice" ) ;
328
+ } ) ) ;
329
+ assert!( catch_unwind_result3. is_err( ) ) ;
330
+ }
331
+ ) ;
332
+
333
+ #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
334
+ nonpoison_and_poison_unwrap_test ! (
335
+ name: test_mutex_arc_access_in_unwind,
336
+ test_body: {
337
+ use locks:: Mutex ;
338
+
339
+ let arc = Arc :: new( Mutex :: new( 1 ) ) ;
340
+ let arc2 = arc. clone( ) ;
341
+ let _ = thread:: spawn( move || -> ( ) {
342
+ struct Unwinder {
343
+ i: Arc <Mutex <i32 >>,
344
+ }
345
+ impl Drop for Unwinder {
346
+ fn drop( & mut self ) {
347
+ * maybe_unwrap!( self . i. lock( ) ) += 1 ;
348
+ }
349
+ }
350
+ let _u = Unwinder { i: arc2 } ;
351
+ panic!( ) ;
352
+ } )
353
+ . join( ) ;
354
+ let lock = maybe_unwrap!( arc. lock( ) ) ;
355
+ assert_eq!( * lock, 2 ) ;
356
+ }
357
+ ) ;
358
+
280
359
////////////////////////////////////////////////////////////////////////////////////////////////////
281
360
// Poison Tests
282
361
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -440,28 +519,6 @@ fn test_mutex_arc_poison_mapped() {
440
519
assert ! ( arc. is_poisoned( ) ) ;
441
520
}
442
521
443
- #[ test]
444
- #[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
445
- fn test_mutex_arc_access_in_unwind ( ) {
446
- let arc = Arc :: new ( Mutex :: new ( 1 ) ) ;
447
- let arc2 = arc. clone ( ) ;
448
- let _ = thread:: spawn ( move || -> ( ) {
449
- struct Unwinder {
450
- i : Arc < Mutex < i32 > > ,
451
- }
452
- impl Drop for Unwinder {
453
- fn drop ( & mut self ) {
454
- * self . i . lock ( ) . unwrap ( ) += 1 ;
455
- }
456
- }
457
- let _u = Unwinder { i : arc2 } ;
458
- panic ! ( ) ;
459
- } )
460
- . join ( ) ;
461
- let lock = arc. lock ( ) . unwrap ( ) ;
462
- assert_eq ! ( * lock, 2 ) ;
463
- }
464
-
465
522
#[ test]
466
523
#[ cfg_attr( not( panic = "unwind" ) , ignore = "test requires unwinding support" ) ]
467
524
fn panic_while_mapping_unlocked_poison ( ) {
0 commit comments