Argon RTOS  1.3.0
Tiny embedded real-time kernel
ar_kernel.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2018 Immo Software
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice, this list
8  * of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice, this
11  * list of conditions and the following disclaimer in the documentation and/or
12  * other materials provided with the distribution.
13  *
14  * o Neither the name of the copyright holder nor the names of its contributors may
15  * be used to endorse or promote products derived from this software without
16  * specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
35 #if !defined(_AR_KERNEL_H_)
36 #define _AR_KERNEL_H_
37 
38 #include "ar_port.h"
39 #include "ar_config.h"
40 
41 //------------------------------------------------------------------------------
42 // Constants
43 //------------------------------------------------------------------------------
44 
46 #define AR_VERSION (0x00010300)
47 
52 {
54  kArInfiniteTimeout = 0xffffffffUL
55 };
56 
60 typedef enum _ar_status {
61  kArSuccess = 0,
80 } ar_status_t;
81 
86  kArStartThread = true,
88 };
89 
93 typedef enum _ar_thread_state {
102 
107 {
111 };
112 
116 typedef enum _ar_timer_modes {
120 
121 //------------------------------------------------------------------------------
122 // Types
123 //------------------------------------------------------------------------------
124 
125 // Forward declarations.
126 typedef struct _ar_thread ar_thread_t;
127 typedef struct _ar_channel ar_channel_t;
128 typedef struct _ar_queue ar_queue_t;
129 typedef struct _ar_timer ar_timer_t;
130 typedef struct _ar_runloop ar_runloop_t;
131 typedef struct _ar_list_node ar_list_node_t;
132 
134 
135 typedef bool (*ar_sort_predicate_t)(ar_list_node_t * a, ar_list_node_t * b);
137 
141 typedef void (*ar_thread_entry_t)(void * param);
142 
146 typedef void (*ar_timer_entry_t)(ar_timer_t * timer, void * param);
147 
149 typedef void (*ar_runloop_function_t)(void * param);
150 
152 typedef void (*ar_runloop_queue_handler_t)(ar_queue_t * queue, void * param);
153 
155 typedef void (*ar_runloop_channel_handler_t)(ar_channel_t * channel, void * param);
157 
159 
160 
164  ar_list_node_t * m_next;
165  ar_list_node_t * m_prev;
166  void * m_obj;
167 
168  // Internal utility methods.
169 #if defined(__cplusplus)
170  template <typename T> T * getObject() { return reinterpret_cast<T *>(m_obj); }
172  void insertBefore(ar_list_node_t * node);
173 #endif // __cplusplus
174 };
175 
179 typedef struct _ar_list {
180  ar_list_node_t * m_head;
182 
183  // Internal utility methods.
184 #if defined(__cplusplus)
185  template <typename T> T * getHead() { return m_head ? m_head->getObject<T>() : 0; }
187  inline bool isEmpty() const;
188  bool contains(ar_list_node_t * item);
189  void add(ar_list_node_t * item);
190  inline void add(ar_thread_t * item);
191  inline void add(ar_timer_t * item);
192  inline void add(ar_queue_t * item);
193  void remove(ar_list_node_t * item);
194  inline void remove(ar_thread_t * item);
195  inline void remove(ar_timer_t * item);
196  inline void remove(ar_queue_t * item);
197  void check();
198 #endif // __cplusplus
199 } ar_list_t;
201 
207 struct _ar_thread {
208  volatile uint8_t * m_stackPointer;
209  ar_thread_port_data_t m_portData;
210  const char * m_name;
211  uint32_t * m_stackBottom;
212  uint8_t m_priority;
215  ar_list_node_t m_threadNode;
216 #if AR_GLOBAL_OBJECT_LISTS
217  ar_list_node_t m_createdNode;
218 #endif // AR_GLOBAL_OBJECT_LISTS
219  ar_list_node_t m_blockedNode;
220  uint32_t m_wakeupTime;
222  void * m_channelData;
223  ar_runloop_t * m_runLoop;
224 #if AR_ENABLE_SYSTEM_LOAD
225  uint16_t m_permilleCpu;
226  uint32_t m_loadAccumulator;
227 #endif // AR_ENABLE_SYSTEM_LOAD
228  uint32_t * m_stackTop;
229  uint32_t m_uniqueId;
230 
231  // Internal utility methods.
232 #if defined(__cplusplus)
233  void block(ar_list_t & blockedList, uint32_t timeout);
234  void unblockWithStatus(ar_list_t & blockedList, ar_status_t unblockStatus);
235 #endif // __cplusplus
236 };
237 
246 typedef struct _ar_thread_status {
247  ar_thread_t * m_thread;
248  const char * m_name;
249  uint32_t m_uniqueId;
250  uint32_t m_cpu;
252  uint32_t m_maxStackUsed;
253  uint32_t m_stackSize;
255 
261 typedef struct _ar_semaphore {
262  const char * m_name;
263  volatile unsigned m_count;
265 #if AR_GLOBAL_OBJECT_LISTS
266  ar_list_node_t m_createdNode;
267 #endif // AR_GLOBAL_OBJECT_LISTS
269 
275 typedef struct _ar_mutex {
276  const char * m_name;
277  volatile ar_thread_t * m_owner;
278  volatile unsigned m_ownerLockCount;
281 #if AR_GLOBAL_OBJECT_LISTS
282  ar_list_node_t m_createdNode;
283 #endif // AR_GLOBAL_OBJECT_LISTS
284 } ar_mutex_t;
285 
291 struct _ar_channel {
292  const char * m_name;
293  uint32_t m_width;
296 #if AR_GLOBAL_OBJECT_LISTS
297  ar_list_node_t m_createdNode;
298 #endif // AR_GLOBAL_OBJECT_LISTS
299 };
300 
306 struct _ar_queue {
307  const char * m_name;
308  uint8_t * m_elements;
309  unsigned m_elementSize;
310  unsigned m_capacity;
311  unsigned m_head;
312  unsigned m_tail;
313  unsigned m_count;
316  ar_runloop_t * m_runLoop;
317  ar_list_node_t m_runLoopNode;
318  ar_runloop_queue_handler_t m_runLoopHandler;
320 #if AR_GLOBAL_OBJECT_LISTS
321  ar_list_node_t m_createdNode;
322 #endif // AR_GLOBAL_OBJECT_LISTS
323 };
324 
330 struct _ar_timer {
331  const char * m_name;
332  ar_list_node_t m_activeNode;
333 #if AR_GLOBAL_OBJECT_LISTS
334  ar_list_node_t m_createdNode;
335 #endif // AR_GLOBAL_OBJECT_LISTS
337  void * m_param;
339  bool m_isActive;
340  bool m_isRunning;
341  uint32_t m_delay;
342  uint32_t m_wakeupTime;
343  ar_runloop_t * m_runLoop;
344 };
345 
351 struct _ar_runloop {
352  const char * m_name;
353  ar_thread_t * m_thread;
357  ar_runloop_function_t function;
358  void * param;
360  volatile int32_t m_functionCount;
361  volatile int32_t m_functionHead;
362  volatile int32_t m_functionTail;
363  bool m_isRunning;
364  volatile bool m_stop;
365 #if AR_GLOBAL_OBJECT_LISTS
366  ar_list_node_t m_createdNode;
367 #endif // AR_GLOBAL_OBJECT_LISTS
368 };
369 
375 typedef union _ar_runloop_result {
376  ar_queue_t * m_queue;
377  uint32_t m_signal;
379 
380 //------------------------------------------------------------------------------
381 // API
382 //------------------------------------------------------------------------------
383 
384 #if defined(__cplusplus)
385 extern "C" {
386 #endif
387 
390 
392 
393 
400 void ar_kernel_run(void);
401 
405 bool ar_kernel_is_running(void);
406 
415 uint32_t ar_get_system_load(void);
417 
419 
422 
424 
425 
451 ar_status_t ar_thread_create(ar_thread_t * thread, const char * name, ar_thread_entry_t entry, void * param, void * stack, unsigned stackSize, uint8_t priority, bool startImmediately);
452 
458 ar_status_t ar_thread_delete(ar_thread_t * thread);
459 
472 ar_status_t ar_thread_suspend(ar_thread_t * thread);
473 
488 ar_status_t ar_thread_resume(ar_thread_t * thread);
489 
495 ar_thread_state_t ar_thread_get_state(ar_thread_t * thread);
496 
502 uint8_t ar_thread_get_priority(ar_thread_t * thread);
503 
521 ar_status_t ar_thread_set_priority(ar_thread_t * thread, uint8_t newPriority);
522 
528 ar_thread_t * ar_thread_get_current(void);
529 
536 ar_runloop_t * ar_thread_get_runloop(ar_thread_t * thread);
537 
549 void ar_thread_sleep(uint32_t milliseconds);
550 
562 void ar_thread_sleep_until(uint32_t wakeup);
563 
569 const char * ar_thread_get_name(ar_thread_t * thread);
570 
578 uint32_t ar_thread_get_load(ar_thread_t * thread);
579 
587 uint32_t ar_thread_get_stack_used(ar_thread_t * thread);
588 
596 uint32_t ar_thread_get_report(ar_thread_status_t report[], uint32_t maxEntries);
598 
600 
603 
605 
606 
618 ar_status_t ar_semaphore_create(ar_semaphore_t * sem, const char * name, unsigned count);
619 
630 
657 ar_status_t ar_semaphore_get(ar_semaphore_t * sem, uint32_t timeout);
658 
669 
675 uint32_t ar_semaphore_get_count(ar_semaphore_t * sem);
676 
682 const char * ar_semaphore_get_name(ar_semaphore_t * sem);
684 
686 
689 
691 
692 
702 ar_status_t ar_mutex_create(ar_mutex_t * mutex, const char * name);
703 
712 
739 ar_status_t ar_mutex_get(ar_mutex_t * mutex, uint32_t timeout);
740 
761 
767 bool ar_mutex_is_locked(ar_mutex_t * mutex);
768 
774 ar_thread_t * ar_mutex_get_owner(ar_mutex_t * mutex);
775 
781 const char * ar_mutex_get_name(ar_mutex_t * mutex);
783 
785 
788 
790 
791 
799 ar_status_t ar_channel_create(ar_channel_t * channel, const char * name, uint32_t width);
800 
806 ar_status_t ar_channel_delete(ar_channel_t * channel);
807 
819 ar_status_t ar_channel_send(ar_channel_t * channel, const void * value, uint32_t timeout);
820 
829 ar_status_t ar_channel_receive(ar_channel_t * channel, void * value, uint32_t timeout);
830 
836 const char * ar_channel_get_name(ar_channel_t * channel);
838 
840 
843 
845 
846 
858 ar_status_t ar_queue_create(ar_queue_t * queue, const char * name, void * storage, unsigned elementSize, unsigned capacity);
859 
865 ar_status_t ar_queue_delete(ar_queue_t * queue);
866 
883 ar_status_t ar_queue_send(ar_queue_t * queue, const void * element, uint32_t timeout);
884 
898 ar_status_t ar_queue_receive(ar_queue_t * queue, void * element, uint32_t timeout);
899 
907 bool ar_queue_is_empty(ar_queue_t * queue);
908 
916 uint32_t ar_queue_get_count(ar_queue_t * queue);
917 
925 const char * ar_queue_get_name(ar_queue_t * queue);
927 
929 
932 
934 
935 
949 ar_status_t ar_timer_create(ar_timer_t * timer, const char * name, ar_timer_entry_t callback, void * param, ar_timer_mode_t timerMode, uint32_t delay);
950 
958 ar_status_t ar_timer_delete(ar_timer_t * timer);
959 
967 ar_status_t ar_timer_start(ar_timer_t * timer);
968 
976 ar_status_t ar_timer_stop(ar_timer_t * timer);
977 
986 bool ar_timer_is_active(ar_timer_t * timer);
987 
996 ar_status_t ar_timer_set_delay(ar_timer_t * timer, uint32_t newDelay);
997 
1005 uint32_t ar_timer_get_delay(ar_timer_t * timer);
1006 
1014 const char * ar_timer_get_name(ar_timer_t * timer);
1016 
1018 
1021 
1023 
1024 
1038 ar_status_t ar_runloop_create(ar_runloop_t * runloop, const char * name);
1039 
1052 ar_status_t ar_runloop_delete(ar_runloop_t * runloop);
1053 
1085 ar_status_t ar_runloop_run(ar_runloop_t * runloop, uint32_t timeout, ar_runloop_result_t * object);
1086 
1101 ar_status_t ar_runloop_stop(ar_runloop_t * runloop);
1102 
1119 ar_status_t ar_runloop_perform(ar_runloop_t * runloop, ar_runloop_function_t function, void * param);
1120 
1132 ar_status_t ar_runloop_signal(ar_runloop_t * runloop, uint32_t signal);
1133 
1145 ar_status_t ar_runloop_add_timer(ar_runloop_t * runloop, ar_timer_t * timer);
1146 
1162 ar_status_t ar_runloop_add_queue(ar_runloop_t * runloop, ar_queue_t * queue, ar_runloop_queue_handler_t callback, void * param);
1163 
1170 ar_runloop_t * ar_runloop_get_current(void);
1171 
1178 const char * ar_runloop_get_name(ar_runloop_t * runloop);
1179 
1181 
1185 
1187 
1188 
1191 uint32_t ar_get_tick_count(void);
1192 
1198 uint32_t ar_get_millisecond_count(void);
1199 
1204 uint64_t ar_get_microseconds();
1205 
1209 uint32_t ar_get_milliseconds_per_tick(void);
1210 
1214 static inline uint32_t ar_ticks_to_milliseconds(uint32_t ticks) { return ticks * ar_get_milliseconds_per_tick(); }
1215 
1219 static inline uint32_t ar_milliseconds_to_ticks(uint32_t milliseconds) { return milliseconds / ar_get_milliseconds_per_tick(); }
1221 
1223 
1226 
1228 
1229 
1238 int8_t ar_atomic_add8(volatile int8_t * value, int8_t delta);
1239 
1249 int16_t ar_atomic_add16(volatile int16_t * value, int16_t delta);
1250 
1260 int32_t ar_atomic_add32(volatile int32_t * value, int32_t delta);
1261 
1279 bool ar_atomic_cas8(volatile int8_t * value, int8_t expectedValue, int8_t newValue);
1280 
1298 bool ar_atomic_cas16(volatile int16_t * value, int16_t expectedValue, int16_t newValue);
1299 
1317 bool ar_atomic_cas32(volatile int32_t * value, int32_t expectedValue, int32_t newValue);
1319 
1321 
1322 #if defined(__cplusplus)
1323 }
1324 #endif
1325 
1327 
1328 #endif // _AR_KERNEL_H_
1329 //------------------------------------------------------------------------------
1330 // EOF
1331 //------------------------------------------------------------------------------
uint64_t ar_get_microseconds()
Get a microsecond timestamp.
Thread.
Definition: ar_kernel.h:207
const char * m_name
Name of the mutex.
Definition: ar_kernel.h:276
bool ar_queue_is_empty(ar_queue_t *queue)
Returns whether the queue is currently empty.
uint32_t ar_thread_get_report(ar_thread_status_t report[], uint32_t maxEntries)
Get a report of all thread&#39;s status.
ar_list_t m_blockedReceivers
List of blocked receiver threads.
Definition: ar_kernel.h:295
ar_status_t
Argon status and error codes.
Definition: ar_kernel.h:60
bool ar_mutex_is_locked(ar_mutex_t *mutex)
Returns whether the mutex is currently locked.
bool m_isRunning
Whether the timer callback is executing.
Definition: ar_kernel.h:340
ar_status_t ar_queue_delete(ar_queue_t *queue)
Delete an existing queue.
ar_status_t ar_mutex_create(ar_mutex_t *mutex, const char *name)
Create a new mutex object.
Hopefully a thread is never in this state.
Definition: ar_kernel.h:94
volatile int32_t m_functionCount
Number of functions in the queue.
Definition: ar_kernel.h:360
uint32_t ar_get_system_load(void)
Returns the current system load.
volatile ar_thread_t * m_owner
Current owner thread of the mutex.
Definition: ar_kernel.h:277
uint32_t ar_timer_get_delay(ar_timer_t *timer)
Get the current delay for the timer.
Linked list node.
Definition: ar_kernel.h:163
static uint32_t ar_milliseconds_to_ticks(uint32_t milliseconds)
Convert milliseconds to ticks.
Definition: ar_kernel.h:1219
uint32_t ar_get_tick_count(void)
Return the current time in ticks.
ar_status_t ar_runloop_add_timer(ar_runloop_t *runloop, ar_timer_t *timer)
Associate a timer with a runloop.
ar_list_node_t m_threadNode
Main thread list node.
Definition: ar_kernel.h:215
ar_runloop_t * ar_thread_get_runloop(ar_thread_t *thread)
Get the runloop currently associated with the given thread.
void * m_param
Arbitrary parameter for the callback.
Definition: ar_kernel.h:337
uint32_t ar_queue_get_count(ar_queue_t *queue)
Returns the current number of elements in the queue.
const char * ar_semaphore_get_name(ar_semaphore_t *sem)
Get the semaphore&#39;s name.
ar_list_t m_blockedList
List of threads blocked on the semaphore.
Definition: ar_kernel.h:264
volatile bool m_stop
Flag to force the runloop to stop.
Definition: ar_kernel.h:364
The timer is not running.
Definition: ar_kernel.h:72
uint8_t m_originalPriority
Original priority of the owner thread before its priority was raised.
Definition: ar_kernel.h:279
void ar_kernel_run(void)
Start the kernel running.
ar_list_t m_blockedSenders
List of blocked sender threads.
Definition: ar_kernel.h:294
bool ar_kernel_is_running(void)
Returns whether the kernel is running or not.
Automatically run the thread.
Definition: ar_kernel.h:86
ar_status_t ar_timer_create(ar_timer_t *timer, const char *name, ar_timer_entry_t callback, void *param, ar_timer_mode_t timerMode, uint32_t delay)
Create a new timer.
uint32_t m_wakeupTime
Expiration time in ticks.
Definition: ar_kernel.h:342
const char * m_name
Name of the runloop.
Definition: ar_kernel.h:352
The timer is not associated with a run loop.
Definition: ar_kernel.h:73
ar_list_node_t * m_head
Pointer to the head of the list. Will be NULL if the list is empty.
Definition: ar_kernel.h:180
ar_status_t ar_timer_start(ar_timer_t *timer)
Start the timer running.
bool ar_timer_is_active(ar_timer_t *timer)
Returns whether the timer is currently running.
bool(* ar_sort_predicate_t)(ar_list_node_t *a, ar_list_node_t *b)
Function type used for sorting object lists.
Definition: ar_kernel.h:136
Timer fires a single time.
Definition: ar_kernel.h:117
void(* ar_thread_entry_t)(void *param)
Prototype for the thread entry point.
Definition: ar_kernel.h:141
ar_thread_port_data_t m_portData
Port-specific thread data.
Definition: ar_kernel.h:209
ar_status_t ar_channel_delete(ar_channel_t *channel)
Delete an existing channel.
ar_status_t ar_queue_create(ar_queue_t *queue, const char *name, void *storage, unsigned elementSize, unsigned capacity)
Create a new queue.
ar_thread_state_t m_state
Current thread state.
Definition: ar_kernel.h:251
const char * ar_timer_get_name(ar_timer_t *timer)
Get the timer&#39;s name.
ar_thread_entry_t m_entry
Function pointer for the thread&#39;s entry point.
Definition: ar_kernel.h:214
volatile unsigned m_count
Current semaphore count. Value of 0 means the semaphore is owned.
Definition: ar_kernel.h:263
unsigned m_count
Current number of elements in the queue.
Definition: ar_kernel.h:313
Create the thread suspended.
Definition: ar_kernel.h:87
ar_queue_t * m_queue
Queue that received an item.
Definition: ar_kernel.h:376
The mutex is already unlocked.
Definition: ar_kernel.h:70
Queue.
Definition: ar_kernel.h:306
The thread&#39;s stack size is too small.
Definition: ar_kernel.h:67
Thread is sleeping.
Definition: ar_kernel.h:99
ar_status_t ar_runloop_stop(ar_runloop_t *runloop)
Stop a runloop.
ar_status_t ar_thread_delete(ar_thread_t *thread)
Delete a thread.
The requested operation cannot be performed from interrupt context.
Definition: ar_kernel.h:68
ar_status_t ar_channel_create(ar_channel_t *channel, const char *name, uint32_t width)
Create a new channel.
ar_status_t ar_thread_suspend(ar_thread_t *thread)
Put thread in suspended state.
Definition: ar_kernel.h:356
unsigned m_head
Index of queue head.
Definition: ar_kernel.h:311
ar_status_t ar_mutex_delete(ar_mutex_t *mutex)
Delete a mutex.
uint32_t m_maxStackUsed
Maximum number of bytes used in the thread&#39;s stack.
Definition: ar_kernel.h:252
Priority value for the highest priority user thread.
Definition: ar_kernel.h:110
volatile unsigned m_ownerLockCount
Number of times the owner thread has locked the mutex.
Definition: ar_kernel.h:278
bool ar_atomic_cas16(volatile int16_t *value, int16_t expectedValue, int16_t newValue)
Atomic 16-bit compare and swap operation.
uint32_t ar_thread_get_load(ar_thread_t *thread)
Get the amount of CPU time the thread is using.
ar_status_t ar_queue_send(ar_queue_t *queue, const void *element, uint32_t timeout)
Add an item to the queue.
ar_status_t ar_thread_set_priority(ar_thread_t *thread, uint8_t newPriority)
Change a thread&#39;s priority.
ar_status_t ar_mutex_put(ar_mutex_t *mutex)
Unlock the mutex.
#define AR_RUNLOOP_FUNCTION_QUEUE_SIZE
Maximum number of functions queued in a run loop.
Definition: ar_config.h:157
uint32_t ar_semaphore_get_count(ar_semaphore_t *sem)
Returns the current semaphore count.
The thread is currently running.
Definition: ar_kernel.h:97
ar_status_t ar_mutex_get(ar_mutex_t *mutex, uint32_t timeout)
Lock the mutex.
Linked list.
Definition: ar_kernel.h:179
const char * m_name
Name of the timer.
Definition: ar_kernel.h:331
bool ar_atomic_cas32(volatile int32_t *value, int32_t expectedValue, int32_t newValue)
Atomic 32-bit compare and swap operation.
ar_timer_mode_t
Modes of operation for timers.
Definition: ar_kernel.h:116
_ar_thread_start
Options for creating a new thread.
Definition: ar_kernel.h:85
ar_status_t ar_semaphore_create(ar_semaphore_t *sem, const char *name, unsigned count)
Create a new semaphore.
Channel.
Definition: ar_kernel.h:291
uint32_t m_uniqueId
Unique ID for this thread.
Definition: ar_kernel.h:249
ar_status_t ar_runloop_delete(ar_runloop_t *runloop)
Destroy a runloop.
ar_thread_t * ar_mutex_get_owner(ar_mutex_t *mutex)
Returns the current owning thread, if there is one.
ar_status_t ar_runloop_create(ar_runloop_t *runloop, const char *name)
Create a new runloop.
Timer repeatedly fires every time the interval elapses.
Definition: ar_kernel.h:118
uint32_t ar_get_milliseconds_per_tick(void)
Get the number of milliseconds per tick.
unsigned m_elementSize
Number of bytes occupied by each element.
Definition: ar_kernel.h:309
unsigned m_capacity
Maximum number of elements the queue can hold.
Definition: ar_kernel.h:310
Allocation failed.
Definition: ar_kernel.h:74
Thread is eligible to be run.
Definition: ar_kernel.h:96
bool ar_atomic_cas8(volatile int8_t *value, int8_t expectedValue, int8_t newValue)
Atomic 8-bit compare and swap operation.
ar_status_t ar_runloop_add_queue(ar_runloop_t *runloop, ar_queue_t *queue, ar_runloop_queue_handler_t callback, void *param)
Add a queue to a runloop.
Thread has exited.
Definition: ar_kernel.h:100
ar_status_t ar_channel_send(ar_channel_t *channel, const void *value, uint32_t timeout)
Send to a channel.
void * m_obj
Pointer to the object on the list.
Definition: ar_kernel.h:166
Timer.
Definition: ar_kernel.h:330
ar_runloop_t * m_runLoop
Runloop to which the timer is bound.
Definition: ar_kernel.h:343
ar_thread_t * m_thread
Thread the runloop is running on. NULL when the runloop is not running.
Definition: ar_kernel.h:353
unsigned m_tail
Index of queue tail.
Definition: ar_kernel.h:312
const char * m_name
Name of the queue.
Definition: ar_kernel.h:307
bool m_isRunning
Whether the runloop is currently running.
Definition: ar_kernel.h:363
ar_sort_predicate_t m_predicate
Sort predicate to use for this list. Items are added to the end if NULL.
Definition: ar_kernel.h:181
Run loop.
Definition: ar_kernel.h:351
ar_status_t ar_semaphore_delete(ar_semaphore_t *sem)
Delete a semaphore.
uint32_t m_signal
Value of received signal.
Definition: ar_kernel.h:377
const char * m_name
Thread&#39;s name.
Definition: ar_kernel.h:248
ar_list_t m_sendBlockedList
List of threads blocked waiting to send.
Definition: ar_kernel.h:314
Thread is not eligible for execution.
Definition: ar_kernel.h:95
The idle thread&#39;s priority. No other thread is allowed to have this priority.
Definition: ar_kernel.h:108
Priority value for the lowest priority user thread.
Definition: ar_kernel.h:109
ar_list_node_t m_runLoopNode
List node for the runloop&#39;s queue list.
Definition: ar_kernel.h:317
Run loop result.
Definition: ar_kernel.h:375
ar_status_t ar_runloop_perform(ar_runloop_t *runloop, ar_runloop_function_t function, void *param)
Invoke a function on a runloop.
ar_status_t ar_queue_receive(ar_queue_t *queue, void *element, uint32_t timeout)
Remove an item from the queue.
uint32_t * m_stackTop
Saved stack top address for computing stack usage.
Definition: ar_kernel.h:228
uint8_t m_priority
Thread priority. 0 is the lowest priority.
Definition: ar_kernel.h:212
uint32_t ar_get_millisecond_count(void)
Return the current time in milliseconds.
ar_status_t ar_timer_delete(ar_timer_t *timer)
Delete a timer.
Operation was successful.
Definition: ar_kernel.h:61
uint32_t m_wakeupTime
Tick count when a sleeping thread will awaken.
Definition: ar_kernel.h:220
The queue is at maximum capacity and cannot accept more elements.
Definition: ar_kernel.h:64
The runloop is already running on another thread.
Definition: ar_kernel.h:77
struct _ar_runloop::_ar_runloop_function_info m_functions[AR_RUNLOOP_FUNCTION_QUEUE_SIZE]
Function queue.
Configuration settings for the Argon RTOS.
static uint32_t ar_ticks_to_milliseconds(uint32_t ticks)
Convert ticks to milliseconds.
Definition: ar_kernel.h:1214
uint32_t m_uniqueId
Unique ID for this thread.
Definition: ar_kernel.h:229
int8_t ar_atomic_add8(volatile int8_t *value, int8_t delta)
Atomic 8-bit add operation.
The caller is not the owning thread.
Definition: ar_kernel.h:69
ar_list_t m_timers
Timers associated with the runloop.
Definition: ar_kernel.h:354
int16_t ar_atomic_add16(volatile int16_t *value, int16_t delta)
Atomic 16-bit add operation.
Pass this value to wait forever to acquire a resource.
Definition: ar_kernel.h:54
void insertBefore(ar_list_node_t *node)
Insert this node before another node on the list.
void(* ar_timer_entry_t)(ar_timer_t *timer, void *param)
Callback routine for timer expiration.
Definition: ar_kernel.h:146
The thread is blocked on another object.
Definition: ar_kernel.h:98
void ar_thread_sleep(uint32_t milliseconds)
Put the current thread to sleep for a certain amount of time.
_ar_thread_priorities
Range of priorities for threads.
Definition: ar_kernel.h:106
ar_list_node_t * m_next
Next node in the list.
Definition: ar_kernel.h:164
The object is already attached to a runloop.
Definition: ar_kernel.h:76
ar_timer_mode_t m_mode
One-shot or periodic mode.
Definition: ar_kernel.h:338
const char * ar_mutex_get_name(ar_mutex_t *mutex)
Get the mutex&#39;s name.
uint8_t ar_thread_get_priority(ar_thread_t *thread)
Return the thread&#39;s current priority.
ar_list_node_t m_blockedNode
Blocked list node.
Definition: ar_kernel.h:219
_ar_timeouts
Timeout constants.
Definition: ar_kernel.h:51
const char * m_name
Thread name.
Definition: ar_kernel.h:210
Counting semaphore.
Definition: ar_kernel.h:261
ar_status_t ar_runloop_run(ar_runloop_t *runloop, uint32_t timeout, ar_runloop_result_t *object)
Run a runloop for a period of time.
uint8_t * m_elements
Pointer to element storage.
Definition: ar_kernel.h:308
ar_thread_t * m_thread
Pointer to the thread&#39;s structure.
Definition: ar_kernel.h:247
ar_list_t m_blockedList
List of threads blocked on the mutex.
Definition: ar_kernel.h:280
ar_status_t ar_channel_receive(ar_channel_t *channel, void *value, uint32_t timeout)
Receive from a channel.
Current status of a thread.
Definition: ar_kernel.h:246
An object was deleted while a thread was blocked on it. This may be a semaphore, mutex, or queue.
Definition: ar_kernel.h:63
uint32_t m_cpu
Per mille CPU usage of the thread over the last sampling period, with a range of 1-1000.
Definition: ar_kernel.h:250
volatile int32_t m_functionTail
Function queue tail.
Definition: ar_kernel.h:362
ar_list_node_t * m_prev
Previous node in the list.
Definition: ar_kernel.h:165
The runloop was stopped.
Definition: ar_kernel.h:78
ar_status_t ar_thread_resume(ar_thread_t *thread)
Make the thread eligible for execution.
ar_list_t m_receiveBlockedList
List of threads blocked waiting to receive data.
Definition: ar_kernel.h:315
ar_status_t ar_semaphore_put(ar_semaphore_t *sem)
Release the semaphore.
ar_status_t ar_timer_stop(ar_timer_t *timer)
Stop the timer.
volatile int32_t m_functionHead
Function queue head.
Definition: ar_kernel.h:361
ar_status_t ar_semaphore_get(ar_semaphore_t *sem, uint32_t timeout)
Acquire the semaphore.
ar_list_node_t m_activeNode
Node for the list of active timers.
Definition: ar_kernel.h:332
ar_status_t ar_thread_create(ar_thread_t *thread, const char *name, ar_thread_entry_t entry, void *param, void *stack, unsigned stackSize, uint8_t priority, bool startImmediately)
Create a new thread.
volatile uint8_t * m_stackPointer
Current stack pointer.
Definition: ar_kernel.h:208
ar_thread_state_t m_state
Current thread state.
Definition: ar_kernel.h:213
An invalid parameter value was passed to the function.
Definition: ar_kernel.h:71
int32_t ar_atomic_add32(volatile int32_t *value, int32_t delta)
Atomic 32-bit add operation.
const char * ar_queue_get_name(ar_queue_t *queue)
Get the queue&#39;s name.
ar_status_t m_unblockStatus
Status code to return from a blocking function upon unblocking.
Definition: ar_kernel.h:221
uint32_t m_width
Size in bytes of the channel&#39;s data.
Definition: ar_kernel.h:293
ar_runloop_t * m_runLoop
Runloop the queue is bound to.
Definition: ar_kernel.h:316
uint32_t m_stackSize
Total bytes allocated to the thread&#39;s stack.
Definition: ar_kernel.h:253
const char * m_name
Name of the semaphore.
Definition: ar_kernel.h:262
const char * ar_thread_get_name(ar_thread_t *thread)
Get the thread&#39;s name.
ar_timer_entry_t m_callback
Timer expiration callback.
Definition: ar_kernel.h:336
const char * m_name
Name of the channel.
Definition: ar_kernel.h:292
void ar_thread_sleep_until(uint32_t wakeup)
Put the current thread to sleep until a specific time.
uint32_t m_delay
Delay in ticks.
Definition: ar_kernel.h:341
void * m_runLoopHandlerParam
User parameter for handler function.
Definition: ar_kernel.h:319
ar_runloop_t * ar_runloop_get_current(void)
Return the current runloop.
ar_runloop_t * m_runLoop
Run loop associated with this thread.
Definition: ar_kernel.h:223
Return immediately if a resource cannot be acquired.
Definition: ar_kernel.h:53
Mutex.
Definition: ar_kernel.h:275
No elements are in the queue.
Definition: ar_kernel.h:65
T * getObject()
Convert the m_obj pointer to a particular type.
Definition: ar_kernel.h:171
ar_thread_state_t
Potential thread states.
Definition: ar_kernel.h:93
ar_status_t ar_runloop_signal(ar_runloop_t *runloop, uint32_t signal)
Send a signal to a runloop.
const char * ar_runloop_get_name(ar_runloop_t *runloop)
Return the runloop&#39;s name.
The requested thread priority is invalid.
Definition: ar_kernel.h:66
const char * ar_channel_get_name(ar_channel_t *channel)
Get a channel&#39;s name.
void * param
User parameter passed to the callback.
Definition: ar_kernel.h:358
void * m_channelData
Receive or send data pointer for blocked channel.
Definition: ar_kernel.h:222
ar_list_t m_queues
Queues associated with the runloop.
Definition: ar_kernel.h:355
ar_thread_t * ar_thread_get_current(void)
Returns the currently running thread object.
ar_runloop_queue_handler_t m_runLoopHandler
Handler function.
Definition: ar_kernel.h:318
ar_status_t ar_timer_set_delay(ar_timer_t *timer, uint32_t newDelay)
Adjust the timer&#39;s delay.
Timeout while blocked on an object.
Definition: ar_kernel.h:62
The runloop exited due to a value received on an associated queue.
Definition: ar_kernel.h:79
bool m_isActive
Whether the timer is running and on the active timers list.
Definition: ar_kernel.h:339
uint32_t * m_stackBottom
Beginning of stack.
Definition: ar_kernel.h:211
uint32_t ar_thread_get_stack_used(ar_thread_t *thread)
Get the maximum stack usage of the specified thread.
The thread is an invalid state for the given operation.
Definition: ar_kernel.h:75
ar_thread_state_t ar_thread_get_state(ar_thread_t *thread)
Return the current state of the thread.