Skip to content

Commit

Permalink
zephyr: sys: sync: Remove Clone from Mutex and Semaphore
Browse files Browse the repository at this point in the history
Although these, in their current state, are safe to Clone, having these
semantics will make it difficult for us to later add these types that
are allocated from a pool.

Uses that currently expect to clone can generally wrap these in an Arc,
to allow for the sharing.

Signed-off-by: David Brown <[email protected]>
  • Loading branch information
d3zd3z committed Oct 21, 2024
1 parent 3f00639 commit b3b33a3
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 7 deletions.
8 changes: 3 additions & 5 deletions samples/philosophers/src/semsync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use alloc::vec::Vec;
use alloc::boxed::Box;

use zephyr::{
kobj_define,
sync::Arc,
time::Forever,
kobj_define, sync::Arc, sys::sync::Semaphore, time::Forever
};

use crate::{ForkSync, NUM_PHIL};
Expand All @@ -22,7 +20,7 @@ use crate::{ForkSync, NUM_PHIL};
pub struct SemSync {
/// The forks for this philosopher. This is a big excessive, as we really don't need all of
/// them, but the ForSync code uses the index here.
forks: [zephyr::sys::sync::Semaphore; NUM_PHIL],
forks: [Arc<Semaphore>; NUM_PHIL],
}

impl ForkSync for SemSync {
Expand All @@ -39,7 +37,7 @@ impl ForkSync for SemSync {
pub fn semaphore_sync() -> Vec<Arc<dyn ForkSync>> {
let forks = SEMS.each_ref().map(|m| {
// Each fork starts as taken.
m.init_once((1, 1)).unwrap()
Arc::new(m.init_once((1, 1)).unwrap())
});

let syncers = (0..NUM_PHIL).map(|_| {
Expand Down
1 change: 0 additions & 1 deletion zephyr/src/sys/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ use crate::sys::K_FOREVER;
/// type.
///
/// [`sync::Mutex`]: http://example.com/TODO
#[derive(Clone)]
pub struct Mutex {
/// The raw Zephyr mutex.
item: *mut k_mutex,
Expand Down
1 change: 0 additions & 1 deletion zephyr/src/sys/sync/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use crate::{
pub use crate::raw::K_SEM_MAX_LIMIT;

/// A zephyr `k_sem` usable from safe Rust code.
#[derive(Clone)]
pub struct Semaphore {
/// The raw Zephyr `k_sem`.
item: *mut k_sem,
Expand Down

0 comments on commit b3b33a3

Please sign in to comment.