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 / authority / lock-gate.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 | global lerr ulong 1 |
| 7 | text lpath "build/authority-store/lock" |
| 8 | |
| 9 | fn lpid p ulong to void |
| 10 | put lb 0 uchar and p 255 |
| 11 | put lb 1 uchar and shr p 8 255 |
| 12 | put lb 2 uchar and shr p 16 255 |
| 13 | put lb 3 uchar and shr p 24 255 |
| 14 | put lb 4 uchar and shr p 32 255 |
| 15 | put lb 5 uchar and shr p 40 255 |
| 16 | put lb 6 uchar and shr p 48 255 |
| 17 | put lb 7 uchar shr p 56 |
| 18 | end |
| 19 | |
| 20 | fn lget to ulong |
| 21 | v = ulong 0 |
| 22 | for i ulong 8 |
| 23 | v = or v shl ulong get lb i mul i 8 |
| 24 | end |
| 25 | ret v |
| 26 | end |
| 27 | |
| 28 | note p10 effect |
| 29 | fn ltake p ulong to ulong |
| 30 | fd = fopen lpath 0 len lpath 193 384 |
| 31 | if lt fd 0 |
| 32 | if eq fd -17 |
| 33 | ret 20 |
| 34 | end |
| 35 | ret 40 |
| 36 | end |
| 37 | lpid p |
| 38 | n = write int fd lb 0 8 |
| 39 | if ne n 8 |
| 40 | if ne fclose int fd 0 |
| 41 | ret 30 |
| 42 | end |
| 43 | ret 31 |
| 44 | end |
| 45 | if ne fclose int fd 0 |
| 46 | ret 31 |
| 47 | end |
| 48 | ret 0 |
| 49 | end |
| 50 | |
| 51 | note p10 effect |
| 52 | fn lread to ulong |
| 53 | put lerr 0 0 |
| 54 | fd = fopen lpath 0 len lpath 0 0 |
| 55 | if lt fd 0 |
| 56 | put lerr 0 32 |
| 57 | ret 0 |
| 58 | end |
| 59 | n = read int fd lb 0 8 |
| 60 | if ne fclose int fd 0 |
| 61 | put lerr 0 33 |
| 62 | ret 0 |
| 63 | end |
| 64 | if ne n 8 |
| 65 | put lerr 0 34 |
| 66 | ret 0 |
| 67 | end |
| 68 | ret lget |
| 69 | end |
| 70 | |
| 71 | note p10 effect |
| 72 | fn lrm to ulong |
| 73 | r = unlink lpath 0 len lpath |
| 74 | if lt r 0 |
| 75 | ret 41 |
| 76 | end |
| 77 | ret 0 |
| 78 | end |
| 79 | |
| 80 | note p10 effect |
| 81 | fn ltry p ulong to ulong |
| 82 | r = ltake p |
| 83 | if eq r 0 |
| 84 | ret 0 |
| 85 | end |
| 86 | if ne r 20 |
| 87 | ret r |
| 88 | end |
| 89 | q = lread |
| 90 | if ne get lerr 0 0 |
| 91 | ret 40 |
| 92 | end |
| 93 | k = killpid int q 0 |
| 94 | if ne k -3 |
| 95 | ret 20 |
| 96 | end |
| 97 | r = lrm |
| 98 | if ne r 0 |
| 99 | ret 41 |
| 100 | end |
| 101 | ret ltake p |
| 102 | end |
| 103 | |
| 104 | note p10 effect |
| 105 | fn lacq p ulong tries ulong to ulong |
| 106 | if gt tries 1000 |
| 107 | ret 43 |
| 108 | end |
| 109 | i = ulong 0 |
| 110 | while lt i tries bound 1000 |
| 111 | r = ltry p |
| 112 | if eq r 0 |
| 113 | ret 0 |
| 114 | end |
| 115 | if ne r 20 |
| 116 | ret r |
| 117 | end |
| 118 | if ne sleepns 1000000 0 |
| 119 | ret 42 |
| 120 | end |
| 121 | i = add i 1 |
| 122 | end |
| 123 | ret 22 |
| 124 | end |
| 125 | |
| 126 | fn lrel to ulong |
| 127 | ret lrm |
| 128 | end |
| 129 | note SPDX-License-Identifier: AGPL-3.0-only |
| 130 | note Lock checks: exclusive acquire, busy refusal, release, a forked waiter, stale-pid reclaim, failed release, |
| 131 | note an owner pid equal to an old lread error code (32), and the lacq retry cap. |
| 132 | text passm "lock checks=10 fails=0\n" |
| 133 | global checks ulong 1 |
| 134 | global fails ulong 1 |
| 135 | global lbuf uchar 8 |
| 136 | global rb uchar 8 |
| 137 | text childf "build/authority-store/lock.child" |
| 138 | text bd "build" |
| 139 | text ad "build/authority-store" |
| 140 | fn check got ulong want ulong to void |
| 141 | put checks 0 add get checks 0 1 |
| 142 | if ne got want |
| 143 | put fails 0 add get fails 0 1 |
| 144 | end |
| 145 | end |
| 146 | fn cbyte c uchar to void |
| 147 | r = fopen childf 0 len childf 0 0 |
| 148 | if lt r 0 |
| 149 | put rb 0 255 |
| 150 | ret |
| 151 | end |
| 152 | n = read int r rb 0 1 |
| 153 | if ne fclose int r 0 |
| 154 | ret |
| 155 | end |
| 156 | end |
| 157 | fn main to int |
| 158 | r = makedir bd 0 len bd 448 |
| 159 | if or eq r 0 eq r -17 |
| 160 | else |
| 161 | ret 90 |
| 162 | end |
| 163 | r = makedir ad 0 len ad 448 |
| 164 | if or eq r 0 eq r -17 |
| 165 | else |
| 166 | ret 91 |
| 167 | end |
| 168 | u = lrel |
| 169 | u = ltry ulong getpid |
| 170 | check u 0 |
| 171 | u = ltry ulong getpid |
| 172 | check u 20 |
| 173 | u = lrel |
| 174 | if ne u 0 |
| 175 | ret 92 |
| 176 | end |
| 177 | u = ltry ulong getpid |
| 178 | check u 0 |
| 179 | pid = fork |
| 180 | if lt pid 0 |
| 181 | ret 93 |
| 182 | end |
| 183 | if eq pid 0 |
| 184 | u = lacq ulong getpid 1000 |
| 185 | put lbuf 0 uchar u |
| 186 | fd = fopen childf 0 len childf 577 384 |
| 187 | if lt fd 0 |
| 188 | exit 94 |
| 189 | end |
| 190 | n = write int fd lbuf 0 1 |
| 191 | if ne n 1 |
| 192 | exit 95 |
| 193 | end |
| 194 | exit 0 |
| 195 | end |
| 196 | if ne sleepns 50000000 0 |
| 197 | ret 96 |
| 198 | end |
| 199 | u = lrel |
| 200 | if ne u 0 |
| 201 | ret 97 |
| 202 | end |
| 203 | wr = waitpid int pid lbuf 0 8 0 |
| 204 | if ne wr long pid |
| 205 | ret 98 |
| 206 | end |
| 207 | cbyte 0 |
| 208 | check ulong get rb 0 0 |
| 209 | note Owner pid 32 was once read back as lread error 32 and refused with 40; it is a real pid now. |
| 210 | lpid 32 |
| 211 | fd = fopen lpath 0 len lpath 577 384 |
| 212 | if lt fd 0 |
| 213 | ret 103 |
| 214 | end |
| 215 | n = write int fd lb 0 8 |
| 216 | if ne n 8 |
| 217 | ret 104 |
| 218 | end |
| 219 | if ne fclose int fd 0 |
| 220 | ret 105 |
| 221 | end |
| 222 | want = ulong 20 |
| 223 | if eq killpid 32 0 -3 |
| 224 | want = ulong 0 |
| 225 | end |
| 226 | u = ltry ulong getpid |
| 227 | check u want |
| 228 | u = lacq ulong getpid 1001 |
| 229 | check u 43 |
| 230 | lpid 999999 |
| 231 | fd = fopen lpath 0 len lpath 577 384 |
| 232 | if lt fd 0 |
| 233 | ret 99 |
| 234 | end |
| 235 | n = write int fd lb 0 8 |
| 236 | if ne n 8 |
| 237 | ret 100 |
| 238 | end |
| 239 | u = ltry ulong getpid |
| 240 | check u 0 |
| 241 | u = lrel |
| 242 | check u 0 |
| 243 | note A directory cannot be unlinked as a regular file, even when run as root. |
| 244 | note Refuse a pre-existing fixture rather than replacing anything at this path. |
| 245 | r = makedir lpath 0 len lpath 448 |
| 246 | if ne r 0 |
| 247 | ret 101 |
| 248 | end |
| 249 | u = lrel |
| 250 | check u 41 |
| 251 | r = rmdir lpath 0 len lpath |
| 252 | if ne r 0 |
| 253 | ret 102 |
| 254 | end |
| 255 | note Missing-path removal is also a failure, not an idempotent success. |
| 256 | u = lrel |
| 257 | check u 41 |
| 258 | if ne get fails 0 0 |
| 259 | ret 1 |
| 260 | end |
| 261 | nw = write 1 passm 0 len passm |
| 262 | if ne nw long len passm |
| 263 | ret 2 |
| 264 | end |
| 265 | ret 0 |
| 266 | end |