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