Skip to content

Commit 70e2710

Browse files
committed
zephyr: sync: Add Mutex::new and Condvar::new
Add dynamic allocation support to the sync Mutex and Condvar. When allocation is available, and userspace is not used, these can be used pretty much the same as those from std. Signed-off-by: David Brown <[email protected]>
1 parent 150ec35 commit 70e2710

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

zephyr/src/sync.rs

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ impl<T> Mutex<T> {
119119
pub const fn new_from(t: T, raw_mutex: sys::Mutex) -> Mutex<T> {
120120
Mutex { inner: raw_mutex, data: UnsafeCell::new(t) }
121121
}
122+
123+
/// Construct a new Mutex, dynamically allocating the underlying sys Mutex.
124+
#[cfg(CONFIG_RUST_ALLOC)]
125+
pub fn new(t: T) -> Mutex<T> {
126+
Mutex::new_from(t, sys::Mutex::new().unwrap())
127+
}
122128
}
123129

124130
impl<T: ?Sized> Mutex<T> {
@@ -216,6 +222,12 @@ impl Condvar {
216222
Condvar { inner: raw_condvar }
217223
}
218224

225+
/// Construct a new Condvar, dynamically allocating the underlying Zephyr `k_condvar`.
226+
#[cfg(CONFIG_RUST_ALLOC)]
227+
pub fn new() -> Condvar {
228+
Condvar::new_from(sys::Condvar::new().unwrap())
229+
}
230+
219231
/// Blocks the current thread until this conditional variable receives a notification.
220232
///
221233
/// This function will automatically unlock the mutex specified (represented by `guard`) and

0 commit comments

Comments
 (0)