freertos-in-rust 0.3.0

Pure-Rust no_std FreeRTOS kernel translation with safe Rust APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Safe Semaphore wrappers
//!
//! Provides binary and counting semaphores for task synchronization.
//! Unlike mutexes, semaphores don't have priority inheritance and
//! can be given from ISR context.

use core::ptr;

use crate::kernel::queue::{
    queueQUEUE_TYPE_BINARY_SEMAPHORE, queueSEND_TO_BACK, uxQueueMessagesWaiting,
    uxQueueMessagesWaitingFromISR, xQueueCreateCountingSemaphoreStatic, xQueueGenericCreateStatic,
    xQueueGenericSend, xQueueGiveFromISR, xQueueReceiveFromISR, xQueueSemaphoreTake, QueueHandle_t,
    StaticQueue_t,
};
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
use crate::kernel::queue::{vQueueDelete, xQueueCreateCountingSemaphore, xQueueGenericCreate};
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
use crate::sync::is_in_isr;
use crate::sync::task::TaskContext;
use crate::sync::{assert_can_block, assert_task_context};
use crate::types::*;

/// A binary semaphore for task synchronization.
///
/// Binary semaphores are useful for signaling between tasks or from
/// an ISR to a task. Unlike mutexes, they don't have priority inheritance
/// and can be "given" from interrupt context.
///
/// # Example
///
/// ```no_run
/// use freertos_in_rust::sync::{BinarySemaphore, TaskContext};
/// use freertos_in_rust::kernel::queue::StaticQueue_t;
///
/// let context = unsafe { TaskContext::assume() };
/// let control = Box::leak(Box::new(StaticQueue_t::new()));
/// let sem = BinarySemaphore::new_static(&context, control).expect("semaphore creation failed");
///
/// // In one task: wait for signal
/// sem.take(&context);  // Blocks until signaled
///
/// // In another task: signal
/// sem.give(&context);
///
/// // An ISR uses unsafe `give_from_isr` instead.
/// ```
///
/// ```compile_fail
/// use freertos_in_rust::sync::BinarySemaphore;
/// fn no_context(semaphore: &BinarySemaphore) {
///     let _ = semaphore.try_take(); // a `&TaskContext` capability is required
/// }
/// ```
pub struct BinarySemaphore {
    handle: QueueHandle_t,
    owns_handle: bool,
}

// Safety: BinarySemaphore can be shared between tasks
unsafe impl Sync for BinarySemaphore {}
unsafe impl Send for BinarySemaphore {}

impl BinarySemaphore {
    /// Creates a new binary semaphore in the "not given" state.
    ///
    /// Returns `None` if creation failed (e.g., out of memory).
    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    pub fn new(_context: &TaskContext) -> Option<Self> {
        assert_task_context("BinarySemaphore::new");
        let handle = unsafe { xQueueGenericCreate(1, 0, queueQUEUE_TYPE_BINARY_SEMAPHORE) };
        if handle.is_null() {
            None
        } else {
            Some(Self {
                handle,
                owns_handle: true,
            })
        }
    }

    /// Creates a binary semaphore using static storage.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use freertos_in_rust::sync::{BinarySemaphore, TaskContext};
    /// use freertos_in_rust::kernel::queue::StaticQueue_t;
    ///
    /// let context = unsafe { TaskContext::assume() };
    /// let control = Box::leak(Box::new(StaticQueue_t::new()));
    /// let sem = BinarySemaphore::new_static(&context, control).expect("creation failed");
    /// ```
    pub fn new_static(
        _context: &TaskContext,
        sem_buffer: &'static mut StaticQueue_t,
    ) -> Option<Self> {
        assert_task_context("BinarySemaphore::new_static");
        let handle = unsafe {
            xQueueGenericCreateStatic(
                1,
                0,
                ptr::null_mut(),
                sem_buffer as *mut StaticQueue_t,
                queueQUEUE_TYPE_BINARY_SEMAPHORE,
            )
        };
        if handle.is_null() {
            None
        } else {
            Some(Self {
                handle,
                owns_handle: true,
            })
        }
    }

