forked from sustrik/libmill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmill.h
350 lines (279 loc) · 12.7 KB
/
libmill.h
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Copyright (c) 2015 Martin Sustrik
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.
*/
#ifndef LIBMILL_H_INCLUDED
#define LIBMILL_H_INCLUDED
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
/******************************************************************************/
/* ABI versioning support */
/******************************************************************************/
/* Don't change this unless you know exactly what you're doing and have */
/* read and understand the following documents: */
/* www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html */
/* www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html */
/* The current interface version. */
#define MILL_VERSION_CURRENT 13
/* The latest revision of the current interface. */
#define MILL_VERSION_REVISION 0
/* How many past interface versions are still supported. */
#define MILL_VERSION_AGE 1
/******************************************************************************/
/* Symbol visibility */
/******************************************************************************/
#if defined MILL_NO_EXPORTS
# define MILL_EXPORT
#else
# if defined _WIN32
# if defined MILL_EXPORTS
# define MILL_EXPORT __declspec(dllexport)
# else
# define MILL_EXPORT __declspec(dllimport)
# endif
# else
# if defined __SUNPRO_C
# define MILL_EXPORT __global
# elif (defined __GNUC__ && __GNUC__ >= 4) || \
defined __INTEL_COMPILER || defined __clang__
# define MILL_EXPORT __attribute__ ((visibility("default")))
# else
# define MILL_EXPORT
# endif
# endif
#endif
/******************************************************************************/
/* Helpers */
/******************************************************************************/
MILL_EXPORT int64_t now(void);
/******************************************************************************/
/* Coroutines */
/******************************************************************************/
MILL_EXPORT void goprepare(int count, size_t stack_size, size_t val_size);
MILL_EXPORT extern volatile int mill_unoptimisable1;
MILL_EXPORT extern volatile void *mill_unoptimisable2;
MILL_EXPORT void *mill_go_prologue(const char *created);
MILL_EXPORT void mill_go_epilogue(void);
#define mill_string2(x) #x
#define mill_string(x) mill_string2(x)
#if defined __GNUC__ || defined __clang__
#define coroutine __attribute__((noinline))
#else
#define coroutine
#endif
#define go(fn) \
do {\
void *mill_sp = mill_go_prologue(__FILE__ ":" mill_string(__LINE__));\
if(mill_sp) {\
int mill_anchor[mill_unoptimisable1];\
mill_unoptimisable2 = &mill_anchor;\
char mill_filler[(char*)&mill_anchor - (char*)(mill_sp)];\
mill_unoptimisable2 = &mill_filler;\
fn;\
mill_go_epilogue();\
}\
} while(0)
#define yield() mill_yield(__FILE__ ":" mill_string(__LINE__))
MILL_EXPORT void mill_yield(const char *current);
#define msleep(deadline) mill_msleep((deadline),\
__FILE__ ":" mill_string(__LINE__))
MILL_EXPORT void mill_msleep(int64_t deadline, const char *current);
#define fdwait(fd, events, deadline) mill_fdwait((fd), (events), (deadline),\
__FILE__ ":" mill_string(__LINE__))
MILL_EXPORT void fdclean(int fd);
#define FDW_IN 1
#define FDW_OUT 2
#define FDW_ERR 4
MILL_EXPORT int mill_fdwait(int fd, int events, int64_t deadline,
const char *current);
MILL_EXPORT pid_t mfork(void);
MILL_EXPORT void *cls(void);
MILL_EXPORT void setcls(void *val);
/******************************************************************************/
/* Channels */
/******************************************************************************/
typedef struct mill_chan *chan;
#define MILL_CLAUSELEN (sizeof(struct{void *f1; void *f2; void *f3; void *f4; \
void *f5; void *f6; int f7; int f8; int f9;}))
#define chmake(type, bufsz) mill_chmake(sizeof(type), bufsz,\
__FILE__ ":" mill_string(__LINE__))
#define chdup(channel) mill_chdup((channel),\
__FILE__ ":" mill_string(__LINE__))
#define chs(channel, type, value) \
do {\
type mill_val = (value);\
mill_chs((channel), &mill_val, sizeof(type),\
__FILE__ ":" mill_string(__LINE__));\
} while(0)
#define chr(channel, type) \
(*(type*)mill_chr((channel), sizeof(type),\
__FILE__ ":" mill_string(__LINE__)))
#define chdone(channel, type, value) \
do {\
type mill_val = (value);\
mill_chdone((channel), &mill_val, sizeof(type),\
__FILE__ ":" mill_string(__LINE__));\
} while(0)
#define chclose(channel) mill_chclose((channel),\
__FILE__ ":" mill_string(__LINE__))
MILL_EXPORT chan mill_chmake(size_t sz, size_t bufsz, const char *created);
MILL_EXPORT chan mill_chdup(chan ch, const char *created);
MILL_EXPORT void mill_chs(chan ch, void *val, size_t sz, const char *current);
MILL_EXPORT void *mill_chr(chan ch, size_t sz, const char *current);
MILL_EXPORT void mill_chdone(chan ch, void *val, size_t sz,
const char *current);
MILL_EXPORT void mill_chclose(chan ch, const char *current);
#define mill_concat(x,y) x##y
#define choose \
{\
mill_choose_init(__FILE__ ":" mill_string(__LINE__));\
int mill_idx = -2;\
while(1) {\
if(mill_idx != -2) {\
if(0)
#define mill_in(chan, type, name, idx) \
break;\
}\
goto mill_concat(mill_label, idx);\
}\
char mill_concat(mill_clause, idx)[MILL_CLAUSELEN];\
mill_choose_in(\
&mill_concat(mill_clause, idx)[0],\
(chan),\
sizeof(type),\
idx);\
if(0) {\
type name;\
mill_concat(mill_label, idx):\
if(mill_idx == idx) {\
name = *(type*)mill_choose_val(sizeof(type));\
goto mill_concat(mill_dummylabel, idx);\
mill_concat(mill_dummylabel, idx)
#define in(chan, type, name) mill_in((chan), type, name, __COUNTER__)
#define mill_out(chan, type, val, idx) \
break;\
}\
goto mill_concat(mill_label, idx);\
}\
char mill_concat(mill_clause, idx)[MILL_CLAUSELEN];\
type mill_concat(mill_val, idx) = (val);\
mill_choose_out(\
&mill_concat(mill_clause, idx)[0],\
(chan),\
&mill_concat(mill_val, idx),\
sizeof(type),\
idx);\
if(0) {\
mill_concat(mill_label, idx):\
if(mill_idx == idx) {\
goto mill_concat(mill_dummylabel, idx);\
mill_concat(mill_dummylabel, idx)
#define out(chan, type, val) mill_out((chan), type, (val), __COUNTER__)
#define mill_otherwise(idx) \
break;\
}\
goto mill_concat(mill_label, idx);\
}\
mill_choose_otherwise();\
if(0) {\
mill_concat(mill_label, idx):\
if(mill_idx == -1) {\
goto mill_concat(mill_dummylabel, idx);\
mill_concat(mill_dummylabel, idx)
#define otherwise mill_otherwise(__COUNTER__)
#define end \
break;\
}\
}\
mill_idx = mill_choose_wait();\
}
MILL_EXPORT void mill_choose_init(const char *current);
MILL_EXPORT void mill_choose_in(void *clause, chan ch, size_t sz, int idx);
MILL_EXPORT void mill_choose_out(void *clause, chan ch, void *val, size_t sz,
int idx);
MILL_EXPORT void mill_choose_otherwise(void);
MILL_EXPORT int mill_choose_wait(void);
MILL_EXPORT void *mill_choose_val(size_t sz);
/******************************************************************************/
/* IP address library */
/******************************************************************************/
#define IPADDR_IPV4 1
#define IPADDR_IPV6 2
#define IPADDR_PREF_IPV4 3
#define IPADDR_PREF_IPV6 4
#define IPADDR_MAXSTRLEN 46
typedef struct {char data[32];} ipaddr;
MILL_EXPORT ipaddr iplocal(const char *name, int port, int mode);
MILL_EXPORT ipaddr ipremote(const char *name, int port, int mode,
int64_t deadline);
MILL_EXPORT const char *ipaddrstr(ipaddr addr, char *ipstr);
/******************************************************************************/
/* TCP library */
/******************************************************************************/
typedef struct mill_tcpsock *tcpsock;
MILL_EXPORT tcpsock tcplisten(ipaddr addr, int backlog);
MILL_EXPORT int tcpport(tcpsock s);
MILL_EXPORT tcpsock tcpaccept(tcpsock s, int64_t deadline);
MILL_EXPORT ipaddr tcpaddr(tcpsock s);
MILL_EXPORT tcpsock tcpconnect(ipaddr addr, int64_t deadline);
MILL_EXPORT size_t tcpsend(tcpsock s, const void *buf, size_t len,
int64_t deadline);
MILL_EXPORT void tcpflush(tcpsock s, int64_t deadline);
MILL_EXPORT size_t tcprecv(tcpsock s, void *buf, size_t len, int64_t deadline);
MILL_EXPORT size_t tcprecvuntil(tcpsock s, void *buf, size_t len,
const char *delims, size_t delimcount, int64_t deadline);
MILL_EXPORT void tcpclose(tcpsock s);
MILL_EXPORT tcpsock tcpattach(int fd, int listening);
MILL_EXPORT int tcpdetach(tcpsock s);
/******************************************************************************/
/* UDP library */
/******************************************************************************/
typedef struct mill_udpsock *udpsock;
MILL_EXPORT udpsock udplisten(ipaddr addr);
MILL_EXPORT int udpport(udpsock s);
MILL_EXPORT void udpsend(udpsock s, ipaddr addr, const void *buf, size_t len);
MILL_EXPORT size_t udprecv(udpsock s, ipaddr *addr,
void *buf, size_t len, int64_t deadline);
MILL_EXPORT void udpclose(udpsock s);
MILL_EXPORT udpsock udpattach(int fd);
MILL_EXPORT int udpdetach(udpsock s);
/******************************************************************************/
/* UNIX library */
/******************************************************************************/
typedef struct mill_unixsock *unixsock;
MILL_EXPORT unixsock unixlisten(const char *addr, int backlog);
MILL_EXPORT unixsock unixaccept(unixsock s, int64_t deadline);
MILL_EXPORT unixsock unixconnect(const char *addr);
MILL_EXPORT void unixpair(unixsock *a, unixsock *b);
MILL_EXPORT size_t unixsend(unixsock s, const void *buf, size_t len,
int64_t deadline);
MILL_EXPORT void unixflush(unixsock s, int64_t deadline);
MILL_EXPORT size_t unixrecv(unixsock s, void *buf, size_t len,
int64_t deadline);
MILL_EXPORT size_t unixrecvuntil(unixsock s, void *buf, size_t len,
const char *delims, size_t delimcount, int64_t deadline);
MILL_EXPORT void unixclose(unixsock s);
MILL_EXPORT unixsock unixattach(int fd, int listening);
MILL_EXPORT int unixdetach(unixsock s);
/******************************************************************************/
/* Debugging */
/******************************************************************************/
MILL_EXPORT void goredump(void);
MILL_EXPORT void gotrace(int level);
#endif