-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoroutine.h
More file actions
151 lines (138 loc) · 4.35 KB
/
Copy pathcoroutine.h
File metadata and controls
151 lines (138 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* coroutine - C coroutine library
* Copyright (C) 2026 notweerdmonk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file coroutine.h
* @author notweerdmonk
* @brief Implementation of coroutines using Duff's device.
* @see https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
*/
#ifndef _COROUTINE_H_
#define _COROUTINE_H_
#include <stdint.h>
#undef thread_local
#ifdef __GNUC__
#define thread_local __thread
#else
#define thread_local _Thread_local
#endif
/**
* @brief Begin a coroutine inside a function.
*
* There can be only one coroutine inside a function.
*/
#define COROUTINE_BEGIN() \
static thread_local int16_t cr_state = 0; switch(cr_state) { case 0:
/**
* @brief Return from the function using a coroutine.
*
* Execution resumes from this point when the function gets called again.
*
* @return Address of the coroutine state variable.
*/
#define COROUTINE_YIELD() \
do { \
cr_state = __LINE__; return &cr_state; case __LINE__:; \
} while (0)
/**
* @brief Call another function containing a coroutine and return.
*
* The callee function gets called every time the caller function
* is called until COROUTINE_STOP() is used inside the callee. In that case
* execution continues normally from this point in the caller function.
*
* @return Address of the coroutine state variable.
*/
#if !(defined __AVR__ || defined AVR) || \
(__GNUC__ >= 7 && __GNUC_MINOR__ >= 1)
#define COROUTINE_DELEGATE(func, ...) \
do { \
__attribute__((__fallthrough__)); \
case __LINE__: cr_state = *(uint16_t*)func(__VA_ARGS__); \
if (cr_state > 0) { cr_state = __LINE__; return &cr_state; } \
} while (0)
#else
#define COROUTINE_DELEGATE(func, ...) \
do { \
/* fallthrough */ \
case __LINE__: cr_state = *(uint16_t*)func(__VA_ARGS__); \
if (cr_state > 0) { cr_state = __LINE__; return &cr_state; } \
} while (0)
#endif
/**
* @brief Return from a function without altering the state of its coroutine.
*
* Execution will continue from the point of previous COROUTINE_YIELD() or
* COROUTINE_BEGIN() when the function is called again.
*
* @return Address of the coroutine state variable.
*/
#define COROUTINE_RETURN() \
return &cr_state
/**
* @brief Reset a coroutine.
*
* Execution will start from COROUTINE_BEGIN() when the function is called
* again.
*
* @return Address of the coroutine state variable.
*/
#define COROUTINE_RESET() \
do { \
cr_state = 0; return &cr_state; \
} while (0)
/**
* @brief Reset a coroutine and return NULL to mark occurence of an error.
*
* Execution will start from COROUTINE_BEGIN() when the function is called
* again.
*
* @return NULL
*/
#define COROUTINE_ERROR() \
do { \
cr_state = 0; return NULL; \
} while (0)
/**
* @brief Stop a coroutine.
*
* Execution will start from COROUTINE_BEGIN() when the function is called
* again. This macro should be used to exit a delegated coroutine.
*
* @return Address of the coroutine state variable.
*
* @see To return from a coroutine see COROUTINE_RETURN().
* @see To reset a coroutine see COROUTINE_RESET().
*/
#define COROUTINE_STOP() \
} COROUTINE_RESET()
/**
* @brief End a coroutine.
*
* Execution will start from COROUTINE_BEGIN() when the function is called
* again.
*
* @return Address of the coroutine state variable.
*/
#define COROUTINE_END() \
} return &cr_state
#endif /* _COROUTINE_H_ */