interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / profile / cases / lock-943d955.om

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