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