Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(clippy::unused_unit)] // false positive fixed in Rust 1.89

use std::fmt;
use std::marker::PhantomData;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::pin::Pin;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError, RwLock, TryLockError};
use std::task::{Context, Poll, Waker};
extern crate alloc;

use alloc::rc::Rc;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::marker::PhantomData;
use core::panic::{RefUnwindSafe, UnwindSafe};
use core::pin::Pin;
use core::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use core::task::{Context, Poll, Waker};
use std::sync::{Mutex, MutexGuard, PoisonError, RwLock, TryLockError};

use async_task::{Builder, Runnable};
use concurrent_queue::ConcurrentQueue;
Expand Down Expand Up @@ -90,10 +94,10 @@ pub use static_executors::*;
/// ```
pub struct Executor<'a> {
/// The executor state.
pub(crate) state: AtomicPtr<State>,
state: AtomicPtr<State>,

/// Makes the `'a` lifetime invariant.
_marker: PhantomData<std::cell::UnsafeCell<&'a ()>>,
_marker: PhantomData<core::cell::UnsafeCell<&'a ()>>,
}

// SAFETY: Executor stores no thread local state that can be accessed via other thread.
Expand Down Expand Up @@ -122,7 +126,7 @@ impl<'a> Executor<'a> {
/// ```
pub const fn new() -> Self {
Self {
state: AtomicPtr::new(std::ptr::null_mut()),
state: AtomicPtr::new(core::ptr::null_mut()),
_marker: PhantomData,
}
}
Expand Down Expand Up @@ -186,7 +190,7 @@ impl<'a> Executor<'a> {
/// ```
/// use async_executor::Executor;
/// use futures_lite::{stream, prelude::*};
/// use std::future::ready;
/// use core::future::ready;
///
/// # futures_lite::future::block_on(async {
/// let mut ex = Executor::new();
Expand Down Expand Up @@ -367,7 +371,7 @@ impl<'a> Executor<'a> {
let state = Arc::new(State::new());
let ptr = Arc::into_raw(state).cast_mut();
if let Err(actual) = atomic_ptr.compare_exchange(
std::ptr::null_mut(),
core::ptr::null_mut(),
ptr,
Ordering::AcqRel,
Ordering::Acquire,
Expand Down Expand Up @@ -527,7 +531,7 @@ impl<'a> LocalExecutor<'a> {
/// ```
/// use async_executor::LocalExecutor;
/// use futures_lite::{stream, prelude::*};
/// use std::future::ready;
/// use core::future::ready;
///
/// # futures_lite::future::block_on(async {
/// let mut ex = LocalExecutor::new();
Expand Down
17 changes: 9 additions & 8 deletions src/static_executors.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::{debug_state, Executor, LocalExecutor, State};
use async_task::{Builder, Runnable, Task};
use slab::Slab;
use std::{
use core::{
cell::UnsafeCell,
fmt,
future::Future,
marker::PhantomData,
panic::{RefUnwindSafe, UnwindSafe},
sync::{atomic::Ordering, PoisonError},
sync::atomic::Ordering,
};
use slab::Slab;
use std::sync::PoisonError;

impl Executor<'static> {
/// Consumes the [`Executor`] and intentionally leaks it.
Expand Down Expand Up @@ -46,7 +47,7 @@ impl Executor<'static> {
unsafe { &*ptr }
};

std::mem::forget(self);
core::mem::forget(self);

let mut active = state.active.lock().unwrap_or_else(PoisonError::into_inner);
if !active.is_empty() {
Expand All @@ -60,7 +61,7 @@ impl Executor<'static> {

// SAFETY: StaticExecutor has the same memory layout as State as it's repr(transparent).
// The lifetime is not altered: 'static -> 'static.
let static_executor: &'static StaticExecutor = unsafe { std::mem::transmute(state) };
let static_executor: &'static StaticExecutor = unsafe { core::mem::transmute(state) };
static_executor
}
}
Expand Down Expand Up @@ -101,7 +102,7 @@ impl LocalExecutor<'static> {
unsafe { &*ptr }
};

std::mem::forget(self);
core::mem::forget(self);

let mut active = state.active.lock().unwrap_or_else(PoisonError::into_inner);
if !active.is_empty() {
Expand All @@ -115,7 +116,7 @@ impl LocalExecutor<'static> {

// SAFETY: StaticLocalExecutor has the same memory layout as State as it's repr(transparent).
// The lifetime is not altered: 'static -> 'static.
let static_executor: &'static StaticLocalExecutor = unsafe { std::mem::transmute(state) };
let static_executor: &'static StaticLocalExecutor = unsafe { core::mem::transmute(state) };
static_executor
}
}
Expand All @@ -133,7 +134,7 @@ impl LocalExecutor<'static> {
/// [`StaticExecutor::run`] will cause the all spawned tasks to permanently leak. Any
/// tasks at the time will not be cancelled.
///
/// [`static`]: https://doc.rust-lang.org/std/keyword.static.html
/// [`static`]: https://doc.rust-lang.org/core/keyword.static.html
#[repr(transparent)]
pub struct StaticExecutor {
state: State,
Expand Down