From 0ad25c631c4f90cedaeae5d537787a1ca13b6199 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:54:53 +0000 Subject: [PATCH] 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. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index e46c509..3a80f4b 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -90,8 +90,8 @@ ``` module Lock; type Lock* = record ... end; -proc take*(l: ^Lock); -proc give*(l: ^Lock); +proc take*(var l: Lock); +proc give*(var l: Lock); module Thread; const Failed*: ERROR = "can't start a thread"; @@ -101,8 +101,8 @@ proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *) -proc lock*(m: ^Mutex); -proc unlock*(m: ^Mutex); +proc lock*(var m: Mutex); +proc unlock*(var m: Mutex); ``` A `Lock` is for short sections: a thread that finds it taken tries diff --git a/doc/raz-tutorial.7 b/doc/raz-tutorial.7 index 56cce66..7818a88 100644 --- a/doc/raz-tutorial.7 +++ b/doc/raz-tutorial.7 @@ -1193,9 +1193,9 @@ sum := sum + i; _ := XADD(ADR(hits), 1); end; - Thread.lock(ADR(m)); + Thread.lock(m); total := total + sum; - Thread.unlock(ADR(m)); + Thread.unlock(m); end; proc main*; diff --git a/examples/Workers.raz b/examples/Workers.raz index f1a9629..f0e28aa 100644 --- a/examples/Workers.raz +++ b/examples/Workers.raz @@ -27,9 +27,9 @@ n := XADD(ADR(next), 1); while n < Jobs do r := job(n * 1000); - Thread.lock(ADR(m)); + Thread.lock(m); total := total + r; - Thread.unlock(ADR(m)); + Thread.unlock(m); n := XADD(ADR(next), 1); end; end; diff --git a/lib/Heap.raz b/lib/Heap.raz index 006d788..0b44b1d 100644 --- a/lib/Heap.raz +++ b/lib/Heap.raz @@ -59,8 +59,8 @@ if p = nil then return nil; end; return give(p + align - Header, -n); end; - Lock.take(ADR(lock)); - defer Lock.give(ADR(lock)); + Lock.take(lock); + defer Lock.give(lock); b := lists[c]; if b # nil then lists[c] := b^.next; @@ -83,20 +83,20 @@ begin if p = nil then return; end; h := ^Head(p - Header); - Lock.take(ADR(lock)); + Lock.take(lock); if h^.mark # Out xor INT(h) then Rt.stop("Heap: a block freed twice, or never given out"); end; h^.mark := 0; if h^.size < 0 then - Lock.give(ADR(lock)); + Lock.give(lock); Mem.unmap(^U8(INT(h) - INT(h) mod Mem.Page), -h^.size); else c := h^.size; (* before next, which takes its place, is set *) b := ^Block(h); b^.next := lists[c]; lists[c] := b; - Lock.give(ADR(lock)); + Lock.give(lock); end; end; diff --git a/lib/Lock.raz b/lib/Lock.raz index ce69f9d..8a66309 100644 --- a/lib/Lock.raz +++ b/lib/Lock.raz @@ -9,31 +9,31 @@ type Lock* = record held: INT; end; (* Takes l, waiting while another thread holds it. *) -proc take*(l: ^Lock); +proc take*(var l: Lock); var tries: INT; begin tries := 0; - while not CAS(ADR(l^.held), 0, 1) do + while not CAS(ADR(l.held), 0, 1) do INC(tries); if tries = 64 then Sys.yield; tries := 0; end; end; end; (* Gives up l, which the caller holds. *) -proc give*(l: ^Lock); +proc give*(var l: Lock); begin - _ := XCHG(ADR(l^.held), 0); + _ := XCHG(ADR(l.held), 0); end; (* The module's test, in one thread: taken, given, taken again. *) proc main*; var l: Lock; begin - take(ADR(l)); + take(l); if l.held # 1 then Rt.stop("Lock.take"); end; - give(ADR(l)); - take(ADR(l)); - give(ADR(l)); + give(l); + take(l); + give(l); if l.held # 0 then Rt.stop("Lock.give"); end; end; diff --git a/lib/Out.raz b/lib/Out.raz index f7ce268..8608baf 100644 --- a/lib/Out.raz +++ b/lib/Out.raz @@ -33,9 +33,9 @@ (* Writes out what the buffer holds. *) proc flush*; begin - Lock.take(ADR(lock)); + Lock.take(lock); out; - Lock.give(ADR(lock)); + Lock.give(lock); end; (* Has the buffer written at each line feed, whatever standard output @@ -44,23 +44,23 @@ --line-buffered. *) proc lines*(on: BOOL); begin - Lock.take(ADR(lock)); + Lock.take(lock); eachLine := on; - Lock.give(ADR(lock)); + Lock.give(lock); end; (* Writes the bytes of s. *) proc str*(s: []U8); var i: INT; begin - Lock.take(ADR(lock)); + Lock.take(lock); for i := 0 to LEN(s) - 1 do if n = LEN(buf) then out; end; buf[n] := s[i]; INC(n); if (s[i] = '\n') and eachLine then out; end; end; - Lock.give(ADR(lock)); + Lock.give(lock); end; (* Writes the byte c. *) diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index 8b736f7..9e6ac5a 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -181,24 +181,24 @@ end; (* Takes m, sleeping while another thread holds it. *) -proc lock*(m: ^Mutex); +proc lock*(var m: Mutex); var c: INT; begin - if CAS(ADR(m^.state), 0, 1) then return; end; - c := XCHG(ADR(m^.state), 2); + if CAS(ADR(m.state), 0, 1) then return; end; + c := XCHG(ADR(m.state), 2); while c # 0 do - _ := futex(ADR(m^.state), FutexWait + Private, 2); - c := XCHG(ADR(m^.state), 2); + _ := futex(ADR(m.state), FutexWait + Private, 2); + c := XCHG(ADR(m.state), 2); end; end; (* Gives up m, which the caller holds, waking a thread that waits for it. *) -proc unlock*(m: ^Mutex); +proc unlock*(var m: Mutex); begin - if XADD(ADR(m^.state), -1) # 1 then - _ := XCHG(ADR(m^.state), 0); - _ := futex(ADR(m^.state), FutexWake + Private, 1); + if XADD(ADR(m.state), -1) # 1 then + _ := XCHG(ADR(m.state), 0); + _ := futex(ADR(m.state), FutexWake + Private, 1); end; end; @@ -217,9 +217,9 @@ proc testWork(_arg: ^U8); begin - lock(ADR(testLock)); + lock(testLock); INC(testCount); - unlock(ADR(testLock)); + unlock(testLock); end; proc testArg(i: INT): INT; @@ -242,7 +242,7 @@ if (alive < 1) or (alive > LEN(wave)) then Rt.stop("Thread: alive is 1 to 1000"); end; started := 0; while started < n do - lock(ADR(testLock)); + lock(testLock); k := 0; while (k < alive) and (started + k < n) do t, err := spawn(testWork, nil); @@ -254,7 +254,7 @@ wave[k] := t; INC(k); end; - unlock(ADR(testLock)); + unlock(testLock); for i := 0 to k - 1 do join(wave[i]); end; started := started + k; end; diff --git a/site/content/advanced/15-atomics.txt b/site/content/advanced/15-atomics.txt index 6eed466..cb143e2 100644 --- a/site/content/advanced/15-atomics.txt +++ b/site/content/advanced/15-atomics.txt @@ -135,10 +135,10 @@ var i: INT; begin for i := 1 to 20000 do - Lock.take(ADR(lock)); + Lock.take(lock); INC(deposits); INC(balance, 3); - Lock.give(ADR(lock)); + Lock.give(lock); end; end; diff --git a/site/content/tour/15-threads.txt b/site/content/tour/15-threads.txt index b033e0e..c73fa73 100644 --- a/site/content/tour/15-threads.txt +++ b/site/content/tour/15-threads.txt @@ -60,10 +60,10 @@ var i: INT; begin for i := 1 to 10000 do - Thread.lock(ADR(m)); + Thread.lock(m); balance := balance + 5; INC(deposits); - Thread.unlock(ADR(m)); + Thread.unlock(m); end; end; diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index 928b7c3..f9f4f93 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -46,10 +46,10 @@ too, since pthread_kill is tgkill with C's id for the thread: a C thread that hasn't noted its id yet is waited for. -proc lock*(m: ^Mutex); +proc lock*(var m: Mutex); Takes m, sleeping while another thread holds it. -proc unlock*(m: ^Mutex); +proc unlock*(var m: Mutex); Gives up m, which the caller holds, waking a thread that waits for it. diff --git a/test/run/Spawn.raz b/test/run/Spawn.raz index 3d273fd..00b8b62 100644 --- a/test/run/Spawn.raz +++ b/test/run/Spawn.raz @@ -18,9 +18,9 @@ begin list := nil; for i := 1 to Rounds do - Thread.lock(ADR(m)); + Thread.lock(m); INC(count); - Thread.unlock(ADR(m)); + Thread.unlock(m); _ := XADD(ADR(atomic), 1); NEW(p, Heap.heap); p^.next := list; diff --git a/test/run/SpawnC.raz b/test/run/SpawnC.raz index 8b4b53b..c47014b 100644 --- a/test/run/SpawnC.raz +++ b/test/run/SpawnC.raz @@ -19,9 +19,9 @@ begin list := nil; for i := 1 to Rounds do - Thread.lock(ADR(m)); + Thread.lock(m); INC(count); - Thread.unlock(ADR(m)); + Thread.unlock(m); _ := XADD(ADR(atomic), 1); NEW(p, Heap.heap); p^.next := list;