Skip to content

Commit

Permalink
Put loom behind cfg(loom) again
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Jun 11, 2024
1 parent a2081b6 commit f704c15
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std = []
# Enables async receiving by implementing Future
async = []

[dependencies]
[target.'cfg(loom)'.dependencies]
# Only used for internal correctness testing. Never enable this feature when using oneshot.
loom = { version = "0.7.2", features = ["futures"], optional = true }

Expand All @@ -36,7 +36,7 @@ tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros", "time"]
async-std = { version = "1", features = ["attributes"] }

[lints.rust]
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(oneshot_test_delay)'] }
unexpected_cfgs = { level = "deny", check-cfg = ['cfg(loom)', 'cfg(oneshot_test_delay)'] }

[[bench]]
name = "benches"
Expand Down
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
#![deny(rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
extern crate alloc;

use core::{
Expand All @@ -125,20 +125,20 @@ use core::{
ptr::{self, NonNull},
};

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
use core::{
cell::UnsafeCell,
sync::atomic::{fence, AtomicU8, Ordering::*},
};
#[cfg(feature = "loom")]
#[cfg(loom)]
use loom::{
cell::UnsafeCell,
sync::atomic::{fence, AtomicU8, Ordering::*},
};

#[cfg(all(feature = "async", not(feature = "loom")))]
#[cfg(all(feature = "async", not(loom)))]
use core::hint;
#[cfg(all(feature = "async", feature = "loom"))]
#[cfg(all(feature = "async", loom))]
use loom::hint;

#[cfg(feature = "async")]
Expand All @@ -151,10 +151,10 @@ use std::time::{Duration, Instant};

#[cfg(feature = "std")]
mod thread {
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
pub use std::thread::{current, park, park_timeout, Thread};

#[cfg(feature = "loom")]
#[cfg(loom)]
pub use loom::thread::{current, park, Thread};

// loom does not support parking with a timeout. So we just
Expand All @@ -163,17 +163,17 @@ mod thread {
// One thing to note is that very short timeouts are needed
// when using loom, since otherwise the looping will cause
// an overflow in loom.
#[cfg(feature = "loom")]
#[cfg(loom)]
pub fn park_timeout(_timeout: std::time::Duration) {
loom::thread::yield_now()
}
}

#[cfg(feature = "loom")]
#[cfg(loom)]
mod loombox;
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
use alloc::boxed::Box;
#[cfg(feature = "loom")]
#[cfg(loom)]
use loombox::Box;

mod errors;
Expand Down Expand Up @@ -1055,12 +1055,12 @@ impl<T> Channel<T> {

#[inline(always)]
unsafe fn message(&self) -> &MaybeUninit<T> {
#[cfg(feature = "loom")]
#[cfg(loom)]
{
self.message.with(|ptr| &*ptr)
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
{
&*self.message.get()
}
Expand All @@ -1071,12 +1071,12 @@ impl<T> Channel<T> {
where
F: FnOnce(&mut MaybeUninit<T>),
{
#[cfg(feature = "loom")]
#[cfg(loom)]
{
self.message.with_mut(|ptr| op(&mut *ptr))
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
{
op(&mut *self.message.get())
}
Expand All @@ -1088,12 +1088,12 @@ impl<T> Channel<T> {
where
F: FnOnce(&mut MaybeUninit<ReceiverWaker>),
{
#[cfg(feature = "loom")]
#[cfg(loom)]
{
self.waker.with_mut(|ptr| op(&mut *ptr))
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
{
op(&mut *self.waker.get())
}
Expand All @@ -1106,12 +1106,12 @@ impl<T> Channel<T> {

#[inline(always)]
unsafe fn take_message(&self) -> T {
#[cfg(feature = "loom")]
#[cfg(loom)]
{
self.message.with(|ptr| ptr::read(ptr)).assume_init()
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
{
ptr::read(self.message.get()).assume_init()
}
Expand All @@ -1130,12 +1130,12 @@ impl<T> Channel<T> {

#[inline(always)]
unsafe fn take_waker(&self) -> ReceiverWaker {
#[cfg(feature = "loom")]
#[cfg(loom)]
{
self.waker.with(|ptr| ptr::read(ptr)).assume_init()
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
{
ptr::read(self.waker.get()).assume_init()
}
Expand Down Expand Up @@ -1235,7 +1235,7 @@ impl ReceiverWaker {
}
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
#[test]
fn receiver_waker_size() {
let expected: usize = match (cfg!(feature = "std"), cfg!(feature = "async")) {
Expand Down
2 changes: 1 addition & 1 deletion tests/async.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(all(feature = "async", not(feature = "loom")))]
#![cfg(all(feature = "async", not(loom)))]

use core::mem;
use core::time::Duration;
Expand Down
4 changes: 2 additions & 2 deletions tests/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use core::{future, mem, pin, task};

#[cfg(feature = "loom")]
#[cfg(loom)]
pub use loom::sync::{Arc, Mutex};
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
pub use std::sync::{Arc, Mutex};

mod helpers;
Expand Down
12 changes: 6 additions & 6 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

extern crate alloc;

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
use alloc::sync::Arc;
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst};
#[cfg(feature = "loom")]
#[cfg(loom)]
use loom::sync::{
atomic::{AtomicUsize, Ordering::SeqCst},
Arc,
};

#[cfg(feature = "loom")]
#[cfg(loom)]
pub mod waker;

pub fn maybe_loom_model(test: impl Fn() + Sync + Send + 'static) {
#[cfg(feature = "loom")]
#[cfg(loom)]
loom::model(test);
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
test();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/loom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "loom")]
#![cfg(loom)]

use oneshot::TryRecvError;

Expand Down
2 changes: 1 addition & 1 deletion tests/raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(not(feature = "loom"))]
#![cfg(not(loom))]

use oneshot::{channel, Receiver, Sender};

Expand Down
22 changes: 11 additions & 11 deletions tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use std::time::{Duration, Instant};

#[cfg(feature = "std")]
mod thread {
#[cfg(feature = "loom")]
#[cfg(loom)]
pub use loom::thread::spawn;
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
pub use std::thread::{sleep, spawn};

#[cfg(feature = "loom")]
#[cfg(loom)]
pub fn sleep(_timeout: core::time::Duration) {
loom::thread::yield_now()
}
Expand Down Expand Up @@ -59,7 +59,7 @@ fn send_before_recv() {
// FIXME: This test does not work with loom. There is something that happens after the
// channel object becomes larger than ~500 bytes and that makes an atomic read from the state
// result in "signal: 10, SIGBUS: access to undefined memory"
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
maybe_loom_model(|| {
let (sender, receiver) = oneshot::channel::<[u8; 4096]>();
assert!(sender.send([0b10101010; 4096]).is_ok());
Expand Down Expand Up @@ -246,16 +246,16 @@ fn recv_deadline_and_timeout_no_time() {
}

// This test doesn't give meaningful results when run with oneshot_test_delay and loom
#[cfg(all(feature = "std", not(all(oneshot_test_delay, feature = "loom"))))]
#[cfg(all(feature = "std", not(all(oneshot_test_delay, loom))))]
#[test]
fn recv_deadline_time_should_elapse() {
maybe_loom_model(|| {
let (_sender, receiver) = oneshot::channel::<u128>();

let start = Instant::now();
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
let timeout = Duration::from_millis(100);
#[cfg(feature = "loom")]
#[cfg(loom)]
let timeout = Duration::from_millis(1);
assert_eq!(
receiver.recv_deadline(start + timeout),
Expand All @@ -266,16 +266,16 @@ fn recv_deadline_time_should_elapse() {
})
}

#[cfg(all(feature = "std", not(all(oneshot_test_delay, feature = "loom"))))]
#[cfg(all(feature = "std", not(all(oneshot_test_delay, loom))))]
#[test]
fn recv_timeout_time_should_elapse() {
maybe_loom_model(|| {
let (_sender, receiver) = oneshot::channel::<u128>();

let start = Instant::now();
#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
let timeout = Duration::from_millis(100);
#[cfg(feature = "loom")]
#[cfg(loom)]
let timeout = Duration::from_millis(1);

assert_eq!(
Expand All @@ -287,7 +287,7 @@ fn recv_timeout_time_should_elapse() {
})
}

#[cfg(not(feature = "loom"))]
#[cfg(not(loom))]
#[test]
fn non_send_type_can_be_used_on_same_thread() {
use std::ptr;
Expand Down

0 comments on commit f704c15

Please sign in to comment.