forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceu_os.h
More file actions
400 lines (332 loc) · 9.42 KB
/
Copy pathceu_os.h
File metadata and controls
400 lines (332 loc) · 9.42 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#ifndef _CEU_OS_H
#define _CEU_OS_H
#include <stddef.h>
#include "ceu_types.h"
#ifdef CEU_OS
/* TODO: all should be configurable */
#define CEU_EXTS
#define CEU_WCLOCKS
#define CEU_ASYNCS
#define CEU_RET
#define CEU_CLEAR
/*
*/
#define CEU_INTS
#define CEU_ORGS
#define CEU_PSES
#define CEU_NEWS
#define CEU_NEWS_MALLOC
#define CEU_NEWS_POOL
/*
#define CEU_THREADS
*/
#define CEU_QUEUE_MAX 65536
/*
#define CEU_QUEUE_MAX 256
*/
#define CEU_IN__NONE 0
#define CEU_IN__STK 255
#define CEU_IN__ORG 254
#define CEU_IN__ORG_PSED 253
#define CEU_IN__INIT 252
#define CEU_IN__CLEAR 251
#define CEU_IN__WCLOCK 250
#define CEU_IN__ASYNC 249
#define CEU_IN__THREAD 248
#define CEU_IN_OS_START 247
#define CEU_IN_OS_STOP 246
#define CEU_IN_OS_DT 245
typedef s8 tceu_nlbl;
#define ceu_out_malloc(app,size) \
((__typeof__(ceu_sys_malloc)*)((app)->sys_vec[CEU_SYS_MALLOC]))(app,size)
#define ceu_out_free(app,ptr) \
((__typeof__(ceu_sys_free)*)((app)->sys_vec[CEU_SYS_FREE]))(ptr)
#define ceu_out_load(addr) \
((__typeof__(ceu_sys_load)*)((_ceu_app)->sys_vec[CEU_SYS_LOAD]))(addr)
#define ceu_out_start(app) \
((__typeof__(ceu_sys_start)*)((_ceu_app)->sys_vec[CEU_SYS_START]))(app)
#define ceu_out_link(app1,evt1 , app2,evt2) \
((__typeof__(ceu_sys_link)*)((_ceu_app)->sys_vec[CEU_SYS_LINK]))(app1,evt1,app2,evt2)
/* TODO: make all ceu_out_* to pass "_ceu_app" automatically? */
#define ceu_out_emit_buf(app,id,sz,buf) \
((__typeof__(ceu_sys_emit)*)((app)->sys_vec[CEU_SYS_EMIT]))(app,id,(tceu_evtp)NULL,sz,buf)
#define ceu_out_emit_val(app,id,param) \
((__typeof__(ceu_sys_emit)*)((app)->sys_vec[CEU_SYS_EMIT]))(app,id,param,0,NULL)
#define ceu_out_call_val(app,id,param) \
((__typeof__(ceu_sys_call)*)((app)->sys_vec[CEU_SYS_CALL]))(app,id,param)
#define ceu_out_go(app,evt,evtp) \
((__typeof__(ceu_sys_go)*)((app)->sys_vec[CEU_SYS_GO]))(app,evt,evtp)
#define ceu_out_org_init(app,org,n,lbl,seqno,par_org,par_trl) \
((__typeof__(ceu_sys_org_init)*)((app)->sys_vec[CEU_SYS_ORG_INIT]))(org,n,lbl,seqno,par_org,par_trl)
#else /* CEU_OS */
#include "_ceu_app.h"
#define ceu_out_malloc(app,size) \
ceu_sys_malloc(size)
#define ceu_out_free(app,ptr) \
ceu_sys_free(ptr)
#define ceu_out_emit_buf(app,id,sz,buf) \
ceu_out_emit_val(app,id,(tceu_evtp)(void*)buf)
#define ceu_out_org_init(app,org,n,lbl,seqno,par_org,par_trl) \
ceu_sys_org_init(org,n,lbl,seqno,par_org,par_trl)
#define ceu_out_go(app,evt,evtp) \
ceu_sys_go(app,evt,evtp)
#endif
#define ceu_in_emit_val(app,id,param) \
ceu_out_go(app,id,param)
#ifdef CEU_THREADS
/* TODO: app */
#include "ceu_threads.h"
#endif
typedef u8 tceu_nevt; /* max number of events */
/* TODO: should "u8" be fixed? */
#ifdef __cplusplus
#define CEU_WCLOCK_INACTIVE 0x7fffffffL /* TODO */
#else
#define CEU_WCLOCK_INACTIVE INT32_MAX
#endif
#define CEU_WCLOCK_EXPIRED (CEU_WCLOCK_INACTIVE-1)
/* TCEU_TRL */
typedef union tceu_trl {
tceu_nevt evt;
struct { /* TODO(ram): bitfields */
tceu_nevt evt1;
tceu_nlbl lbl;
u8 seqno; /* TODO(ram): 2 bits is enough */
};
struct { /* TODO(ram): bitfields */
tceu_nevt evt2;
tceu_nlbl lbl2;
u8 stk;
};
#ifdef CEU_ORGS
struct { /* TODO(ram): bad for alignment */
tceu_nevt evt3;
struct tceu_org_lnk* lnks;
};
#endif
} tceu_trl;
/* TCEU_EVTP */
typedef union tceu_evtp {
int v;
void* ptr;
s32 dt;
#ifdef CEU_THREADS
CEU_THREADS_T thread;
#endif
} tceu_evtp;
/* TCEU_STK */
/* TODO(speed): hold nxt trl to run */
typedef struct tceu_stk {
tceu_evtp evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
void* evto;
#endif
#endif
tceu_nevt evt;
} tceu_stk;
/* TCEU_LNK */
/* simulates an org prv/nxt */
typedef struct tceu_org_lnk {
struct tceu_org* prv; /* TODO(ram): lnks[0] does not use */
struct tceu_org* nxt; /* prv, n, lnk */
u8 lnk;
u8 n; /* use for ands/fins */
} tceu_org_lnk;
/* TCEU_ORG */
typedef struct tceu_org
{
#ifdef CEU_ORGS
struct tceu_org* prv; /* linked list for the scheduler */
struct tceu_org* nxt;
u8 lnk;
#endif
#if defined(CEU_ORGS) || defined(CEU_OS)
u8 n; /* number of trails (TODO(ram): opt, metadata) */
#endif
/* prv/nxt/lnk/n must be in the same order as "tceu_org_lnk" */
#ifdef CEU_ORGS
#ifdef CEU_IFCS
tceu_ncls cls; /* class id */
#endif
#ifdef CEU_NEWS
u8 isDyn: 1; /* created w/ new or spawn? */
u8 isSpw: 1; /* free on termination? */
#endif
#endif /* CEU_ORGS */
#ifdef CEU_NEWS_POOL
void* pool; /* TODO(ram): opt, traverse lst of cls pools */
#endif
tceu_trl trls[0]; /* first trail */
} tceu_org;
/* TCEU_GO */
typedef struct tceu_go {
int evt;
tceu_evtp evtp;
#ifdef CEU_INTS
#ifdef CEU_ORGS
tceu_org* evto; /* org that emitted current event */
#endif
#endif
#if defined(CEU_ORGS) || defined(CEU_OS)
#define CEU_MAX_STACK 255 /* TODO 256??? */
/* TODO: CEU_ORGS is calculable // CEU_NEWS isn't (255?) */
tceu_stk stk[CEU_MAX_STACK];
#else
tceu_stk stk[CEU_NTRAILS];
#endif
/* current traversal state */
int stki; /* points to next */
tceu_trl* trl;
tceu_nlbl lbl;
tceu_org* org;
/* traversals may be bounded to org/trl
* default (NULL) is to traverse everything */
#ifdef CEU_CLEAR
void* stop; /* stop at this trl/org */
#endif
} tceu_go;
/* TCEU_LST */
#ifdef CEU_DEBUG
typedef struct tceu_lst {
#ifdef CEU_ORGS
void* org;
#endif
tceu_trl* trl;
tceu_nlbl lbl;
} tceu_lst;
#endif
/* TCEU_APP */
typedef struct tceu_app {
/* global seqno: incremented on every reaction
* awaiting trails matches only if trl->seqno < seqno,
* i.e., previously awaiting the event
*/
u8 seqno: 2;
#if defined(CEU_RET) || defined(CEU_OS)
u8 isAlive: 1;
#endif
#ifdef CEU_ASYNCS
u8 pendingAsyncs: 1;
#endif
#ifdef CEU_OS
struct tceu_app* nxt;
#endif
#ifdef CEU_RET
int ret;
#endif
#ifdef CEU_WCLOCKS
int wclk_late;
s32 wclk_min;
s32 wclk_min_tmp;
#endif
#ifndef CEU_OS
#ifdef CEU_DEBUG
tceu_lst lst; /* segfault printf */
#endif
#endif
#ifdef CEU_THREADS
CEU_THREADS_MUTEX_T threads_mutex;
/*CEU_THREADS_COND_T threads_cond;*/
u8 threads_n; /* number of running threads */
/* TODO: u8? */
#endif
int (*code) (struct tceu_app*,tceu_go*);
void (*init) (struct tceu_app*);
#ifdef CEU_OS
tceu_evtp (*calls) (struct tceu_app*,tceu_nevt,tceu_evtp);
void** sys_vec;
void* addr;
#endif
tceu_org* data;
} tceu_app;
#ifdef CEU_OS
typedef void (*tceu_init) (tceu_app* app);
typedef void (*tceu_export) (uint* size, tceu_init** init);
#endif
/* TCEU_THREADS_P */
#ifdef CEU_THREADS
typedef struct {
tceu_app* app;
tceu_org* org;
s8* st; /* thread state:
* 0=ini (sync spawns)
* 1=cpy (async copies)
* 2=lck (sync locks)
* 3=end (sync/async terminates)
*/
} tceu_threads_p;
#endif
/* RET_* */
enum {
RET_HALT = 0,
RET_END
/*RET_GOTO,*/
#if defined(CEU_INTS) || defined(CEU_ORGS)
, RET_ORG
#endif
#if defined(CEU_CLEAR) || defined(CEU_ORGS)
, RET_TRL
#endif
#ifdef CEU_ASYNCS
, RET_ASYNC
#endif
};
void* ceu_sys_malloc (size_t size);
void ceu_sys_free (void* ptr);
void ceu_sys_org_init (tceu_org* org, int n, int lbl, int seqno,
tceu_org* par_org, int par_trl);
#ifdef CEU_PSES
void ceu_pause (tceu_trl* trl, tceu_trl* trlF, int psed);
#endif
void ceu_sys_go (tceu_app* app, int evt, tceu_evtp evtp);
int ceu_go_all (tceu_app* app);
#ifdef CEU_OS
/* TCEU_LINK */
typedef struct tceu_lnk {
tceu_app* src_app;
tceu_nevt src_evt;
tceu_app* dst_app;
tceu_nevt dst_evt;
struct tceu_lnk* nxt;
} tceu_lnk;
/* TCEU_QUEUE */
typedef struct {
tceu_app* app;
tceu_nevt evt;
tceu_evtp param;
#if CEU_QUEUE_MAX == 256
s8 sz;
#else
s16 sz; /* signed because of fill */
#endif
char buf[0];
} tceu_queue;
int ceu_scheduler (int(*dt)());
tceu_queue* ceu_sys_queue_nxt (void);
void ceu_sys_queue_rem (void);
tceu_app* ceu_sys_load (void* addr);
void ceu_sys_start (tceu_app* app);
int ceu_sys_link (tceu_app* src_app, tceu_nevt src_evt, tceu_app* dst_app, tceu_nevt dst_evt);
int ceu_sys_unlink (tceu_app* src_app, tceu_nevt src_evt, tceu_app* dst_app, tceu_nevt dst_evt);
int ceu_sys_emit (tceu_app* app, tceu_nevt evt, tceu_evtp param,
int sz, char* buf);
tceu_evtp ceu_sys_call (tceu_app* app, tceu_nevt evt, tceu_evtp param);
enum {
CEU_SYS_MALLOC = 0,
CEU_SYS_FREE,
CEU_SYS_LOAD,
CEU_SYS_START,
CEU_SYS_LINK,
CEU_SYS_UNLINK,
CEU_SYS_EMIT,
CEU_SYS_CALL,
CEU_SYS_GO,
CEU_SYS_ORG_INIT,
CEU_SYS_MAX
};
/* SYS_VECTOR
*/
extern void* CEU_SYS_VEC[CEU_SYS_MAX];
#endif /* CEU_OS */
#endif /* _CEU_OS_H */