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 / authority / lock-gate.om

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