File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
tests/ui/threads-sendsync Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ //@ run-pass
2+ //@ needs-threads
3+
4+ use std:: alloc:: { GlobalAlloc , Layout , System } ;
5+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
6+
7+ static GLOBAL : AtomicUsize = AtomicUsize :: new ( 0 ) ;
8+
9+ struct Local ;
10+
11+ thread_local ! {
12+ static LOCAL : Local = {
13+ GLOBAL . fetch_or( 1 , Ordering :: Relaxed ) ;
14+ Local
15+ } ;
16+ }
17+
18+ impl Drop for Local {
19+ fn drop ( & mut self ) {
20+ GLOBAL . fetch_or ( 2 , Ordering :: Relaxed ) ;
21+ }
22+ }
23+
24+ #[ global_allocator]
25+ static ALLOC : Alloc = Alloc ;
26+ struct Alloc ;
27+
28+ unsafe impl GlobalAlloc for Alloc {
29+ unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
30+ LOCAL . with ( |_local| { } ) ;
31+ unsafe { System . alloc ( layout) }
32+ }
33+
34+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
35+ LOCAL . with ( |_local| { } ) ;
36+ unsafe { System . dealloc ( ptr, layout) }
37+ }
38+ }
39+
40+ fn main ( ) {
41+ std:: thread:: spawn ( || {
42+ std:: hint:: black_box ( vec ! [ 1 , 2 ] ) ;
43+ assert ! ( GLOBAL . load( Ordering :: Relaxed ) == 1 ) ;
44+ } )
45+ . join ( )
46+ . unwrap ( ) ;
47+ assert ! ( GLOBAL . load( Ordering :: Relaxed ) == 3 ) ;
48+ }
You can’t perform that action at this time.
0 commit comments