Skip to content

Commit

Permalink
sel4-externally-shared: Refactor
Browse files Browse the repository at this point in the history
Including splitting out sel4-atomic-ptr into its own crate

Signed-off-by: Nick Spinale <nick@nickspinale.com>
  • Loading branch information
nspin committed Jan 28, 2024
1 parent b6e3299 commit 50ebd98
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 118 deletions.
11 changes: 10 additions & 1 deletion 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 @@ -69,6 +69,7 @@ members = [
"crates/sel4-async/single-threaded-executor",
"crates/sel4-async/time",
"crates/sel4-async/unsync",
"crates/sel4-atomic-ptr",
"crates/sel4-backtrace",
"crates/sel4-backtrace/addr2line-context-helper",
"crates/sel4-backtrace/cli",
Expand Down
18 changes: 18 additions & 0 deletions crates/sel4-atomic-ptr/Cargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: MIT OR Apache-2.0
#

{ mk, mkDefaultFrontmatterWithReuseArgs, defaultReuseFrontmatterArgs, versions }:

mk rec {
nix.frontmatter = mkDefaultFrontmatterWithReuseArgs (defaultReuseFrontmatterArgs // {
licenseID = package.license;
});
package.name = "sel4-atomic-ptr";
package.license = "MIT OR Apache-2.0";
dependencies = {
inherit (versions) cfg-if;
};
}
20 changes: 20 additions & 0 deletions crates/sel4-atomic-ptr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: MIT OR Apache-2.0
#
#
# This file is generated from './Cargo.nix'. You can edit this file directly
# if you are not using this project's Cargo manifest management tools.
# See 'hacking/cargo-manifest-management/README.md' for more information.
#

[package]
name = "sel4-atomic-ptr"
version = "0.1.0"
authors = ["Nick Spinale <nick.spinale@coliasgroup.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[dependencies]
cfg-if = "1.0.0"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,43 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//

#![no_std]
#![feature(core_intrinsics)]
#![allow(internal_features)]

use core::fmt;
use core::marker::PhantomData;
use core::mem;
use core::ptr::NonNull;
use core::sync::atomic;

use volatile::access::{ReadOnly, ReadWrite, WriteOnly};

mod generic;
mod ops;
mod ordering;

use ordering::OrderingExhaustive;

#[repr(transparent)]
pub struct AtomicPtr<'a, T, A = ReadWrite> {
pub struct AtomicPtr<'a, T> {
pointer: NonNull<T>,
reference: PhantomData<&'a T>,
access: PhantomData<A>,
}

impl<'a, T, A> Copy for AtomicPtr<'a, T, A> {}
impl<'a, T> Copy for AtomicPtr<'a, T> {}

impl<T, A> Clone for AtomicPtr<'_, T, A> {
impl<T> Clone for AtomicPtr<'_, T> {
fn clone(&self) -> Self {
*self
}
}

impl<T, A> fmt::Debug for AtomicPtr<'_, T, A> {
impl<T> fmt::Debug for AtomicPtr<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AtomicPtr")
.field("pointer", &self.pointer)
.field("access", &self.access)
.finish()
f.debug_tuple("AtomicPtr").field(&self.pointer).finish()
}
}

impl<'a, T, A> AtomicPtr<'a, T, A> {
impl<'a, T> AtomicPtr<'a, T> {
/// # Safety
///
/// Necessary but not sufficient:
Expand All @@ -52,21 +50,12 @@ impl<'a, T, A> AtomicPtr<'a, T, A> {
AtomicPtr {
pointer,
reference: PhantomData,
access: PhantomData,
}
}

pub fn as_raw_ptr(self) -> NonNull<T> {
self.pointer
}

pub fn read_only(self) -> AtomicPtr<'a, T, ReadOnly> {
unsafe { AtomicPtr::new(self.pointer) }
}

pub fn write_only(self) -> AtomicPtr<'a, T, WriteOnly> {
unsafe { AtomicPtr::new(self.pointer) }
}
}

#[allow(clippy::missing_safety_doc)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

