interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / node / transport-gate.om

540 lines · 10492 bytes Raw Blame History
1note Omega Node transport, concatenate with a program-mode consumer.
2note Trusted forked processes only. This is transport, not write authority.
3note One publisher PID, 64 immutable records per epoch; reuse only via node_reset.
4note Layout: 4096 byte header then 64 slots of 4160 bytes.
5note Header u64: magic, ABI, published count, stop, publisher PID, epoch.
6note Slot u64: version, length, topic mask; payload begins at offset 64.
7const NCAP ulong 64
8const NBYTES ulong 270336
9const NMAGIC ulong 1313817669
10
11fn node_new to long
12h = mapnew neg int 1 0 NBYTES 3 33
13if le h 0
14ret h
15end
16r = mapset h 8 8 1
17r = mapset h 32 8 ulong getpid
18r = mapset h 0 8 NMAGIC
19ret h
20end
21
22fn node_valid h long to bool
23ret and eq mapget h 0 8 NMAGIC eq mapget h 8 8 1
24end
25
26fn node_offset v ulong to ulong
27ret add 4096 mul sub v 1 4160
28end
29
30note Return version, or -1 stopped, -2 full, -3 length, -4 topic,
31note -5 invalid header, -6 wrong publisher. Refusals do not publish.
32fn node_publish h long topics ulong src view uchar 4096 n ulong to long
33if not node_valid h
34ret neg long 5
35end
36if ne mapget h 32 8 ulong getpid
37ret neg long 6
38end
39if ne mapget h 24 8 0
40ret neg long 1
41end
42if gt n 4096
43ret neg long 3
44end
45if eq topics 0
46ret neg long 4
47end
48v = mapget h 16 8
49if gt v NCAP
50ret neg long 5
51end
52if eq v NCAP
53ret neg long 2
54end
55v = add v 1
56off = node_offset v
57r = mapwrite h add off 64 src 0 n
58r = mapset h add off 8 8 n
59r = mapset h add off 16 8 topics
60note mapset has barriers before and after its aligned store.
61r = mapset h off 8 v
62r = mapset h 16 8 v
63ret long v
64end
65
66note Header and record checks precede copying. No partial publication is read.
67note Returns payload length, -5 invalid header/record, -7 unavailable version.
68fn node_read h long v ulong dst edit uchar 4096 to long
69if not node_valid h
70ret neg long 5
71end
72if or eq v 0 gt v NCAP
73ret neg long 7
74end
75latest = mapget h 16 8
76if gt latest NCAP
77ret neg long 5
78end
79if gt v latest
80ret neg long 7
81end
82off = node_offset v
83if ne mapget h off 8 v
84ret neg long 5
85end
86n = mapget h add off 8 8
87if gt n 4096
88ret neg long 5
89end
90ret mapread h add off 64 dst 0 n
91end
92
93note Return the next matching version after cursor, zero if caught up,
94note or negative refusal. Caller advances its cursor through returned versions.
95fn node_next h long cursor ulong topics ulong to long
96if not node_valid h
97ret neg long 5
98end
99if or gt cursor NCAP eq topics 0
100ret neg long 7
101end
102latest = mapget h 16 8
103if or gt latest NCAP gt cursor latest
104ret neg long 5
105end
106v = cursor
107note At most NCAP published slots can be examined.
108while lt v latest bound 64
109v = add v 1
110off = node_offset v
111if ne mapget h off 8 v
112ret neg long 5
113end
114if ne and mapget h add off 16 8 topics 0
115ret long v
116end
117end
118ret 0
119end
120
121fn node_stop h long to long
122if not node_valid h
123ret neg long 5
124end
125if ne mapget h 32 8 ulong getpid
126ret neg long 6
127end
128ret mapset h 24 8 1
129end
130
131note Creator only. Retire every record so a pool can reuse the arena for a new
132note epoch. The caller must first prove no reader holds the old epoch (pool.om).
133note The count drops to zero before any record version is cleared.
134fn node_reset h long epoch ulong to long
135if not node_valid h
136ret neg long 5
137end
138if ne mapget h 32 8 ulong getpid
139ret neg long 6
140end
141if eq epoch 0
142ret neg long 7
143end
144r = mapset h 16 8 0
145for i ulong NCAP
146r = mapset h node_offset add i 1 8 0
147end
148r = mapset h 24 8 0
149r = mapset h 40 8 epoch
150ret 0
151end
152
153fn node_epoch h long to ulong
154ret mapget h 40 8
155end
156
157note The latest published version of the arena's current epoch (0 when empty).
158fn node_count h long to ulong
159ret mapget h 16 8
160end
161
162note One nonblocking eventfd per consumer. Saturation means already readable.
163note Interrupted operations retry at most 64 times, then return EINTR.
164fn node_notify fd int to long
165buf = new uchar 8 0
166put buf 0 1
167for i ulong 64
168r = write fd buf 0 8
169if eq r 8
170ret 0
171end
172if eq r neg long 11
173ret 0
174end
175if ne r neg long 4
176if lt r 0
177ret r
178end
179ret neg long 5
180end
181end
182ret neg long 4
183end
184
185fn node_drain fd int to long
186buf = new uchar 8 0
187for i ulong 64
188r = read fd buf 0 8
189if or eq r 8 eq r neg long 11
190ret 0
191end
192if ne r neg long 4
193if lt r 0
194ret r
195end
196ret neg long 5
197end
198end
199ret neg long 4
200end
201
202fn node_pollfd p edit uchar 16 off ulong fd int to void
203for i ulong 4
204put p add off i uchar and shr uint fd uint mul i 8 255
205end
206put p add off 4 1
207end
208
209note Returns 2 stop, 1 wake, 0 timeout or negative errno.
210note Control takes priority when both descriptors are ready.
211note EINTR returns to caller so it can recompute its monotonic deadline.
212fn node_wait h long wake int stop int interval long to long
213p = new uchar 16 0
214if not node_valid h
215ret neg long 5
216end
217if or lt wake 0 lt stop 0
218ret neg long 9
219end
220if eq wake stop
221ret neg long 22
222end
223if ne mapget h 24 8 0
224ret 2
225end
226node_pollfd p 0 wake
227node_pollfd p 8 stop
228r = pollfds p 0 16 interval
229if lt r 0
230ret r
231end
232if ne mapget h 24 8 0
233ret 2
234end
235if ne and get p 14 56 0
236ret neg long 5
237end
238if ne and get p 14 1 0
239r = node_drain stop
240if lt r 0
241ret r
242end
243ret 2
244end
245if ne and get p 6 56 0
246ret neg long 5
247end
248if ne and get p 6 1 0
249r = node_drain wake
250if lt r 0
251ret r
252end
253ret 1
254end
255ret 0
256end
257note Concatenate after transport.om. CPU-only process and transport acceptance.
258text nok "PASS node transport\n"
259global nh long 1
260global nf int 6
261global np long 2
262global ns uchar 4
263
264fn node_check ok bool code int to void
265if not ok
266exit code
267end
268end
269
270fn node_refusals to void
271src = new uchar 4096 42
272dst = new uchar 4096 99
273h = node_new
274node_check gt h 0 10
275node_check eq node_next h 0 1 0 11
276node_check eq node_next h 1 1 neg long 5 12
277node_check eq node_read h 1 dst neg long 7 13
278node_check eq get dst 0 99 14
279node_check eq node_publish h 1 src 4097 neg long 3 15
280node_check eq node_publish h 0 src 1 neg long 4 16
281node_check eq mapget h 16 8 0 17
282node_check eq node_publish h 1 src 0 1 18
283node_check eq node_read h 1 dst 0 19
284node_check eq get dst 0 99 20
285node_check eq node_next h 0 2 0 21
286node_check eq node_next h 0 1 1 22
287node_check eq node_read h 0 dst neg long 7 23
288node_check eq node_read h 65 dst neg long 7 24
289node_check eq node_next h 65 1 neg long 7 25
290node_check eq node_next h 0 0 neg long 7 26
291node_check eq node_stop h 0 27
292node_check eq node_publish h 1 src 1 neg long 1 28
293node_check eq mapget h 16 8 1 29
294node_check eq mapdrop h 0 30
295end
296
297fn node_faults to void
298src = new uchar 4096 42
299dst = new uchar 4096 99
300h = node_new
301node_check gt h 0 31
302node_check eq node_publish h 1 src 1 1 32
303r = mapset h 4096 8 9
304node_check eq node_read h 1 dst neg long 5 33
305node_check eq node_next h 0 1 neg long 5 34
306node_check eq get dst 0 99 35
307r = mapset h 4096 8 1
308r = mapset h 4104 8 4097
309node_check eq node_read h 1 dst neg long 5 36
310r = mapset h 16 8 65
311node_check eq node_read h 1 dst neg long 5 37
312node_check eq node_next h 0 1 neg long 5 38
313r = mapset h 0 8 0
314node_check eq node_read h 1 dst neg long 5 39
315node_check eq node_publish h 1 src 1 neg long 5 40
316node_check eq node_stop h neg long 5 41
317node_check eq mapdrop h 0 42
318end
319
320fn node_wait_checks to void
321h = node_new
322node_check gt h 0 43
323w = eventfd uint 0
324s = eventfd uint 0
325node_check ge w 0 44
326node_check ge s 0 45
327node_check eq node_wait h int w int s 0 0 46
328node_check eq node_wait h int w int w 0 neg long 22 47
329node_check eq node_wait h neg int 1 int s 0 neg long 9 48
330node_check eq node_notify int w 0 49
331node_check eq node_notify int w 0 50
332node_check eq node_wait h int w int s 0 1 51
333node_check eq node_wait h int w int s 0 0 52
334node_check eq node_notify int s 0 53
335node_check eq node_notify int w 0 54
336node_check eq node_wait h int w int s 0 2 55
337node_check eq node_stop h 0 56
338node_check eq node_wait h int w int s 0 2 57
339node_check eq fclose int w 0 58
340node_check eq fclose int s 0 59
341node_check eq node_notify int w neg long 9 60
342node_check eq mapdrop h 0 61
343end
344
345fn node_worker idx ulong to int
346buf = new uchar 4096 0
347h = get nh 0
348if ne node_publish h 1 buf 1 neg long 6
349ret 70
350end
351if ne node_stop h neg long 6
352ret 71
353end
354cursor = ulong 0
355count = ulong 0
356topic = add idx 1
357r = node_wait h get nf idx get nf add idx 2 5000000000
358if ne r 1
359ret 72
360end
361note A single wake must let each worker discover every matching version.
362for k ulong 64
363v = node_next h cursor topic
364if eq v 0
365break
366end
367if lt v 0
368ret 73
369end
370cursor = ulong v
371if ne node_read h cursor buf 4096
372ret 74
373end
374for j ulong 4096
375if ne get buf j uchar cursor
376ret 75
377end
378end
379count = add count 1
380end
381if ne count 32
382ret 76
383end
384if ne node_notify get nf add idx 4 0
385ret 77
386end
387note Parent signals stop only after both independent acknowledgements.
388while true bound 64
389r = node_wait h get nf idx get nf add idx 2 5000000000
390if eq r 2
391ret 0
392end
393if ne r 1
394ret 78
395end
396end
397ret 79
398end
399
400fn node_publish_batch to bool
401buf = new uchar 4096 0
402h = get nh 0
403for k ulong 64
404v = add k 1
405for j ulong 4096
406put buf j uchar v
407end
408if ne node_publish h add rem v 2 1 buf 4096 long v
409ret false
410end
411end
412if ne node_publish h 1 buf 1 neg long 2
413ret false
414end
415if ne mapget h 16 8 64
416ret false
417end
418note The first version still has its original bytes after arena exhaustion.
419if ne node_read h 1 buf 4096
420ret false
421end
422for j ulong 4096
423if ne get buf j 1
424ret false
425end
426end
427ret true
428end
429
430fn node_processes to int
431put nh 0 node_new
432if le get nh 0 0
433ret 80
434end
435for i ulong 6
436put nf i neg int 1
437end
438result = int 0
439for i ulong 6
440fd = eventfd uint 0
441if lt fd 0
442result = 81
443break
444end
445put nf i int fd
446end
447if eq result 0
448for i ulong 2
449pid = fork
450if eq pid 0
451exit node_worker i
452end
453if lt pid 0
454result = 82
455break
456end
457put np i pid
458end
459end
460if eq result 0
461if not node_publish_batch
462result = 83
463end
464end
465if eq result 0
466for i ulong 2
467if ne node_notify get nf i 0
468result = 84
469end
470end
471end
472if eq result 0
473for i ulong 2
474r = waitio get nf add i 4 1 5000000000
475if ne r 1
476result = 85
477end
478end
479end
480if eq node_stop get nh 0 0
481for i ulong 2
482if ge get nf add i 2 0
483r = node_notify get nf add i 2
484end
485end
486else
487result = 86
488end
489for i ulong 2
490if gt get np i 0
491note Bounded nonblocking reap, then SIGKILL the exact child if still live.
492done = false
493for j ulong 100
494r = waitpid int get np i ns 0 4 1
495if eq r get np i
496done = true
497if or ne get ns 0 0 ne get ns 1 0
498result = 87
499end
500break
501end
502if ne r 0
503result = 88
504break
505end
506r = sleepns 10000000
507end
508if not done
509r = killpid int get np i 9
510r = waitpid int get np i ns 0 4 0
511result = 89
512end
513end
514end
515for i ulong 6
516if ge get nf i 0
517if ne fclose get nf i 0
518result = 90
519end
520end
521end
522if ne mapdrop get nh 0 0
523result = 91
524end
525ret result
526end
527
528fn main to int
529node_refusals
530node_faults
531node_wait_checks
532r = node_processes
533if ne r 0
534ret r
535end
536if ne write 1 nok 0 len nok long len nok
537ret 92
538end
539ret 0
540end