-
Notifications
You must be signed in to change notification settings - Fork 96
The Pull Request for Issue 101 for no_std #102
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
Open
Lol3rrr
wants to merge
13
commits into
jonhoo:main
Choose a base branch
from
Lol3rrr:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
16de6fb
Writer now has custom yield
f5dde6b
Improved the Documentation for the new methods
328667b
Added "std" feature and replaced most uses of "std"
1b8148e
Small changes from Review
7200e93
Initial HandleList implementation
2725aa3
Initial switch to HandleList
f881ff1
Fixed some more TODOs
9cf1237
Updated tests for no_std
72ae2c6
Added a github workflow to check for no_std compilation
0e0b406
Updated the HandleList to now free entries on Drop
ba70de8
Fixed most of the simpler Problems that had straight forward solutions
f2952d5
Add better way to get length of epochs snapshot
490377a
First Reuse impl
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| on: | ||
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| name: no-std | ||
| jobs: | ||
| nostd: | ||
| runs-on: ubuntu-latest | ||
| name: ${{ matrix.target }} | ||
| strategy: | ||
| matrix: | ||
| target: [thumbv7m-none-eabi, aarch64-unknown-none] | ||
| steps: | ||
| - uses: actions-rs/toolchain@v1 | ||
| with: | ||
| profile: minimal | ||
| toolchain: stable | ||
| target: ${{ matrix.target }} | ||
| - uses: actions/checkout@v2 | ||
| - name: cargo check | ||
| uses: actions-rs/cargo@v1 | ||
| with: | ||
| command: check | ||
| args: --target ${{ matrix.target }} --no-default-features |
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,227 @@ | ||
| use core::fmt::{Debug, Formatter}; | ||
|
|
||
| use crate::sync::{Arc, AtomicPtr, AtomicUsize, Ordering}; | ||
| use alloc::boxed::Box; | ||
|
|
||
| // TODO | ||
| // * For now I'm just using Ordering::SeqCst, because I havent really looked into what exactly we | ||
| // need for the Ordering, so this should probably be made more accurate in the Future | ||
|
|
||
| /// A Lock-Free List of Handles | ||
| pub struct HandleList { | ||
| inner: Arc<InnerList>, | ||
| } | ||
|
|
||
| struct InnerList { | ||
| // The Head of the List | ||
| head: AtomicPtr<ListEntry>, | ||
| } | ||
|
|
||
| /// A Snapshot of the HandleList | ||
| /// | ||
| /// Iterating over this Snapshot only yields the Entries that were present when this Snapshot was taken | ||
| pub struct ListSnapshot { | ||
| // The Head-Ptr at the time of creation | ||
| head: *const ListEntry, | ||
|
|
||
| // This entry exists to make sure that we keep the inner List alive and it wont be freed from under us | ||
| _list: Arc<InnerList>, | ||
| } | ||
|
|
||
| /// An Iterator over the Entries in a Snapshot | ||
| pub struct SnapshotIter { | ||
| // A Pointer to the next Entry that will be yielded | ||
| current: *const ListEntry, | ||
| } | ||
|
|
||
| struct ListEntry { | ||
| data: Arc<AtomicUsize>, | ||
| // We can use a normal Ptr here because we never append or remove Entries and only add new Entries | ||
| // by changing the Head, so we never modify this Ptr and therefore dont need an AtomicPtr | ||
| next: *const Self, | ||
| } | ||
|
|
||
| impl HandleList { | ||
| /// Creates a new empty HandleList | ||
| pub fn new() -> Self { | ||
| Self { | ||
| inner: Arc::new(InnerList { | ||
| head: AtomicPtr::new(core::ptr::null_mut()), | ||
| }), | ||
| } | ||
| } | ||
|
|
||
| /// Adds a new Entry to the List and returns the Counter for the Entry | ||
| pub fn new_entry(&self) -> Arc<AtomicUsize> { | ||
| let count = Arc::new(AtomicUsize::new(0)); | ||
|
|
||
| self.add_counter(count.clone()); | ||
| count | ||
| } | ||
| fn add_counter(&self, count: Arc<AtomicUsize>) { | ||
| let n_node = Box::new(ListEntry { | ||
| data: count, | ||
| next: core::ptr::null(), | ||
| }); | ||
| let n_node_ptr = Box::into_raw(n_node); | ||
|
|
||
| let mut current_head = self.inner.head.load(Ordering::SeqCst); | ||
| loop { | ||
| // Safety | ||
| // This is save, because we have not stored the Ptr elsewhere so we have exclusive | ||
| // access. | ||
| // The Ptr is also still valid, because we never free Entries on the List | ||
| unsafe { (*n_node_ptr).next = current_head }; | ||
|
|
||
| // Attempt to add the Entry to the List by setting it as the new Head | ||
| match self.inner.head.compare_exchange( | ||
| current_head, | ||
| n_node_ptr, | ||
| Ordering::SeqCst, | ||
| Ordering::SeqCst, | ||
| ) { | ||
| Ok(_) => return, | ||
| Err(n_head) => { | ||
| // Store the found Head-Ptr to avoid an extra load at the start of every loop | ||
| current_head = n_head; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Creates a new Snapshot of the List at this Point in Time | ||
| pub fn snapshot(&self) -> ListSnapshot { | ||
| ListSnapshot { | ||
| head: self.inner.head.load(Ordering::SeqCst), | ||
| _list: self.inner.clone(), | ||
| } | ||
| } | ||
|
|
||
| /// Inserts the Items of the Iterator, but in reverse order | ||
| #[cfg(test)] | ||
| pub fn extend<I>(&self, iter: I) | ||
| where | ||
| I: IntoIterator<Item = Arc<AtomicUsize>>, | ||
| { | ||
| for item in iter.into_iter() { | ||
| self.add_counter(item); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for HandleList { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
| impl Debug for HandleList { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { | ||
| // TODO | ||
| // Figure out how exactly we want the Debug output to look | ||
| write!(f, "HandleList") | ||
| } | ||
| } | ||
| impl Clone for HandleList { | ||
| fn clone(&self) -> Self { | ||
| Self { | ||
| inner: Arc::clone(&self.inner), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ListSnapshot { | ||
| /// Obtain an iterator over the Entries in this Snapshot | ||
| pub fn iter(&self) -> SnapshotIter { | ||
| SnapshotIter { current: self.head } | ||
| } | ||
| } | ||
|
|
||
| impl Iterator for SnapshotIter { | ||
| // TODO | ||
| // Maybe don't return an owned Value here | ||
| type Item = Arc<AtomicUsize>; | ||
Lol3rrr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| if self.current.is_null() { | ||
| return None; | ||
| } | ||
|
|
||
| // Safety | ||
| // The Ptr is not null, because of the previous if-statement. | ||
| // The Data is also not freed, because we never free Entries on the List. | ||
Lol3rrr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // We also have no one mutating Entries on the List and therefore we can access this without | ||
| // any extra synchronization needed. | ||
|
||
| let entry = unsafe { &*self.current }; | ||
|
|
||
| self.current = entry.next; | ||
|
|
||
| Some(entry.data.clone()) | ||
| } | ||
| } | ||
|
|
||
| impl Drop for InnerList { | ||
| fn drop(&mut self) { | ||
| // We iterate over all the Entries of the List and free every Entry of the List | ||
| let mut current = self.head.load(Ordering::SeqCst); | ||
Lol3rrr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while !current.is_null() { | ||
| // # Safety | ||
| // This is safe, because we only enter the loop body if the Pointer is not null and we | ||
| // also know that the Entry is not yet freed because we only free them once we are dropped | ||
| // and because we are now in Drop, noone before us has freed any Entry on the List | ||
| let current_r = unsafe { &*current }; | ||
|
|
||
| let next = current_r.next as *mut ListEntry; | ||
Lol3rrr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // # Safety | ||
| // This is safe, because of the same garantuees detailed above for `current_r` | ||
Lol3rrr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let entry = unsafe { Box::from_raw(current) }; | ||
| drop(entry); | ||
|
|
||
| current = next; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn create_list() { | ||
| let list = HandleList::new(); | ||
| drop(list); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_snapshot() { | ||
| let list = HandleList::new(); | ||
|
|
||
| let snapshot = list.snapshot(); | ||
|
|
||
| // Assert that the Iterator over the Snapshot is empty | ||
| assert_eq!(0, snapshot.iter().count()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn snapshots_and_entries() { | ||
| let list = HandleList::new(); | ||
|
|
||
| let empty_snapshot = list.snapshot(); | ||
| assert_eq!(0, empty_snapshot.iter().count()); | ||
|
|
||
| let entry = list.new_entry(); | ||
| entry.store(1, Ordering::SeqCst); | ||
|
|
||
| // Make sure that the Snapshot we got before adding a new Entry is still empty | ||
| assert_eq!(0, empty_snapshot.iter().count()); | ||
|
|
||
| let second_snapshot = list.snapshot(); | ||
| assert_eq!(1, second_snapshot.iter().count()); | ||
|
|
||
| let snapshot_entry = second_snapshot.iter().next().unwrap(); | ||
| assert_eq!( | ||
| entry.load(Ordering::SeqCst), | ||
| snapshot_entry.load(Ordering::SeqCst) | ||
| ); | ||
| } | ||
| } | ||
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.