From 4b9262b911e028557317b7f55939120f27352b54 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Thu, 18 Jul 2024 20:15:01 +0200 Subject: [PATCH] okok --- .github/workflows/test.yml | 10 +++++++++- test-crates/bin/src/main.rs | 9 ++------- test-crates/mod_a/src/lib.rs | 7 +++---- test-crates/mod_b/src/lib.rs | 7 +++---- test-crates/mokio/src/lib.rs | 15 +++++++++++++++ 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ecf3b73..71c9c45 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -22,4 +22,12 @@ jobs: profile: minimal override: true - name: Run cargo command - run: cargo run --manifest-path test-crates/bin/Cargo.toml + run: | + cargo run --manifest-path test-crates/bin/Cargo.toml + - name: Run cargo command (release) + run: | + cargo run --manifest-path test-crates/bin/Cargo.toml --release + - name: Run cargo command (release with SOPRINTLN=1) + run: | + export SOPRINTLN=1 + cargo run --manifest-path test-crates/bin/Cargo.toml --release diff --git a/test-crates/bin/src/main.rs b/test-crates/bin/src/main.rs index 2279416..9ec6976 100644 --- a/test-crates/bin/src/main.rs +++ b/test-crates/bin/src/main.rs @@ -72,11 +72,6 @@ fn main() { let init_b: libloading::Symbol = 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), @@ -153,6 +148,6 @@ fn main() { // 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); + // same for DANGEROUS, it's just guarded by a mutex internally + assert_eq!(mokio::get_dangerous(), 16); } diff --git a/test-crates/mod_a/src/lib.rs b/test-crates/mod_a/src/lib.rs index e6634c4..bcfa624 100644 --- a/test-crates/mod_a/src/lib.rs +++ b/test-crates/mod_a/src/lib.rs @@ -6,8 +6,7 @@ pub fn init() { soprintln::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 - }); + + let dangerous = mokio::inc_dangerous(); + soprintln!("DANGEROUS is now {}", dangerous); } diff --git a/test-crates/mod_b/src/lib.rs b/test-crates/mod_b/src/lib.rs index e6634c4..bcfa624 100644 --- a/test-crates/mod_b/src/lib.rs +++ b/test-crates/mod_b/src/lib.rs @@ -6,8 +6,7 @@ pub fn init() { soprintln::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 - }); + + let dangerous = mokio::inc_dangerous(); + soprintln!("DANGEROUS is now {}", dangerous); } diff --git a/test-crates/mokio/src/lib.rs b/test-crates/mokio/src/lib.rs index 998117e..349c9ab 100644 --- a/test-crates/mokio/src/lib.rs +++ b/test-crates/mokio/src/lib.rs @@ -3,10 +3,25 @@ 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; + static DANGEROUS_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(()); } rubicon::thread_local! { pub static MOKIO_TL1: AtomicU64 = AtomicU64::new(0); pub static MOKIO_TL2: AtomicU64 = AtomicU64::new(0); } + +pub fn inc_dangerous() -> u64 { + let _guard = DANGEROUS_MUTEX.lock().unwrap(); + unsafe { + DANGEROUS += 1; + DANGEROUS + } +} + +pub fn get_dangerous() -> u64 { + let _guard = DANGEROUS_MUTEX.lock().unwrap(); + unsafe { DANGEROUS } +}