-
Notifications
You must be signed in to change notification settings - Fork 35
Implement an algorithm to make this crate no_std, take 3 #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8ee0e9
Create a new no_std implementation
notgull 3582d8f
Fix the use-after-free issue
notgull 6018104
Fix MSRV
notgull d28cb57
Add no_std build to the CI
notgull 590ce24
Fmt
notgull 98a28ea
Fix no-dev-deps typo
notgull a0cb3ff
Code review
notgull 7707b73
Use concurrent-queue instead of home-grown queue
notgull 9c31ae6
refactor: Move drop glue of AddListener into separate struct
fogti 38367af
Re-enable MIRI
notgull d247ef9
Fix MSRV build + Add more tests
notgull 6cc89e2
Code review
notgull 62dd915
Migrate away from concurrent-queue
notgull ce4b7c1
Fix the MIRI deadlock
notgull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| //! The inner mechanism powering the `Event` type. | ||
|
|
||
| use crate::list::{Entry, List}; | ||
| use crate::node::Node; | ||
| use crate::queue::Queue; | ||
| use crate::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; | ||
| use crate::sync::cell::UnsafeCell; | ||
| use crate::Task; | ||
|
|
||
| use alloc::vec; | ||
| use alloc::vec::Vec; | ||
|
|
||
| use core::ops; | ||
| use core::ptr::NonNull; | ||
|
|
||
| /// Inner state of [`Event`]. | ||
| pub(crate) struct Inner { | ||
| /// The number of notified entries, or `usize::MAX` if all of them have been notified. | ||
| /// | ||
| /// If there are no entries, this value is set to `usize::MAX`. | ||
| pub(crate) notified: AtomicUsize, | ||
|
|
||
| /// A linked list holding registered listeners. | ||
| list: Mutex<List>, | ||
|
|
||
| /// Queue of nodes waiting to be processed. | ||
| queue: Queue, | ||
|
|
||
| /// A single cached list entry to avoid allocations on the fast path of the insertion. | ||
| cache: UnsafeCell<Entry>, | ||
| } | ||
|
|
||
| impl Inner { | ||
| /// Create a new `Inner`. | ||
| pub(crate) fn new() -> Self { | ||
| Self { | ||
| notified: AtomicUsize::new(core::usize::MAX), | ||
| list: Mutex::new(List::new()), | ||
| queue: Queue::new(), | ||
| cache: UnsafeCell::new(Entry::new()), | ||
| } | ||
| } | ||
|
|
||
| /// Locks the list. | ||
| pub(crate) fn lock(&self) -> Option<ListGuard<'_>> { | ||
| self.list.try_lock().map(|guard| ListGuard { | ||
| inner: self, | ||
| guard: Some(guard), | ||
| }) | ||
| } | ||
|
|
||
| /// Push a pending operation to the queue. | ||
| #[cold] | ||
| pub(crate) fn push(&self, node: Node) { | ||
| self.queue.push(node); | ||
|
|
||
| // Acquire and drop the lock to make sure that the queue is flushed. | ||
| let _guard = self.lock(); | ||
| } | ||
|
|
||
| /// Returns the pointer to the single cached list entry. | ||
| #[inline(always)] | ||
| pub(crate) fn cache_ptr(&self) -> NonNull<Entry> { | ||
| unsafe { NonNull::new_unchecked(self.cache.get()) } | ||
| } | ||
| } | ||
|
|
||
| /// The guard returned by [`Inner::lock`]. | ||
| pub(crate) struct ListGuard<'a> { | ||
| /// Reference to the inner state. | ||
| inner: &'a Inner, | ||
|
|
||
| /// The locked list. | ||
| guard: Option<MutexGuard<'a, List>>, | ||
| } | ||
|
|
||
| impl ListGuard<'_> { | ||
| #[cold] | ||
| fn process_nodes_slow( | ||
| &mut self, | ||
| start_node: Node, | ||
| tasks: &mut Vec<Task>, | ||
| guard: &mut MutexGuard<'_, List>, | ||
| ) { | ||
| // Process the start node. | ||
| tasks.extend(start_node.apply(guard, self.inner)); | ||
|
|
||
| // Process all remaining nodes. | ||
| while let Some(node) = self.inner.queue.pop() { | ||
| tasks.extend(node.apply(guard, self.inner)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ops::Deref for ListGuard<'_> { | ||
| type Target = List; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.guard.as_ref().unwrap() | ||
| } | ||
| } | ||
|
|
||
| impl ops::DerefMut for ListGuard<'_> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| self.guard.as_mut().unwrap() | ||
| } | ||
| } | ||
|
|
||
| impl Drop for ListGuard<'_> { | ||
| fn drop(&mut self) { | ||
| let Self { inner, guard } = self; | ||
| let mut list = guard.take().unwrap(); | ||
|
|
||
| // Tasks to wakeup after releasing the lock. | ||
| let mut tasks = vec![]; | ||
|
|
||
| // Process every node left in the queue. | ||
| if let Some(start_node) = inner.queue.pop() { | ||
| self.process_nodes_slow(start_node, &mut tasks, &mut list); | ||
| } | ||
|
|
||
| // Update the atomic `notified` counter. | ||
| let notified = if list.notified < list.len { | ||
| list.notified | ||
| } else { | ||
| core::usize::MAX | ||
| }; | ||
|
|
||
| self.inner.notified.store(notified, Ordering::Release); | ||
|
|
||
| // Drop the actual lock. | ||
| drop(list); | ||
|
|
||
| // Wakeup all tasks. | ||
| for task in tasks { | ||
| task.wake(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A simple mutex type that optimistically assumes that the lock is uncontended. | ||
| struct Mutex<T> { | ||
| /// The inner value. | ||
| value: UnsafeCell<T>, | ||
|
|
||
| /// Whether the mutex is locked. | ||
| locked: AtomicBool, | ||
| } | ||
|
|
||
| impl<T> Mutex<T> { | ||
| /// Create a new mutex. | ||
| pub(crate) fn new(value: T) -> Self { | ||
| Self { | ||
| value: UnsafeCell::new(value), | ||
| locked: AtomicBool::new(false), | ||
| } | ||
| } | ||
|
|
||
| /// Lock the mutex. | ||
| pub(crate) fn try_lock(&self) -> Option<MutexGuard<'_, T>> { | ||
| // Try to lock the mutex. | ||
| if self | ||
| .locked | ||
| .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) | ||
| .is_ok() | ||
| { | ||
| // We have successfully locked the mutex. | ||
| Some(MutexGuard { mutex: self }) | ||
| } else { | ||
| self.try_lock_slow() | ||
| } | ||
| } | ||
|
|
||
| #[cold] | ||
| fn try_lock_slow(&self) -> Option<MutexGuard<'_, T>> { | ||
| // Assume that the contention is short-term. | ||
| // Spin for a while to see if the mutex becomes unlocked. | ||
| let mut spins = 100; | ||
|
|
||
| loop { | ||
| if self | ||
| .locked | ||
| .compare_exchange_weak(false, true, Ordering::Acquire, Ordering::Relaxed) | ||
| .is_ok() | ||
| { | ||
| // We have successfully locked the mutex. | ||
| return Some(MutexGuard { mutex: self }); | ||
| } | ||
|
|
||
| // Use atomic loads instead of compare-exchange. | ||
| while self.locked.load(Ordering::Relaxed) { | ||
| if spins <= 0 { | ||
| return None; | ||
| } | ||
|
|
||
| spins -= 1; | ||
notgull marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct MutexGuard<'a, T> { | ||
| mutex: &'a Mutex<T>, | ||
| } | ||
|
|
||
| impl<'a, T> Drop for MutexGuard<'a, T> { | ||
| fn drop(&mut self) { | ||
| self.mutex.locked.store(false, Ordering::Release); | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T> ops::Deref for MutexGuard<'a, T> { | ||
| type Target = T; | ||
|
|
||
| fn deref(&self) -> &T { | ||
| unsafe { &*self.mutex.value.get() } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T> ops::DerefMut for MutexGuard<'a, T> { | ||
| fn deref_mut(&mut self) -> &mut T { | ||
| unsafe { &mut *self.mutex.value.get() } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.