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.om

278 lines · 6253 bytes Raw Blame History
1note Omega Node epoch pool. Concatenate after transport.om, before super.om.
2note K arenas (1..8) and one control page are mapped before any worker fork,
3note so every forked worker inherits the same shared mappings.
4note Single writer: the creator writes the current epoch, the arena table and
5note every arena record. Worker w alone writes hold word w while it lives;
6note the creator clears hold w only after it has reaped worker w.
7note Control u64: 0 magic, 8 ABI, 16 current epoch, 24 stop, 32 creator PID,
8note 40 arena count K, 48 worker slots W, 64+8k arena handle k,
9note 128+8k epoch of arena k (0 free), 256+8w hold of worker w (0 none),
10note 768+8w worker PID, 1280+8w worker state (0 empty, 1 live, 2 reaped).
11const PMAGIC ulong 1347374924
12const PCAP ulong 8
13const PWCAP ulong 64
14const PBYTES ulong 4096
15
16fn pool_valid c long to bool
17ret and eq mapget c 0 8 PMAGIC eq mapget c 8 8 1
18end
19
20fn pool_mine c long to bool
21ret eq mapget c 32 8 ulong getpid
22end
23
24fn pool_arena c long k ulong to long
25ret long mapget c add 64 mul k 8 8
26end
27
28note Drop the first n arenas and the control page of a pool being refused.
29fn pool_unwind c long n ulong to long
30for i ulong n bound 8
31r = mapdrop pool_arena c i
32end
33ret mapdrop c
34end
35
36note Return the control handle, or -7 bad size, or a negative mapping errno.
37note Arena 0 starts as epoch 1. Call before forking any worker.
38fn pool_new k ulong w ulong to long
39if or eq k 0 gt k PCAP
40ret neg long 7
41end
42if or eq w 0 gt w PWCAP
43ret neg long 7
44end
45c = mapnew neg int 1 0 PBYTES 3 33
46if le c 0
47ret c
48end
49r = mapset c 32 8 ulong getpid
50r = mapset c 40 8 k
51r = mapset c 48 8 w
52for i ulong k bound 8
53h = node_new
54if le h 0
55r = pool_unwind c i
56ret h
57end
58r = mapset c add 64 mul i 8 8 ulong h
59end
60r = node_reset pool_arena c 0 1
61r = mapset c 128 8 1
62r = mapset c 16 8 1
63r = mapset c 8 8 1
64r = mapset c 0 8 PMAGIC
65ret c
66end
67
68note Arena index holding epoch e, or -5 when no arena holds it.
69fn pool_slot c long e ulong to long
70k = mapget c 40 8
71if gt k PCAP
72ret neg long 5
73end
74for i ulong k bound 8
75if eq mapget c add 128 mul i 8 8 e
76ret long i
77end
78end
79ret neg long 5
80end
81
82note Creator: the arena of the current epoch, or a negative refusal.
83fn pool_current c long to long
84if not pool_valid c
85ret neg long 5
86end
87k = pool_slot c mapget c 16 8
88if lt k 0
89ret k
90end
91ret pool_arena c ulong k
92end
93
94fn pool_epoch c long to ulong
95ret mapget c 16 8
96end
97
98note Worker step: store hold e for worker w, then read the epoch again. mapset
99note and mapget fence both sides, so a creator that moved the epoch before it
100note scanned holds either sees this hold or this read sees the move. Returns
101note the arena of e, -8 when the epoch moved, or -5 when no arena holds e.
102note Every refusal clears the hold.
103fn pool_hold c long w ulong e ulong to long
104if ge w mapget c 48 8
105ret neg long 7
106end
107hw = add 256 mul w 8
108r = mapset c hw 8 e
109if ne mapget c 16 8 e
110r = mapset c hw 8 0
111ret neg long 8
112end
113k = pool_slot c e
114if lt k 0
115r = mapset c hw 8 0
116ret k
117end
118h = pool_arena c ulong k
119if ne node_epoch h e
120r = mapset c hw 8 0
121ret neg long 5
122end
123ret h
124end
125
126note Worker w holds the current epoch and returns its arena. Returns -1
127note stopped, -5 invalid, -7 bad worker, -8 the epoch kept moving.
128fn pool_acquire c long w ulong to long
129if not pool_valid c
130ret neg long 5
131end
132if ge w mapget c 48 8
133ret neg long 7
134end
135for t ulong 16
136if ne mapget c 24 8 0
137r = mapset c add 256 mul w 8 8 0
138ret neg long 1
139end
140h = pool_hold c w mapget c 16 8
141if ne h neg long 8
142ret h
143end
144end
145ret neg long 8
146end
147
148note Worker w gives up its hold. Its old arena must not be read afterwards.
149fn pool_release c long w ulong to long
150if ge w mapget c 48 8
151ret neg long 7
152end
153ret mapset c add 256 mul w 8 8 0
154end
155
156fn pool_held c long e ulong to bool
157n = mapget c 48 8
158for w ulong n bound 64
159if eq mapget c add 256 mul w 8 8 e
160ret true
161end
162end
163ret false
164end
165
166note Creator step: retire arena i and open epoch n in it, then publish n.
167fn pool_reuse c long i ulong n ulong to long
168off = add 128 mul i 8
169r = mapset c off 8 0
170r = node_reset pool_arena c i n
171if lt r 0
172ret r
173end
174r = mapset c off 8 n
175r = mapset c 16 8 n
176ret long n
177end
178
179note Creator step: an arena that is free or whose older epoch no worker holds,
180note or -9. Call only after the current epoch has moved past that arena.
181fn pool_pick c long to long
182e = mapget c 16 8
183k = mapget c 40 8
184for i ulong k bound 8
185ek = mapget c add 128 mul i 8 8
186if ne ek e
187if or eq ek 0 not pool_held c ek
188ret long i
189end
190end
191end
192ret neg long 9
193end
194
195note Creator: open the next epoch in a free or unheld arena and make it current.
196note Returns the new epoch, -1 stopped, -5 invalid, -6 not creator, or -9 when
197note every other arena is still held: the pool fails closed, nothing changes.
198fn pool_roll c long to long
199if not pool_valid c
200ret neg long 5
201end
202if not pool_mine c
203ret neg long 6
204end
205if ne mapget c 24 8 0
206ret neg long 1
207end
208k = pool_pick c
209if lt k 0
210ret k
211end
212ret pool_reuse c ulong k add mapget c 16 8 1
213end
214
215note Creator: publish to the current epoch; when it is full, roll once and retry.
216note Returns the version, or the refusal of the publish or of the roll.
217fn pool_publish c long topics ulong src view uchar 4096 n ulong to long
218h = pool_current c
219if lt h 0
220ret h
221end
222v = node_publish h topics src n
223if ne v neg long 2
224ret v
225end
226r = pool_roll c
227if lt r 0
228ret r
229end
230h = pool_current c
231if lt h 0
232ret h
233end
234ret node_publish h topics src n
235end
236
237fn pool_stop c long to long
238if not pool_valid c
239ret neg long 5
240end
241if not pool_mine c
242ret neg long 6
243end
244ret mapset c 24 8 1
245end
246
247fn pool_stopped c long to bool
248ret ne mapget c 24 8 0
249end
250
251note Creator: record worker w's PID and state for observers.
252fn pool_setw c long w ulong pid ulong st ulong to long
253if not pool_mine c
254ret neg long 6
255end
256if ge w mapget c 48 8
257ret neg long 7
258end
259r = mapset c add 768 mul w 8 8 pid
260ret mapset c add 1280 mul w 8 8 st
261end
262
263note Creator: clear hold w. Only after worker w has been reaped.
264fn pool_clear c long w ulong to long
265if not pool_mine c
266ret neg long 6
267end
268if ge w mapget c 48 8
269ret neg long 7
270end
271ret mapset c add 256 mul w 8 8 0
272end
273
274note Drop every mapping of the pool in this process. Other processes keep
275note their own inherited mappings until they drop them or exit.
276fn pool_free c long to long
277ret pool_unwind c mapget c 40 8
278end