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 / profile / cases / lock-943d955.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Cross-process lock for the authority store. A lock is an O_EXCL file containing the owner pid. |
| 3 | note A contending process retries with a bounded sleep; a lock whose pid is gone is reclaimed. |
| 4 | note This serializes the read-check-persist window. It does not serialize the effect itself. |
| 5 | global lb uchar 8 |
| 6 | text lpath "build/authority-store/lock" |
| 7 | |
| 8 | fn lpid p ulong to void |
| 9 | put lb 0 uchar and p 255 |
| 10 | put lb 1 uchar and shr p 8 255 |
| 11 | put lb 2 uchar and shr p 16 255 |
| 12 | put lb 3 uchar and shr p 24 255 |
| 13 | put lb 4 uchar and shr p 32 255 |
| 14 | put lb 5 uchar and shr p 40 255 |
| 15 | put lb 6 uchar and shr p 48 255 |
| 16 | put lb 7 uchar shr p 56 |
| 17 | end |
| 18 | |
| 19 | fn lget to ulong |
| 20 | v = ulong 0 |
| 21 | for i ulong 8 |
| 22 | v = or v shl ulong get lb i mul i 8 |
| 23 | end |
| 24 | ret v |
| 25 | end |
| 26 | |
| 27 | fn ltake p ulong to ulong |
| 28 | fd = fopen lpath 0 len lpath 193 384 |
| 29 | if lt fd 0 |
| 30 | if eq fd -17 |
| 31 | ret 20 |
| 32 | end |
| 33 | ret 40 |
| 34 | end |
| 35 | lpid p |
| 36 | n = write int fd lb 0 8 |
| 37 | if ne n 8 |
| 38 | if ne fclose int fd 0 |
| 39 | ret 30 |
| 40 | end |
| 41 | ret 31 |
| 42 | end |
| 43 | if ne fclose int fd 0 |
| 44 | ret 31 |
| 45 | end |
| 46 | ret 0 |
| 47 | end |
| 48 | |
| 49 | fn lread to ulong |
| 50 | fd = fopen lpath 0 len lpath 0 0 |
| 51 | if lt fd 0 |
| 52 | ret 32 |
| 53 | end |
| 54 | n = read int fd lb 0 8 |
| 55 | if ne fclose int fd 0 |
| 56 | ret 33 |
| 57 | end |
| 58 | if ne n 8 |
| 59 | ret 34 |
| 60 | end |
| 61 | ret lget |
| 62 | end |
| 63 | |
| 64 | fn lrm to ulong |
| 65 | r = unlink lpath 0 len lpath |
| 66 | if lt r 0 |
| 67 | ret 41 |
| 68 | end |
| 69 | ret 0 |
| 70 | end |
| 71 | |
| 72 | fn ltry p ulong to ulong |
| 73 | r = ltake p |
| 74 | if eq r 0 |
| 75 | ret 0 |
| 76 | end |
| 77 | if ne r 20 |
| 78 | ret r |
| 79 | end |
| 80 | q = lread |
| 81 | if or eq q 32 or eq q 33 eq q 34 |
| 82 | ret 40 |
| 83 | end |
| 84 | k = killpid int q 0 |
| 85 | if ne k -3 |
| 86 | ret 20 |
| 87 | end |
| 88 | r = lrm |
| 89 | if ne r 0 |
| 90 | ret 41 |
| 91 | end |
| 92 | ret ltake p |
| 93 | end |
| 94 | |
| 95 | fn lacq p ulong tries ulong to ulong |
| 96 | i = ulong 0 |
| 97 | while lt i tries |
| 98 | r = ltry p |
| 99 | if eq r 0 |
| 100 | ret 0 |
| 101 | end |
| 102 | if ne r 20 |
| 103 | ret r |
| 104 | end |
| 105 | if ne sleepns 1000000 0 |
| 106 | ret 42 |
| 107 | end |
| 108 | i = add i 1 |
| 109 | end |
| 110 | ret 22 |
| 111 | end |
| 112 | |
| 113 | fn lrel to ulong |
| 114 | ret lrm |
| 115 | end |