RTOS Wrapper
API reference
Loading...
Searching...
No Matches
os_threadx.h
Go to the documentation of this file.
1#pragma once
19#include <stddef.h>
20#include <stdint.h>
21#include "tx_api.h"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27typedef TX_THREAD OS_Thread;
28typedef TX_MUTEX OS_Mutex;
29typedef TX_SEMAPHORE OS_Semaphore;
30typedef TX_QUEUE OS_Queue;
31typedef TX_EVENT_FLAGS_GROUP OS_EventGroup;
32typedef TX_TIMER OS_Timer;
33typedef void* OS_StreamBuffer;
34
35typedef unsigned long OS_Ulong;
36typedef unsigned int OS_Uint;
37
38enum {
39 OS_WAIT_FOREVER = (int)TX_WAIT_FOREVER,
40 OS_NO_WAIT = (int)TX_NO_WAIT,
41 OS_SUCCESS = (int)TX_SUCCESS,
42 OS_AND = (int)TX_AND,
43 OS_OR = (int)TX_OR,
45 OS_AUTO_START = (int)TX_AUTO_START,
46 OS_DONT_START = (int)TX_DONT_START,
47 OS_INHERIT = (int)TX_INHERIT
48};
49
50static inline OS_Ulong os_time_get(void) { return (OS_Ulong)tx_time_get(); }
51
52static inline OS_Uint os_thread_create(OS_Thread *t, const char *name,
53 void (*entry)(OS_Ulong),
54 OS_Ulong input, uint8_t *stack,
55 OS_Ulong stack_size, OS_Uint priority,
56 OS_Uint preempt, OS_Ulong slice,
57 OS_Uint auto_start)
58{
59 const UINT prio = (priority == 0u) ? 16u : (UINT)priority;
60 const UINT pre = (preempt == 0u) ? prio : (UINT)preempt;
61 const ULONG ts = (slice == 0u) ? TX_NO_TIME_SLICE : (ULONG)slice;
62 const UINT as = auto_start ? TX_AUTO_START : TX_DONT_START;
63 return (OS_Uint)tx_thread_create(t, (CHAR*)name, (VOID (*)(ULONG))entry,
64 (ULONG)input, stack, (ULONG)stack_size,
65 prio, pre, ts, as);
66}
67
69static inline OS_Uint os_thread_create_pinned(OS_Thread *t, const char *name,
70 void (*entry)(OS_Ulong),
71 OS_Ulong input, uint8_t *stack,
72 OS_Ulong stack_size, OS_Uint priority,
73 OS_Uint preempt, OS_Ulong slice,
74 OS_Uint auto_start, int core_id)
75{
76 (void)core_id;
77 return os_thread_create(t, name, entry, input, stack, stack_size,
78 priority, preempt, slice, auto_start);
79}
80
81static inline int os_get_current_core_id(void) { return 0; }
83{ return (OS_Uint)tx_thread_resume(t); }
85{ return (OS_Uint)tx_thread_suspend(t); }
87{ return (OS_Uint)tx_thread_delete(t); }
89{ return (OS_Uint)tx_thread_terminate(t); }
91{
92 /* ThreadX TX_NULL is (void*)0 and is not a typed C++ null. */
93 CHAR *unused_name = 0;
94 ULONG unused_run = 0;
95 UINT unused_prio = 0;
96 UINT unused_preempt = 0;
97 ULONG unused_slice = 0;
98 TX_THREAD *unused_next = 0;
99 TX_THREAD *unused_suspended = 0;
100 UINT s = 0;
101 const UINT err = tx_thread_info_get(t, &unused_name, &s, &unused_run,
102 &unused_prio, &unused_preempt,
103 &unused_slice, &unused_next,
104 &unused_suspended);
105 if (state) {
106 *state = (OS_Uint)s;
107 }
108 return (OS_Uint)err;
109}
110static inline OS_Uint os_thread_sleep(OS_Ulong ticks)
111{ return (OS_Uint)tx_thread_sleep((ULONG)ticks); }
112static inline OS_Uint os_thread_yield(void)
113{ (void)tx_thread_relinquish(); return OS_SUCCESS; }
114static inline int os_kernel_is_running(void)
115{ return tx_thread_identify() != TX_NULL; }
117{ return (t != 0 && tx_thread_identify() == t) ? 1u : 0u; }
118
119static inline OS_Uint os_mutex_create(OS_Mutex *m, const char *name, OS_Uint inherit)
120{
121 return (OS_Uint)tx_mutex_create(m, (CHAR*)name,
122 inherit ? TX_INHERIT : TX_NO_INHERIT);
123}
124static inline OS_Uint os_mutex_get(OS_Mutex *m, OS_Ulong wait)
125{ return (OS_Uint)tx_mutex_get(m, (ULONG)wait); }
127{ return (OS_Uint)tx_mutex_put(m); }
129{ return (OS_Uint)tx_mutex_delete(m); }
130
131static inline OS_Uint os_recursive_mutex_create(OS_Mutex *m, const char *name)
132{ return os_mutex_create(m, name, OS_INHERIT); }
134{ return os_mutex_get(m, wait); }
136{ return os_mutex_put(m); }
138{ return os_mutex_delete(m); }
139
140static inline OS_Uint os_semaphore_create(OS_Semaphore *s, const char *name, OS_Uint initial)
141{
142 return (OS_Uint)tx_semaphore_create(s, (CHAR*)name, (ULONG)initial);
143}
145{ return (OS_Uint)tx_semaphore_delete(s); }
147{ return (OS_Uint)tx_semaphore_put(s); }
149{ return (OS_Uint)tx_semaphore_get(s, (ULONG)wait); }
151{
152 CHAR *unused_name = 0;
153 TX_THREAD *unused_first = 0;
154 ULONG unused_suspended = 0;
155 TX_SEMAPHORE *unused_next = 0;
156 ULONG c = 0;
157 const UINT err = tx_semaphore_info_get(s, &unused_name, &c, &unused_first,
158 &unused_suspended, &unused_next);
159 if (count) {
160 *count = (OS_Ulong)c;
161 }
162 return (OS_Uint)err;
163}
164
166enum {
167 OS_THREAD_READY = TX_READY,
168 OS_THREAD_SUSPENDED = TX_SUSPENDED,
169 OS_THREAD_BLOCKED = TX_SLEEP
171
172static inline int os_thread_is_live(const OS_Thread *t)
173{
174 return (t != 0) && (t->tx_thread_id != 0u);
175}
176static inline void os_thread_clear(OS_Thread *t)
177{
178 if (t) {
179 t->tx_thread_id = 0u;
180 }
181}
182static inline int os_mutex_is_live(const OS_Mutex *m)
183{
184 return (m != 0) && (m->tx_mutex_id != 0u);
185}
186static inline int os_thread_state_is_suspended(OS_Uint state)
187{
188 return (state != (OS_Uint)TX_READY) &&
189 (state != (OS_Uint)TX_COMPLETED) &&
190 (state != (OS_Uint)TX_TERMINATED);
191}
192
194#define OS_QUEUE_STORE_BYTES(item_words, length) \
195 ((size_t)(item_words) * sizeof(ULONG) * (size_t)(length))
196
197static inline OS_Uint os_queue_create(OS_Queue *q, const char *name, OS_Uint item_words,
198 void *store, OS_Ulong length)
199{
200 return (OS_Uint)tx_queue_create(q, (CHAR*)name, (UINT)item_words, store,
201 (ULONG)(length * item_words * sizeof(ULONG)));
202}
204{ return (OS_Uint)tx_queue_delete(q); }
205static inline OS_Uint os_queue_send(OS_Queue *q, void *msg, OS_Ulong wait)
206{ return (OS_Uint)tx_queue_send(q, msg, (ULONG)wait); }
207static inline OS_Uint os_queue_receive(OS_Queue *q, void *msg, OS_Ulong wait)
208{ return (OS_Uint)tx_queue_receive(q, msg, (ULONG)wait); }
209
210static inline OS_Uint os_event_group_create(OS_EventGroup *g, const char *name)
211{ return (OS_Uint)tx_event_flags_create(g, (CHAR*)name); }
213{ return (OS_Uint)tx_event_flags_delete(g); }
215{ return (OS_Uint)tx_event_flags_set(g, (ULONG)flags, TX_OR); }
217{ return (OS_Uint)tx_event_flags_set(g, (ULONG)~flags, TX_AND); }
219 OS_Ulong *actual, OS_Ulong wait)
220{
221 ULONG got = 0;
222 /* TX_AND_CLEAR / TX_OR_CLEAR only with OS_EVENT_CLEAR; otherwise no bit is cleared. */
223 const int all = ((option & ~(OS_Uint)OS_EVENT_CLEAR) == (OS_Uint)OS_AND);
224 const int clear = (option & (OS_Uint)OS_EVENT_CLEAR) != 0U;
225 const UINT opt = all ? (clear ? TX_AND_CLEAR : TX_AND) : (clear ? TX_OR_CLEAR : TX_OR);
226 const UINT err = tx_event_flags_get(g, (ULONG)flags, opt, &got, (ULONG)wait);
227 if (actual) {
228 *actual = (OS_Ulong)got;
229 }
230 return (OS_Uint)err;
231}
232
233static inline OS_Uint os_timer_create(OS_Timer *t, const char *name,
234 void (*cb)(OS_Ulong), OS_Ulong id,
235 OS_Ulong initial, OS_Ulong reload,
236 OS_Uint auto_act)
237{
238 const UINT err = tx_timer_create(t, (CHAR*)name, (VOID (*)(ULONG))cb,
239 (ULONG)id, (ULONG)initial,
240 (ULONG)reload, TX_NO_ACTIVATE);
241 if (err != TX_SUCCESS) {
242 return (OS_Uint)err;
243 }
244 if (auto_act) {
245 return (OS_Uint)tx_timer_activate(t);
246 }
247 return OS_SUCCESS;
248}
250{ return (OS_Uint)tx_timer_delete(t); }
252{ return (OS_Uint)tx_timer_activate(t); }
254{ return (OS_Uint)tx_timer_deactivate(t); }
255
256/* No ThreadX stream-buffer primitive — report failure, never fake success. */
257static inline OS_Uint os_stream_buffer_create(OS_StreamBuffer *b, size_t capacity, size_t trigger)
258{ (void)capacity; (void)trigger; *b = 0; return OS_SUCCESS; }
260{ *b = 0; return OS_SUCCESS; }
261static inline OS_Uint os_stream_buffer_send(OS_StreamBuffer *b, const void *data, size_t len, OS_Ulong wait)
262{ (void)b; (void)data; (void)len; (void)wait; return (OS_Uint)1; }
263static inline OS_Uint os_stream_buffer_receive(OS_StreamBuffer *b, void *data, size_t len, OS_Ulong wait)
264{ (void)b; (void)data; (void)len; (void)wait; return (OS_Uint)1; }
265
266typedef UINT OS_Critical;
268static inline void os_critical_enter(void)
269{ os_critical_mux = tx_interrupt_control(TX_INT_DISABLE); }
270static inline void os_critical_exit(void)
271{ (void)tx_interrupt_control(os_critical_mux); }
272
273#ifdef __cplusplus
274}
275#endif
void * OS_StreamBuffer
Definition os_cmsis.h:33
osEventFlagsId_t OS_EventGroup
Definition os_cmsis.h:31
unsigned int OS_Uint
Definition os_cmsis.h:36
osMessageQueueId_t OS_Queue
Definition os_cmsis.h:30
unsigned long OS_Ulong
Definition os_cmsis.h:35
osThreadId_t OS_Thread
Definition os_cmsis.h:27
osTimerId_t OS_Timer
Definition os_cmsis.h:32
uint8_t OS_Critical
Definition os_cmsis.h:426
osSemaphoreId_t OS_Semaphore
Definition os_cmsis.h:29
osMutexId_t OS_Mutex
Definition os_cmsis.h:28
static void os_critical_enter(void)
Definition os_threadx.h:268
@ OS_THREAD_READY
Definition os_threadx.h:167
@ OS_THREAD_BLOCKED
Definition os_threadx.h:169
@ OS_THREAD_SUSPENDED
Definition os_threadx.h:168
static OS_Uint os_semaphore_put(OS_Semaphore *s)
Definition os_threadx.h:146
TX_TIMER OS_Timer
Definition os_threadx.h:32
static OS_Uint os_queue_delete(OS_Queue *q)
Definition os_threadx.h:203
static OS_Uint os_thread_create_pinned(OS_Thread *t, const char *name, void(*entry)(OS_Ulong), OS_Ulong input, uint8_t *stack, OS_Ulong stack_size, OS_Uint priority, OS_Uint preempt, OS_Ulong slice, OS_Uint auto_start, int core_id)
Definition os_threadx.h:69
static OS_Uint os_queue_create(OS_Queue *q, const char *name, OS_Uint item_words, void *store, OS_Ulong length)
Definition os_threadx.h:197
UINT OS_Critical
Definition os_threadx.h:266
static int os_mutex_is_live(const OS_Mutex *m)
Definition os_threadx.h:182
static OS_Uint os_semaphore_get(OS_Semaphore *s, OS_Ulong wait)
Definition os_threadx.h:148
void * OS_StreamBuffer
Definition os_threadx.h:33
static OS_Uint os_thread_suspend(OS_Thread *t)
Definition os_threadx.h:84
static OS_Uint os_thread_resume(OS_Thread *t)
Definition os_threadx.h:82
TX_SEMAPHORE OS_Semaphore
Definition os_threadx.h:29
static OS_Uint os_recursive_mutex_create(OS_Mutex *m, const char *name)
Definition os_threadx.h:131
static OS_Uint os_event_group_set(OS_EventGroup *g, OS_Ulong flags)
Definition os_threadx.h:214
static OS_Uint os_mutex_put(OS_Mutex *m)
Definition os_threadx.h:126
static OS_Uint os_event_group_get(OS_EventGroup *g, OS_Ulong flags, OS_Uint option, OS_Ulong *actual, OS_Ulong wait)
Definition os_threadx.h:218
OS_Critical os_critical_mux
Definition CriticalSection.cpp:11
static OS_Uint os_semaphore_create(OS_Semaphore *s, const char *name, OS_Uint initial)
Definition os_threadx.h:140
static OS_Uint os_event_group_delete(OS_EventGroup *g)
Definition os_threadx.h:212
static int os_kernel_is_running(void)
Definition os_threadx.h:114
static OS_Uint os_mutex_get(OS_Mutex *m, OS_Ulong wait)
Definition os_threadx.h:124
static OS_Uint os_mutex_delete(OS_Mutex *m)
Definition os_threadx.h:128
static OS_Uint os_event_group_clear(OS_EventGroup *g, OS_Ulong flags)
Definition os_threadx.h:216
static OS_Uint os_semaphore_info_get(OS_Semaphore *s, OS_Ulong *count)
Definition os_threadx.h:150
static void os_critical_exit(void)
Definition os_threadx.h:270
static OS_Uint os_timer_create(OS_Timer *t, const char *name, void(*cb)(OS_Ulong), OS_Ulong id, OS_Ulong initial, OS_Ulong reload, OS_Uint auto_act)
Definition os_threadx.h:233
static int os_thread_is_live(const OS_Thread *t)
Definition os_threadx.h:172
TX_EVENT_FLAGS_GROUP OS_EventGroup
Definition os_threadx.h:31
static OS_Uint os_stream_buffer_send(OS_StreamBuffer *b, const void *data, size_t len, OS_Ulong wait)
Definition os_threadx.h:261
@ OS_AUTO_START
Definition os_threadx.h:45
@ OS_SUCCESS
Definition os_threadx.h:41
@ OS_EVENT_CLEAR
Definition os_threadx.h:44
@ OS_DONT_START
Definition os_threadx.h:46
@ OS_INHERIT
Definition os_threadx.h:47
@ OS_OR
Definition os_threadx.h:43
@ OS_WAIT_FOREVER
Definition os_threadx.h:39
@ OS_AND
Definition os_threadx.h:42
@ OS_NO_WAIT
Definition os_threadx.h:40
static OS_Uint os_thread_create(OS_Thread *t, const char *name, void(*entry)(OS_Ulong), OS_Ulong input, uint8_t *stack, OS_Ulong stack_size, OS_Uint priority, OS_Uint preempt, OS_Ulong slice, OS_Uint auto_start)
Definition os_threadx.h:52
unsigned int OS_Uint
Definition os_threadx.h:36
static void os_thread_clear(OS_Thread *t)
Definition os_threadx.h:176
TX_MUTEX OS_Mutex
Definition os_threadx.h:28
static OS_Uint os_queue_receive(OS_Queue *q, void *msg, OS_Ulong wait)
Definition os_threadx.h:207
static OS_Uint os_thread_delete(OS_Thread *t)
Definition os_threadx.h:86
TX_QUEUE OS_Queue
Definition os_threadx.h:30
static int os_get_current_core_id(void)
Definition os_threadx.h:81
static OS_Uint os_timer_deactivate(OS_Timer *t)
Definition os_threadx.h:253
static OS_Uint os_mutex_create(OS_Mutex *m, const char *name, OS_Uint inherit)
Definition os_threadx.h:119
unsigned long OS_Ulong
Definition os_threadx.h:35
static OS_Uint os_recursive_mutex_put(OS_Mutex *m)
Definition os_threadx.h:135
static OS_Uint os_recursive_mutex_delete(OS_Mutex *m)
Definition os_threadx.h:137
static OS_Uint os_stream_buffer_receive(OS_StreamBuffer *b, void *data, size_t len, OS_Ulong wait)
Definition os_threadx.h:263
static OS_Uint os_timer_activate(OS_Timer *t)
Definition os_threadx.h:251
static OS_Uint os_stream_buffer_delete(OS_StreamBuffer *b)
Definition os_threadx.h:259
static OS_Uint os_thread_terminate(OS_Thread *t)
Definition os_threadx.h:88
static OS_Uint os_thread_yield(void)
Definition os_threadx.h:112
static OS_Ulong os_time_get(void)
Definition os_threadx.h:50
TX_THREAD OS_Thread
Definition os_threadx.h:27
static OS_Uint os_event_group_create(OS_EventGroup *g, const char *name)
Definition os_threadx.h:210
static OS_Uint os_queue_send(OS_Queue *q, void *msg, OS_Ulong wait)
Definition os_threadx.h:205
static OS_Uint os_recursive_mutex_get(OS_Mutex *m, OS_Ulong wait)
Definition os_threadx.h:133
static OS_Uint os_thread_is_current(const OS_Thread *t)
Definition os_threadx.h:116
static OS_Uint os_thread_sleep(OS_Ulong ticks)
Definition os_threadx.h:110
static int os_thread_state_is_suspended(OS_Uint state)
Definition os_threadx.h:186
static OS_Uint os_timer_delete(OS_Timer *t)
Definition os_threadx.h:249
static OS_Uint os_stream_buffer_create(OS_StreamBuffer *b, size_t capacity, size_t trigger)
Definition os_threadx.h:257
static OS_Uint os_semaphore_delete(OS_Semaphore *s)
Definition os_threadx.h:144
static OS_Uint os_thread_info_get(OS_Thread *t, OS_Uint *state)
Definition os_threadx.h:90