Lock and Thread's Mutex are taken and given by var (report 2026-10.33) Lock.take and give, Thread.lock and unlock take var l: Lock and var m: Mutex; none kept the pointer. Inside, CAS, XCHG and XADD still take ADR(l.held), as the atomics want an address. The seed compiles Lock, which Heap and Out import; the fixpoint holds, and so does make stress.
| @@ -90,8 +90,8 @@ | ||
| 90 | 90 | ``` |
| 91 | 91 | module Lock; |
| 92 | 92 | type Lock* = record ... end; |
| 93 | -proc take*(l: ^Lock); | |
| 94 | -proc give*(l: ^Lock); | |
| 93 | +proc take*(var l: Lock); | |
| 94 | +proc give*(var l: Lock); | |
| 95 | 95 | |
| 96 | 96 | module Thread; |
| 97 | 97 | const Failed*: ERROR = "can't start a thread"; |
| @@ -101,8 +101,8 @@ | ||
| 101 | 101 | proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) |
| 102 | 102 | proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) |
| 103 | 103 | proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *) |
| 104 | -proc lock*(m: ^Mutex); | |
| 105 | -proc unlock*(m: ^Mutex); | |
| 104 | +proc lock*(var m: Mutex); | |
| 105 | +proc unlock*(var m: Mutex); | |
| 106 | 106 | ``` |
| 107 | 107 | |
| 108 | 108 | A `Lock` is for short sections: a thread that finds it taken tries |
| @@ -1193,9 +1193,9 @@ | ||
| 1193 | 1193 | sum := sum + i; |
| 1194 | 1194 | _ := XADD(ADR(hits), 1); |
| 1195 | 1195 | end; |
| 1196 | - Thread.lock(ADR(m)); | |
| 1196 | + Thread.lock(m); | |
| 1197 | 1197 | total := total + sum; |
| 1198 | - Thread.unlock(ADR(m)); | |
| 1198 | + Thread.unlock(m); | |
| 1199 | 1199 | end; |
| 1200 | 1200 | |
| 1201 | 1201 | proc main*; |
| @@ -27,9 +27,9 @@ | ||
| 27 | 27 | n := XADD(ADR(next), 1); |
| 28 | 28 | while n < Jobs do |
| 29 | 29 | r := job(n * 1000); |
| 30 | - Thread.lock(ADR(m)); | |
| 30 | + Thread.lock(m); | |
| 31 | 31 | total := total + r; |
| 32 | - Thread.unlock(ADR(m)); | |
| 32 | + Thread.unlock(m); | |
| 33 | 33 | n := XADD(ADR(next), 1); |
| 34 | 34 | end; |
| 35 | 35 | end; |
| @@ -59,8 +59,8 @@ | ||
| 59 | 59 | if p = nil then return nil; end; |
| 60 | 60 | return give(p + align - Header, -n); |
| 61 | 61 | end; |
| 62 | - Lock.take(ADR(lock)); | |
| 63 | - defer Lock.give(ADR(lock)); | |
| 62 | + Lock.take(lock); | |
| 63 | + defer Lock.give(lock); | |
| 64 | 64 | b := lists[c]; |
| 65 | 65 | if b # nil then |
| 66 | 66 | lists[c] := b^.next; |
| @@ -83,20 +83,20 @@ | ||
| 83 | 83 | begin |
| 84 | 84 | if p = nil then return; end; |
| 85 | 85 | h := ^Head(p - Header); |
| 86 | - Lock.take(ADR(lock)); | |
| 86 | + Lock.take(lock); | |
| 87 | 87 | if h^.mark # Out xor INT(h) then |
| 88 | 88 | Rt.stop("Heap: a block freed twice, or never given out"); |
| 89 | 89 | end; |
| 90 | 90 | h^.mark := 0; |
| 91 | 91 | if h^.size < 0 then |
| 92 | - Lock.give(ADR(lock)); | |
| 92 | + Lock.give(lock); | |
| 93 | 93 | Mem.unmap(^U8(INT(h) - INT(h) mod Mem.Page), -h^.size); |
| 94 | 94 | else |
| 95 | 95 | c := h^.size; (* before next, which takes its place, is set *) |
| 96 | 96 | b := ^Block(h); |
| 97 | 97 | b^.next := lists[c]; |
| 98 | 98 | lists[c] := b; |
| 99 | - Lock.give(ADR(lock)); | |
| 99 | + Lock.give(lock); | |
| 100 | 100 | end; |
| 101 | 101 | end; |
| 102 | 102 |
| @@ -9,31 +9,31 @@ | ||
| 9 | 9 | type Lock* = record held: INT; end; |
| 10 | 10 | |
| 11 | 11 | (* Takes l, waiting while another thread holds it. *) |
| 12 | -proc take*(l: ^Lock); | |
| 12 | +proc take*(var l: Lock); | |
| 13 | 13 | var tries: INT; |
| 14 | 14 | begin |
| 15 | 15 | tries := 0; |
| 16 | - while not CAS(ADR(l^.held), 0, 1) do | |
| 16 | + while not CAS(ADR(l.held), 0, 1) do | |
| 17 | 17 | INC(tries); |
| 18 | 18 | if tries = 64 then Sys.yield; tries := 0; end; |
| 19 | 19 | end; |
| 20 | 20 | end; |
| 21 | 21 | |
| 22 | 22 | (* Gives up l, which the caller holds. *) |
| 23 | -proc give*(l: ^Lock); | |
| 23 | +proc give*(var l: Lock); | |
| 24 | 24 | begin |
| 25 | - _ := XCHG(ADR(l^.held), 0); | |
| 25 | + _ := XCHG(ADR(l.held), 0); | |
| 26 | 26 | end; |
| 27 | 27 | |
| 28 | 28 | (* The module's test, in one thread: taken, given, taken again. *) |
| 29 | 29 | proc main*; |
| 30 | 30 | var l: Lock; |
| 31 | 31 | begin |
| 32 | - take(ADR(l)); | |
| 32 | + take(l); | |
| 33 | 33 | if l.held # 1 then Rt.stop("Lock.take"); end; |
| 34 | - give(ADR(l)); | |
| 35 | - take(ADR(l)); | |
| 36 | - give(ADR(l)); | |
| 34 | + give(l); | |
| 35 | + take(l); | |
| 36 | + give(l); | |
| 37 | 37 | if l.held # 0 then Rt.stop("Lock.give"); end; |
| 38 | 38 | end; |
| 39 | 39 |
| @@ -33,9 +33,9 @@ | ||
| 33 | 33 | (* Writes out what the buffer holds. *) |
| 34 | 34 | proc flush*; |
| 35 | 35 | begin |
| 36 | - Lock.take(ADR(lock)); | |
| 36 | + Lock.take(lock); | |
| 37 | 37 | out; |
| 38 | - Lock.give(ADR(lock)); | |
| 38 | + Lock.give(lock); | |
| 39 | 39 | end; |
| 40 | 40 | |
| 41 | 41 | (* Has the buffer written at each line feed, whatever standard output |
| @@ -44,23 +44,23 @@ | ||
| 44 | 44 | --line-buffered. *) |
| 45 | 45 | proc lines*(on: BOOL); |
| 46 | 46 | begin |
| 47 | - Lock.take(ADR(lock)); | |
| 47 | + Lock.take(lock); | |
| 48 | 48 | eachLine := on; |
| 49 | - Lock.give(ADR(lock)); | |
| 49 | + Lock.give(lock); | |
| 50 | 50 | end; |
| 51 | 51 | |
| 52 | 52 | (* Writes the bytes of s. *) |
| 53 | 53 | proc str*(s: []U8); |
| 54 | 54 | var i: INT; |
| 55 | 55 | begin |
| 56 | - Lock.take(ADR(lock)); | |
| 56 | + Lock.take(lock); | |
| 57 | 57 | for i := 0 to LEN(s) - 1 do |
| 58 | 58 | if n = LEN(buf) then out; end; |
| 59 | 59 | buf[n] := s[i]; |
| 60 | 60 | INC(n); |
| 61 | 61 | if (s[i] = '\n') and eachLine then out; end; |
| 62 | 62 | end; |
| 63 | - Lock.give(ADR(lock)); | |
| 63 | + Lock.give(lock); | |
| 64 | 64 | end; |
| 65 | 65 | |
| 66 | 66 | (* Writes the byte c. *) |
| @@ -181,24 +181,24 @@ | ||
| 181 | 181 | end; |
| 182 | 182 | |
| 183 | 183 | (* Takes m, sleeping while another thread holds it. *) |
| 184 | -proc lock*(m: ^Mutex); | |
| 184 | +proc lock*(var m: Mutex); | |
| 185 | 185 | var c: INT; |
| 186 | 186 | begin |
| 187 | - if CAS(ADR(m^.state), 0, 1) then return; end; | |
| 188 | - c := XCHG(ADR(m^.state), 2); | |
| 187 | + if CAS(ADR(m.state), 0, 1) then return; end; | |
| 188 | + c := XCHG(ADR(m.state), 2); | |
| 189 | 189 | while c # 0 do |
| 190 | - _ := futex(ADR(m^.state), FutexWait + Private, 2); | |
| 191 | - c := XCHG(ADR(m^.state), 2); | |
| 190 | + _ := futex(ADR(m.state), FutexWait + Private, 2); | |
| 191 | + c := XCHG(ADR(m.state), 2); | |
| 192 | 192 | end; |
| 193 | 193 | end; |
| 194 | 194 | |
| 195 | 195 | (* Gives up m, which the caller holds, waking a thread that waits |
| 196 | 196 | for it. *) |
| 197 | -proc unlock*(m: ^Mutex); | |
| 197 | +proc unlock*(var m: Mutex); | |
| 198 | 198 | begin |
| 199 | - if XADD(ADR(m^.state), -1) # 1 then | |
| 200 | - _ := XCHG(ADR(m^.state), 0); | |
| 201 | - _ := futex(ADR(m^.state), FutexWake + Private, 1); | |
| 199 | + if XADD(ADR(m.state), -1) # 1 then | |
| 200 | + _ := XCHG(ADR(m.state), 0); | |
| 201 | + _ := futex(ADR(m.state), FutexWake + Private, 1); | |
| 202 | 202 | end; |
| 203 | 203 | end; |
| 204 | 204 | |
| @@ -217,9 +217,9 @@ | ||
| 217 | 217 | |
| 218 | 218 | proc testWork(_arg: ^U8); |
| 219 | 219 | begin |
| 220 | - lock(ADR(testLock)); | |
| 220 | + lock(testLock); | |
| 221 | 221 | INC(testCount); |
| 222 | - unlock(ADR(testLock)); | |
| 222 | + unlock(testLock); | |
| 223 | 223 | end; |
| 224 | 224 | |
| 225 | 225 | proc testArg(i: INT): INT; |
| @@ -242,7 +242,7 @@ | ||
| 242 | 242 | if (alive < 1) or (alive > LEN(wave)) then Rt.stop("Thread: alive is 1 to 1000"); end; |
| 243 | 243 | started := 0; |
| 244 | 244 | while started < n do |
| 245 | - lock(ADR(testLock)); | |
| 245 | + lock(testLock); | |
| 246 | 246 | k := 0; |
| 247 | 247 | while (k < alive) and (started + k < n) do |
| 248 | 248 | t, err := spawn(testWork, nil); |
| @@ -254,7 +254,7 @@ | ||
| 254 | 254 | wave[k] := t; |
| 255 | 255 | INC(k); |
| 256 | 256 | end; |
| 257 | - unlock(ADR(testLock)); | |
| 257 | + unlock(testLock); | |
| 258 | 258 | for i := 0 to k - 1 do join(wave[i]); end; |
| 259 | 259 | started := started + k; |
| 260 | 260 | end; |
| @@ -135,10 +135,10 @@ | ||
| 135 | 135 | var i: INT; |
| 136 | 136 | begin |
| 137 | 137 | for i := 1 to 20000 do |
| 138 | - Lock.take(ADR(lock)); | |
| 138 | + Lock.take(lock); | |
| 139 | 139 | INC(deposits); |
| 140 | 140 | INC(balance, 3); |
| 141 | - Lock.give(ADR(lock)); | |
| 141 | + Lock.give(lock); | |
| 142 | 142 | end; |
| 143 | 143 | end; |
| 144 | 144 |
| @@ -60,10 +60,10 @@ | ||
| 60 | 60 | var i: INT; |
| 61 | 61 | begin |
| 62 | 62 | for i := 1 to 10000 do |
| 63 | - Thread.lock(ADR(m)); | |
| 63 | + Thread.lock(m); | |
| 64 | 64 | balance := balance + 5; |
| 65 | 65 | INC(deposits); |
| 66 | - Thread.unlock(ADR(m)); | |
| 66 | + Thread.unlock(m); | |
| 67 | 67 | end; |
| 68 | 68 | end; |
| 69 | 69 |
| @@ -46,10 +46,10 @@ | ||
| 46 | 46 | too, since pthread_kill is tgkill with C's id for the thread: a C |
| 47 | 47 | thread that hasn't noted its id yet is waited for. |
| 48 | 48 | |
| 49 | -proc lock*(m: ^Mutex); | |
| 49 | +proc lock*(var m: Mutex); | |
| 50 | 50 | Takes m, sleeping while another thread holds it. |
| 51 | 51 | |
| 52 | -proc unlock*(m: ^Mutex); | |
| 52 | +proc unlock*(var m: Mutex); | |
| 53 | 53 | Gives up m, which the caller holds, waking a thread that waits |
| 54 | 54 | for it. |
| 55 | 55 |
| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | begin |
| 19 | 19 | list := nil; |
| 20 | 20 | for i := 1 to Rounds do |
| 21 | - Thread.lock(ADR(m)); | |
| 21 | + Thread.lock(m); | |
| 22 | 22 | INC(count); |
| 23 | - Thread.unlock(ADR(m)); | |
| 23 | + Thread.unlock(m); | |
| 24 | 24 | _ := XADD(ADR(atomic), 1); |
| 25 | 25 | NEW(p, Heap.heap); |
| 26 | 26 | p^.next := list; |
| @@ -19,9 +19,9 @@ | ||
| 19 | 19 | begin |
| 20 | 20 | list := nil; |
| 21 | 21 | for i := 1 to Rounds do |
| 22 | - Thread.lock(ADR(m)); | |
| 22 | + Thread.lock(m); | |
| 23 | 23 | INC(count); |
| 24 | - Thread.unlock(ADR(m)); | |
| 24 | + Thread.unlock(m); | |
| 25 | 25 | _ := XADD(ADR(atomic), 1); |
| 26 | 26 | NEW(p, Heap.heap); |
| 27 | 27 | p^.next := list; |