use core::sync::atomic::Ordering;

use volatile::access::{Readable, Writable};

use super::{generic, Atomic, AtomicPtr};

impl<'a, T, A> AtomicPtr<'a, T, A> {
impl<'a, T> AtomicPtr<'a, T> {
fn as_mut_ptr(self) -> *mut T {
self.pointer.as_ptr()
}
Expand All @@ -20,15 +18,13 @@ impl<'a, T, A> AtomicPtr<'a, T, A> {
}
}

impl<'a, T: Atomic, A: Readable> AtomicPtr<'a, T, A> {
impl<'a, T: Atomic> AtomicPtr<'a, T> {
#[inline]
pub fn load(self, order: Ordering) -> T {
// SAFETY: data races are prevented by atomic intrinsics.
unsafe { generic::atomic_load(self.as_const_ptr(), order.into()) }
}
}

impl<'a, T: Atomic, A: Readable + Writable> AtomicPtr<'a, T, A> {
#[inline]
pub fn store(self, val: T, order: Ordering) {
// SAFETY: data races are prevented by atomic intrinsics.
Expand Down
File renamed without changes.
11 changes: 4 additions & 7 deletions crates/sel4-externally-shared/Cargo.nix
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: MIT OR Apache-2.0
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk, mkDefaultFrontmatterWithReuseArgs, defaultReuseFrontmatterArgs, localCrates, versions, volatileSource }:
{ mk, localCrates, versions, volatileSource }:

mk rec {
nix.frontmatter = mkDefaultFrontmatterWithReuseArgs (defaultReuseFrontmatterArgs // {
licenseID = package.license;
});
package.name = "sel4-externally-shared";
package.license = "MIT OR Apache-2.0";
dependencies = {
inherit (versions) zerocopy;
inherit (versions) cfg-if zerocopy;
volatile = volatileSource;
inherit (localCrates)
sel4-atomic-ptr
# volatile
;
};
Expand Down
8 changes: 5 additions & 3 deletions crates/sel4-externally-shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: MIT OR Apache-2.0
# SPDX-License-Identifier: BSD-2-Clause
#
#
# This file is generated from './Cargo.nix'. You can edit this file directly
Expand All @@ -14,15 +14,17 @@ name = "sel4-externally-shared"
version = "0.1.0"
authors = ["Nick Spinale <nick.spinale@coliasgroup.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
license = "BSD-2-Clause"

[features]
unstable = ["volatile/unstable"]
very_unstable = ["volatile/very_unstable"]

[dependencies]
cfg-if = "1.0.0"
sel4-atomic-ptr = { path = "../sel4-atomic-ptr" }
zerocopy = "0.7.32"

[dependencies.volatile]
git = "https://github.com/coliasgroup/volatile.git"
tag = "keep/8aee5539716d3d38247f46eddd42b382"
tag = "keep/aa7512906e9b76066ed928eb6986b0f9"
19 changes: 8 additions & 11 deletions crates/sel4-externally-shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: BSD-2-Clause
//

#![no_std]
#![feature(core_intrinsics)]
#![allow(internal_features)]
#![cfg_attr(feature = "unstable", feature(core_intrinsics))]
#![cfg_attr(feature = "unstable", allow(internal_features))]

use core::ptr::NonNull;

use volatile::access::{Access, ReadWrite};

pub use volatile::{access, map_field, VolatilePtr, VolatileRef};

mod atomics;

pub mod ops;

pub use atomics::{Atomic, AtomicPtr};
pub use sel4_atomic_ptr::{Atomic, AtomicPtr};

// TODO
pub type ExternallySharedOps = ops::ZerocopyOps<ops::NormalOps>;
// pub type ExternallySharedOps = ops::ZerocopyOps<ops::VolatileOps>;
// pub type ExternallySharedOps = ops::ZerocopyOps<ops::BytewiseOps<ops::UnorderedAtomicOps>>;

pub type ExternallySharedRef<'a, T, A = ReadWrite> = VolatileRef<'a, T, A, ExternallySharedOps>;

pub type ExternallySharedPtr<'a, T, A = ReadWrite> = VolatilePtr<'a, T, A, ExternallySharedOps>;

pub trait ExternallySharedRefExt<'a, T: ?Sized, A: Access> {
Expand All @@ -43,14 +40,14 @@ impl<'a, T: ?Sized, A: Access> ExternallySharedRefExt<'a, T, A> for ExternallySh
}
}

pub trait ExternallySharedPtrExt<'a, T: ?Sized, A> {
fn atomic(self) -> AtomicPtr<'a, T, A>
pub trait ExternallySharedPtrExt<'a, T: ?Sized> {
fn atomic(self) -> AtomicPtr<'a, T>
where
T: Atomic;
}

impl<'a, T: ?Sized, A> ExternallySharedPtrExt<'a, T, A> for ExternallySharedPtr<'a, T, A> {
fn atomic(self) -> AtomicPtr<'a, T, A>
impl<'a, T: ?Sized> ExternallySharedPtrExt<'a, T> for ExternallySharedPtr<'a, T, ReadWrite> {
fn atomic(self) -> AtomicPtr<'a, T>
where
T: Atomic,
{
Expand Down
18 changes: 7 additions & 11 deletions crates/sel4-externally-shared/src/ops/bytewise_ops.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: BSD-2-Clause
//

#![cfg_attr(not(feature = "unstable"), allow(unused_imports))]

use core::marker::PhantomData;
use core::mem;

use volatile::ops::{Ops, UnitaryOps};
use volatile::ops::{BulkOps, Ops, UnitaryOps};
use zerocopy::{AsBytes, FromBytes};

#[cfg(feature = "unstable")]
use volatile::ops::BulkOps;

#[derive(Debug, Default, Copy, Clone)]
pub struct BytewiseOps<O>(O);
#[derive(Default, Copy, Clone)]
pub struct BytewiseOps<O> {
_phantom: PhantomData<O>,
}

impl<O: Ops> Ops for BytewiseOps<O> {}

#[cfg(feature = "unstable")]
impl<O: BulkOps<u8>, T: FromBytes + AsBytes> UnitaryOps<T> for BytewiseOps<O> {
unsafe fn read(src: *const T) -> T {
let mut val = T::new_zeroed();
Expand All @@ -34,7 +31,6 @@ impl<O: BulkOps<u8>, T: FromBytes + AsBytes> UnitaryOps<T> for BytewiseOps<O> {
}
}

#[cfg(feature = "unstable")]
impl<O: BulkOps<u8>, T: FromBytes + AsBytes> BulkOps<T> for BytewiseOps<O> {
unsafe fn memmove(dst: *mut T, src: *const T, count: usize) {
unsafe { O::memmove(dst.cast(), src.cast(), count * mem::size_of::<T>()) }
Expand Down
2 changes: 1 addition & 1 deletion crates/sel4-externally-shared/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: BSD-2-Clause
//

pub use volatile::ops::*;
Expand Down
12 changes: 4 additions & 8 deletions crates/sel4-externally-shared/src/ops/normal_ops.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-License-Identifier: BSD-2-Clause
//

use core::ptr;

use volatile::ops::{Ops, UnitaryOps};
use volatile::ops::{BulkOps, Ops, UnitaryOps};

#[cfg(feature = "unstable")]
use volatile::ops::BulkOps;

#[derive(Debug, Default, Copy, Clone)]
pub struct NormalOps;
#[derive(Default, Copy, Clone)]
pub struct NormalOps(());

impl Ops for NormalOps {}

Expand All @@ -26,7 +23,6 @@ impl<T> UnitaryOps<T> for NormalOps {
}
}

#[cfg(feature = "unstable")]
impl<T> BulkOps<T> for NormalOps {
unsafe fn memmove(dst: *mut T, src: *const T, count: usize) {
unsafe { ptr::copy(src, dst, count) }
Expand Down
Loading

0 comments on commit 50ebd98

Please sign in to comment.