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 / pool-gate.om

1453 lines · 29558 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 Omega Node epoch pool. Concatenate after transport.om, before super.om.
258note K arenas (1..8) and one control page are mapped before any worker fork,
259note so every forked worker inherits the same shared mappings.
260note Single writer: the creator writes the current epoch, the arena table and
261note every arena record. Worker w alone writes hold word w while it lives;
262note the creator clears hold w only after it has reaped worker w.
263note Control u64: 0 magic, 8 ABI, 16 current epoch, 24 stop, 32 creator PID,
264note 40 arena count K, 48 worker slots W, 64+8k arena handle k,
265note 128+8k epoch of arena k (0 free), 256+8w hold of worker w (0 none),
266note 768+8w worker PID, 1280+8w worker state (0 empty, 1 live, 2 reaped).
267const PMAGIC ulong 1347374924
268const PCAP ulong 8
269const PWCAP ulong 64
270const PBYTES ulong 4096
271
272fn pool_valid c long to bool
273ret and eq mapget c 0 8 PMAGIC eq mapget c 8 8 1
274end
275
276fn pool_mine c long to bool
277ret eq mapget c 32 8 ulong getpid
278end
279
280fn pool_arena c long k ulong to long
281ret long mapget c add 64 mul k 8 8
282end
283
284note Drop the first n arenas and the control page of a pool being refused.
285fn pool_unwind c long n ulong to long
286for i ulong n bound 8
287r = mapdrop pool_arena c i
288end
289ret mapdrop c
290end
291
292note Return the control handle, or -7 bad size, or a negative mapping errno.
293note Arena 0 starts as epoch 1. Call before forking any worker.
294fn pool_new k ulong w ulong to long
295if or eq k 0 gt k PCAP
296ret neg long 7
297end
298if or eq w 0 gt w PWCAP
299ret neg long 7
300end
301c = mapnew neg int 1 0 PBYTES 3 33
302if le c 0
303ret c
304end
305r = mapset c 32 8 ulong getpid
306r = mapset c 40 8 k
307r = mapset c 48 8 w
308for i ulong k bound 8
309h = node_new
310if le h 0
311r = pool_unwind c i
312ret h
313end
314r = mapset c add 64 mul i 8 8 ulong h
315end
316r = node_reset pool_arena c 0 1
317r = mapset c 128 8 1
318r = mapset c 16 8 1
319r = mapset c 8 8 1
320r = mapset c 0 8 PMAGIC
321ret c
322end
323
324note Arena index holding epoch e, or -5 when no arena holds it.
325fn pool_slot c long e ulong to long
326k = mapget c 40 8
327if gt k PCAP
328ret neg long 5
329end
330for i ulong k bound 8
331if eq mapget c add 128 mul i 8 8 e
332ret long i
333end
334end
335ret neg long 5
336end
337
338note Creator: the arena of the current epoch, or a negative refusal.
339fn pool_current c long to long
340if not pool_valid c
341ret neg long 5
342end
343k = pool_slot c mapget c 16 8
344if lt k 0
345ret k
346end
347ret pool_arena c ulong k
348end
349
350fn pool_epoch c long to ulong
351ret mapget c 16 8
352end
353
354note Worker step: store hold e for worker w, then read the epoch again. mapset
355note and mapget fence both sides, so a creator that moved the epoch before it
356note scanned holds either sees this hold or this read sees the move. Returns
357note the arena of e, -8 when the epoch moved, or -5 when no arena holds e.
358note Every refusal clears the hold.
359fn pool_hold c long w ulong e ulong to long
360if ge w mapget c 48 8
361ret neg long 7
362end
363hw = add 256 mul w 8
364r = mapset c hw 8 e
365if ne mapget c 16 8 e
366r = mapset c hw 8 0
367ret neg long 8
368end
369k = pool_slot c e
370if lt k 0
371r = mapset c hw 8 0
372ret k
373end
374h = pool_arena c ulong k
375if ne node_epoch h e
376r = mapset c hw 8 0
377ret neg long 5
378end
379ret h
380end
381
382note Worker w holds the current epoch and returns its arena. Returns -1
383note stopped, -5 invalid, -7 bad worker, -8 the epoch kept moving.
384fn pool_acquire c long w ulong to long
385if not pool_valid c
386ret neg long 5
387end
388if ge w mapget c 48 8
389ret neg long 7
390end
391for t ulong 16
392if ne mapget c 24 8 0
393r = mapset c add 256 mul w 8 8 0
394ret neg long 1
395end
396h = pool_hold c w mapget c 16 8
397if ne h neg long 8
398ret h
399end
400end
401ret neg long 8
402end
403
404note Worker w gives up its hold. Its old arena must not be read afterwards.
405fn pool_release c long w ulong to long
406if ge w mapget c 48 8
407ret neg long 7
408end
409ret mapset c add 256 mul w 8 8 0
410end
411
412fn pool_held c long e ulong to bool
413n = mapget c 48 8
414for w ulong n bound 64
415if eq mapget c add 256 mul w 8 8 e
416ret true
417end
418end
419ret false
420end
421
422note Creator step: retire arena i and open epoch n in it, then publish n.
423fn pool_reuse c long i ulong n ulong to long
424off = add 128 mul i 8
425r = mapset c off 8 0
426r = node_reset pool_arena c i n
427if lt r 0
428ret r
429end
430r = mapset c off 8 n
431r = mapset c 16 8 n
432ret long n
433end
434
435note Creator step: an arena that is free or whose older epoch no worker holds,
436note or -9. Call only after the current epoch has moved past that arena.
437fn pool_pick c long to long
438e = mapget c 16 8
439k = mapget c 40 8
440for i ulong k bound 8
441ek = mapget c add 128 mul i 8 8
442if ne ek e
443if or eq ek 0 not pool_held c ek
444ret long i
445end
446end
447end
448ret neg long 9
449end
450
451note Creator: open the next epoch in a free or unheld arena and make it current.
452note Returns the new epoch, -1 stopped, -5 invalid, -6 not creator, or -9 when
453note every other arena is still held: the pool fails closed, nothing changes.
454fn pool_roll c long to long
455if not pool_valid c
456ret neg long 5
457end
458if not pool_mine c
459ret neg long 6
460end
461if ne mapget c 24 8 0
462ret neg long 1
463end
464k = pool_pick c
465if lt k 0
466ret k
467end
468ret pool_reuse c ulong k add mapget c 16 8 1
469end
470
471note Creator: publish to the current epoch; when it is full, roll once and retry.
472note Returns the version, or the refusal of the publish or of the roll.
473fn pool_publish c long topics ulong src view uchar 4096 n ulong to long
474h = pool_current c
475if lt h 0
476ret h
477end
478v = node_publish h topics src n
479if ne v neg long 2
480ret v
481end
482r = pool_roll c
483if lt r 0
484ret r
485end
486h = pool_current c
487if lt h 0
488ret h
489end
490ret node_publish h topics src n
491end
492
493fn pool_stop c long to long
494if not pool_valid c
495ret neg long 5
496end
497if not pool_mine c
498ret neg long 6
499end
500ret mapset c 24 8 1
501end
502
503fn pool_stopped c long to bool
504ret ne mapget c 24 8 0
505end
506
507note Creator: record worker w's PID and state for observers.
508fn pool_setw c long w ulong pid ulong st ulong to long
509if not pool_mine c
510ret neg long 6
511end
512if ge w mapget c 48 8
513ret neg long 7
514end
515r = mapset c add 768 mul w 8 8 pid
516ret mapset c add 1280 mul w 8 8 st
517end
518
519note Creator: clear hold w. Only after worker w has been reaped.
520fn pool_clear c long w ulong to long
521if not pool_mine c
522ret neg long 6
523end
524if ge w mapget c 48 8
525ret neg long 7
526end
527ret mapset c add 256 mul w 8 8 0
528end
529
530note Drop every mapping of the pool in this process. Other processes keep
531note their own inherited mappings until they drop them or exit.
532fn pool_free c long to long
533ret pool_unwind c mapget c 40 8
534end
535note Omega Node worker supervision. Concatenate after transport.om and pool.om.
536note The creator forks trusted workers from its own binary: sup_spawn returns 0
537note in the child, which runs its worker function and exits with its result.
538note Death is seen without polling PIDs. Worker w alone holds the write end of
539note its close-on-exec life pipe, so its exit hangs up the creator's read end.
540note Every worker holds the read end of the creator's life pipe, whose only
541note writer is the creator, so creator death hangs up every worker.
542note Per worker: wake, stop and ack eventfds, kept across respawns.
543note Workers must not exec: exec closes the life pipe and reads as death.
544global swpid long 64
545global swwake int 64
546global swstop int 64
547global swack int 64
548global swlife int 64
549global swst ulong 64
550global swpw ulong 64
551global swpar int 2
552global swme ulong 1
553global swws uchar 4
554global swpf uchar 8
555global swpoll uchar 1024
556
557fn sup_fd off ulong to int
558v = ulong get swpf off
559v = or v shl ulong get swpf add off 1 8
560v = or v shl ulong get swpf add off 2 16
561v = or v shl ulong get swpf add off 3 24
562ret int v
563end
564
565note Poll record j at swpoll offset off; a negative fd is stored as itself
566note (bit pattern) so the kernel ignores that record.
567fn sup_gpfd off ulong fd int to void
568u = bitcast uint fd
569for i ulong 4
570put swpoll add off i uchar and shr u uint mul i 8 255
571end
572put swpoll add off 4 1
573put swpoll add off 5 0
574put swpoll add off 6 0
575put swpoll add off 7 0
576end
577
578fn sup_lpfd p edit uchar 24 off ulong fd int to void
579for i ulong 4
580put p add off i uchar and shr uint fd uint mul i 8 255
581end
582put p add off 4 1
583end
584
585note Read and reset an eventfd counter: the number of hints since the last
586note take, 0 when none is pending, or a negative errno.
587fn sup_take fd int to long
588buf = new uchar 8 0
589for i ulong 64
590r = read fd buf 0 8
591if eq r 8
592v = ulong 0
593for j ulong 8
594v = or v shl ulong get buf j mul j 8
595end
596ret long and v 9223372036854775807
597end
598if eq r neg long 11
599ret 0
600end
601if ne r neg long 4
602if lt r 0
603ret r
604end
605ret neg long 5
606end
607end
608ret neg long 4
609end
610
611fn sup_closeall to void
612for i ulong 64
613if ge get swwake i 0
614r = fclose get swwake i
615end
616if ge get swstop i 0
617r = fclose get swstop i
618end
619if ge get swack i 0
620r = fclose get swack i
621end
622if ge get swlife i 0
623r = fclose get swlife i
624end
625put swwake i neg int 1
626put swstop i neg int 1
627put swack i neg int 1
628put swlife i neg int 1
629end
630for i ulong 2
631if ge get swpar i 0
632r = fclose get swpar i
633end
634put swpar i neg int 1
635end
636end
637
638note Creator: create the creator life pipe and three eventfds per worker slot.
639note Call once after pool_new and before sup_spawn. 0 or a negative errno;
640note a failure closes everything this call opened.
641fn sup_init c long to long
642if not pool_mine c
643ret neg long 6
644end
645for i ulong 64
646put swpid i 0
647put swst i 0
648put swwake i neg int 1
649put swstop i neg int 1
650put swack i neg int 1
651put swlife i neg int 1
652end
653put swme 0 0
654put swpar 0 neg int 1
655put swpar 1 neg int 1
656r = pipefds swpf 0 8 524288
657if lt r 0
658ret r
659end
660put swpar 0 sup_fd 0
661put swpar 1 sup_fd 4
662n = mapget c 48 8
663for i ulong n bound 64
664a = eventfd uint 0
665if lt a 0
666sup_closeall
667ret a
668end
669put swwake i int a
670a = eventfd uint 0
671if lt a 0
672sup_closeall
673ret a
674end
675put swstop i int a
676a = eventfd uint 0
677if lt a 0
678sup_closeall
679ret a
680end
681put swack i int a
682end
683ret 0
684end
685
686note Creator: fork worker w. Returns 0 in the child, the child PID in the
687note creator, -7 bad slot, -16 slot still live, or a negative errno. The
688note child keeps only its own life write end; the creator keeps the read end.
689fn sup_spawn c long w ulong to long
690if not pool_mine c
691ret neg long 6
692end
693if ge w mapget c 48 8
694ret neg long 7
695end
696if ne get swpid w 0
697ret neg long 16
698end
699r = pipefds swpf 0 8 524288
700if lt r 0
701ret r
702end
703rd = sup_fd 0
704wr = sup_fd 4
705pid = fork
706if eq pid 0
707r = fclose get swpar 1
708put swpar 1 neg int 1
709r = fclose rd
710put swlife w wr
711put swme 0 add w 1
712ret 0
713end
714r = fclose wr
715if lt pid 0
716r = fclose rd
717ret pid
718end
719put swpid w pid
720put swlife w rd
721r = pool_setw c w ulong pid 1
722ret pid
723end
724
725note Worker: 3 creator gone, 2 stop, 1 wake hint, 0 timeout, negative errno.
726note Creator loss outranks stop, which outranks work. A set stop flag makes
727note the poll nonblocking. Hints are drained; rescan shared state after 1.
728fn sup_wwait c long w ulong interval long to long
729p = new uchar 24 0
730if ge w 64
731ret neg long 7
732end
733iv = interval
734if pool_stopped c
735iv = 0
736end
737sup_lpfd p 0 get swwake w
738sup_lpfd p 8 get swstop w
739sup_lpfd p 16 get swpar 0
740r = pollfds p 0 24 iv
741if lt r 0
742ret r
743end
744if ne get p 22 0
745ret 3
746end
747if pool_stopped c
748ret 2
749end
750if ne and get p 14 56 0
751ret neg long 5
752end
753if ne and get p 14 1 0
754r = node_drain get swstop w
755ret 2
756end
757if ne and get p 6 56 0
758ret neg long 5
759end
760if ne and get p 6 1 0
761r = node_drain get swwake w
762if lt r 0
763ret r
764end
765ret 1
766end
767ret 0
768end
769
770note Worker or creator: post a hint on worker w's ack eventfd.
771fn sup_ack w ulong to long
772if ge w 64
773ret neg long 7
774end
775ret node_notify get swack w
776end
777
778note Creator: wake worker w with a work hint.
779fn sup_wake w ulong to long
780if ge w 64
781ret neg long 7
782end
783if le get swpid w 0
784ret neg long 3
785end
786ret node_notify get swwake w
787end
788
789fn sup_live c long to ulong
790n = mapget c 48 8
791k = ulong 0
792for w ulong n bound 64
793if gt get swpid w 0
794k = add k 1
795end
796end
797ret k
798end
799
800note Creator: reap worker w, then release what it held. A worker that has not
801note exited within about one second gets SIGKILL first. Only after the exact
802note PID is reaped are its hold, PID and life pipe released. Returns the raw
803note wait status, -3 no live worker, or the waitpid errno (nothing released).
804fn sup_reap c long w ulong to long
805if ge w 64
806ret neg long 7
807end
808pid = get swpid w
809if le pid 0
810ret neg long 3
811end
812done = false
813for t ulong 100
814r = waitpid int pid swws 0 4 1
815if eq r pid
816done = true
817break
818end
819if ne r 0
820ret r
821end
822r = sleepns 10000000
823end
824if not done
825r = killpid int pid 9
826r = waitpid int pid swws 0 4 0
827if ne r pid
828ret r
829end
830end
831st = ulong get swws 0
832st = or st shl ulong get swws 1 8
833put swst w st
834r = fclose get swlife w
835put swlife w neg int 1
836put swpid w 0
837r = pool_clear c w
838r = pool_setw c w 0 2
839ret long st
840end
841
842note Creator: wait up to interval ns for deaths and, when acks is true, acks.
843note Ack counts are left pending for sup_take. Every worker whose life pipe
844note hung up is reaped. Returns the number reaped, or a negative errno.
845fn sup_pollx c long interval long acks bool to long
846n = mapget c 48 8
847m = ulong 0
848for w ulong n bound 64
849if gt get swpid w 0
850o = mul m 16
851if acks
852sup_gpfd o get swack w
853else
854sup_gpfd o neg int 1
855end
856sup_gpfd add o 8 get swlife w
857put swpw m w
858m = add m 1
859end
860end
861if eq m 0
862ret 0
863end
864r = pollfds swpoll 0 mul m 16 interval
865if lt r 0
866ret r
867end
868dead = long 0
869for j ulong m bound 64
870o = add mul j 16 14
871if ne get swpoll o 0
872r = sup_reap c get swpw j
873if lt r 0
874ret r
875end
876dead = add dead 1
877end
878end
879ret dead
880end
881
882note Creator: wait up to interval ns for acks and deaths (see sup_pollx).
883fn sup_poll c long interval long to long
884ret sup_pollx c interval true
885end
886
887note Creator: stop admission, post every live worker's stop eventfd, then reap
888note until all have exited or interval ns pass; survivors get SIGKILL and are
889note reaped. Returns how many had to be killed, or a refusal.
890fn sup_stop c long interval long to long
891r = pool_stop c
892if lt r 0
893ret r
894end
895n = mapget c 48 8
896for w ulong n bound 64
897if gt get swpid w 0
898r = node_notify get swstop w
899end
900end
901dl = add monons interval
902for t ulong 100000
903if eq sup_live c 0
904ret 0
905end
906now = monons
907if ge now dl
908break
909end
910r = sup_pollx c sub dl now false
911if lt r 0
912if ne r neg long 4
913break
914end
915end
916end
917killed = long 0
918for w ulong n bound 64
919if gt get swpid w 0
920r = killpid int get swpid w 9
921r = sup_reap c w
922killed = add killed 1
923end
924end
925ret killed
926end
927note Concatenate after transport.om, pool.om and super.om. CPU-only acceptance
928note of epoch turnover, held old versions, worker death, respawn, orphaning and
929note bounded teardown with real forked processes. No GPU, model or network.
930text pok "PASS node pool\n"
931global ptc long 1
932global ptm long 1
933global pth long 1
934global ptws uchar 4
935
936fn pt_ok ok bool code int to void
937if not ok
938exit code
939end
940end
941
942fn pt_fd4 p view uchar 8 off ulong to int
943v = ulong get p off
944v = or v shl ulong get p add off 1 8
945v = or v shl ulong get p add off 2 16
946v = or v shl ulong get p add off 3 24
947ret int v
948end
949
950fn pt_byte e ulong v ulong to uchar
951ret uchar and add mul e 16 v 255
952end
953
954note Creator: publish n full records into the current epoch, bytes f(epoch, v).
955fn pt_fill c long n ulong to bool
956buf = new uchar 4096 0
957e = pool_epoch c
958h = pool_current c
959if lt h 0
960ret false
961end
962for k ulong n bound 64
963v = add mapget h 16 8 1
964b = pt_byte e v
965for j ulong 4096
966put buf j b
967end
968if ne pool_publish c 1 buf 4096 long v
969ret false
970end
971end
972ret true
973end
974
975note 0 when arena h holds exactly n complete records of its own epoch, else a code.
976fn pt_verify h long n ulong to int
977buf = new uchar 4096 0
978e = node_epoch h
979cur = ulong 0
980cnt = ulong 0
981for k ulong 65
982v = node_next h cur 1
983if eq v 0
984break
985end
986if lt v 0
987ret 1
988end
989cur = ulong v
990if ne node_read h cur buf 4096
991ret 2
992end
993b = pt_byte e cur
994for j ulong 4096
995if ne get buf j b
996ret 3
997end
998end
999cnt = add cnt 1
1000end
1001if ne cnt n
1002ret 4
1003end
1004ret 0
1005end
1006
1007fn pt_refusals to void
1008pt_ok eq pool_new 0 4 neg long 7 10
1009pt_ok eq pool_new 9 4 neg long 7 11
1010pt_ok eq pool_new 2 0 neg long 7 12
1011pt_ok eq pool_new 2 65 neg long 7 13
1012c = pool_new 2 4
1013pt_ok gt c 0 14
1014pt_ok eq pool_epoch c 1 15
1015pt_ok eq pool_acquire c 4 neg long 7 16
1016pt_ok eq pool_release c 4 neg long 7 17
1017pt_ok eq node_epoch pool_current c 1 18
1018pt_ok eq node_reset pool_current c 0 neg long 7 19
1019pt_ok eq pool_free c 0 20
1020end
1021
1022note One process: the creator also plays worker slots 0 and 1, whose holds are
1023note plain words. Epoch numbers never repeat; arenas are reused only unheld.
1024fn pt_epochs to void
1025dst = new uchar 4096 0
1026c = pool_new 2 4
1027pt_ok gt c 0 21
1028pt_ok pt_fill c 1 22
1029h1 = pool_acquire c 0
1030pt_ok eq h1 pool_current c 23
1031pt_ok eq mapget c 256 8 1 24
1032pt_ok eq pool_roll c 2 25
1033pt_ok ne pool_current c h1 26
1034pt_ok eq node_next pool_current c 0 1 0 27
1035pt_ok eq pool_roll c neg long 9 28
1036pt_ok eq pool_epoch c 2 29
1037pt_ok eq pt_verify h1 1 0 30
1038pt_ok eq pool_release c 0 0 31
1039pt_ok eq pool_roll c 3 32
1040pt_ok eq pool_slot c 3 0 33
1041pt_ok eq node_read h1 1 dst neg long 7 34
1042pt_ok eq node_epoch h1 3 35
1043pt_ok pt_fill c 64 36
1044for j ulong 4096
1045put dst j pt_byte 4 1
1046end
1047pt_ok eq pool_publish c 1 dst 4096 1 37
1048pt_ok eq pool_epoch c 4 38
1049h4 = pool_acquire c 1
1050pt_ok eq node_epoch h4 4 39
1051pt_ok pt_fill c 63 40
1052pt_ok eq pool_epoch c 4 41
1053pt_ok eq pool_roll c 5 42
1054pt_ok pt_fill c 64 43
1055pt_ok eq pool_publish c 1 dst 1 neg long 9 44
1056pt_ok eq pool_epoch c 5 45
1057pt_ok eq pt_verify h4 64 0 46
1058pt_ok eq pool_stop c 0 47
1059pt_ok eq pool_roll c neg long 1 48
1060pt_ok eq pool_acquire c 2 neg long 1 49
1061pt_ok eq mapget c 272 8 0 50
1062pt_ok eq pool_free c 0 51
1063end
1064
1065note The announce race, made deterministic by running the protocol steps in
1066note the order that breaks a hold without its recheck: worker 2 read epoch e,
1067note the creator moved past e and picked e's arena, then worker 2 stores its
1068note hold. The recheck must refuse; reuse of the arena must then proceed.
1069fn pt_race to void
1070c = pool_new 2 4
1071pt_ok gt c 0 140
1072pt_ok pt_fill c 1 141
1073e = pool_epoch c
1074pt_ok eq pool_roll c 2 142
1075pt_ok eq pool_pick c 0 143
1076pt_ok eq pool_hold c 2 e neg long 8 144
1077pt_ok eq mapget c 272 8 0 145
1078pt_ok eq pool_reuse c 0 3 3 146
1079pt_ok eq pool_acquire c 2 pool_current c 147
1080pt_ok eq pool_hold c 4 3 neg long 7 148
1081pt_ok eq pool_free c 0 149
1082end
1083
1084note Worker phase: 1 acquire and verify, 2 verify the held epoch again,
1085note 3 release, acquire the current epoch and verify, 4 stall ignoring stop.
1086fn pt_step c long w ulong ph ulong to int
1087buf = new uchar 4096 0
1088m = get ptm 0
1089n = mapget m 8 8
1090if eq ph 1
1091if ne pool_roll c neg long 6
1092ret 11
1093end
1094if ne pool_stop c neg long 6
1095ret 12
1096end
1097if ne pool_clear c w neg long 6
1098ret 13
1099end
1100h = pool_acquire c w
1101if le h 0
1102ret 14
1103end
1104put pth 0 h
1105if ne node_publish h 1 buf 1 neg long 6
1106ret 15
1107end
1108ret pt_verify h n
1109end
1110if eq ph 2
1111ret pt_verify get pth 0 mapget m 16 8
1112end
1113if eq ph 3
1114r = pool_release c w
1115h = pool_acquire c w
1116if le h 0
1117ret 16
1118end
1119put pth 0 h
1120ret pt_verify h n
1121end
1122if eq ph 4
1123r = sleepns 30000000000
1124ret 17
1125end
1126ret 18
1127end
1128
1129fn pt_worker w ulong to int
1130c = get ptc 0
1131m = get ptm 0
1132put pth 0 0
1133for it ulong 64
1134r = sup_wwait c w 5000000000
1135if eq r 2
1136if gt get pth 0 0
1137r = pool_release c w
1138end
1139ret 0
1140end
1141if eq r 3
1142ret 41
1143end
1144if eq r 0
1145ret 42
1146end
1147if ne r 1
1148ret 43
1149end
1150res = pt_step c w mapget m 0 8
1151r = mapset m add 64 mul w 8 8 ulong res
1152if ne sup_ack w 0
1153ret 44
1154end
1155end
1156ret 45
1157end
1158
1159note Creator: wake worker w for phase ph and wait up to about 5 s for its ack.
1160note Returns the worker's phase result, or 90 no ack, 91 the worker died.
1161fn pt_phase c long w ulong ph ulong to int
1162m = get ptm 0
1163r = mapset m 0 8 ph
1164r = mapset m add 64 mul w 8 8 99
1165if ne sup_wake w 0
1166ret 92
1167end
1168for t ulong 500
1169if gt sup_take get swack w 0
1170ret int mapget m add 64 mul w 8 8
1171end
1172if le get swpid w 0
1173ret 91
1174end
1175r = sup_poll c 10000000
1176end
1177ret 90
1178end
1179
1180fn pt_spawn c long w ulong to bool
1181p = sup_spawn c w
1182if eq p 0
1183exit pt_worker w
1184end
1185ret gt p 0
1186end
1187
1188fn pt_procs to void
1189c = pool_new 2 4
1190pt_ok gt c 0 52
1191put ptc 0 c
1192m = mapnew neg int 1 0 4096 3 33
1193pt_ok gt m 0 53
1194put ptm 0 m
1195pt_ok eq sup_init c 0 54
1196pt_ok pt_fill c 8 55
1197r = mapset m 8 8 8
1198r = mapset m 16 8 8
1199pt_ok pt_spawn c 0 56
1200pt_ok pt_spawn c 1 57
1201pt_ok pt_spawn c 2 58
1202pt_ok eq sup_spawn c 2 neg long 16 59
1203for w ulong 3
1204pt_ok eq pt_phase c w 1 0 60
1205end
1206pt_ok eq mapget c 272 8 1 61
1207pt_ok eq mapget c 1288 8 1 62
1208pt_ok eq pool_roll c 2 63
1209pt_ok pt_fill c 5 64
1210pt_ok eq pool_roll c neg long 9 65
1211note An older epoch stays complete for its holder after newer publication.
1212pt_ok eq pt_phase c 0 2 0 66
1213r = mapset m 8 8 5
1214pt_ok eq pt_phase c 0 3 0 67
1215pt_ok eq pt_phase c 1 3 0 68
1216pt_ok eq mapget c 256 8 2 69
1217pt_ok eq pool_roll c neg long 9 70
1218note Kill the last holder of epoch 1. Its death is seen through its life pipe.
1219pt_ok eq killpid int get swpid 2 9 0 71
1220dead = long 0
1221for t ulong 500
1222dead = add dead sup_poll c 10000000
1223if gt dead 0
1224break
1225end
1226end
1227pt_ok eq dead 1 72
1228pt_ok eq and get swst 2 127 9 73
1229pt_ok eq mapget c 272 8 0 74
1230pt_ok eq mapget c 1296 8 2 75
1231pt_ok eq pool_roll c 3 76
1232pt_ok pt_fill c 3 77
1233r = mapset m 8 8 3
1234pt_ok pt_spawn c 2 78
1235pt_ok eq pt_phase c 2 3 0 79
1236r = mapset m 0 8 4
1237pt_ok eq sup_wake 1 0 80
1238r = sleepns 100000000
1239note A pending ack from the stalled worker must not turn the stop wait into a
1240note spin that kills it before the 2 s deadline.
1241pt_ok eq sup_ack 1 0 89
1242t0 = monons
1243k = sup_stop c 2000000000
1244pt_ok ge sub monons t0 1900000000 90
1245pt_ok eq k 1 81
1246pt_ok eq get swst 0 0 82
1247pt_ok eq get swst 2 0 83
1248pt_ok eq and get swst 1 127 9 84
1249pt_ok eq waitpid neg int 1 ptws 0 4 1 neg long 10 85
1250pt_ok eq sup_live c 0 86
1251sup_closeall
1252pt_ok eq pool_free c 0 87
1253pt_ok eq mapdrop m 0 88
1254end
1255
1256note A creator that exits without stopping its worker must not leave the worker
1257note waiting: the worker sees the creator's life pipe hang up and reports 3.
1258fn pt_orphan to void
1259pf = new uchar 8 0
1260b = new uchar 1 0
1261pt_ok ge pipefds pf 0 8 524288 0 93
1262rd = pt_fd4 pf 0
1263wr = pt_fd4 pf 4
1264s = fork
1265if eq s 0
1266c = pool_new 1 1
1267if le c 0
1268exit 50
1269end
1270if ne sup_init c 0
1271exit 51
1272end
1273put ptc 0 c
1274p = sup_spawn c 0
1275if eq p 0
1276r = sup_wwait c 0 5000000000
1277put b 0 uchar and ulong add r 128 255
1278r = write wr b 0 1
1279exit 0
1280end
1281exit 0
1282end
1283pt_ok gt s 0 94
1284r = fclose wr
1285pt_ok eq waitpid int s ptws 0 4 0 s 95
1286pt_ok eq get ptws 0 0 96
1287pt_ok gt waitio rd 1 6000000000 0 97
1288pt_ok eq read rd b 0 1 1 98
1289pt_ok eq get b 0 131 99
1290r = fclose rd
1291end
1292
1293note Stress: 0 when arena h shows only complete records of epoch e from start
1294note to end of one hold. A reclaimed held arena changes its epoch, drops its
1295note count or rewrites bytes, so a missing hold check shows up here.
1296fn pt_scan h long e ulong to int
1297buf = new uchar 4096 0
1298if ne node_epoch h e
1299ret 1
1300end
1301cur = ulong 0
1302for k ulong 65
1303v = node_next h cur 1
1304if eq v 0
1305break
1306end
1307if lt v 0
1308ret 2
1309end
1310cur = ulong v
1311if ne node_read h cur buf 4096
1312ret 3
1313end
1314b = pt_byte e cur
1315for j ulong 4096
1316if ne get buf j b
1317ret 4
1318end
1319end
1320end
1321if ne node_epoch h e
1322ret 5
1323end
1324ret 0
1325end
1326
1327fn pt_sworker w ulong to int
1328c = get ptc 0
1329m = get ptm 0
1330n = ulong 0
1331for it ulong 10000000
1332if pool_stopped c
1333r = mapset m add 64 mul w 8 8 n
1334ret 0
1335end
1336h = pool_acquire c w
1337if eq h neg long 1
1338r = mapset m add 64 mul w 8 8 n
1339ret 0
1340end
1341if gt h 0
1342r = pt_scan h mapget c add 256 mul w 8 8
1343if ne r 0
1344ret add 20 r
1345end
1346q = pool_release c w
1347n = add n 1
1348end
1349end
1350ret 29
1351end
1352
1353fn pt_num v ulong to void
1354d = new uchar 20 0
1355x = v
1356k = ulong 20
1357for i ulong 20
1358k = sub k 1
1359put d k uchar add rem x 10 48
1360x = div x 10
1361if eq x 0
1362break
1363end
1364end
1365r = write 1 d k sub 20 k
1366end
1367
1368text pstress "stress rolls="
1369text pbusy " busy="
1370text preads " reads="
1371text pnl "\n"
1372text psp " "
1373
1374note Two workers acquire, scan and release in a tight loop while the creator
1375note rolls 2000 epochs over two arenas, publishing four records per epoch.
1376fn pt_stress to void
1377c = pool_new 2 2
1378pt_ok gt c 0 120
1379put ptc 0 c
1380m = mapnew neg int 1 0 4096 3 33
1381pt_ok gt m 0 121
1382put ptm 0 m
1383pt_ok eq sup_init c 0 122
1384pt_ok pt_fill c 4 123
1385for w ulong 2
1386p = sup_spawn c w
1387if eq p 0
1388exit pt_sworker w
1389end
1390pt_ok gt p 0 124
1391end
1392rolls = ulong 0
1393busy = ulong 0
1394for i ulong 100000000
1395if ge rolls 2000
1396break
1397end
1398r = pool_roll c
1399if eq r neg long 9
1400busy = add busy 1
1401if eq and busy 1023 0
1402pt_ok eq sup_poll c 0 0 133
1403end
1404cpurelax
1405continue
1406end
1407pt_ok gt r 0 125
1408rolls = add rolls 1
1409pt_ok pt_fill c 4 126
1410end
1411pt_ok eq rolls 2000 127
1412pt_ok eq sup_stop c 5000000000 0 128
1413pt_ok eq get swst 0 0 129
1414pt_ok eq get swst 1 0 130
1415r = write 1 pstress 0 len pstress
1416pt_num rolls
1417r = write 1 pbusy 0 len pbusy
1418pt_num busy
1419r = write 1 preads 0 len preads
1420pt_num mapget m 64 8
1421r = write 1 psp 0 1
1422pt_num mapget m 72 8
1423r = write 1 pnl 0 1
1424sup_closeall
1425pt_ok eq pool_free c 0 131
1426pt_ok eq mapdrop m 0 132
1427end
1428
1429fn pt_isarg to bool
1430a = new uchar 16 0
1431if ne argcnt 2
1432ret false
1433end
1434ret eq argread 1 a 0 16 6
1435end
1436
1437note With one argument (any 6 bytes, "stress"), run only the stress phase.
1438fn main to int
1439if pt_isarg
1440pt_stress
1441ret 0
1442end
1443pt_refusals
1444pt_epochs
1445pt_race
1446pt_orphan
1447pt_procs
1448pt_stress
1449if ne write 1 pok 0 len pok long len pok
1450ret 100
1451end
1452ret 0
1453end