-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nick Spinale <[email protected]>
- Loading branch information
Showing
8 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,6 @@ | ||
[package] | ||
name = "sel4-one-ref-cell" | ||
version = "0.1.0" | ||
authors = ["Nick Spinale <[email protected]>"] | ||
edition = "2021" | ||
license = "BSD-2-Clause" |
This file contains 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,45 @@ | ||
#![no_std] | ||
#![feature(sync_unsafe_cell)] | ||
|
||
use core::cell::SyncUnsafeCell; | ||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
pub struct OneRefCell<T> { | ||
taken: AtomicBool, | ||
value: SyncUnsafeCell<T>, | ||
} | ||
|
||
impl<T: Default> Default for OneRefCell<T> { | ||
fn default() -> Self { | ||
Self::new(Default::default()) | ||
} | ||
} | ||
|
||
impl<T> From<T> for OneRefCell<T> { | ||
fn from(t: T) -> Self { | ||
Self::new(t) | ||
} | ||
} | ||
|
||
impl<T> OneRefCell<T> { | ||
pub const fn new(value: T) -> Self { | ||
Self { | ||
taken: AtomicBool::new(false), | ||
value: SyncUnsafeCell::new(value), | ||
} | ||
} | ||
|
||
pub fn take(&self) -> Result<&mut T, Error> { | ||
if self.taken.swap(true, Ordering::SeqCst) { | ||
Err(Error::AlreadyTaken) | ||
} else { | ||
let ptr = self.value.get(); | ||
Ok(unsafe { ptr.as_mut() }.unwrap()) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] | ||
pub enum Error { | ||
AlreadyTaken, | ||
} |
This file contains 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
5 changes: 5 additions & 0 deletions
5
hacking/cargo-manifest-sources/crates/sel4-one-ref-cell/crate.nix
This file contains 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,5 @@ | ||
{ mk }: | ||
|
||
mk { | ||
package.name = "sel4-one-ref-cell"; | ||
} |