    /// Takes (acquires) the semaphore, blocking indefinitely.
    ///
    /// Returns `true` if taken, though with infinite timeout this
    /// always returns `true` under normal operation.
    pub fn take(&self, context: &TaskContext) -> bool {
        self.take_timeout(context, portMAX_DELAY)
    }

    /// Attempts to take the semaphore without blocking.
    ///
    /// Returns `true` if the semaphore was taken, `false` if it
    /// wasn't available.
    pub fn try_take(&self, context: &TaskContext) -> bool {
        self.take_timeout(context, 0)
    }

    /// Attempts to take the semaphore with a timeout.
    ///
    /// Returns `true` if taken within the timeout, `false` otherwise.
    pub fn take_timeout(&self, _context: &TaskContext, ticks: TickType_t) -> bool {
        assert_can_block("BinarySemaphore::take_timeout", ticks);
        unsafe { xQueueSemaphoreTake(self.handle, ticks) == pdTRUE }
    }

    /// Gives (releases) the semaphore.
    ///
    /// This can be called even if the semaphore hasn't been taken -
    /// it simply ensures the semaphore is in the "given" state.
    pub fn give(&self, _context: &TaskContext) -> bool {
        assert_task_context("BinarySemaphore::give");
        unsafe { xQueueGenericSend(self.handle, core::ptr::null(), 0, queueSEND_TO_BACK) == pdTRUE }
    }

    /// Gives the semaphore from interrupt context.
    ///
    /// This method does not request a context switch. If it sets
    /// `higher_priority_task_woken` to `pdTRUE`, the ISR must yield using the
    /// active port's ISR-yield primitive before returning.
    ///
    /// # Safety
    ///
    /// The caller must be executing at an interrupt priority from which
    /// FreeRTOS `FromISR` APIs are permitted. The wake flag must be initialized
    /// to `pdFALSE` before the first operation in an ISR.
    pub unsafe fn give_from_isr(&self, higher_priority_task_woken: &mut BaseType_t) -> bool {
        // Safety: `self` owns a live semaphore, the wake flag is valid, and
        // the caller upholds the ISR-priority contract.
        unsafe { xQueueGiveFromISR(self.handle, higher_priority_task_woken) == pdPASS }
    }

    /// Takes the semaphore from interrupt context without blocking.
    ///
    /// # Safety
    ///
    /// The caller must obey the interrupt-priority and wake-flag requirements
    /// documented on [`BinarySemaphore::give_from_isr`].
    pub unsafe fn take_from_isr(&self, higher_priority_task_woken: &mut BaseType_t) -> bool {
        // Safety: item size is zero for a semaphore, so a null receive buffer
        // is valid; the caller upholds the ISR-priority contract.
        unsafe {
            xQueueReceiveFromISR(self.handle, ptr::null_mut(), higher_priority_task_woken) == pdPASS
        }
    }

    /// Returns `true` if the semaphore is currently available.
    pub fn is_available(&self, _context: &TaskContext) -> bool {
        assert_task_context("BinarySemaphore::is_available");
        unsafe { uxQueueMessagesWaiting(self.handle) != 0 }
    }

    /// Returns `true` if the semaphore is available from interrupt context.
    ///
    /// # Safety
    ///
    /// The caller must be executing at an interrupt priority from which
    /// FreeRTOS `FromISR` APIs are permitted.
    pub unsafe fn is_available_from_isr(&self) -> bool {
        // Safety: `self` owns a live semaphore and the caller upholds the ISR
        // context contract.
        unsafe { uxQueueMessagesWaitingFromISR(self.handle) != 0 }
    }

    /// Returns the raw FreeRTOS handle for interop with raw APIs.
    ///
    /// # Safety
    ///
    /// The caller must not let the handle outlive this owner, delete or reset
    /// the semaphore behind the wrapper, or violate the raw operation's
    /// task/ISR-context requirements.
    pub unsafe fn raw_handle(&self) -> QueueHandle_t {
        self.handle
    }

    /// Consumes the wrapper and returns the raw handle without deleting it.
    pub fn into_raw(mut self) -> QueueHandle_t {
        self.owns_handle = false;
        self.handle
    }

