Skip to content

Commit

Permalink
crates/sel4-one-ref-cell: Introduce
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Spinale <[email protected]>
  • Loading branch information
nspin committed Oct 2, 2023
1 parent a282654 commit eaa2d63
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ members = [
"crates/sel4-microkit/message",
"crates/sel4-microkit/message/types",
"crates/sel4-newlib",
"crates/sel4-one-ref-cell",
"crates/sel4-panicking",
"crates/sel4-panicking/env",
"crates/sel4-platform-info",
Expand Down
1 change: 1 addition & 0 deletions crates/private/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sel4-logging = { path = "../../sel4-logging" }
sel4-microkit = { path = "../../sel4-microkit", features = ["full"], optional = true }
sel4-microkit-message = { path = "../../sel4-microkit/message", optional = true }
sel4-microkit-message-types = { path = "../../sel4-microkit/message/types", optional = true }
sel4-one-ref-cell = { path = "../../sel4-one-ref-cell" }
sel4-root-task = { path = "../../sel4-root-task", features = ["full"], optional = true }
sel4-shared-ring-buffer = { path = "../../sel4-shared-ring-buffer" }
sel4-shared-ring-buffer-block-io = { path = "../../sel4-shared-ring-buffer/block-io" }
Expand Down
2 changes: 2 additions & 0 deletions crates/private/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ definitely! {
sel4
sel4_async_block_io
sel4_async_block_io_cpiofs
sel4_async_block_io_fat
sel4_async_network
sel4_async_request_statuses
sel4_async_single_threaded_executor
Expand All @@ -86,6 +87,7 @@ definitely! {
sel4_immediate_sync_once_cell
sel4_immutable_cell
sel4_logging
sel4_one_ref_cell
sel4_shared_ring_buffer
sel4_shared_ring_buffer_block_io
sel4_shared_ring_buffer_block_io_types
Expand Down
6 changes: 6 additions & 0 deletions crates/sel4-one-ref-cell/Cargo.toml
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"
45 changes: 45 additions & 0 deletions crates/sel4-one-ref-cell/src/lib.rs
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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mk {
sel4-immediate-sync-once-cell
sel4-immutable-cell
sel4-logging
sel4-one-ref-cell
sel4-shared-ring-buffer
sel4-shared-ring-buffer-block-io
sel4-shared-ring-buffer-block-io-types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ mk }:

mk {
package.name = "sel4-one-ref-cell";
}

0 comments on commit eaa2d63

Please sign in to comment.