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
/***************************************************************************
*
* 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 type definitions and handle wrappers.
//!
//! This module provides type aliases and handle types that interface with the
//! POSIX threading API (pthreads). Types are generated at build time based on
//! the detected target architecture's native word size.
//!
//! # Generated Types
//!
//! The following types are generated by the build script, based on the host
//! architecture reported by `uname`:
//!
//! - `TickType` - System tick counter type (`u32` on 32-bit, `u64` on 64-bit)
//! - `BaseType` - Basic signed integer type for return values
//! - `UBaseType` - Basic unsigned integer type
//! - `StackType` - Type used for stack allocation
//!
//! # Handle Types
//!
//! POSIX OS objects are referenced through opaque pointers (handles):
//!
//! - `ThreadHandle` - References a pthread
//! - `QueueHandle` - References a message queue
//! - `SemaphoreHandle` - References a semaphore
//! - `MutexHandle` - References a mutex
//! - `EventGroupHandle` - References an event group
//! - `TimerHandle` - References a timer
// Include build-time generated types based on the target architecture.
// This file is generated by the build script and contains:
// - TickType: System tick counter type
// - BaseType: Basic signed integer type
// - UBaseType: Basic unsigned integer type
// - StackType: Stack allocation type
include!;
use ;
use Debug;
use crate;
/// POSIX opaque handle types for OS primitives.
///
/// These handles are opaque pointers used to reference POSIX/pthread-backed
/// objects. They should not be dereferenced directly; instead, use the safe
/// wrappers provided by this crate (e.g., `Thread`, `Queue`, `Semaphore`, etc.).
/// Backing handle for [`crate::os::Queue`], [`crate::os::Semaphore`] and
/// [`crate::os::EventGroup`]: a `pthread_mutex_t` + `pthread_cond_t` pair.
///
/// These three primitives share the same "wait on a condition, guarded by a
/// mutex" shape, so they all reuse this one handle type (see
/// [`QueueHandle`], [`SemaphoreHandle`], [`EventGroupHandle`]) instead of
/// each defining their own. Not constructible from outside this crate other
/// than via [`Default`]; the safe wrapper types are what application code
/// should use.
///
/// # Examples
///
/// ```
/// use osal_rs::os::types::ClockMonotonicHandle;
///
/// // A never-initialized handle reports as empty.
/// let handle = ClockMonotonicHandle::default();
/// assert!(handle.is_empty());
/// ```
pthread_mutex_t,
pub pthread_cond_t,
);
/// Opaque POSIX thread identifier (`pthread_t`).
///
/// glibc defines `pthread_t` as `unsigned long int`, so `c_ulong` has the
/// correct size/representation on every target this crate builds for. `0`
/// is used throughout this crate as the "no thread" sentinel (see
/// [`crate::os::ThreadFn::is_null`]).
pub type ThreadHandle = c_ulong;
/// Backing handle for [`crate::os::Queue`]/[`crate::os::QueueStreamed`].
/// See [`ClockMonotonicHandle`] for why this is a mutex/condvar pair rather
/// than a queue-specific type.
pub type QueueHandle = ClockMonotonicHandle;
/// Backing handle for [`crate::os::Semaphore`].
/// See [`ClockMonotonicHandle`] for why this is a mutex/condvar pair rather
/// than a semaphore-specific type (plain POSIX unnamed semaphores can't
/// enforce a maximum count or use priority inheritance).
pub type SemaphoreHandle = ClockMonotonicHandle;
/// Backing handle for [`crate::os::EventGroup`].
/// See [`ClockMonotonicHandle`] for why this is a mutex/condvar pair rather
/// than an event-group-specific type.
pub type EventGroupHandle = ClockMonotonicHandle;
/// Opaque POSIX per-process timer identifier (`timer_t`, `<time.h>`).
///
/// glibc defines `timer_t` as `void *`, so `*mut c_void` has the correct
/// size/representation on every target this crate builds for.
pub type TimerHandle = *mut c_void;
/// Backing handle for [`crate::os::Mutex`]/[`crate::os::RawMutex`]: a bare
/// `pthread_mutex_t`, with no condition variable attached since a mutex has
/// nothing to wait on beyond acquiring the lock itself.
pub type MutexHandle = pthread_mutex_t;
/// Type alias for event group bits.
///
/// Represents a set of event flags where each bit can be set or cleared
/// independently. The underlying type is `TickType`, matching the native
/// word size of the target architecture. The top byte is reserved (see
/// [`crate::os::EventGroup::MAX_MASK`]), so only the lower bits are usable
/// as flags.
///
/// # Examples
///
/// ```
/// use osal_rs::os::types::EventBits;
///
/// const READY: EventBits = 1 << 0;
/// const ERROR: EventBits = 1 << 1;
///
/// let bits: EventBits = READY | ERROR;
/// assert_eq!(bits & READY, READY);
/// ```
pub type EventBits = TickType;