@@ -138,7 +138,7 @@ struct _pbio_os_process_t {
138
138
*
139
139
* @param [in] state Protothread state.
140
140
*/
141
- #define ASYNC_BEGIN (state ) { PB_LC_RESUME(state)
141
+ #define ASYNC_BEGIN (state ) { char do_yield_now = 0; if (do_yield_now) {;} PB_LC_RESUME(state)
142
142
143
143
/**
144
144
* Declare the end of a protothread and returns with given code.
@@ -180,6 +180,13 @@ struct _pbio_os_process_t {
180
180
} \
181
181
} while (0)
182
182
183
+ /**
184
+ * Awaits a protothread until it is done.
185
+ *
186
+ * @param [in] state Protothread state.
187
+ * @param [in] child Protothread state of the child.
188
+ * @param [in] statement The statement to await.
189
+ */
183
190
#define AWAIT (state , child , statement ) \
184
191
do { \
185
192
ASYNC_INIT((child)); \
@@ -189,6 +196,15 @@ struct _pbio_os_process_t {
189
196
} \
190
197
} while (0)
191
198
199
+ /**
200
+ * Awaits two protothreads until one of them is done.
201
+ *
202
+ * @param [in] state Protothread state.
203
+ * @param [in] child1 Protothread state of the first child.
204
+ * @param [in] child2 Protothread state of the second child.
205
+ * @param [in] statement1 The first statement to await.
206
+ * @param [in] statement2 The second statement to await.
207
+ */
192
208
#define AWAIT_RACE (state , child1 , child2 , statement1 , statement2 ) \
193
209
do { \
194
210
ASYNC_INIT((child1)); \
@@ -199,6 +215,24 @@ struct _pbio_os_process_t {
199
215
} \
200
216
} while (0)
201
217
218
+ /**
219
+ * Yields the protothread once and polls to request handling again immediately.
220
+ *
221
+ * Should be used sparingly as it can cause busy waiting. Processes will keep
222
+ * running, but there is always another event pending.
223
+ *
224
+ * @param [in] state Protothread state.
225
+ */
226
+ #define AWAIT_ONCE (state ) \
227
+ do { \
228
+ do_yield_now = 1; \
229
+ PB_LC_SET(state); \
230
+ if (do_yield_now) { \
231
+ pbio_os_request_poll(); \
232
+ return PBIO_ERROR_AGAIN; \
233
+ } \
234
+ } while (0)
235
+
202
236
#define AWAIT_MS (state , timer , duration ) \
203
237
do { \
204
238
pbio_os_timer_set(timer, duration); \
0 commit comments