librfn
An ad-hoc utility library
protothreads.h
Go to the documentation of this file.
1 /*
2  * protothreads.h
3  *
4  * Part of librfn (a general utility library from redfelineninja.org.uk)
5  *
6  * Copyright (C) 2013-2014 Daniel Thompson <daniel@redfelineninja.org.uk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  */
13 
14 #ifndef RF_PROTOTHREADS_H_
15 #define RF_PROTOTHREADS_H_
16 
17 #include <assert.h>
18 #include <stdint.h>
19 
80 typedef enum {
84 } pt_state_t;
85 
86 typedef uint16_t pt_t;
87 
88 #define PT_INIT(pt) \
89  do { \
90  *(pt) = 0; \
91  } while (0)
92 
93 #define PT_BEGIN(pt) \
94  { \
95  pt_t *missing_PT_BEGIN = pt; \
96  (void) missing_PT_BEGIN; \
97  switch (*missing_PT_BEGIN) { \
98  case 0:
99 
100 #define PT_END() \
101  break; \
102  default: \
103  assert(0); \
104  } \
105  } \
106  return PT_EXITED
107 
108 #define PT_WAIT() \
109  do { \
110  *missing_PT_BEGIN = __LINE__; \
111  return PT_WAITING; \
112  case __LINE__: \
113  ; \
114  } while (0)
115 
116 #define PT_WAIT_UNTIL(c) \
117  do { \
118  *missing_PT_BEGIN = __LINE__; \
119  case __LINE__: \
120  if (!(c)) \
121  return PT_WAITING; \
122  } while (0)
123 
124 #define PT_YIELD() \
125  do { \
126  *missing_PT_BEGIN = __LINE__; \
127  return PT_YIELDED; \
128  case __LINE__: \
129  ; \
130  } while (0)
131 
132 #define PT_EXIT() \
133  return PT_EXITED
134 
140 #define PT_EXIT_ON(x) \
141  do { \
142  if (x) \
143  return PT_EXITED; \
144  } while (0)
145 
149 #define PT_SPAWN(child, thread) \
150  do { \
151  pt_state_t ptres; \
152  PT_INIT(child); \
153  *missing_PT_BEGIN = __LINE__; \
154  case __LINE__: \
155  ptres = (thread); \
156  if (ptres != PT_EXITED) \
157  return ptres; \
158  } while (0)
159 
167 #define PT_CALL(child, thread) \
168  do { \
169  PT_INIT(child); \
170  while (PT_EXITED != (thread)) \
171  ; \
172  } while (0)
173 
175 #endif // RF_PROTOTHREADS_H_
uint16_t pt_t
Definition: protothreads.h:86
pt_state_t
Definition: protothreads.h:80