File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
2+
3+ static COUNT : AtomicUsize = AtomicUsize :: new ( 0 ) ;
4+
5+ unsafe extern "C" fn ctor ( ) {
6+ COUNT . fetch_add ( 1 , Ordering :: Relaxed ) ;
7+ }
8+
9+ macro_rules! ctor {
10+ ( $ident: ident = $ctor: ident) => {
11+ #[ cfg_attr(
12+ all( any(
13+ target_os = "linux" ,
14+ target_os = "android" ,
15+ target_os = "dragonfly" ,
16+ target_os = "freebsd" ,
17+ target_os = "haiku" ,
18+ target_os = "illumos" ,
19+ target_os = "netbsd" ,
20+ target_os = "openbsd" ,
21+ target_os = "none" ,
22+ target_family = "wasm" ,
23+ ) ) ,
24+ link_section = ".init_array"
25+ ) ]
26+ #[ cfg_attr( windows, link_section = ".CRT$XCU" ) ]
27+ #[ cfg_attr(
28+ any( target_os = "macos" , target_os = "ios" ) ,
29+ link_section = "__DATA,__mod_init_func"
30+ ) ]
31+ #[ used]
32+ static $ident: unsafe extern "C" fn ( ) = $ctor;
33+ } ;
34+ }
35+
36+ ctor ! { CTOR1 = ctor }
37+ ctor ! { CTOR2 = ctor }
38+ ctor ! { CTOR3 = ctor }
39+
40+ fn main ( ) {
41+ assert_eq ! ( COUNT . load( Ordering :: Relaxed ) , 3 , "ctors did not run" ) ;
42+ }
You can’t perform that action at this time.
0 commit comments