|
| 1 | +//! Base Semaphore |
| 2 | +//! |
| 3 | +//! This is an experiment into a different approach to Zephyr kernel objects. |
| 4 | +//! |
| 5 | +//! Currently, these kernel objects are directed through "Fixed", which is an enum referencing with |
| 6 | +//! a pointer to something static declared, or to a `Pin<Box<UnsafeCell<T>>>`. This was done in an |
| 7 | +//! attempt to keep things performant, but we actually always still end up with both an enum |
| 8 | +//! discriminant, as well as an extra indirection for the static one. |
| 9 | +//! |
| 10 | +//! The deep issue here is that Zephyr objects inherently cannot be moved. Zephyr uses a `dlist` |
| 11 | +//! structure in most objects that has a pointer back to itself to indicate the empty list. |
| 12 | +//! |
| 13 | +//! To work around this, we will implement objects as a pairing of an `AtomicUsize` and a |
| 14 | +//! `UnsafeCell<k_sem>` (for whatever underlying type). The atomic will go through a small number |
| 15 | +//! of states: |
| 16 | +//! |
| 17 | +//! - 0: indicates that this object is uninitialized. |
| 18 | +//! - ptr: where ptr is the address of Self for an initialized object. |
| 19 | +//! |
| 20 | +//! On each use, the atomic value can be read (Relaxed is fine here), and if a 0 is seen, perform an |
| 21 | +//! initialization. The initialization will lock a simple critical section, checking the atomic |
| 22 | +//! again, to make sure it didn't get initialized by something intercepting it. If the check sees a |
| 23 | +//! 'ptr' value that is not the same as Self, it indicates the object has been moved after |
| 24 | +//! initialization, and will simply panic. |
| 25 | +
|
| 26 | +// To measure performance, this module implements this for `k_sem` without abstractions around it. |
| 27 | +// The idea is to compare performance with the above `Fixed` implementation. |
| 28 | + |
| 29 | +use core::{cell::UnsafeCell, ffi::c_uint, mem, sync::atomic::Ordering}; |
| 30 | + |
| 31 | +use zephyr::{error::to_result_void, raw::{k_sem, k_sem_give, k_sem_init, k_sem_take}, sync::atomic::AtomicUsize, time::Timeout}; |
| 32 | +use zephyr::Result; |
| 33 | + |
| 34 | +pub struct Semaphore { |
| 35 | + state: AtomicUsize, |
| 36 | + item: UnsafeCell<k_sem>, |
| 37 | +} |
| 38 | + |
| 39 | +// SAFETY: These are both Send and Sync. The semaphore itself is safe, and the atomic+critical |
| 40 | +// section protects the state. |
| 41 | +unsafe impl Send for Semaphore { } |
| 42 | +unsafe impl Sync for Semaphore { } |
| 43 | + |
| 44 | +impl Semaphore { |
| 45 | + /// Construct a new semaphore, with the given initial_count and limit. There is a bit of |
| 46 | + /// trickery to pass the initial values through to the initializer, but otherwise this is just a |
| 47 | + /// basic initialization. |
| 48 | + pub fn new(initial_count: c_uint, limit: c_uint) -> Semaphore { |
| 49 | + let this = Self { |
| 50 | + state: AtomicUsize::new(0), |
| 51 | + item: unsafe { UnsafeCell::new(mem::zeroed()) }, |
| 52 | + }; |
| 53 | + |
| 54 | + // Set the initial count and limit in the semaphore to use for later initialization. |
| 55 | + unsafe { |
| 56 | + let ptr = this.item.get(); |
| 57 | + (*ptr).count = initial_count; |
| 58 | + (*ptr).limit = limit; |
| 59 | + } |
| 60 | + |
| 61 | + this |
| 62 | + } |
| 63 | + |
| 64 | + /// Get the raw pointer, initializing the `k_sem` if needed. |
| 65 | + fn get(&self) -> *mut k_sem { |
| 66 | + // First load can be relaxed, for performance reasons. If it is seen as uninitialized, the |
| 67 | + // below Acquire load will see the correct value. |
| 68 | + let state = self.state.load(Ordering::Relaxed); |
| 69 | + if state == self as *const Self as usize { |
| 70 | + return self.item.get(); |
| 71 | + } else if state != 0 { |
| 72 | + panic!("Semaphore was moved after first use"); |
| 73 | + } |
| 74 | + |
| 75 | + critical_section::with(|_| { |
| 76 | + // Reload, with Acquire ordering to see a determined value. |
| 77 | + let state = self.state.load(Ordering::Acquire); |
| 78 | + if state == self as *const Self as usize { |
| 79 | + return self.item.get(); |
| 80 | + } else if state != 0 { |
| 81 | + panic!("Semaphore was moved after first use"); |
| 82 | + } |
| 83 | + |
| 84 | + // Perform the initialization. We're within the critical section, and know that nobody |
| 85 | + // could be using this. |
| 86 | + unsafe { |
| 87 | + let ptr = self.item.get(); |
| 88 | + let initial_count = (*ptr).count; |
| 89 | + let limit = (*ptr).limit; |
| 90 | + |
| 91 | + k_sem_init(ptr, initial_count, limit); |
| 92 | + } |
| 93 | + |
| 94 | + self.state.store(self as *const Self as usize, Ordering::Release); |
| 95 | + self.item.get() |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + /// Synchronous take. |
| 100 | + pub fn take(&self, timeout: impl Into<Timeout>) -> Result<()> { |
| 101 | + let timeout: Timeout = timeout.into(); |
| 102 | + let ptr = self.get(); |
| 103 | + let ret = unsafe { k_sem_take(ptr, timeout.0) }; |
| 104 | + to_result_void(ret) |
| 105 | + } |
| 106 | + |
| 107 | + pub fn give(&self) { |
| 108 | + let ptr = self.get(); |
| 109 | + unsafe { k_sem_give(ptr) }; |
| 110 | + } |
| 111 | +} |
0 commit comments