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