COSA
An Object-Oriented Platform for Arduino Programming
Thread.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_NUCLEO_THREAD_HH
22 #define COSA_NUCLEO_THREAD_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Linkage.hh"
26 #include <setjmp.h>
27 
28 namespace Nucleo {
29 
33 class Thread : public Link {
34 public:
39  static Thread* running()
40  {
41  return (s_running);
42  }
43 
50  static void begin(Thread* thread = NULL, size_t size = 0);
51 
62  virtual void run();
63 
69  void resume(Thread* thread);
70 
75  void yield()
76  __attribute__((always_inline))
77  {
78  Thread* thread = (Thread*) succ();
79  if (thread == this) thread = &s_main;
80  resume(thread);
81  }
82 
90  void delay(uint32_t ms);
91 
97  void enqueue(Head* queue, Thread* thread = NULL);
98 
105  void dequeue(Head* queue, bool flag = true);
106 
111  static void service();
112 
113 protected:
115  static const size_t MAIN_STACK_MAX = 64;
116 
118  static Head s_delayed;
119 
121  static Thread s_main;
122 
124  static Thread* s_running;
125 
127  static size_t s_top;
128 
130  jmp_buf m_context;
131 
133  uint32_t m_expires;
134 
140  void init(void* stack);
141 
143  friend class Semaphore;
144 };
145 
146 };
147 #endif
Definition: Actor.hh:26
void dequeue(Head *queue, bool flag=true)
Definition: Thread.cpp:107
void delay(uint32_t ms)
Definition: Thread.cpp:121
#define NULL
Definition: Types.h:101
static void begin(Thread *thread=NULL, size_t size=0)
Definition: Thread.cpp:57
static Head s_delayed
Definition: Thread.hh:118
jmp_buf m_context
Definition: Thread.hh:130
void init(void *stack)
Definition: Thread.cpp:49
static void service()
Definition: Thread.cpp:133
static Thread * running()
Definition: Thread.hh:39
uint32_t m_expires
Definition: Thread.hh:133
static Thread s_main
Definition: Thread.hh:121
static const size_t MAIN_STACK_MAX
Definition: Thread.hh:115
void resume(Thread *thread)
Definition: Thread.cpp:90
void enqueue(Head *queue, Thread *thread=NULL)
Definition: Thread.cpp:98
void yield()
Definition: Thread.hh:75
virtual void run()
Definition: Thread.cpp:72
Linkage * succ() const
Definition: Linkage.hh:51
static Thread * s_running
Definition: Thread.hh:124
static size_t s_top
Definition: Thread.hh:127
Definition: Linkage.hh:132