    /// Explicitly deletes the underlying FreeRTOS semaphore.
    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    pub fn delete(mut self, _context: &TaskContext) {
        assert_task_context("BinarySemaphore::delete");
        unsafe { vQueueDelete(self.handle) };
        self.owns_handle = false;
    }
}

/// A counting semaphore for resource management.
///
/// Counting semaphores track a count of available resources. Each `take`
/// decrements the count (blocking if zero), and each `give` increments it.
///
/// # Example
///
/// ```no_run
/// use freertos_in_rust::sync::{CountingSemaphore, TaskContext};
/// use freertos_in_rust::kernel::queue::StaticQueue_t;
///
/// let context = unsafe { TaskContext::assume() };
/// let control = Box::leak(Box::new(StaticQueue_t::new()));
/// let pool = CountingSemaphore::new_static(&context, 5, 3, control).expect("creation failed");
///
/// // Acquire a resource (decrements count)
/// pool.take(&context);
///
/// // Release a resource (increments count)
/// pool.give(&context);
/// ```
pub struct CountingSemaphore {
    handle: QueueHandle_t,
    owns_handle: bool,
}

unsafe impl Sync for CountingSemaphore {}
unsafe impl Send for CountingSemaphore {}

impl CountingSemaphore {
    /// Creates a counting semaphore.
    ///
    /// # Arguments
    ///
    /// * `max_count` - Maximum count value
    /// * `initial_count` - Initial count (must be <= max_count)
    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    pub fn new(
        _context: &TaskContext,
        max_count: UBaseType_t,
        initial_count: UBaseType_t,
    ) -> Option<Self> {
        assert_task_context("CountingSemaphore::new");
        Self::validate_counts(max_count, initial_count)?;
        let handle = unsafe { xQueueCreateCountingSemaphore(max_count, initial_count) };
        if handle.is_null() {
            None
        } else {
            Some(Self {
                handle,
                owns_handle: true,
            })
        }
    }

    /// Creates a counting semaphore using static storage.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use freertos_in_rust::sync::{CountingSemaphore, TaskContext};
    /// use freertos_in_rust::kernel::queue::StaticQueue_t;
    ///
    /// let context = unsafe { TaskContext::assume() };
    /// let control = Box::leak(Box::new(StaticQueue_t::new()));
    /// let pool = CountingSemaphore::new_static(&context, 5, 3, control)
    ///     .expect("semaphore creation failed");
    /// ```
    pub fn new_static(
        _context: &TaskContext,
        max_count: UBaseType_t,
        initial_count: UBaseType_t,
        sem_buffer: &'static mut StaticQueue_t,
    ) -> Option<Self> {
        assert_task_context("CountingSemaphore::new_static");
        Self::validate_counts(max_count, initial_count)?;
        let handle = unsafe {
            xQueueCreateCountingSemaphoreStatic(
                max_count,
                initial_count,
                sem_buffer as *mut StaticQueue_t,
            )
        };
        if handle.is_null() {
            None
        } else {
            Some(Self {
                handle,
                owns_handle: true,
            })
        }
    }

    fn validate_counts(max_count: UBaseType_t, initial_count: UBaseType_t) -> Option<()> {
        if max_count == 0 || initial_count > max_count {
            None
        } else {
            Some(())
        }
    }

    /// Takes (decrements) the semaphore, blocking indefinitely.
    pub fn take(&self, context: &TaskContext) -> bool {
        self.take_timeout(context, portMAX_DELAY)
    }

    /// Attempts to take without blocking.
    pub fn try_take(&self, context: &TaskContext) -> bool {
        self.take_timeout(context, 0)
    }

    /// Attempts to take with timeout.
    pub fn take_timeout(&self, _context: &TaskContext, ticks: TickType_t) -> bool {
        assert_can_block("CountingSemaphore::take_timeout", ticks);
        unsafe { xQueueSemaphoreTake(self.handle, ticks) == pdTRUE }
    }

