From b3b33a3f6d5584aee6fa93193a8642fdb3dbe18c Mon Sep 17 00:00:00 2001 From: David Brown Date: Mon, 21 Oct 2024 08:31:12 -0600 Subject: [PATCH] zephyr: sys: sync: Remove Clone from Mutex and Semaphore 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 --- samples/philosophers/src/semsync.rs | 8 +++----- zephyr/src/sys/sync/mutex.rs | 1 - zephyr/src/sys/sync/semaphore.rs | 1 - 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/samples/philosophers/src/semsync.rs b/samples/philosophers/src/semsync.rs index 03bf8ce..889ab41 100644 --- a/samples/philosophers/src/semsync.rs +++ b/samples/philosophers/src/semsync.rs @@ -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}; @@ -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; NUM_PHIL], } impl ForkSync for SemSync { @@ -39,7 +37,7 @@ impl ForkSync for SemSync { pub fn semaphore_sync() -> Vec> { 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(|_| { diff --git a/zephyr/src/sys/sync/mutex.rs b/zephyr/src/sys/sync/mutex.rs index 9b63e58..41bfe46 100644 --- a/zephyr/src/sys/sync/mutex.rs +++ b/zephyr/src/sys/sync/mutex.rs @@ -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, diff --git a/zephyr/src/sys/sync/semaphore.rs b/zephyr/src/sys/sync/semaphore.rs index 3c69009..edfafae 100644 --- a/zephyr/src/sys/sync/semaphore.rs +++ b/zephyr/src/sys/sync/semaphore.rs @@ -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,