RUI3 (RAK Unified Interface 3) - RAK4631
RAKThread.h
Go to the documentation of this file.
1 #ifndef __RAK_THREAD_H__
2 #define __RAK_THREAD_H__
3 
4 #define LABEL_INIT(s) s = NULL
5 
6 #define LABEL_RESUME(s) \
7  do { \
8  if(s != NULL) { \
9  goto *s; \
10  } \
11  } while(0)
12 
13 #define LABEL_CONCAT2(s1, s2) s1##s2
14 #define LABEL_CONCAT(s1, s2) LABEL_CONCAT2(s1, s2)
15 
16 #define LABEL_SET(s) \
17  do { \
18  LABEL_CONCAT(LABEL_LABEL, __LINE__): \
19  (s) = &&LABEL_CONCAT(LABEL_LABEL, __LINE__); \
20  } while(0)
21 
22 #define LABEL_END(s)
23 
24 struct rt {
25  void *label;
26 };
27 
28 #define RT_WAITING 0
29 #define RT_YIELDED 1
30 #define RT_EXITED 2
31 #define RT_ENDED 3
32 
44 #define RT_INIT(rt) LABEL_INIT((rt)->label)
45 
57 #define RT_BEGIN(rt) { char RT_YIELD_FLAG = 1; LABEL_RESUME((rt)->label)
58 
70 #define RT_END(rt) LABEL_END((rt)->label); RT_YIELD_FLAG = 0; \
71  RT_INIT(rt); return RT_ENDED; }
72 
85 #define RT_WAIT_UNTIL(rt, condition) \
86  do { \
87  LABEL_SET((rt)->label); \
88  if(!(condition)) { \
89  return RT_WAITING; \
90  } \
91  } while(0)
92 
105 #define RT_WAIT_WHILE(rt, cond) RT_WAIT_UNTIL((rt), !(cond))
106 
119 #define RT_WAIT_THREAD(rt, thread) RT_WAIT_WHILE((rt), RT_SCHEDULE(thread))
120 
134 #define RT_SPAWN(rt, child, thread) \
135  do { \
136  RT_INIT((child)); \
137  RT_WAIT_THREAD((rt), (thread)); \
138  } while(0)
139 
151 #define RT_RESTART(rt) \
152  do { \
153  RT_INIT(rt); \
154  return RT_WAITING; \
155  } while(0)
156 
168 #define RT_EXIT(rt) \
169  do { \
170  RT_INIT(rt); \
171  return RT_EXITED; \
172  } while(0)
173 
185 #define RT_SCHEDULE(f) ((f) < RT_EXITED)
186 
198 #define RT_YIELD(rt) \
199  do { \
200  RT_YIELD_FLAG = 0; \
201  LABEL_SET((rt)->label); \
202  if(RT_YIELD_FLAG == 0) { \
203  return RT_YIELDED; \
204  } \
205  } while(0)
206 
219 #define RT_YIELD_UNTIL(rt, cond) \
220  do { \
221  RT_YIELD_FLAG = 0; \
222  LABEL_SET((rt)->label); \
223  if((RT_YIELD_FLAG == 0) || !(cond)) { \
224  return RT_YIELDED; \
225  } \
226  } while(0)
227 
240 #define RT_SLEEP(rt, delay) \
241 { \
242  do { \
243  static unsigned long protothreads_sleep; \
244  protothreads_sleep = millis(); \
245  RT_WAIT_UNTIL(rt, millis() - protothreads_sleep > delay); \
246  } while(false); \
247 }
248 
252 #endif
Definition: RAKThread.h:24
void * label
Definition: RAKThread.h:25