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.om

128 lines · 1840 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