Skip to content
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

Introduce sel4-one-ref-cell crate #20

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-immediate-sync-once-cell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct ImmediateSyncOnceCell<T> {
inner: SyncUnsafeCell<Option<T>>,
}

impl<T> Default for ImmediateSyncOnceCell<T> {
fn default() -> Self {
Self::new()
}
}

impl<T> ImmediateSyncOnceCell<T> {
pub const fn new() -> Self {
Self {
Expand Down
8 changes: 4 additions & 4 deletions crates/sel4-immutable-cell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub struct ImmutableCell<T: ?Sized> {
}

impl<T: Default> Default for ImmutableCell<T> {
fn default() -> ImmutableCell<T> {
ImmutableCell::new(Default::default())
fn default() -> Self {
Self::new(Default::default())
}
}

impl<T> From<T> for ImmutableCell<T> {
fn from(t: T) -> ImmutableCell<T> {
ImmutableCell::new(t)
fn from(t: T) -> Self {
Self::new(t)
}
}

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";
}