mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00
sync: make const_new
methods always available (#5885)
Since MSRV is bumped to 1.63, `Mutex::new` is now usable in const context. Also use `assert!` in const function to ensure correctness instead of silently truncating the value and remove cfg `tokio_no_const_mutex_new`. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
fb08591b43
commit
efe3ab679a
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@ -442,7 +442,6 @@ jobs:
|
||||
|
||||
# Run a platform without AtomicU64 and no const Mutex::new
|
||||
- target: armv5te-unknown-linux-gnueabi
|
||||
rustflags: --cfg tokio_no_const_mutex_new
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Rust stable
|
||||
@ -485,7 +484,6 @@ jobs:
|
||||
|
||||
# Run a platform without AtomicU64 and no const Mutex::new
|
||||
- target: armv5te-unknown-linux-gnueabi
|
||||
rustflags: --cfg tokio_no_const_mutex_new
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Install Rust stable
|
||||
@ -568,10 +566,6 @@ jobs:
|
||||
|
||||
# https://github.com/tokio-rs/tokio/pull/5356
|
||||
# https://github.com/tokio-rs/tokio/issues/5373
|
||||
- name: Check without const_mutex_new
|
||||
run: cargo hack check -p tokio --feature-powerset --depth 2 --keep-going
|
||||
env:
|
||||
RUSTFLAGS: --cfg tokio_unstable --cfg tokio_taskdump -Dwarnings --cfg tokio_no_atomic_u64 --cfg tokio_no_const_mutex_new
|
||||
- name: Check with const_mutex_new
|
||||
run: cargo hack check -p tokio --feature-powerset --depth 2 --keep-going
|
||||
env:
|
||||
|
@ -13,7 +13,6 @@ impl<T> Mutex<T> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[cfg(not(tokio_no_const_mutex_new))]
|
||||
pub(crate) const fn const_new(t: T) -> Mutex<T> {
|
||||
Mutex(sync::Mutex::new(t))
|
||||
}
|
||||
|
@ -541,13 +541,7 @@ macro_rules! cfg_not_has_atomic_u64 {
|
||||
macro_rules! cfg_has_const_mutex_new {
|
||||
($($item:item)*) => {
|
||||
$(
|
||||
#[cfg(all(
|
||||
not(all(loom, test)),
|
||||
any(
|
||||
feature = "parking_lot",
|
||||
not(tokio_no_const_mutex_new)
|
||||
)
|
||||
))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
$item
|
||||
)*
|
||||
}
|
||||
@ -556,13 +550,7 @@ macro_rules! cfg_has_const_mutex_new {
|
||||
macro_rules! cfg_not_has_const_mutex_new {
|
||||
($($item:item)*) => {
|
||||
$(
|
||||
#[cfg(not(all(
|
||||
not(all(loom, test)),
|
||||
any(
|
||||
feature = "parking_lot",
|
||||
not(tokio_no_const_mutex_new)
|
||||
)
|
||||
)))]
|
||||
#[cfg(all(loom, test))]
|
||||
$item
|
||||
)*
|
||||
}
|
||||
|
@ -178,14 +178,9 @@ impl Semaphore {
|
||||
/// Creates a new semaphore with the initial number of permits.
|
||||
///
|
||||
/// Maximum number of permits on 32-bit platforms is `1<<29`.
|
||||
///
|
||||
/// If the specified number of permits exceeds the maximum permit amount
|
||||
/// Then the value will get clamped to the maximum number of permits.
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
pub(crate) const fn const_new(mut permits: usize) -> Self {
|
||||
// NOTE: assertions and by extension panics are still being worked on: https://github.com/rust-lang/rust/issues/74925
|
||||
// currently we just clamp the permit count when it exceeds the max
|
||||
permits &= Self::MAX_PERMITS;
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub(crate) const fn const_new(permits: usize) -> Self {
|
||||
assert!(permits <= Self::MAX_PERMITS);
|
||||
|
||||
Self {
|
||||
permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
|
||||
@ -198,6 +193,19 @@ impl Semaphore {
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new closed semaphore with 0 permits.
|
||||
pub(crate) fn new_closed() -> Self {
|
||||
Self {
|
||||
permits: AtomicUsize::new(Self::CLOSED),
|
||||
waiters: Mutex::new(Waitlist {
|
||||
queue: LinkedList::new(),
|
||||
closed: true,
|
||||
}),
|
||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||
resource_span: tracing::Span::none(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current number of available permits.
|
||||
pub(crate) fn available_permits(&self) -> usize {
|
||||
self.permits.load(Acquire) >> Self::PERMIT_SHIFT
|
||||
|
@ -378,8 +378,7 @@ impl<T: ?Sized> Mutex<T> {
|
||||
///
|
||||
/// static LOCK: Mutex<i32> = Mutex::const_new(5);
|
||||
/// ```
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test)),))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_new(t: T) -> Self
|
||||
where
|
||||
T: Sized,
|
||||
|
@ -443,8 +443,7 @@ impl Notify {
|
||||
///
|
||||
/// static NOTIFY: Notify = Notify::const_new();
|
||||
/// ```
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_new() -> Notify {
|
||||
Notify {
|
||||
state: AtomicUsize::new(0),
|
||||
|
@ -114,12 +114,10 @@ impl<T> Drop for OnceCell<T> {
|
||||
|
||||
impl<T> From<T> for OnceCell<T> {
|
||||
fn from(value: T) -> Self {
|
||||
let semaphore = Semaphore::new(0);
|
||||
semaphore.close();
|
||||
OnceCell {
|
||||
value_set: AtomicBool::new(true),
|
||||
value: UnsafeCell::new(MaybeUninit::new(value)),
|
||||
semaphore,
|
||||
semaphore: Semaphore::new_closed(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -139,6 +137,10 @@ impl<T> OnceCell<T> {
|
||||
/// If the `Option` is `None`, this is equivalent to `OnceCell::new`.
|
||||
///
|
||||
/// [`OnceCell::new`]: crate::sync::OnceCell::new
|
||||
// Once https://github.com/rust-lang/rust/issues/73255 lands
|
||||
// and tokio MSRV is bumped to the rustc version with it stablised,
|
||||
// we can made this function available in const context,
|
||||
// by creating `Semaphore::const_new_closed`.
|
||||
pub fn new_with(value: Option<T>) -> Self {
|
||||
if let Some(v) = value {
|
||||
OnceCell::from(v)
|
||||
@ -171,8 +173,7 @@ impl<T> OnceCell<T> {
|
||||
/// assert_eq!(*result, 2);
|
||||
/// }
|
||||
/// ```
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_new() -> Self {
|
||||
OnceCell {
|
||||
value_set: AtomicBool::new(false),
|
||||
|
@ -334,8 +334,7 @@ impl<T: ?Sized> RwLock<T> {
|
||||
///
|
||||
/// static LOCK: RwLock<i32> = RwLock::const_new(5);
|
||||
/// ```
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_new(value: T) -> RwLock<T>
|
||||
where
|
||||
T: Sized,
|
||||
@ -359,13 +358,13 @@ impl<T: ?Sized> RwLock<T> {
|
||||
///
|
||||
/// static LOCK: RwLock<i32> = RwLock::const_with_max_readers(5, 1024);
|
||||
/// ```
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
pub const fn const_with_max_readers(value: T, mut max_reads: u32) -> RwLock<T>
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_with_max_readers(value: T, max_reads: u32) -> RwLock<T>
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
max_reads &= MAX_READS;
|
||||
assert!(max_reads <= MAX_READS);
|
||||
|
||||
RwLock {
|
||||
mr: max_reads,
|
||||
c: UnsafeCell::new(value),
|
||||
|
@ -172,20 +172,22 @@ impl Semaphore {
|
||||
///
|
||||
/// static SEM: Semaphore = Semaphore::const_new(10);
|
||||
/// ```
|
||||
///
|
||||
#[cfg(all(feature = "parking_lot", not(all(loom, test))))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "parking_lot")))]
|
||||
#[cfg(not(all(loom, test)))]
|
||||
pub const fn const_new(permits: usize) -> Self {
|
||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||
return Self {
|
||||
Self {
|
||||
ll_sem: ll::Semaphore::const_new(permits),
|
||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||
resource_span: tracing::Span::none(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(not(tokio_unstable), not(feature = "tracing")))]
|
||||
return Self {
|
||||
ll_sem: ll::Semaphore::const_new(permits),
|
||||
};
|
||||
/// Creates a new closed semaphore with 0 permits.
|
||||
pub(crate) fn new_closed() -> Self {
|
||||
Self {
|
||||
ll_sem: ll::Semaphore::new_closed(),
|
||||
#[cfg(all(tokio_unstable, feature = "tracing"))]
|
||||
resource_span: tracing::Span::none(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the current number of available permits.
|
||||
|
@ -5,12 +5,7 @@ cfg_io_driver! {
|
||||
#[cfg(feature = "rt")]
|
||||
pub(crate) mod atomic_cell;
|
||||
|
||||
#[cfg(any(
|
||||
feature = "rt",
|
||||
feature = "signal",
|
||||
feature = "process",
|
||||
tokio_no_const_mutex_new,
|
||||
))]
|
||||
#[cfg(any(feature = "rt", feature = "signal", feature = "process"))]
|
||||
pub(crate) mod once_cell;
|
||||
|
||||
#[cfg(any(
|
||||
|
@ -4,7 +4,11 @@
|
||||
use std::mem;
|
||||
use std::ops::Drop;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
use std::time::Duration;
|
||||
use tokio::runtime;
|
||||
use tokio::sync::OnceCell;
|
||||
use tokio::sync::SetError;
|
||||
use tokio::time;
|
||||
|
||||
#[test]
|
||||
fn drop_cell() {
|
||||
@ -102,184 +106,170 @@ fn from() {
|
||||
assert_eq!(*cell.get().unwrap(), 2);
|
||||
}
|
||||
|
||||
#[cfg(feature = "parking_lot")]
|
||||
mod parking_lot {
|
||||
use super::*;
|
||||
|
||||
use tokio::runtime;
|
||||
use tokio::sync::SetError;
|
||||
use tokio::time;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
async fn func1() -> u32 {
|
||||
5
|
||||
}
|
||||
|
||||
async fn func2() -> u32 {
|
||||
time::sleep(Duration::from_millis(1)).await;
|
||||
10
|
||||
}
|
||||
|
||||
async fn func_err() -> Result<u32, ()> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
async fn func_ok() -> Result<u32, ()> {
|
||||
Ok(10)
|
||||
}
|
||||
|
||||
async fn func_panic() -> u32 {
|
||||
time::sleep(Duration::from_millis(1)).await;
|
||||
panic!();
|
||||
}
|
||||
|
||||
async fn sleep_and_set() -> u32 {
|
||||
// Simulate sleep by pausing time and waiting for another thread to
|
||||
// resume clock when calling `set`, then finding the cell being initialized
|
||||
// by this call
|
||||
time::sleep(Duration::from_millis(2)).await;
|
||||
5
|
||||
}
|
||||
|
||||
async fn advance_time_and_set(
|
||||
cell: &'static OnceCell<u32>,
|
||||
v: u32,
|
||||
) -> Result<(), SetError<u32>> {
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
cell.set(v)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_init() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.start_paused(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_init(func2).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
time::resume();
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert_eq!(*result2, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_init_panic() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
time::pause();
|
||||
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_init(func_panic).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert_eq!(*result2, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_and_get() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let _ = rt.spawn(async { ONCE.set(5) }).await;
|
||||
let value = ONCE.get().unwrap();
|
||||
assert_eq!(*value, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_uninit() {
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
let uninit = ONCE.get();
|
||||
assert!(uninit.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_twice() {
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
let first = ONCE.set(5);
|
||||
assert_eq!(first, Ok(()));
|
||||
let second = ONCE.set(6);
|
||||
assert!(second.err().unwrap().is_already_init_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_while_initializing() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
time::pause();
|
||||
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(sleep_and_set).await });
|
||||
let handle2 = rt.spawn(async { advance_time_and_set(&ONCE, 10).await });
|
||||
|
||||
time::advance(Duration::from_millis(2)).await;
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert!(result2.err().unwrap().is_initializing_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_try_init() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.start_paused(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_try_init(func_err).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_try_init(func_ok).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
time::resume();
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
assert!(result1.is_err());
|
||||
|
||||
let result2 = handle2.await.unwrap();
|
||||
assert_eq!(*result2.unwrap(), 10);
|
||||
});
|
||||
}
|
||||
async fn func1() -> u32 {
|
||||
5
|
||||
}
|
||||
|
||||
async fn func2() -> u32 {
|
||||
time::sleep(Duration::from_millis(1)).await;
|
||||
10
|
||||
}
|
||||
|
||||
async fn func_err() -> Result<u32, ()> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
async fn func_ok() -> Result<u32, ()> {
|
||||
Ok(10)
|
||||
}
|
||||
|
||||
async fn func_panic() -> u32 {
|
||||
time::sleep(Duration::from_millis(1)).await;
|
||||
panic!();
|
||||
}
|
||||
|
||||
async fn sleep_and_set() -> u32 {
|
||||
// Simulate sleep by pausing time and waiting for another thread to
|
||||
// resume clock when calling `set`, then finding the cell being initialized
|
||||
// by this call
|
||||
time::sleep(Duration::from_millis(2)).await;
|
||||
5
|
||||
}
|
||||
|
||||
async fn advance_time_and_set(cell: &'static OnceCell<u32>, v: u32) -> Result<(), SetError<u32>> {
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
cell.set(v)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_init() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.start_paused(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_init(func2).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
time::resume();
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert_eq!(*result2, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_init_panic() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
time::pause();
|
||||
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(func1).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_init(func_panic).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert_eq!(*result2, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_and_get() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let _ = rt.spawn(async { ONCE.set(5) }).await;
|
||||
let value = ONCE.get().unwrap();
|
||||
assert_eq!(*value, 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_uninit() {
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
let uninit = ONCE.get();
|
||||
assert!(uninit.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_twice() {
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
let first = ONCE.set(5);
|
||||
assert_eq!(first, Ok(()));
|
||||
let second = ONCE.set(6);
|
||||
assert!(second.err().unwrap().is_already_init_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_while_initializing() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
time::pause();
|
||||
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_init(sleep_and_set).await });
|
||||
let handle2 = rt.spawn(async { advance_time_and_set(&ONCE, 10).await });
|
||||
|
||||
time::advance(Duration::from_millis(2)).await;
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
let result2 = handle2.await.unwrap();
|
||||
|
||||
assert_eq!(*result1, 5);
|
||||
assert!(result2.err().unwrap().is_initializing_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_or_try_init() {
|
||||
let rt = runtime::Builder::new_current_thread()
|
||||
.enable_time()
|
||||
.start_paused(true)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
static ONCE: OnceCell<u32> = OnceCell::const_new();
|
||||
|
||||
rt.block_on(async {
|
||||
let handle1 = rt.spawn(async { ONCE.get_or_try_init(func_err).await });
|
||||
let handle2 = rt.spawn(async { ONCE.get_or_try_init(func_ok).await });
|
||||
|
||||
time::advance(Duration::from_millis(1)).await;
|
||||
time::resume();
|
||||
|
||||
let result1 = handle1.await.unwrap();
|
||||
assert!(result1.is_err());
|
||||
|
||||
let result2 = handle2.await.unwrap();
|
||||
assert_eq!(*result2.unwrap(), 10);
|
||||
});
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#![warn(rust_2018_idioms)]
|
||||
#![cfg(all(feature = "full"))]
|
||||
#![cfg(feature = "full")]
|
||||
|
||||
use futures::future::FutureExt;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::task::JoinSet;
|
||||
use tokio::time::Duration;
|
||||
@ -184,52 +185,45 @@ async fn abort_all() {
|
||||
assert_eq!(set.len(), 0);
|
||||
}
|
||||
|
||||
#[cfg(feature = "parking_lot")]
|
||||
mod parking_lot {
|
||||
use super::*;
|
||||
// This ensures that `join_next` works correctly when the coop budget is
|
||||
// exhausted.
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn join_set_coop() {
|
||||
// Large enough to trigger coop.
|
||||
const TASK_NUM: u32 = 1000;
|
||||
|
||||
use futures::future::FutureExt;
|
||||
static SEM: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(0);
|
||||
|
||||
// This ensures that `join_next` works correctly when the coop budget is
|
||||
// exhausted.
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn join_set_coop() {
|
||||
// Large enough to trigger coop.
|
||||
const TASK_NUM: u32 = 1000;
|
||||
let mut set = JoinSet::new();
|
||||
|
||||
static SEM: tokio::sync::Semaphore = tokio::sync::Semaphore::const_new(0);
|
||||
|
||||
let mut set = JoinSet::new();
|
||||
|
||||
for _ in 0..TASK_NUM {
|
||||
set.spawn(async {
|
||||
SEM.add_permits(1);
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for all tasks to complete.
|
||||
//
|
||||
// Since this is a `current_thread` runtime, there's no race condition
|
||||
// between the last permit being added and the task completing.
|
||||
let _ = SEM.acquire_many(TASK_NUM).await.unwrap();
|
||||
|
||||
let mut count = 0;
|
||||
let mut coop_count = 0;
|
||||
loop {
|
||||
match set.join_next().now_or_never() {
|
||||
Some(Some(Ok(()))) => {}
|
||||
Some(Some(Err(err))) => panic!("failed: {}", err),
|
||||
None => {
|
||||
coop_count += 1;
|
||||
tokio::task::yield_now().await;
|
||||
continue;
|
||||
}
|
||||
Some(None) => break,
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
assert!(coop_count >= 1);
|
||||
assert_eq!(count, TASK_NUM);
|
||||
for _ in 0..TASK_NUM {
|
||||
set.spawn(async {
|
||||
SEM.add_permits(1);
|
||||
});
|
||||
}
|
||||
|
||||
// Wait for all tasks to complete.
|
||||
//
|
||||
// Since this is a `current_thread` runtime, there's no race condition
|
||||
// between the last permit being added and the task completing.
|
||||
let _ = SEM.acquire_many(TASK_NUM).await.unwrap();
|
||||
|
||||
let mut count = 0;
|
||||
let mut coop_count = 0;
|
||||
loop {
|
||||
match set.join_next().now_or_never() {
|
||||
Some(Some(Ok(()))) => {}
|
||||
Some(Some(Err(err))) => panic!("failed: {}", err),
|
||||
None => {
|
||||
coop_count += 1;
|
||||
tokio::task::yield_now().await;
|
||||
continue;
|
||||
}
|
||||
Some(None) => break,
|
||||
}
|
||||
|
||||
count += 1;
|
||||
}
|
||||
assert!(coop_count >= 1);
|
||||
assert_eq!(count, TASK_NUM);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user