35 #if !defined(_AR_CLASSES_H_) 36 #define _AR_CLASSES_H_ 41 #if defined(__cplusplus) 91 Thread(
const char * name,
ar_thread_entry_t entry,
void * param,
void * stack,
unsigned stackSize, uint8_t priority,
bool startImmediately=
true)
93 init(name, entry, param, stack, stackSize, priority, startImmediately);
113 Thread(
const char * name, T *
object,
void (T::*entry)(),
void * stack,
unsigned stackSize, uint8_t priority,
bool startImmediately=
true)
115 init<T>(name, object, entry, stack, stackSize, priority, startImmediately);
130 Thread(
const char * name,
ar_thread_entry_t entry,
void * param,
unsigned stackSize, uint8_t priority,
bool startImmediately=
true)
132 init(name, entry, param, NULL, stackSize, priority, startImmediately);
149 Thread(
const char * name, T *
object,
void (T::*entry)(),
unsigned stackSize, uint8_t priority,
bool startImmediately=
true)
151 init<T>(name, object, entry, NULL, stackSize, priority, startImmediately);
202 ar_status_t init(
const char * name, T *
object,
void (T::*entry)(),
void * stack,
unsigned stackSize, uint8_t priority,
bool startImmediately=
true)
204 return initForMemberFunction(name,
object, member_thread_entry<T>, &entry,
sizeof(entry), stack, stackSize, priority, startImmediately);
321 T * obj =
static_cast<T *
>(param);
322 void (T::*member)(void);
324 memcpy((
char*)&member, storage,
sizeof(member));
344 template <u
int32_t S>
359 ThreadWithStack(
const char * name, T *
object,
void (T::*entry)(), uint8_t priority,
bool startImmediately=
true)
361 Thread::init<T>(name, object, entry,
m_stack, S, priority, startImmediately);
372 ar_status_t init(
const char * name, T *
object,
void (T::*entry)(), uint8_t priority,
bool startImmediately=
true)
374 return Thread::init<T>(name, object, entry,
m_stack, S, priority, startImmediately);
425 const char *
getName()
const {
return m_name; }
511 const char *
getName()
const {
return m_name; }
545 ar_thread_t *
getOwner() {
return (ar_thread_t *)m_owner; }
629 template <
typename T>
663 friend T& operator <<= (T& lhs, TypedChannel<T>& rhs)
696 Queue(
const char * name,
void * storage,
unsigned elementSize,
unsigned capacity)
698 init(name, storage, elementSize, capacity);
710 ar_status_t init(
const char * name,
void * storage,
unsigned elementSize,
unsigned capacity)
793 template <
typename T,
unsigned N>
815 return Queue::send((
const void *)&element, timeout);
835 *resultStatus = status;
869 init(name, callback, param, timerMode, delay);
930 init(name,
object, callback, timerMode, delay);
957 T * obj =
static_cast<T *
>(param);
1117 #endif // defined(__cplusplus) 1119 #endif // _AR_CLASSES_H_ ar_status_t signal(uint32_t signal=0)
Send a signal to a runloop.
Definition: ar_classes.h:1076
void stop()
Stop the timer.
Definition: ar_classes.h:885
Thread.
Definition: ar_kernel.h:207
bool isLocked()
Returns whether the mutex is currently locked.
Definition: ar_classes.h:551
ar_status_t init(const char *name)
Create a new runloop.
Definition: ar_classes.h:1001
Mutex object.
Definition: ar_classes.h:486
const char * getName() const
Get the semaphore's name.
Definition: ar_classes.h:425
~RunLoop()
Runloop destructor.
Definition: ar_classes.h:988
ar_status_t send(const void *value, uint32_t timeout=kArInfiniteTimeout)
Send to channel.
Definition: ar_classes.h:611
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.
~Semaphore()
Destructor.
Definition: ar_classes.h:422
void invoke(T *obj)
Call the member function.
Definition: ar_classes.h:948
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.
bool isActive() const
Returns whether the timer is currently running.
Definition: ar_classes.h:888
ar_status_t perform(ar_runloop_function_t function, void *param=0)
Invoke a function on a runloop.
Definition: ar_classes.h:1065
Timer(const char *name, callback_t callback, void *param, ar_timer_mode_t timerMode, uint32_t delay)
Constructor.
Definition: ar_classes.h:867
Run loop.
Definition: ar_classes.h:974
Queue(const char *name, void *storage, unsigned elementSize, unsigned capacity)
Constructor.
Definition: ar_classes.h:696
ar_status_t ar_runloop_add_timer(ar_runloop_t *runloop, ar_timer_t *timer)
Associate a timer with a runloop.
const char * getName() const
Get the run loop's name.
Definition: ar_classes.h:1004
ar_status_t init(const char *name, uint32_t width=0)
Channel initialiser.
Definition: ar_classes.h:608
unsigned getCount() const
Returns the current number of elements in the queue.
Definition: ar_classes.h:752
Thread(const char *name, T *object, void(T::*entry)(), unsigned stackSize, uint8_t priority, bool startImmediately=true)
Constructor to set the thread entry to a member function, using a dynamic stack.
Definition: ar_classes.h:149
ar_status_t addQueue(ar_queue_t *queue, ar_runloop_queue_handler_t callback=NULL, void *param=NULL)
Add a queue to a runloop.
Definition: ar_classes.h:1104
friend T & operator>>(T &lhs, TypedChannel< T > &rhs)
Send to channel.
Definition: ar_classes.h:670
uint8_t m_stack[S]
Stack space for the thread.
Definition: ar_classes.h:378
ar_status_t send(T element, uint32_t timeout=kArInfiniteTimeout)
Add an item to the queue.
Definition: ar_classes.h:813
virtual void threadEntry(void *param)
Virtual thread entry point.
uint8_t getPriority() const
Return the thread's current priority.
Definition: ar_classes.h:267
const char * m_name
Name of the runloop.
Definition: ar_kernel.h:352
const char * getName() const
Get the timer's name.
Definition: ar_classes.h:879
ar_status_t get(uint32_t timeout=kArInfiniteTimeout)
Lock the mutex.
Definition: ar_classes.h:531
ar_status_t ar_timer_start(ar_timer_t *timer)
Start the timer running.
TypedChannel()
Default constructor.
Definition: ar_classes.h:634
void(* ar_thread_entry_t)(void *param)
Prototype for the thread entry point.
Definition: ar_kernel.h:141
Typed channel.
Definition: ar_classes.h:630
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.
Semaphore(const char *name, unsigned count=1)
Constructor.
Definition: ar_classes.h:402
Mutex(const char *name)
Constructor.
Definition: ar_classes.h:493
unsigned m_count
Current number of elements in the queue.
Definition: ar_kernel.h:313
void setDelay(uint32_t delay)
Adjust the timer's delay.
Definition: ar_classes.h:891
callback_t m_userCallback
The user timer callback.
Definition: ar_classes.h:898
Queue.
Definition: ar_kernel.h:306
ar_status_t ar_runloop_stop(ar_runloop_t *runloop)
Stop a runloop.
ar_status_t send(const void *element, uint32_t timeout=kArInfiniteTimeout)
Add an item to the queue.
Definition: ar_classes.h:734
const char * getName() const
Get the mutex's name.
Definition: ar_classes.h:511
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.
Mutex()
Default constructor.
Definition: ar_classes.h:490
void resume()
Make the thread eligible for execution.
Definition: ar_classes.h:239
ar_status_t ar_mutex_delete(ar_mutex_t *mutex)
Delete a mutex.
static void member_thread_entry(void *param)
Template function to invoke a thread entry point that is a member function.
Definition: ar_classes.h:318
T m_storage[N]
Static storage for the queue elements.
Definition: ar_classes.h:841
static void sleepUntil(unsigned wakeup)
Put the current thread to sleep until a specific time.
Definition: ar_classes.h:259
uint32_t ar_thread_get_load(ar_thread_t *thread)
Get the amount of CPU time the thread is using.
ar_status_t send(const T &value, uint32_t timeout=kArInfiniteTimeout)
Send to channel.
Definition: ar_classes.h:643
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's priority.
ar_status_t ar_mutex_put(ar_mutex_t *mutex)
Unlock the mutex.
ar_status_t ar_mutex_get(ar_mutex_t *mutex, uint32_t timeout)
Lock the mutex.
const char * getName() const
Get the queue's name.
Definition: ar_classes.h:719
~Timer()
Destructor.
Definition: ar_classes.h:873
const char * m_name
Name of the timer.
Definition: ar_kernel.h:331
ar_timer_mode_t
Modes of operation for timers.
Definition: ar_kernel.h:116
ar_status_t receive(T *element, uint32_t timeout=kArInfiniteTimeout)
Remove an item from the queue.
Definition: ar_classes.h:819
Header for the Argon RTOS C API.
~Queue()
Queue cleanup.
Definition: ar_classes.h:716
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
ar_status_t ar_runloop_delete(ar_runloop_t *runloop)
Destroy a runloop.
ar_status_t init(const char *name, unsigned count=1)
Initialiser.
Definition: ar_classes.h:416
Thread(const char *name, T *object, void(T::*entry)(), void *stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
Constructor to set the thread entry to a member function.
Definition: ar_classes.h:113
ar_status_t ar_runloop_create(ar_runloop_t *runloop, const char *name)
Create a new runloop.
ThreadWithStack(const char *name, T *object, void(T::*entry)(), uint8_t priority, bool startImmediately=true)
Constructor to set the thread entry to a member function.
Definition: ar_classes.h:359
A blocking queue for inter-thread messaging.
Definition: ar_classes.h:689
static ar_runloop_t * getCurrent(void)
Return the current runloop.
Definition: ar_classes.h:1112
ar_status_t receive(void *element, uint32_t timeout=kArInfiniteTimeout)
Remove an item from the queue.
Definition: ar_classes.h:746
ar_status_t put()
Unlock the mutex.
Definition: ar_classes.h:542
ar_status_t init(const char *name, T *object, void(T::*entry)(), uint8_t priority, bool startImmediately=true)
Initializer to set the thread entry to a member function.
Definition: ar_classes.h:372
bool isEmpty() const
Returns whether the queue is currently empty.
Definition: ar_classes.h:749
Utility class to automatically get and put a mutex.
Definition: ar_classes.h:562
uint8_t * m_allocatedStack
Dynamically allocated stack.
Definition: ar_classes.h:305
ThreadWithStack(const char *name, ar_thread_entry_t entry, void *param, uint8_t priority, bool startImmediately=true)
Constructor to use a normal function as entry poin.
Definition: ar_classes.h:352
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.
Timer()
Default constructor.
Definition: ar_classes.h:864
static void thread_entry(void *param)
Static thread entry callback to invoke the virtual method.
Thread(const char *name, ar_thread_entry_t entry, void *param, void *stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
Constructor.
Definition: ar_classes.h:91
Queue()
Default constructor.
Definition: ar_classes.h:693
ar_status_t ar_channel_send(ar_channel_t *channel, const void *value, uint32_t timeout)
Send to a channel.
TypedChannel(const char *name)
Constructor.
Definition: ar_classes.h:637
Timer.
Definition: ar_kernel.h:330
ar_status_t addTimer(ar_timer_t *timer)
Associate a timer with a runloop.
Definition: ar_classes.h:1088
callback_t m_memberCallback
The user timer callback.
Definition: ar_classes.h:945
uint32_t getStackUsed()
Get the thread's maximum stack usage.
Definition: ar_classes.h:292
T receive(uint32_t timeout=kArInfiniteTimeout, ar_status_t *resultStatus=NULL)
Alternate form of typed receive.
Definition: ar_classes.h:829
Timer object.
Definition: ar_classes.h:856
const char * m_name
Name of the queue.
Definition: ar_kernel.h:307
ar_thread_t * getOwner()
Returns the current owning thread, if there is one.
Definition: ar_classes.h:545
Semaphore()
Default constructor.
Definition: ar_classes.h:399
Channel(const char *name, uint32_t width=0)
Constructor.
Definition: ar_classes.h:602
ar_status_t put()
Release the semaphore.
Definition: ar_classes.h:459
Run loop.
Definition: ar_kernel.h:351
ar_status_t ar_semaphore_delete(ar_semaphore_t *sem)
Delete a semaphore.
const char * getName() const
Get the thread's name.
Definition: ar_classes.h:209
unsigned getCount() const
Returns the current semaphore count.
Definition: ar_classes.h:462
ar_status_t init(const char *name, T *object, void(T::*entry)(), void *stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
Initializer to set the thread entry to a member function.
Definition: ar_classes.h:202
Template class to help statically allocate a Queue.
Definition: ar_classes.h:794
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.
uint8_t m_priority
Thread priority. 0 is the lowest priority.
Definition: ar_kernel.h:212
uint32_t getDelay() const
Get the current delay for the timer.
Definition: ar_classes.h:894
ThreadWithStack()
Default constructor.
Definition: ar_classes.h:349
The Argon RTOS namespace.
Definition: ar_classes.h:44
ar_status_t ar_timer_delete(ar_timer_t *timer)
Delete a timer.
static void sleep(unsigned milliseconds)
Put the current thread to sleep for a certain amount of time.
Definition: ar_classes.h:250
void(T::* callback_t)(Timer *timer)
Timer callback function that takes an instance of this class.
Definition: ar_classes.h:922
Channel()
Default constructor.
Definition: ar_classes.h:599
T receive(uint32_t timeout=kArInfiniteTimeout)
Receive from channel.
Definition: ar_classes.h:649
ar_status_t init(const char *name, void *storage, unsigned elementSize, unsigned capacity)
Queue initialiser.
Definition: ar_classes.h:710
Pass this value to wait forever to acquire a resource.
Definition: ar_kernel.h:54
ar_status_t receive(T &value, uint32_t timeout=kArInfiniteTimeout)
Receive from channel.
Definition: ar_classes.h:657
Template to create a thread and its stack.
Definition: ar_classes.h:345
void ar_thread_sleep(uint32_t milliseconds)
Put the current thread to sleep for a certain amount of time.
static Thread * getCurrent()
Returns the currently running thread object.
Definition: ar_classes.h:300
void start()
Start the timer running.
Definition: ar_classes.h:882
ar_status_t init(const char *name)
Initialiser method.
Definition: ar_classes.h:807
const char * m_name
Thread name.
Definition: ar_kernel.h:210
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.
ar_status_t init(const char *name, ar_thread_entry_t entry, void *param, uint8_t priority, bool startImmediately=true)
Initializer to use a normal function as entry point.
Definition: ar_classes.h:365
ar_status_t receive(void *value, uint32_t timeout=kArInfiniteTimeout)
Receive from channel.
Definition: ar_classes.h:614
ar_status_t stop()
Stop a runloop.
Definition: ar_classes.h:1048
~Mutex()
Cleanup.
Definition: ar_classes.h:508
ar_status_t ar_channel_receive(ar_channel_t *channel, void *value, uint32_t timeout)
Receive from a channel.
Mutex & m_mutex
The mutex to hold.
Definition: ar_classes.h:579
Timer object taking a member function callback.
Definition: ar_classes.h:917
RunLoop()
Default constructor.
Definition: ar_classes.h:978
ar_status_t ar_thread_resume(ar_thread_t *thread)
Make the thread eligible for execution.
uint32_t getLoad()
Get the thread's system load.
Definition: ar_classes.h:289
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.
static void member_callback(Timer *timer, void *param)
Template function to invoke a callback that is a member function.
Definition: ar_classes.h:954
virtual ~Thread()
Destructor.
ar_status_t ar_semaphore_get(ar_semaphore_t *sem, uint32_t timeout)
Acquire the semaphore.
ar_status_t init(const char *name, ar_thread_entry_t entry, void *param, void *stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
Base initialiser.
~Guard()
Destructor that puts the mutex.
Definition: ar_classes.h:573
ar_thread_state_t m_state
Current thread state.
Definition: ar_kernel.h:213
Guard(Mutex &mutex)
Constructor which gets the mutex.
Definition: ar_classes.h:566
StaticQueue()
Default constructor.
Definition: ar_classes.h:798
ar_status_t init(const char *name, callback_t callback, void *param, ar_timer_mode_t timerMode, uint32_t delay)
Initialize the timer.
ar_status_t run(uint32_t timeout=kArInfiniteTimeout, ar_runloop_result_t *object=0)
Run a runloop for a period of time.
Definition: ar_classes.h:1035
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
ar_runloop_t * ar_runloop_get_current(void)
Return the current runloop.
static void timer_wrapper(ar_timer_t *timer, void *arg)
Converts the timer struct to an instance of this class.
Thread()
Default constructor.
Definition: ar_classes.h:74
Counting semaphore class.
Definition: ar_classes.h:395
ar_status_t init(const char *name)
Initialiser.
Definition: ar_classes.h:505
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.
ar_thread_entry_t m_userEntry
User-specified thread entry point function.
Definition: ar_classes.h:306
Channel.
Definition: ar_classes.h:595
void(* callback_t)(Timer *timer, void *param)
Timer callback function that takes an instance of this class.
Definition: ar_classes.h:861
ar_thread_t * ar_thread_get_current(void)
Returns the currently running thread object.
ar_status_t setPriority(uint8_t priority)
Change the thread's priority.
Definition: ar_classes.h:283
StaticQueue(const char *name)
Constructor.
Definition: ar_classes.h:801
ar_status_t init(const char *name)
Channel initialiser.
Definition: ar_classes.h:640
ar_status_t ar_timer_set_delay(ar_timer_t *timer, uint32_t newDelay)
Adjust the timer's delay.
Thread(const char *name, ar_thread_entry_t entry, void *param, unsigned stackSize, uint8_t priority, bool startImmediately=true)
Constructor to dynamically allocate the stack.
Definition: ar_classes.h:130
ar_status_t init(const char *name, T *object, callback_t callback, ar_timer_mode_t timerMode, uint32_t delay)
Initialize the timer with a member function callback.
Definition: ar_classes.h:937
Preemptive thread class.
Definition: ar_classes.h:70
RunLoop(const char *name)
Constructor to init the runloop.
Definition: ar_classes.h:982
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.
ar_thread_state_t getState() const
Return the current state of the thread.
Definition: ar_classes.h:242
ar_status_t initForMemberFunction(const char *name, void *object, ar_thread_entry_t entry, void *memberPointer, uint32_t memberPointerSize, void *stack, unsigned stackSize, uint8_t priority, bool startImmediately)
Special init method to deal with member functions.
void suspend()
Put thread in suspended state.
Definition: ar_classes.h:225
~Channel()
Destructor.
Definition: ar_classes.h:605