Skip to content

Commit

Permalink
Option for unbounded queue
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 9, 2023
1 parent 2e5afe0 commit 705cdac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys" }
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal" }

[features]
default = ["std", "crossbeam-queue"]
default = ["std"]
std = []
unbounded = []

[dependencies]
heapless = { version = "0.7", default-features = false, features = ["cas"] }
crossbeam-queue = { version = "0.3", default-features = false, features = ["alloc"], optional = true }
crossbeam-queue = { version = "0.3", default-features = false, features = ["alloc"] }
async-task = { version = "4", default-features = false }
atomic-waker = { version = "1", default-features = false }
futures-lite = { version = "1", default-features = false }
log = { version = "0.4", default-features = false }

# ESP-IDF dependencies
[target.'cfg(target_os = "espidf")'.dependencies]
esp-idf-hal = { version = "0.41", default-features = false, features = ["wake-from-isr"] }
esp-idf-hal = { version = "0.41", default-features = false }

# WASM dependencies
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
34 changes: 11 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ where
///
/// - [CEventLoopWakeup] implementation for native event loops like those of GLIB, the Matter C++ SDK and others.
pub struct LocalExecutor<'a, W, const C: usize = 64> {
#[cfg(feature = "crossbeam-queue")]
#[cfg(feature = "unbounded")]
queue: Arc<crossbeam_queue::SegQueue<Runnable>>,
#[cfg(not(feature = "unbounded"))]
queue: Arc<crossbeam_queue::ArrayQueue<Runnable>>,
#[cfg(not(feature = "crossbeam-queue"))]
queue: Arc<heapless::mpmc::MpMcQueue<Runnable, C>>,
wakeup: W,
poll_runnable_waker: AtomicWaker,
_marker: PhantomData<core::cell::UnsafeCell<&'a Rc<()>>>,
Expand All @@ -279,10 +279,10 @@ where
/// Creates a new executor instance using the provided `Wakeup` instance.
pub fn wrap(wakeup: W) -> Self {
Self {
#[cfg(feature = "crossbeam-queue")]
#[cfg(feature = "unbounded")]
queue: Arc::new(crossbeam_queue::SegQueue::new()),
#[cfg(not(feature = "unbounded"))]
queue: Arc::new(crossbeam_queue::ArrayQueue::new(C)),
#[cfg(not(feature = "crossbeam-queue"))]
queue: Arc::new(heapless::mpmc::MpMcQueue::<_, C>::new()),
wakeup,
poll_runnable_waker: AtomicWaker::new(),
_marker: PhantomData,
Expand Down Expand Up @@ -321,14 +321,14 @@ where
let wake = self.wakeup.waker();

move |runnable| {
#[cfg(feature = "crossbeam-queue")]
#[cfg(feature = "unbounded")]
{
queue.push(runnable).unwrap();
queue.push(runnable);
}

#[cfg(not(feature = "crossbeam-queue"))]
#[cfg(not(feature = "unbounded"))]
{
queue.enqueue(runnable).unwrap();
queue.push(runnable).unwrap();
}

wake.wake_by_ref();
Expand All @@ -349,19 +349,7 @@ where
/// - `Some(Runnnable)` - the first task scheduled for execution. Calling `Runnable::run` will
/// execute the task. In other words, it will poll its future.
fn try_runnable(&self) -> Option<Runnable> {
let runnable;

#[cfg(feature = "crossbeam-queue")]
{
runnable = self.queue.pop();
}

#[cfg(not(feature = "crossbeam-queue"))]
{
runnable = self.queue.dequeue();
}

runnable
self.queue.pop()
}

/// Polls the first task scheduled for execution by the executor.
Expand Down

0 comments on commit 705cdac

Please sign in to comment.