-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpt.h
More file actions
217 lines (201 loc) · 10.7 KB
/
pt.h
File metadata and controls
217 lines (201 loc) · 10.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef PT_H
#define PT_H
#include <stddef.h>
/* Protothread status values */
#define PT_STATUS_BLOCKED 0
#define PT_STATUS_FINISHED -1
#define PT_STATUS_YIELDED -2
/* Helper macros to generate unique labels */
#define _pt_line3(name, line) _pt_##name##line
#define _pt_line2(name, line) _pt_line3(name, line)
#define _pt_line(name) _pt_line2(name, __LINE__)
#if PT_USE_SETJMP
/*
* Local continuation that uses setjmp()/longjmp() to keep thread state.
*
* Pros: local variables are preserved, works with all control structures.
* Cons: slow, may erase global register variables.
*/
#include <setjmp.h>
struct pt {
jmp_buf env;
int isset;
int status;
void *local;
};
typedef struct pt pt_t;
#define pt_init() \
{ .isset = 0, .status = 0 }
#define pt_reset(pt) \
do { (pt)->isset = 0; (pt)->status = 0; } while (0)
#define pt_begin(pt) \
do { \
if ((pt)->status == PT_STATUS_FINISHED) { \
return; \
} \
if ((pt)->isset) { \
longjmp((pt)->env, 0); \
} \
} while (0)
#define pt_label(pt, stat) \
do { \
(pt)->isset = 1; \
(pt)->status = (stat); \
setjmp((pt)->env); \
} while (0)
#define pt_end(pt) pt_label(pt, PT_STATUS_FINISHED)
#define pt_kill(pt) \
do { \
(pt)->isset = 1; \
(pt)->status = PT_STATUS_FINISHED; \
} while (0)
#elif PT_USE_GOTO
/*
* Local continuation based on goto label references.
*
* Pros: works with all control sturctures.
* Cons: requires GCC or Clang, doesn't preserve local variables.
*/
struct pt {
void *label;
int status;
void *local;
};
typedef struct pt pt_t;
#define pt_init() \
{ .label = NULL, .status = 0 }
#define pt_reset(pt) \
do { (pt)->label = NULL; (pt)->status = 0; } while (0)
#define pt_restart(pt) \
do { pt_reset(pt); return; } while (0)
#define pt_begin(pt) \
do { \
if ((pt)->status == PT_STATUS_FINISHED) { \
return; \
} \
if ((pt)->label != NULL) { \
goto *(pt)->label; \
} \
} while (0)
#define pt_label(pt, stat) \
do { \
(pt)->status = (stat); \
_pt_line(label) : (pt)->label = &&_pt_line(label); \
} while (0)
#define pt_end(pt) pt_label(pt, PT_STATUS_FINISHED)
#define pt_kill(pt) \
do { \
(pt)->status = PT_STATUS_FINISHED; \
} while (0)
#else
/*
* Local continuation based on switch/case and line numbers.
*
* Pros: works with any C99 compiler, most simple implementation.
* Cons: doesn't allow more than one label per line, doesn't preserve local
* variables.
*/
struct pt {
int label;
int status;
void *local;
};
typedef struct pt pt_t;
#define pt_init() \
{ .label = 0, .status = 0 }
#define pt_reset(pt) \
do { (pt)->label = 0; (pt)->status = 0; } while (0)
#define pt_begin(pt) \
if ((pt)->status == PT_STATUS_FINISHED) { \
return; \
} \
switch ((pt)->label) { \
case 0:
#define pt_label(pt, stat) \
do { \
(pt)->label = __LINE__; \
(pt)->status = (stat); \
case __LINE__:; \
} while (0)
#define pt_end(pt) \
pt_label(pt, PT_STATUS_FINISHED); \
}
#define pt_kill(pt) \
do { \
(pt)->status = PT_STATUS_FINISHED; \
} while (0)
#endif
/*
* Core protothreads API
*/
#define pt_status(pt) (pt)->status
#define pt_wait(pt, cond) \
do { \
pt_label(pt, PT_STATUS_BLOCKED); \
if (!(cond)) { \
return; \
} \
} while (0)
#define pt_yield(pt) \
do { \
pt_label(pt, PT_STATUS_YIELDED); \
if (pt_status(pt) == PT_STATUS_YIELDED) { \
(pt)->status = PT_STATUS_BLOCKED; \
return; \
} \
} while (0)
#define pt_exit(pt, stat) \
do { \
pt_label(pt, stat); \
return; \
} while (0)
#define pt_loop(pt, cond) \
for (int _intr = 0; _intr == 0;) \
if (1) { \
pt_label(pt, PT_STATUS_BLOCKED); \
if (!(cond)) { \
break; \
} \
goto _pt_line(body); \
} else \
while (1) \
if (1) { \
_intr = 1; \
break; \
} else \
while (1) \
if (1) { \
return; \
} else \
_pt_line(body) :
/*
* Queues
*/
#define pt_queue(T, size) \
struct { \
T buf[size]; \
unsigned int r; \
unsigned int w; \
}
#define pt_queue_init() \
{ .r = 0, .w = 0 }
#define pt_queue_len(q) (sizeof((q)->buf) / sizeof((q)->buf[0]))
#define pt_queue_cap(q) ((q)->w - (q)->r)
#define pt_queue_empty(q) ((q)->w == (q)->r)
#define pt_queue_full(q) (pt_queue_cap(q) == pt_queue_len(q))
#define pt_queue_reset(q) ((q)->w = (q)->r = 0)
#define pt_queue_push(q, el) \
(!pt_queue_full(q) && ((q)->buf[(q)->w++ % pt_queue_len(q)] = (el), 1))
#define pt_queue_peek(q) \
(pt_queue_empty(q) ? NULL : &(q)->buf[(q)->r % pt_queue_len(q)])
#define pt_queue_pop(q) \
(pt_queue_empty(q) ? NULL : &(q)->buf[(q)->r++ % pt_queue_len(q)])
/*
* Wrapper for system calls and other functions that return -1 and set errno
*/
#define pt_sys(pt, call) \
pt_wait(pt, \
(errno = 0) || \
!(((call) == -1) && (errno == EAGAIN || errno == EWOULDBLOCK || \
errno == EINPROGRESS || errno == EINTR)))
#endif /* PT_H */