Skip to content

Commit 0c76968

Browse files
committed
High-level module reorganization
- Renames "list.rs" and the "list" folder to "no_std" - Uses `no_std` as the `sys` module in preparation for the `std` module - Inlines `sync.rs` into `lib.rs`
1 parent 5c83b86 commit 0c76968

File tree

5 files changed

+110
-93
lines changed

5 files changed

+110
-93
lines changed

src/lib.rs

Lines changed: 107 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ extern crate alloc;
6868
#[cfg(feature = "std")]
6969
extern crate std;
7070

71-
mod list;
72-
mod sync;
71+
#[path = "no_std.rs"]
72+
mod sys;
7373

7474
use alloc::sync::Arc;
7575

@@ -88,83 +88,6 @@ use std::panic::{RefUnwindSafe, UnwindSafe};
8888
#[cfg(feature = "std")]
8989
use std::time::{Duration, Instant};
9090

91-
#[cfg(feature = "std")]
92-
use parking::Unparker;
93-
94-
/// An asynchronous waker or thread unparker that can be used to notify a task or thread.
95-
#[derive(Debug)]
96-
enum Task {
97-
/// A waker that can be used to notify a task.
98-
Waker(Waker),
99-
100-
/// An unparker that can be used to notify a thread.
101-
#[cfg(feature = "std")]
102-
Thread(Unparker),
103-
}
104-
105-
impl Task {
106-
fn as_task_ref(&self) -> TaskRef<'_> {
107-
match self {
108-
Self::Waker(waker) => TaskRef::Waker(waker),
109-
#[cfg(feature = "std")]
110-
Self::Thread(unparker) => TaskRef::Unparker(unparker),
111-
}
112-
}
113-
114-
/// Notifies the task or thread.
115-
fn wake(self) {
116-
match self {
117-
Task::Waker(waker) => waker.wake(),
118-
#[cfg(feature = "std")]
119-
Task::Thread(unparker) => {
120-
unparker.unpark();
121-
}
122-
}
123-
}
124-
}
125-
126-
impl PartialEq for Task {
127-
fn eq(&self, other: &Self) -> bool {
128-
self.as_task_ref().will_wake(other.as_task_ref())
129-
}
130-
}
131-
132-
/// A reference to a task.
133-
#[derive(Debug, Clone, Copy)]
134-
enum TaskRef<'a> {
135-
/// A waker that wakes up a future.
136-
Waker(&'a Waker),
137-
138-
/// An unparker that wakes up a thread.
139-
#[cfg(feature = "std")]
140-
Unparker(&'a parking::Unparker),
141-
}
142-
143-
impl TaskRef<'_> {
144-
/// Tells if this task will wake up the other task.
145-
fn will_wake(self, other: Self) -> bool {
146-
match (self, other) {
147-
(Self::Waker(a), Self::Waker(b)) => a.will_wake(b),
148-
#[cfg(feature = "std")]
149-
(Self::Unparker(_), Self::Unparker(_)) => {
150-
// TODO: Use unreleased will_unpark API.
151-
false
152-
}
153-
#[cfg(feature = "std")]
154-
_ => false,
155-
}
156-
}
157-
158-
/// Converts this task reference to a task by cloning.
159-
fn into_task(self) -> Task {
160-
match self {
161-
Self::Waker(waker) => Task::Waker(waker.clone()),
162-
#[cfg(feature = "std")]
163-
Self::Unparker(unparker) => Task::Thread(unparker.clone()),
164-
}
165-
}
166-
}
167-
16891
/// Inner state of [`Event`].
16992
struct Inner {
17093
/// The number of notified entries, or `usize::MAX` if all of them have been notified.
@@ -173,15 +96,15 @@ struct Inner {
17396
notified: AtomicUsize,
17497

17598
/// Inner queue of event listeners.
176-
list: list::List,
99+
list: sys::List,
177100
}
178101

179102
impl Inner {
180103
/// Create a new `Inner`.
181104
fn new() -> Self {
182105
Self {
183106
notified: AtomicUsize::new(core::usize::MAX),
184-
list: list::List::new(),
107+
list: sys::List::new(),
185108
}
186109
}
187110
}
@@ -255,7 +178,7 @@ impl Event {
255178
// Register the listener.
256179
let mut listener = EventListener {
257180
inner: unsafe { Arc::clone(&ManuallyDrop::new(Arc::from_raw(inner))) },
258-
state: list::Listener::Discarded,
181+
state: sys::Listener::Discarded,
259182
};
260183

261184
listener.inner.insert(&mut listener.state);
@@ -525,7 +448,7 @@ pub struct EventListener {
525448
inner: Arc<Inner>,
526449

527450
/// The current state of the listener.
528-
state: list::Listener,
451+
state: sys::Listener,
529452
}
530453

531454
#[cfg(feature = "std")]
@@ -784,6 +707,80 @@ impl State {
784707
}
785708
}
786709

710+
/// An asynchronous waker or thread unparker that can be used to notify a task or thread.
711+
#[derive(Debug)]
712+
enum Task {
713+
/// A waker that can be used to notify a task.
714+
Waker(Waker),
715+
716+
/// An unparker that can be used to notify a thread.
717+
#[cfg(feature = "std")]
718+
Unparker(parking::Unparker),
719+
}
720+
721+
impl Task {
722+
fn as_task_ref(&self) -> TaskRef<'_> {
723+
match self {
724+
Self::Waker(waker) => TaskRef::Waker(waker),
725+
#[cfg(feature = "std")]
726+
Self::Unparker(unparker) => TaskRef::Unparker(unparker),
727+
}
728+
}
729+
730+
/// Notifies the task or thread.
731+
fn wake(self) {
732+
match self {
733+
Task::Waker(waker) => waker.wake(),
734+
#[cfg(feature = "std")]
735+
Task::Unparker(unparker) => {
736+
unparker.unpark();
737+
}
738+
}
739+
}
740+
}
741+
742+
impl PartialEq for Task {
743+
fn eq(&self, other: &Self) -> bool {
744+
self.as_task_ref().will_wake(other.as_task_ref())
745+
}
746+
}
747+
748+
/// A reference to a task.
749+
#[derive(Debug, Clone, Copy)]
750+
enum TaskRef<'a> {
751+
/// A waker that wakes up a future.
752+
Waker(&'a Waker),
753+
754+
/// An unparker that wakes up a thread.
755+
#[cfg(feature = "std")]
756+
Unparker(&'a parking::Unparker),
757+
}
758+
759+
impl TaskRef<'_> {
760+
/// Tells if this task will wake up the other task.
761+
fn will_wake(self, other: Self) -> bool {
762+
match (self, other) {
763+
(Self::Waker(a), Self::Waker(b)) => a.will_wake(b),
764+
#[cfg(feature = "std")]
765+
(Self::Unparker(_), Self::Unparker(_)) => {
766+
// TODO: Use unreleased will_unpark API.
767+
false
768+
}
769+
#[cfg(feature = "std")]
770+
_ => false,
771+
}
772+
}
773+
774+
/// Converts this task reference to a task by cloning.
775+
fn into_task(self) -> Task {
776+
match self {
777+
Self::Waker(waker) => Task::Waker(waker.clone()),
778+
#[cfg(feature = "std")]
779+
Self::Unparker(unparker) => Task::Unparker(unparker.clone()),
780+
}
781+
}
782+
}
783+
787784
/// Equivalent to `atomic::fence(Ordering::SeqCst)`, but in some cases faster.
788785
#[inline]
789786
fn full_fence() {
@@ -811,3 +808,30 @@ fn full_fence() {
811808
atomic::fence(Ordering::SeqCst);
812809
}
813810
}
811+
812+
/// Synchronization primitive implementation.
813+
mod sync {
814+
pub(super) use alloc::sync::Arc;
815+
pub(super) use core::cell;
816+
pub(super) use core::sync::atomic;
817+
818+
pub(super) trait WithMut {
819+
type Output;
820+
821+
fn with_mut<F, R>(&mut self, f: F) -> R
822+
where
823+
F: FnOnce(&mut Self::Output) -> R;
824+
}
825+
826+
impl<T> WithMut for atomic::AtomicPtr<T> {
827+
type Output = *mut T;
828+
829+
#[inline]
830+
fn with_mut<F, R>(&mut self, f: F) -> R
831+
where
832+
F: FnOnce(&mut Self::Output) -> R,
833+
{
834+
f(self.get_mut())
835+
}
836+
}
837+
}

src/list.rs renamed to src/no_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! The inner list of listeners.
22
3-
#[path = "list/node.rs"]
3+
#[path = "no_std/node.rs"]
44
mod node;
55

6-
#[path = "list/queue.rs"]
6+
#[path = "no_std/queue.rs"]
77
mod queue;
88

99
use node::{Node, TaskWaiting};

src/list/node.rs renamed to src/no_std/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The node that makes up queues.
22
3-
use crate::list::{Listener, ListenerSlab};
3+
use super::{Listener, ListenerSlab};
44
use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
55
use crate::sync::Arc;
66
use crate::{State, Task};
File renamed without changes.

src/sync.rs

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)