Skip to content

Commit

Permalink
Handle static mut
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 18, 2024
1 parent 50c9851 commit 1cf321a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
42 changes: 40 additions & 2 deletions rubicon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,27 +120,49 @@ macro_rules! process_local {
$crate::process_local_inner!($(#[$attrs])* $vis $name, $ty, $expr);
};

// single declaration (mut)
($(#[$attrs:meta])* $vis:vis static mut $name:ident: $ty:ty = $expr:expr $(;)?) => {
$crate::process_local_inner_mut!($(#[$attrs])* $vis $name, $ty, $expr);
};


// handle multiple declarations
($(#[$attrs:meta])* $vis:vis static $name:ident: $ty:ty = $expr:expr; $($rest:tt)*) => {
$crate::process_local_inner!($(#[$attrs])* $vis $name, $ty, $expr);
$crate::process_local!($($rest)*);
};

// handle multiple declarations
($(#[$attrs:meta])* $vis:vis static mut $name:ident: $ty:ty = $expr:expr; $($rest:tt)*) => {
$crate::process_local_inner_mut!($(#[$attrs])* $vis $name, $ty, $expr);
$crate::process_local!($($rest)*);
}
}

#[cfg(feature = "export-globals")]
#[macro_export]
macro_rules! process_local_inner {
($(#[$attrs:meta])* $vis:vis $name:ident, $ty:ty, $expr:expr) => {
$crate::paste! {
// we _could_ export with a mangled name, but we couldn't
// import with a mangled name (extern disables mangling)
#[export_name = stringify!([<$name __rubicon_export>])]
$(#[$attrs])*
$vis static $name: $ty = $expr;
}
};
}

#[cfg(feature = "export-globals")]
#[macro_export]
macro_rules! process_local_inner_mut {
($(#[$attrs:meta])* $vis:vis $name:ident, $ty:ty, $expr:expr) => {
$crate::paste! {
#[export_name = stringify!([<$name __rubicon_export>])]
$(#[$attrs])*
$vis static mut $name: $ty = $expr;
}
};
}

#[cfg(feature = "import-globals")]
#[macro_export]
macro_rules! process_local_inner {
Expand All @@ -157,6 +179,22 @@ macro_rules! process_local_inner {
};
}

#[cfg(feature = "import-globals")]
#[macro_export]
macro_rules! process_local_inner_mut {
($(#[$attrs:meta])* $vis:vis $name:ident, $ty:ty, $expr:expr) => {
$crate::paste! {
// externs require "unsafe" to access, but so do "static mut", so,
// no need to wrap in `TrustedExtern`
extern "Rust" {
#[link_name = stringify!([<$name __rubicon_export>])]
#[allow(improper_ctypes)]
$vis static mut $name: $ty;
}
}
};
}

//===== soprintln!

#[no_mangle]
Expand Down
28 changes: 28 additions & 0 deletions test-crates/bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use std::sync::atomic::Ordering;

use exports::{self as _, mokio};
use rubicon::soprintln;

fn main() {
std::env::set_var("SO_PRINTLN", "1");

let exe_path = std::env::current_exe().expect("Failed to get current exe path");
let project_root = exe_path
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
std::env::set_current_dir(project_root).expect("Failed to change directory");

soprintln!("app starting up...");

let modules = ["../mod_a", "../mod_b"];
Expand Down Expand Up @@ -38,6 +51,11 @@ fn main() {
let init_b: libloading::Symbol<unsafe extern "C" fn()> = unsafe { lib_b.get(b"init").unwrap() };
let init_b = Box::leak(Box::new(init_b));

soprintln!("DANGEROUS is now {}", unsafe {
mokio::DANGEROUS += 1;
mokio::DANGEROUS
});

soprintln!(
"PL1 = {}, TL1 = {} (initial)",
mokio::MOKIO_PL1.load(Ordering::Relaxed),
Expand Down Expand Up @@ -95,6 +113,9 @@ fn main() {
);
}

// TL1 should be 4 (incremented by each `init_X()` call)
assert_eq!(mokio::MOKIO_TL1.with(|s| s.load(Ordering::Relaxed)), 4);

id
})
.unwrap();
Expand All @@ -106,4 +127,11 @@ fn main() {
let id = jh.join().unwrap();
soprintln!("thread {} joined", id);
}

// PL1 should be exactly 16
// 2 per turn, 2 turns on the main thread, 2 turns on each of the 3 worker threads: 16 total
assert_eq!(mokio::MOKIO_PL1.load(Ordering::Relaxed), 16);

// DANGEROUS should be between 1 and 20
assert!(unsafe { mokio::DANGEROUS } >= 1 && unsafe { mokio::DANGEROUS } <= 20);
}
5 changes: 5 additions & 0 deletions test-crates/mod_a/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use rubicon::soprintln;
use std::sync::atomic::Ordering;

#[no_mangle]
pub fn init() {
mokio::MOKIO_TL1.with(|s| s.fetch_add(1, Ordering::Relaxed));
mokio::MOKIO_PL1.fetch_add(1, Ordering::Relaxed);
soprintln!("DANGEROUS is now {}", unsafe {
mokio::DANGEROUS += 1;
mokio::DANGEROUS
});
}
5 changes: 5 additions & 0 deletions test-crates/mod_b/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use rubicon::soprintln;
use std::sync::atomic::Ordering;

#[no_mangle]
pub fn init() {
mokio::MOKIO_TL1.with(|s| s.fetch_add(1, Ordering::Relaxed));
mokio::MOKIO_PL1.fetch_add(1, Ordering::Relaxed);
soprintln!("DANGEROUS is now {}", unsafe {
mokio::DANGEROUS += 1;
mokio::DANGEROUS
});
}
1 change: 1 addition & 0 deletions test-crates/mokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::atomic::AtomicU64;
rubicon::process_local! {
pub static MOKIO_PL1: AtomicU64 = AtomicU64::new(0);
pub static MOKIO_PL2: AtomicU64 = AtomicU64::new(0);
pub static mut DANGEROUS: u64 = 0;
}

rubicon::thread_local! {
Expand Down

0 comments on commit 1cf321a

Please sign in to comment.