    /// Gives (increments) the semaphore.
    pub fn give(&self, _context: &TaskContext) -> bool {
        assert_task_context("CountingSemaphore::give");
        unsafe { xQueueGenericSend(self.handle, core::ptr::null(), 0, queueSEND_TO_BACK) == pdTRUE }
    }

    /// Gives the semaphore from interrupt context.
    ///
    /// This method does not request a context switch. If it sets
    /// `higher_priority_task_woken` to `pdTRUE`, the ISR must yield using the
    /// active port's ISR-yield primitive before returning.
    ///
    /// # Safety
    ///
    /// The caller must be executing at an interrupt priority from which
    /// FreeRTOS `FromISR` APIs are permitted. The wake flag must be initialized
    /// to `pdFALSE` before the first operation in an ISR.
    pub unsafe fn give_from_isr(&self, higher_priority_task_woken: &mut BaseType_t) -> bool {
        // Safety: `self` owns a live semaphore, the wake flag is valid, and
        // the caller upholds the ISR-priority contract.
        unsafe { xQueueGiveFromISR(self.handle, higher_priority_task_woken) == pdPASS }
    }

    /// Takes the semaphore from interrupt context without blocking.
    ///
    /// # Safety
    ///
    /// The caller must obey the interrupt-priority and wake-flag requirements
    /// documented on [`CountingSemaphore::give_from_isr`].
    pub unsafe fn take_from_isr(&self, higher_priority_task_woken: &mut BaseType_t) -> bool {
        // Safety: item size is zero for a semaphore, so a null receive buffer
        // is valid; the caller upholds the ISR-priority contract.
        unsafe {
            xQueueReceiveFromISR(self.handle, ptr::null_mut(), higher_priority_task_woken) == pdPASS
        }
    }

    /// Returns current count.
    pub fn count(&self, _context: &TaskContext) -> UBaseType_t {
        assert_task_context("CountingSemaphore::count");
        unsafe { uxQueueMessagesWaiting(self.handle) }
    }

    /// Returns current count from interrupt context.
    ///
    /// # Safety
    ///
    /// The caller must be executing at an interrupt priority from which
    /// FreeRTOS `FromISR` APIs are permitted.
    pub unsafe fn count_from_isr(&self) -> UBaseType_t {
        // Safety: `self` owns a live semaphore and the caller upholds the ISR
        // context contract.
        unsafe { uxQueueMessagesWaitingFromISR(self.handle) }
    }

    /// Returns the raw handle.
    ///
    /// # Safety
    ///
    /// The caller must not let the handle outlive this owner, delete or reset
    /// the semaphore behind the wrapper, or violate the raw operation's
    /// task/ISR-context requirements.
    pub unsafe fn raw_handle(&self) -> QueueHandle_t {
        self.handle
    }

    /// Consumes the wrapper and returns the raw handle without deleting it.
    pub fn into_raw(mut self) -> QueueHandle_t {
        self.owns_handle = false;
        self.handle
    }

    /// Explicitly deletes the underlying FreeRTOS semaphore.
    #[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
    pub fn delete(mut self, _context: &TaskContext) {
        assert_task_context("CountingSemaphore::delete");
        unsafe { vQueueDelete(self.handle) };
        self.owns_handle = false;
    }
}

#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
impl Drop for BinarySemaphore {
    fn drop(&mut self) {
        if self.owns_handle && !is_in_isr() {
            unsafe { vQueueDelete(self.handle) };
            self.owns_handle = false;
        }
    }
}

#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
impl Drop for CountingSemaphore {
    fn drop(&mut self) {
        if self.owns_handle && !is_in_isr() {
            unsafe { vQueueDelete(self.handle) };
            self.owns_handle = false;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counting_semaphore_rejects_invalid_counts() {
        assert!(CountingSemaphore::validate_counts(0, 0).is_none());
        assert!(CountingSemaphore::validate_counts(2, 3).is_none());
        assert!(CountingSemaphore::validate_counts(2, 2).is_some());
    }

    #[test]
    fn semaphore_handles_are_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<BinarySemaphore>();
        assert_send_sync::<CountingSemaphore>();
    }
}