ChibiOS 21.11.5
chobjects.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 Giovanni Di Sirio.
3
4 This file is part of ChibiOS.
5
6 ChibiOS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation version 3 of the License.
9
10 ChibiOS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/**
20 * @file chobjects.h
21 * @brief Operating System Objects macros and structures.
22 *
23 * @addtogroup os_structures
24 * @{
25 */
26
27#ifndef CHOBJECTS_H
28#define CHOBJECTS_H
29
30/*===========================================================================*/
31/* Module constants. */
32/*===========================================================================*/
33
34/*===========================================================================*/
35/* Module pre-compile time settings. */
36/*===========================================================================*/
37
38/*===========================================================================*/
39/* Derived constants and error checks. */
40/*===========================================================================*/
41
42/*===========================================================================*/
43/* Module data structures and types. */
44/*===========================================================================*/
45
46/**
47 * @brief Global state of the operating system.
48 */
55
56/**
57 * @brief Type of a Virtual Timer.
58 */
60
61/**
62 * @brief Type of a Virtual Timer callback function.
63 *
64 * @param[in] vtp pointer to the @p virtual_timer_t calling this callback
65 * @param[in] p optional argument to the callback
66 */
67typedef void (*vtfunc_t)(virtual_timer_t *vtp, void *p);
68
69/**
70 * @brief Structure representing a Virtual Timer.
71 */
73 /**
74 * @brief Delta list element.
75 */
77 /**
78 * @brief Timer callback function pointer.
79 */
81 /**
82 * @brief Timer callback function parameter.
83 */
84 void *par;
85 /**
86 * @brief Current reload interval.
87 */
89};
90
91/**
92 * @brief Type of virtual timers list header.
93 * @note The timers list is implemented as a double link bidirectional list
94 * in order to make the unlink time constant, the reset of a virtual
95 * timer is often used in the code.
96 */
97typedef struct ch_virtual_timers_list {
98 /**
99 * @brief Delta list header.
100 */
102#if (CH_CFG_ST_TIMEDELTA == 0) || defined(__DOXYGEN__)
103 /**
104 * @brief System Time counter.
105 */
107#endif
108#if (CH_CFG_ST_TIMEDELTA > 0) || defined(__DOXYGEN__)
109 /**
110 * @brief System time of the last tick event.
111 */
113#endif
114#if (CH_CFG_USE_TIMESTAMP == TRUE) || defined(__DOXYGEN__)
115 /**
116 * @brief Last generated time stamp.
117 */
118 volatile uint64_t laststamp;
119#endif
121
122/**
123 * @brief Type of a registry structure.
124 */
125typedef struct ch_registry {
126 /**
127 * @brief Registry queue header.
128 */
131
132/**
133 * @brief Type of a thread reference.
134 */
136
137/**
138 * @brief Type of a threads queue.
139 */
140typedef struct ch_threads_queue {
141 /**
142 * @brief Threads queue header.
143 */
146
147/**
148 * @brief Structure representing a thread.
149 * @note Not all the listed fields are always needed, by switching off some
150 * not needed ChibiOS/RT subsystems it is possible to save RAM space
151 * by shrinking this structure.
152 */
153struct ch_thread {
154 /**
155 * @brief Shared list headers.
156 */
157 union {
158 /**
159 * @brief Threads lists element.
160 */
162 /**
163 * @brief Threads queues element.
164 */
166 /**
167 * @brief Threads ordered queues element.
168 */
171 /**
172 * @brief Processor context.
173 */
175#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
176 /**
177 * @brief Registry queue element.
178 */
180#endif
181 /**
182 * @brief OS instance owner of this thread.
183 */
185#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
186 /**
187 * @brief Thread name or @p NULL.
188 */
189 const char *name;
190#endif
191#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE) || \
192 defined(__DOXYGEN__)
193 /**
194 * @brief Working area base address.
195 * @note This pointer is used for stack overflow checks and for
196 * dynamic threading.
197 */
199#endif
200 /**
201 * @brief Current thread state.
202 */
204 /**
205 * @brief Various thread flags.
206 */
208#if (CH_CFG_USE_REGISTRY == TRUE) || defined(__DOXYGEN__)
209 /**
210 * @brief References to this thread.
211 */
213#endif
214 /**
215 * @brief Number of ticks remaining to this thread.
216 */
217#if (CH_CFG_TIME_QUANTUM > 0) || defined(__DOXYGEN__)
219#endif
220#if (CH_DBG_THREADS_PROFILING == TRUE) || defined(__DOXYGEN__)
221 /**
222 * @brief Thread consumed time in ticks.
223 * @note This field can overflow.
224 */
225 volatile systime_t time;
226#endif
227 /**
228 * @brief State-specific fields.
229 * @note All the fields declared in this union are only valid in the
230 * specified state or condition and are thus volatile.
231 */
232 union {
233 /**
234 * @brief Thread wakeup code.
235 * @note This field contains the low level message sent to the thread
236 * by the waking thread or interrupt handler. The value is valid
237 * after exiting the @p chSchWakeupS() function.
238 */
240 /**
241 * @brief Thread exit code.
242 * @note The thread termination code is stored in this field in order
243 * to be retrieved by the thread performing a @p chThdWait() on
244 * this thread.
245 */
247 /**
248 * @brief Pointer to a generic "wait" object.
249 * @note This field is used to get a generic pointer to a synchronization
250 * object and is valid when the thread is in one of the wait
251 * states.
252 */
253 void *wtobjp;
254 /**
255 * @brief Pointer to a generic thread reference object.
256 * @note This field is used to get a pointer to a synchronization
257 * object and is valid when the thread is in @p CH_STATE_SUSPENDED
258 * state.
259 */
261#if (CH_CFG_USE_SEMAPHORES == TRUE) || defined(__DOXYGEN__)
262 /**
263 * @brief Pointer to a generic semaphore object.
264 * @note This field is used to get a pointer to a synchronization
265 * object and is valid when the thread is in @p CH_STATE_WTSEM
266 * state.
267 */
269#endif
270#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
271 /**
272 * @brief Pointer to a generic mutex object.
273 * @note This field is used to get a pointer to a synchronization
274 * object and is valid when the thread is in @p CH_STATE_WTMTX
275 * state.
276 */
278#endif
279#if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
280 /**
281 * @brief Enabled events mask.
282 * @note This field is only valid while the thread is in the
283 * @p CH_STATE_WTOREVT or @p CH_STATE_WTANDEVT states.
284 */
286#endif
287 } u;
288#if (CH_CFG_USE_WAITEXIT == TRUE) || defined(__DOXYGEN__)
289 /**
290 * @brief Termination waiting list.
291 */
293#endif
294#if (CH_CFG_USE_MESSAGES == TRUE) || defined(__DOXYGEN__)
295 /**
296 * @brief Messages queue.
297 */
299 /**
300 * @brief Sent message.
301 * @note This field is intentionally placed outside the @p u union even
302 * though it is only valid while the thread is in the
303 * @p CH_STATE_SNDMSG or @p CH_STATE_SNDMSGQ states.
304 * @note The reason for keeping it out of the union is that, when
305 * @p CH_CFG_USE_MESSAGES_PRIORITY is enabled, a thread blocked in
306 * @p CH_STATE_SNDMSGQ can be subject to priority inheritance: the
307 * PI walk in @p chMtxLockS() needs to re-enqueue the sender in the
308 * receiver's message queue after boosting its priority, and it does
309 * so by storing a back-pointer to that queue in @p u.wtobjp. At
310 * the same time @p chMsgGet() must still be able to read the sent
311 * message value until @p chMsgRelease() is called. Because both
312 * @p u.wtobjp and @p sentmsg must coexist while the thread is
313 * queued, placing @p sentmsg in the union would cause them to alias
314 * and corrupt each other.
315 */
317#endif
318#if (CH_CFG_USE_EVENTS == TRUE) || defined(__DOXYGEN__)
319 /**
320 * @brief Pending events mask.
321 */
323#endif
324#if (CH_CFG_USE_MUTEXES == TRUE) || defined(__DOXYGEN__)
325 /**
326 * @brief List of the mutexes owned by this thread.
327 * @note The list is terminated by a @p NULL in this field.
328 */
330 /**
331 * @brief Thread's own, non-inherited, priority.
332 */
334#endif
335#if ((CH_CFG_USE_DYNAMIC == TRUE) && (CH_CFG_USE_MEMPOOLS == TRUE)) || \
336 defined(__DOXYGEN__)
337 /**
338 * @brief Memory Pool where the thread workspace is returned.
339 */
340 void *mpool;
341#endif
342#if (CH_DBG_STATISTICS == TRUE) || defined(__DOXYGEN__)
343 /**
344 * @brief Thread statistics.
345 */
347#endif
348#if defined(CH_CFG_THREAD_EXTRA_FIELDS)
349 /* Extra fields defined in chconf.h.*/
351#endif
352};
353
354/**
355 * @brief Type of a ready list header.
356 */
357typedef struct ch_ready_list {
358 /**
359 * @brief Threads ordered queues header.
360 * @note The priority field must be initialized to zero.
361 */
363 /**
364 * @brief The currently running thread.
365 */
368
369/**
370 * @brief Type of an system instance configuration.
371 */
372typedef struct ch_os_instance_config {
373 /**
374 * @brief Instance name.
375 */
376 const char *name;
377#if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE) || \
378 defined(__DOXYGEN__)
379 /**
380 * @brief Lower limit of the main function thread stack.
381 */
383 /**
384 * @brief Upper limit of the main function thread stack.
385 */
387#endif
388#if (CH_CFG_NO_IDLE_THREAD == FALSE) || defined(__DOXYGEN__)
389 /**
390 * @brief Lower limit of the dedicated idle thread stack.
391 */
393 /**
394 * @brief Upper limit of the dedicated idle thread stack.
395 */
397#endif
399
400/**
401 * @brief System instance data structure.
402 */
404 /**
405 * @brief Ready list header.
406 */
408 /**
409 * @brief Virtual timers delta list header.
410 */
412#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_CFG_SMP_MODE == FALSE)) || \
413 defined(__DOXYGEN__)
414 /**
415 * @brief Registry header.
416 * @note This field is present only if the SMP mode is disabled.
417 */
419#endif
420 /**
421 * @brief Core associated to this instance.
422 */
424#if (CH_CFG_SMP_MODE == FALSE) || defined(__DOXYGEN__)
425 /**
426 * @brief Runtime Faults Collection Unit for this instance.
427 * @note This field is present only if the SMP mode is disabled.
428 */
430#endif
431 /**
432 * @brief Pointer to the instance configuration data.
433 */
435 /**
436 * @brief Main thread descriptor.
437 */
439 /**
440 * @brief System debug.
441 */
443#if (CH_DBG_TRACE_MASK != CH_DBG_TRACE_MASK_DISABLED) || defined(__DOXYGEN__)
444 /**
445 * @brief Trace buffer.
446 */
448#endif
449#if (CH_DBG_STATISTICS == TRUE) || defined(__DOXYGEN__)
450 /**
451 * @brief Global kernel statistics.
452 */
454#endif
455#if defined(PORT_INSTANCE_EXTRA_FIELDS) || defined(__DOXYGEN__)
456 /* Extra fields from port layer.*/
457 PORT_INSTANCE_EXTRA_FIELDS
458#endif
459 /* Extra fields from configuration.*/
461};
462
463/**
464 * @brief Type of system data structure.
465 */
466typedef struct ch_system {
467 /**
468 * @brief Operating system state.
469 */
471 /**
472 * @brief Initialized OS instances or @p NULL.
473 */
475#if (CH_CFG_USE_TM == TRUE) || defined(__DOXYGEN__)
476 /**
477 * @brief Time measurement calibration data.
478 */
480#endif
481#if ((CH_CFG_USE_REGISTRY == TRUE) && (CH_CFG_SMP_MODE == TRUE)) || \
482 defined(__DOXYGEN__)
483 /**
484 * @brief Registry header.
485 * @note This field is present only if the SMP mode is enabled.
486 */
488#endif
489#if (CH_CFG_SMP_MODE == TRUE) || defined(__DOXYGEN__)
490 /**
491 * @brief Runtime Faults Collection Unit.
492 * @note This field is present only if the SMP mode is enabled.
493 */
495#endif
496#if defined(PORT_SYSTEM_EXTRA_FIELDS) || defined(__DOXYGEN__)
497 /* Extra fields from port layer.*/
498 PORT_SYSTEM_EXTRA_FIELDS
499#endif
500 /* Extra fields from configuration.*/
503
504/*===========================================================================*/
505/* Module macros. */
506/*===========================================================================*/
507
508/*===========================================================================*/
509/* External declarations. */
510/*===========================================================================*/
511
512#ifdef __cplusplus
513extern "C" {
514#endif
515
516#ifdef __cplusplus
517}
518#endif
519
520/*===========================================================================*/
521/* Module inline functions. */
522/*===========================================================================*/
523
524#endif /* CHOBJECTS_H */
525
526/** @} */
struct ch_system_debug system_debug_t
System debug data structure.
#define CH_CFG_THREAD_EXTRA_FIELDS
Threads descriptor structure extension.
#define CH_CFG_SYSTEM_EXTRA_FIELDS
System structure extension.
#define CH_CFG_OS_INSTANCE_EXTRA_FIELDS
OS instance structure extension.
struct ch_priority_queue ch_priority_queue_t
Type of a generic priority-ordered bidirectional linked list header and element.
Definition chlists.h:77
struct ch_list ch_list_t
Type of a generic single link list header and element.
Definition chlists.h:49
struct ch_delta_list ch_delta_list_t
Type of a generic bidirectional linked delta list header and element.
Definition chlists.h:94
struct ch_queue ch_queue_t
Type of a generic bidirectional linked list header and element.
Definition chlists.h:62
struct ch_ready_list ready_list_t
Type of a ready list header.
uint8_t trefs_t
Definition chearly.h:84
struct ch_virtual_timers_list virtual_timers_list_t
Type of virtual timers list header.
struct ch_virtual_timer virtual_timer_t
Type of a Virtual Timer.
Definition chobjects.h:59
int32_t msg_t
Definition chearly.h:87
unsigned core_id_t
Type of a core identifier.
Definition chearly.h:127
uint8_t tstate_t
Definition chearly.h:83
uint32_t tprio_t
Definition chearly.h:86
struct ch_registry registry_t
Type of a registry structure.
uint8_t tmode_t
Definition chearly.h:82
struct ch_system ch_system_t
Type of system data structure.
struct ch_os_instance os_instance_t
Type of an OS instance structure.
Definition chearly.h:137
struct ch_os_instance_config os_instance_config_t
Type of an system instance configuration.
uint8_t tslices_t
Definition chearly.h:85
uint32_t eventmask_t
Definition chearly.h:89
void(* vtfunc_t)(virtual_timer_t *vtp, void *p)
Type of a Virtual Timer callback function.
Definition chobjects.h:67
thread_t * thread_reference_t
Type of a thread reference.
Definition chobjects.h:135
port_stkalign_t stkalign_t
Definition chearly.h:79
system_state_t
Global state of the operating system.
Definition chobjects.h:49
struct ch_thread thread_t
Type of a thread structure.
Definition chearly.h:132
@ ch_sys_uninit
Definition chobjects.h:50
@ ch_sys_running
Definition chobjects.h:52
@ ch_sys_initializing
Definition chobjects.h:51
@ ch_sys_halted
Definition chobjects.h:53
#define PORT_CORES_NUMBER
Definition chport.h:125
struct ch_rfcu rfcu_t
Type of an RFCU structure.
uint64_t systime_t
Type of system time.
Definition chtime.h:106
uint64_t sysinterval_t
Type of time interval.
Definition chtime.h:118
Mutex structure.
Definition chmtx.h:56
Type of an system instance configuration.
Definition chobjects.h:372
stkalign_t * mainthread_base
Lower limit of the main function thread stack.
Definition chobjects.h:382
stkalign_t * idlethread_base
Lower limit of the dedicated idle thread stack.
Definition chobjects.h:392
stkalign_t * idlethread_end
Upper limit of the dedicated idle thread stack.
Definition chobjects.h:396
stkalign_t * mainthread_end
Upper limit of the main function thread stack.
Definition chobjects.h:386
const char * name
Instance name.
Definition chobjects.h:376
System instance data structure.
Definition chobjects.h:403
kernel_stats_t kernel_stats
Global kernel statistics.
Definition chobjects.h:453
registry_t reglist
Registry header.
Definition chobjects.h:418
rfcu_t rfcu
Runtime Faults Collection Unit for this instance.
Definition chobjects.h:429
const os_instance_config_t * config
Pointer to the instance configuration data.
Definition chobjects.h:434
thread_t mainthread
Main thread descriptor.
Definition chobjects.h:438
virtual_timers_list_t vtlist
Virtual timers delta list header.
Definition chobjects.h:411
core_id_t core_id
Core associated to this instance.
Definition chobjects.h:423
system_debug_t dbg
System debug.
Definition chobjects.h:442
ready_list_t rlist
Ready list header.
Definition chobjects.h:407
trace_buffer_t trace_buffer
Trace buffer.
Definition chobjects.h:447
Type of a ready list header.
Definition chobjects.h:357
thread_t * current
The currently running thread.
Definition chobjects.h:366
ch_priority_queue_t pqueue
Threads ordered queues header.
Definition chobjects.h:362
Type of a registry structure.
Definition chobjects.h:125
ch_queue_t queue
Registry queue header.
Definition chobjects.h:129
Semaphore structure.
Type of system data structure.
Definition chobjects.h:466
tm_calibration_t tmc
Time measurement calibration data.
Definition chobjects.h:479
os_instance_t * instances[PORT_CORES_NUMBER]
Initialized OS instances or NULL.
Definition chobjects.h:474
rfcu_t rfcu
Runtime Faults Collection Unit.
Definition chobjects.h:494
system_state_t state
Operating system state.
Definition chobjects.h:470
registry_t reglist
Registry header.
Definition chobjects.h:487
Structure representing a thread.
Definition chobjects.h:153
ch_queue_t queue
Threads queues element.
Definition chobjects.h:165
union ch_thread::@065317322233202114332352372014266163076165303275 hdr
Shared list headers.
struct ch_mutex * wtmtxp
Pointer to a generic mutex object.
Definition chobjects.h:277
time_measurement_t stats
Thread statistics.
Definition chobjects.h:346
msg_t rdymsg
Thread wakeup code.
Definition chobjects.h:239
tprio_t realprio
Thread's own, non-inherited, priority.
Definition chobjects.h:333
void * mpool
Memory Pool where the thread workspace is returned.
Definition chobjects.h:340
const char * name
Thread name or NULL.
Definition chobjects.h:189
struct ch_mutex * mtxlist
List of the mutexes owned by this thread.
Definition chobjects.h:329
eventmask_t epending
Pending events mask.
Definition chobjects.h:322
ch_list_t list
Threads lists element.
Definition chobjects.h:161
ch_list_t waiting
Termination waiting list.
Definition chobjects.h:292
thread_reference_t * wttrp
Pointer to a generic thread reference object.
Definition chobjects.h:260
void * wtobjp
Pointer to a generic "wait" object.
Definition chobjects.h:253
stkalign_t * wabase
Working area base address.
Definition chobjects.h:198
msg_t exitcode
Thread exit code.
Definition chobjects.h:246
msg_t sentmsg
Sent message.
Definition chobjects.h:316
tslices_t ticks
Number of ticks remaining to this thread.
Definition chobjects.h:218
tstate_t state
Current thread state.
Definition chobjects.h:203
volatile systime_t time
Thread consumed time in ticks.
Definition chobjects.h:225
eventmask_t ewmask
Enabled events mask.
Definition chobjects.h:285
ch_priority_queue_t pqueue
Threads ordered queues element.
Definition chobjects.h:169
ch_queue_t msgqueue
Messages queue.
Definition chobjects.h:298
struct port_context ctx
Processor context.
Definition chobjects.h:174
trefs_t refs
References to this thread.
Definition chobjects.h:212
struct ch_semaphore * wtsemp
Pointer to a generic semaphore object.
Definition chobjects.h:268
ch_queue_t rqueue
Registry queue element.
Definition chobjects.h:179
union ch_thread::@250330312022121344252011223135034045240103044261 u
State-specific fields.
tmode_t flags
Various thread flags.
Definition chobjects.h:207
os_instance_t * owner
OS instance owner of this thread.
Definition chobjects.h:184
Type of a threads queue.
Definition chobjects.h:140
ch_queue_t queue
Threads queue header.
Definition chobjects.h:144
Structure representing a Virtual Timer.
Definition chobjects.h:72
ch_delta_list_t dlist
Delta list element.
Definition chobjects.h:76
void * par
Timer callback function parameter.
Definition chobjects.h:84
sysinterval_t reload
Current reload interval.
Definition chobjects.h:88
vtfunc_t func
Timer callback function pointer.
Definition chobjects.h:80
Type of virtual timers list header.
Definition chobjects.h:97
volatile uint64_t laststamp
Last generated time stamp.
Definition chobjects.h:118
ch_delta_list_t dlist
Delta list header.
Definition chobjects.h:101
volatile systime_t systime
System Time counter.
Definition chobjects.h:106
systime_t lasttime
System time of the last tick event.
Definition chobjects.h:112
Type of a kernel statistics structure.
Definition chstats.h:55
Platform dependent part of the thread_t structure.
Definition chcore.h:168
Type of a thread queue.
Definition osal.h:238
Type of a Time Measurement object.
Definition chtm.h:78
Type of a time measurement calibration data.
Definition chtm.h:62
Trace buffer header.
Definition chtrace.h:191