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
/***************************************************************************
*
* osal-rs
* Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*
***************************************************************************/
//! POSIX implementation of OSAL-RS traits.
//!
//! This module provides concrete implementations of all OSAL-RS traits on top
//! of the POSIX/pthreads API, so OSAL-RS applications can run — and their
//! tests and doc examples can execute for real — on any POSIX host (Linux,
//! macOS, ...) without embedded hardware or a cross toolchain.
//!
//! # Overview
//!
//! POSIX threads (pthreads) provide the primitives this module wraps:
//! - Preemptive multitasking backed by native OS threads
//! - Mutexes with priority inheritance (`PTHREAD_PRIO_INHERIT`)
//! - Semaphores, event groups and queues built on a `pthread_mutex_t` +
//! `pthread_cond_t` pair rather than plain POSIX unnamed semaphores, so
//! `signal()`/`post()` can enforce a maximum count
//! - Software timers, each backed by a dedicated background thread plus a
//! real kernel timer (`timer_create(2)`)
//! - Monotonic timing via `CLOCK_MONOTONIC`
//!
//! This module wraps the POSIX C API with safe Rust abstractions that
//! implement the same OSAL-RS trait interfaces as the FreeRTOS backend
//! (see [`crate::freertos`]), so application code written against
//! `osal_rs::os::*` is portable between the two by switching feature flags.
//!
//! # Modules
//!
//! - [`config`] - POSIX configuration constants (tick period)
//! - [`duration`] - Duration/tick conversions for the POSIX clock
//! - [`event_group`] - Event group synchronization primitives
//! - [`mutex`] - Mutex implementations with priority inheritance
//! - [`queue`] - Message queues for inter-task communication
//! - [`semaphore`] - Binary and counting semaphores
//! - [`system`] - System-level control and timing
//! - [`thread`] - Task/thread creation and management
//! - [`timer`] - Software timers for delayed/periodic callbacks
//! - [`types`] - POSIX-specific type definitions
//!
//! # Requirements
//!
//! - A POSIX-compliant host (glibc/Linux tested)
//! - No special build steps: unlike `freertos`, this backend links only
//! against the host's libc/libpthread — no cross toolchain or RTOS kernel
//! sources required
//!
//! # Usage
//!
//! Types from this module implement the traits defined in [`crate::traits`],
//! the same interfaces the `freertos` backend implements, allowing you to
//! write portable code through the unified [`crate::os`] module.
//!
//! ## Example: Creating a Thread
//!
//! ```
//! use osal_rs::os::*;
//! use std::sync::Arc;
//!
//! let mut thread = Thread::new("worker", 1024, 5);
//! let spawned = thread.spawn_simple(|| {
//! println!("Working...");
//! Ok(Arc::new(()))
//! }).unwrap();
//!
//! spawned.join(core::ptr::null_mut()).unwrap();
//! ```
//!
//! ## Example: Using a Mutex
//!
//! ```
//! use osal_rs::os::*;
//!
//! let mutex = Mutex::new(0);
//! {
//! let mut guard = mutex.lock().unwrap();
//! *guard += 1;
//! } // Lock released here
//!
//! assert_eq!(*mutex.lock().unwrap(), 1);
//! ```
//!
//! ## Example: Using a Queue
//!
//! ```
//! use osal_rs::os::*;
//!
//! let queue = Queue::new(4, 4).unwrap();
//!
//! // Producer
//! queue.post(&[1u8, 2, 3, 4], 100).unwrap();
//!
//! // Consumer
//! let mut buffer = [0u8; 4];
//! queue.fetch(&mut buffer, 100).unwrap();
//! assert_eq!(buffer, [1, 2, 3, 4]);
//! ```
//!
//! ## Example: Using a Semaphore
//!
//! ```
//! use osal_rs::os::*;
//! use osal_rs::utils::OsalRsBool;
//! use core::time::Duration;
//!
//! let sem = Semaphore::new(1, 0).unwrap();
//! sem.signal();
//! assert_eq!(sem.wait(Duration::from_millis(100)), OsalRsBool::True);
//! ```
//!
//! ## Example: Using an Event Group
//!
//! ```
//! use osal_rs::os::*;
//! use osal_rs::os::types::EventBits;
//!
//! const EVENT_A: EventBits = 1 << 0;
//! const EVENT_B: EventBits = 1 << 1;
//!
//! let events = EventGroup::new().unwrap();
//! events.set(EVENT_A | EVENT_B);
//!
//! let bits = events.wait(EVENT_A | EVENT_B, 100);
//! assert_eq!(bits & (EVENT_A | EVENT_B), EVENT_A | EVENT_B);
//! ```
//!
//! ## Example: Using a Timer
//!
//! ```
//! use osal_rs::os::*;
//! use std::sync::Arc;
//! use core::time::Duration;
//!
//! let timer = Timer::new_with_to_tick(
//! "heartbeat",
//! Duration::from_millis(10),
//! false, // one-shot
//! None,
//! |_timer, _param| {
//! println!("Tick!");
//! Ok(Arc::new(()))
//! }
//! ).unwrap();
//!
//! timer.start(0);
//! ```
//!
//! # Platform Support
//!
//! Tested against glibc on Linux. Any POSIX-compliant host implementing
//! pthreads, `timer_create(2)`/`sigwait(3)` and `CLOCK_MONOTONIC` should
//! work, but only Linux/glibc is part of this crate's test suite.
//!
//! # Safety
//!
//! This module uses FFI to call POSIX/pthread functions. The Rust wrappers
//! ensure memory safety through:
//! - Proper lifetime management
//! - Type safety
//! - Resource cleanup (RAII)
//! - Checked conversions
//!
//! # Caveats
//!
//! - `System::start()` on POSIX simply spins until [`crate::os::System::stop`]
//! is called from another thread — there is no scheduler to hand control
//! to, unlike FreeRTOS where it never returns.
//! - Timers each spawn their own background thread and permanently block
//! `SIGALRM` on the thread that creates them (see [`timer`] for why);
//! create a new [`crate::os::Timer`] rather than reusing one that already
//! fired as a one-shot.
/// POSIX FFI (Foreign Function Interface) bindings.
///
/// This module is private and contains unsafe C bindings to the POSIX/pthread API.
/// POSIX configuration constants and utilities.
/// Duration type implementations for POSIX clock/time conversion.
pub
/// Event group synchronization primitives.
pub
/// Mutex implementations with optional priority inheritance.
pub
/// Message queue implementations for inter-task communication.
pub
/// Binary and counting semaphore implementations.
pub
/// System-level control, timing, and scheduler management.
pub
/// Task/thread creation, management, and notifications.
pub
/// Software timer implementations for delayed and periodic callbacks.
pub
/// POSIX-specific type definitions and aliases.