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