Argon RTOS  1.3.0
Tiny embedded real-time kernel
ar_classes.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2016 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_CLASSES_H_)
36 #define _AR_CLASSES_H_
37 
38 #include "ar_kernel.h"
39 #include <string.h>
40 
41 #if defined(__cplusplus)
42 
44 namespace Ar {
45 
46 //------------------------------------------------------------------------------
47 // Classes
48 //------------------------------------------------------------------------------
49 
70 class Thread : public _ar_thread
71 {
72 public:
74  Thread() {}
75 
91  Thread(const char * name, ar_thread_entry_t entry, void * param, void * stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
92  {
93  init(name, entry, param, stack, stackSize, priority, startImmediately);
94  }
95 
112  template <class T>
113  Thread(const char * name, T * object, void (T::*entry)(), void * stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
114  {
115  init<T>(name, object, entry, stack, stackSize, priority, startImmediately);
116  }
117 
130  Thread(const char * name, ar_thread_entry_t entry, void * param, unsigned stackSize, uint8_t priority, bool startImmediately=true)
131  {
132  init(name, entry, param, NULL, stackSize, priority, startImmediately);
133  }
134 
148  template <class T>
149  Thread(const char * name, T * object, void (T::*entry)(), unsigned stackSize, uint8_t priority, bool startImmediately=true)
150  {
151  init<T>(name, object, entry, NULL, stackSize, priority, startImmediately);
152  }
153 
155  virtual ~Thread();
156 
158 
159  ar_status_t init(const char * name, ar_thread_entry_t entry, void * param, void * stack, unsigned stackSize, uint8_t priority, bool startImmediately=true);
181 
201  template <class T>
202  ar_status_t init(const char * name, T * object, void (T::*entry)(), void * stack, unsigned stackSize, uint8_t priority, bool startImmediately=true)
203  {
204  return initForMemberFunction(name, object, member_thread_entry<T>, &entry, sizeof(entry), stack, stackSize, priority, startImmediately);
205  }
207 
209  const char * getName() const { return m_name; }
210 
214 
215  void suspend() { ar_thread_suspend(this); }
226 
239  void resume() { ar_thread_resume(this); }
240 
242  ar_thread_state_t getState() const { return m_state; }
243 
250  static void sleep(unsigned milliseconds) { ar_thread_sleep(milliseconds); }
251 
259  static void sleepUntil(unsigned wakeup) { ar_thread_sleep_until(wakeup); }
261 
265 
266  uint8_t getPriority() const { return m_priority; }
268 
283  ar_status_t setPriority(uint8_t priority) { return ar_thread_set_priority(this, priority); }
285 
287 
288  uint32_t getLoad() { return ar_thread_get_load(this); }
290 
292  uint32_t getStackUsed() { return ar_thread_get_stack_used(this); }
294 
298 
299  static Thread * getCurrent() { return static_cast<Thread *>(ar_thread_get_current()); }
302 
303 protected:
304 
305  uint8_t * m_allocatedStack;
307 
311  virtual void threadEntry(void * param);
312 
314  static void thread_entry(void * param);
315 
317  template <class T>
318  static void member_thread_entry(void * param)
319  {
320  Thread * thread = getCurrent();
321  T * obj = static_cast<T *>(param);
322  void (T::*member)(void);
323  uint32_t * storage = thread->m_stackBottom + 1; // Add 1 to skip over check value.
324  memcpy((char*)&member, storage, sizeof(member));
325  (obj->*member)();
326  }
327 
329  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);
330 
331 private:
333  Thread(const Thread & other);
334 
336  Thread& operator=(const Thread & other);
337 };
338 
344 template <uint32_t S>
345 class ThreadWithStack : public Thread
346 {
347 public:
350 
352  ThreadWithStack(const char * name, ar_thread_entry_t entry, void * param, uint8_t priority, bool startImmediately=true)
353  {
354  Thread::init(name, entry, param, m_stack, S, priority, startImmediately);
355  }
356 
358  template <class T>
359  ThreadWithStack(const char * name, T * object, void (T::*entry)(), uint8_t priority, bool startImmediately=true)
360  {
361  Thread::init<T>(name, object, entry, m_stack, S, priority, startImmediately);
362  }
363 
365  ar_status_t init(const char * name, ar_thread_entry_t entry, void * param, uint8_t priority, bool startImmediately=true)
366  {
367  return Thread::init(name, entry, param, m_stack, S, priority, startImmediately);
368  }
369 
371  template <class T>
372  ar_status_t init(const char * name, T * object, void (T::*entry)(), uint8_t priority, bool startImmediately=true)
373  {
374  return Thread::init<T>(name, object, entry, m_stack, S, priority, startImmediately);
375  }
376 
377 protected:
378  uint8_t m_stack[S];
379 
380 private:
382  ThreadWithStack(const ThreadWithStack<S> & other);
383 
385  ThreadWithStack& operator=(const ThreadWithStack<S> & other);
386 };
387 
395 class Semaphore : public _ar_semaphore
396 {
397 public:
400 
402  Semaphore(const char * name, unsigned count=1)
403  {
404  init(name, count);
405  }
406 
416  ar_status_t init(const char * name, unsigned count=1) { return ar_semaphore_create(this, name, count); }
417 
423 
425  const char * getName() const { return m_name; }
426 
450  ar_status_t get(uint32_t timeout=kArInfiniteTimeout) { return ar_semaphore_get(this, timeout); }
451 
459  ar_status_t put() { return ar_semaphore_put(this); }
460 
462  unsigned getCount() const { return m_count; }
463 
464 private:
466  Semaphore(const Semaphore & other);
467 
469  Semaphore& operator=(const Semaphore & other);
470 };
471 
486 class Mutex : public _ar_mutex
487 {
488 public:
490  Mutex() {}
491 
493  Mutex(const char * name)
494  {
495  init(name);
496  }
497 
505  ar_status_t init(const char * name) { return ar_mutex_create(this, name); }
506 
508  ~Mutex() { ar_mutex_delete(this); }
509 
511  const char * getName() const { return m_name; }
512 
531  ar_status_t get(uint32_t timeout=kArInfiniteTimeout) { return ar_mutex_get(this, timeout); }
532 
542  ar_status_t put() { return ar_mutex_put(this); }
543 
545  ar_thread_t * getOwner() { return (ar_thread_t *)m_owner; }
546 
551  bool isLocked() { return ar_mutex_is_locked(this); }
552 
562  class Guard
563  {
564  public:
566  Guard(Mutex & mutex)
567  : m_mutex(mutex)
568  {
570  }
571 
574  {
575  m_mutex.put();
576  }
577 
578  protected:
580  };
581 
582 private:
584  Mutex(const Mutex & other);
585 
587  Mutex& operator=(const Mutex & other);
588 };
589 
595 class Channel : public _ar_channel
596 {
597 public:
599  Channel() {}
600 
602  Channel(const char * name, uint32_t width=0) { init(name); }
603 
606 
608  ar_status_t init(const char * name, uint32_t width=0) { return ar_channel_create(this, name, width); }
609 
611  ar_status_t send(const void * value, uint32_t timeout=kArInfiniteTimeout) { return ar_channel_send(this, value, timeout); }
612 
614  ar_status_t receive(void * value, uint32_t timeout=kArInfiniteTimeout) { return ar_channel_receive(this, value, timeout); }
615 
616 private:
618  Channel(const Channel & other);
619 
621  Channel& operator=(const Channel & other);
622 };
623 
629 template <typename T>
630 class TypedChannel : public Channel
631 {
632 public:
635 
637  TypedChannel(const char * name) : Channel(name, sizeof(T)) {}
638 
640  ar_status_t init(const char * name) { return Channel::init(name, sizeof(T)); }
641 
643  ar_status_t send(const T & value, uint32_t timeout=kArInfiniteTimeout)
644  {
645  return Channel::send(&value, timeout);
646  }
647 
649  T receive(uint32_t timeout=kArInfiniteTimeout)
650  {
651  T temp;
652  Channel::receive(&temp, timeout);
653  return temp;
654  }
655 
657  ar_status_t receive(T & value, uint32_t timeout=kArInfiniteTimeout)
658  {
659  return Channel::receive(&value, timeout);
660  }
661 
663  friend T& operator <<= (T& lhs, TypedChannel<T>& rhs)
664  {
665  lhs = rhs.receive();
666  return lhs;
667  }
668 
670  friend T& operator >> (T& lhs, TypedChannel<T>& rhs)
671  {
672  rhs.send(lhs);
673  return lhs;
674  }
675 
676 private:
678  TypedChannel(const TypedChannel<T> & other);
679 
681  TypedChannel& operator=(const TypedChannel<T> & other);
682 };
683 
689 class Queue : public _ar_queue
690 {
691 public:
693  Queue() {}
694 
696  Queue(const char * name, void * storage, unsigned elementSize, unsigned capacity)
697  {
698  init(name, storage, elementSize, capacity);
699  }
700 
710  ar_status_t init(const char * name, void * storage, unsigned elementSize, unsigned capacity)
711  {
712  return ar_queue_create(this, name, storage, elementSize, capacity);
713  }
714 
716  ~Queue() { ar_queue_delete(this); }
717 
719  const char * getName() const { return m_name; }
720 
734  ar_status_t send(const void * element, uint32_t timeout=kArInfiniteTimeout) { return ar_queue_send(this, element, timeout); }
735 
746  ar_status_t receive(void * element, uint32_t timeout=kArInfiniteTimeout) { return ar_queue_receive(this, element, timeout); }
747 
749  bool isEmpty() const { return m_count == 0; }
750 
752  unsigned getCount() const { return m_count; }
753 
754 private:
756  Queue(const Queue & other);
757 
759  Queue& operator=(const Queue & other);
760 };
761 
793 template <typename T, unsigned N>
794 class StaticQueue : public Queue
795 {
796 public:
799 
801  StaticQueue(const char * name)
802  {
803  Queue::init(name, m_storage, sizeof(T), N);
804  }
805 
807  ar_status_t init(const char * name)
808  {
809  return Queue::init(name, m_storage, sizeof(T), N);
810  }
811 
813  ar_status_t send(T element, uint32_t timeout=kArInfiniteTimeout)
814  {
815  return Queue::send((const void *)&element, timeout);
816  }
817 
819  ar_status_t receive(T * element, uint32_t timeout=kArInfiniteTimeout)
820  {
821  return Queue::receive((void *)element, timeout);
822  }
823 
829  T receive(uint32_t timeout=kArInfiniteTimeout, ar_status_t * resultStatus=NULL)
830  {
831  T element;
832  ar_status_t status = Queue::receive((void *)&element, timeout);
833  if (resultStatus)
834  {
835  *resultStatus = status;
836  }
837  return element;
838  }
839 
840 protected:
841  T m_storage[N];
842 
843 private:
845  StaticQueue(const StaticQueue<T,N> & other);
846 
848  StaticQueue& operator=(const StaticQueue<T,N> & other);
849 };
850 
856 class Timer : public _ar_timer
857 {
858 public:
859 
861  typedef void (*callback_t)(Timer * timer, void * param);
862 
864  Timer() {}
865 
867  Timer(const char * name, callback_t callback, void * param, ar_timer_mode_t timerMode, uint32_t delay)
868  {
869  init(name, callback, param, timerMode, delay);
870  }
871 
873  ~Timer() { ar_timer_delete(this); }
874 
876  ar_status_t init(const char * name, callback_t callback, void * param, ar_timer_mode_t timerMode, uint32_t delay);
877 
879  const char * getName() const { return m_name; }
880 
882  void start() { ar_timer_start(this); }
883 
885  void stop() { ar_timer_stop(this); }
886 
888  bool isActive() const { return m_isActive; }
889 
891  void setDelay(uint32_t delay) { ar_timer_set_delay(this, delay); }
892 
894  uint32_t getDelay() const { return m_delay; }
895 
896 protected:
897 
899 
901  static void timer_wrapper(ar_timer_t * timer, void * arg);
902 
903 private:
905  Timer(const Timer & other);
906 
908  Timer& operator=(const Timer & other);
909 };
910 
916 template <class T>
918 {
919 public:
920 
922  typedef void (T::*callback_t)(Timer * timer);
923 
926 
928  TimerWithMemberCallback<T>(const char * name, T * object, callback_t callback, ar_timer_mode_t timerMode, uint32_t delay)
929  {
930  init(name, object, callback, timerMode, delay);
931  }
932 
935 
937  ar_status_t init(const char * name, T * object, callback_t callback, ar_timer_mode_t timerMode, uint32_t delay)
938  {
939  m_memberCallback = callback;
940  return Timer::init(name, member_callback, object, timerMode, delay);
941  }
942 
943 protected:
944 
946 
948  void invoke(T * obj)
949  {
950  (obj->*m_memberCallback)(this);
951  }
952 
954  static void member_callback(Timer * timer, void * param)
955  {
956  TimerWithMemberCallback<T> * _this = static_cast<TimerWithMemberCallback<T> *>(timer);
957  T * obj = static_cast<T *>(param);
958  _this->invoke(obj);
959  }
960 
961 private:
964 
966  TimerWithMemberCallback<T>& operator=(const TimerWithMemberCallback<T> & other);
967 };
968 
974 class RunLoop : public _ar_runloop
975 {
976 public:
978  RunLoop() {}
979 
982  RunLoop(const char * name)
983  {
984  init(name);
985  }
986 
989 
1001  ar_status_t init(const char * name) { return ar_runloop_create(this, name); }
1002 
1004  const char * getName() const { return m_name; }
1005 
1035  ar_status_t run(uint32_t timeout=kArInfiniteTimeout, ar_runloop_result_t * object=0) { return ar_runloop_run(this, timeout, object); }
1036 
1048  ar_status_t stop() { return ar_runloop_stop(this); }
1049 
1065  ar_status_t perform(ar_runloop_function_t function, void * param=0) { return ar_runloop_perform(this, function, param); }
1066 
1076  ar_status_t signal(uint32_t signal=0) { return ar_runloop_signal(this, signal); }
1077 
1088  ar_status_t addTimer(ar_timer_t * timer) { return ar_runloop_add_timer(this, timer); }
1089 
1104  ar_status_t addQueue(ar_queue_t * queue, ar_runloop_queue_handler_t callback=NULL, void * param=NULL) { return ar_runloop_add_queue(this, queue, callback, param); }
1105 
1112  static ar_runloop_t * getCurrent(void) { return ar_runloop_get_current(); }
1113 };
1114 
1115 } // namespace Ar
1116 
1117 #endif // defined(__cplusplus)
1118 
1119 #endif // _AR_CLASSES_H_
1120 //------------------------------------------------------------------------------
1121 // EOF
1122 //------------------------------------------------------------------------------
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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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&#39;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