raz / commit 0ad25c63

commit
0ad25c631c4f90cedaeae5d537787a1ca13b6199
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
0f4a4cdd
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.

patch · log

12 files changed, +50 -50

doc/raz-lib.md (history) modified +4 -4

@@ -90,8 +90,8 @@
9090 ```
9191 module Lock;
9292 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);
9595
9696 module Thread;
9797 const Failed*: ERROR = "can't start a thread";
@@ -101,8 +101,8 @@
101101 proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *)
102102 proc join*(t: ^Thread); (* waits for t to end, then frees its stack *)
103103 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);
106106 ```
107107
108108 A `Lock` is for short sections: a thread that finds it taken tries

doc/raz-tutorial.7 (history) modified +2 -2

@@ -1193,9 +1193,9 @@
11931193 sum := sum + i;
11941194 _ := XADD(ADR(hits), 1);
11951195 end;
1196- Thread.lock(ADR(m));
1196+ Thread.lock(m);
11971197 total := total + sum;
1198- Thread.unlock(ADR(m));
1198+ Thread.unlock(m);
11991199 end;
12001200
12011201 proc main*;

examples/Workers.raz (history) modified +2 -2

@@ -27,9 +27,9 @@
2727 n := XADD(ADR(next), 1);
2828 while n < Jobs do
2929 r := job(n * 1000);
30- Thread.lock(ADR(m));
30+ Thread.lock(m);
3131 total := total + r;
32- Thread.unlock(ADR(m));
32+ Thread.unlock(m);
3333 n := XADD(ADR(next), 1);
3434 end;
3535 end;

lib/Heap.raz (history) modified +5 -5

@@ -59,8 +59,8 @@
5959 if p = nil then return nil; end;
6060 return give(p + align - Header, -n);
6161 end;
62- Lock.take(ADR(lock));
63- defer Lock.give(ADR(lock));
62+ Lock.take(lock);
63+ defer Lock.give(lock);
6464 b := lists[c];
6565 if b # nil then
6666 lists[c] := b^.next;
@@ -83,20 +83,20 @@
8383 begin
8484 if p = nil then return; end;
8585 h := ^Head(p - Header);
86- Lock.take(ADR(lock));
86+ Lock.take(lock);
8787 if h^.mark # Out xor INT(h) then
8888 Rt.stop("Heap: a block freed twice, or never given out");
8989 end;
9090 h^.mark := 0;
9191 if h^.size < 0 then
92- Lock.give(ADR(lock));
92+ Lock.give(lock);
9393 Mem.unmap(^U8(INT(h) - INT(h) mod Mem.Page), -h^.size);
9494 else
9595 c := h^.size; (* before next, which takes its place, is set *)
9696 b := ^Block(h);
9797 b^.next := lists[c];
9898 lists[c] := b;
99- Lock.give(ADR(lock));
99+ Lock.give(lock);
100100 end;
101101 end;
102102

lib/Lock.raz (history) modified +8 -8

@@ -9,31 +9,31 @@
99 type Lock* = record held: INT; end;
1010
1111 (* Takes l, waiting while another thread holds it. *)
12-proc take*(l: ^Lock);
12+proc take*(var l: Lock);
1313 var tries: INT;
1414 begin
1515 tries := 0;
16- while not CAS(ADR(l^.held), 0, 1) do
16+ while not CAS(ADR(l.held), 0, 1) do
1717 INC(tries);
1818 if tries = 64 then Sys.yield; tries := 0; end;
1919 end;
2020 end;
2121
2222 (* Gives up l, which the caller holds. *)
23-proc give*(l: ^Lock);
23+proc give*(var l: Lock);
2424 begin
25- _ := XCHG(ADR(l^.held), 0);
25+ _ := XCHG(ADR(l.held), 0);
2626 end;
2727
2828 (* The module's test, in one thread: taken, given, taken again. *)
2929 proc main*;
3030 var l: Lock;
3131 begin
32- take(ADR(l));
32+ take(l);
3333 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);
3737 if l.held # 0 then Rt.stop("Lock.give"); end;
3838 end;
3939

lib/Out.raz (history) modified +6 -6

@@ -33,9 +33,9 @@
3333 (* Writes out what the buffer holds. *)
3434 proc flush*;
3535 begin
36- Lock.take(ADR(lock));
36+ Lock.take(lock);
3737 out;
38- Lock.give(ADR(lock));
38+ Lock.give(lock);
3939 end;
4040
4141 (* Has the buffer written at each line feed, whatever standard output
@@ -44,23 +44,23 @@
4444 --line-buffered. *)
4545 proc lines*(on: BOOL);
4646 begin
47- Lock.take(ADR(lock));
47+ Lock.take(lock);
4848 eachLine := on;
49- Lock.give(ADR(lock));
49+ Lock.give(lock);
5050 end;
5151
5252 (* Writes the bytes of s. *)
5353 proc str*(s: []U8);
5454 var i: INT;
5555 begin
56- Lock.take(ADR(lock));
56+ Lock.take(lock);
5757 for i := 0 to LEN(s) - 1 do
5858 if n = LEN(buf) then out; end;
5959 buf[n] := s[i];
6060 INC(n);
6161 if (s[i] = '\n') and eachLine then out; end;
6262 end;
63- Lock.give(ADR(lock));
63+ Lock.give(lock);
6464 end;
6565
6666 (* Writes the byte c. *)

lib/Thread.linux-x86_64.raz (history) modified +13 -13

@@ -181,24 +181,24 @@
181181 end;
182182
183183 (* Takes m, sleeping while another thread holds it. *)
184-proc lock*(m: ^Mutex);
184+proc lock*(var m: Mutex);
185185 var c: INT;
186186 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);
189189 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);
192192 end;
193193 end;
194194
195195 (* Gives up m, which the caller holds, waking a thread that waits
196196 for it. *)
197-proc unlock*(m: ^Mutex);
197+proc unlock*(var m: Mutex);
198198 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);
202202 end;
203203 end;
204204
@@ -217,9 +217,9 @@
217217
218218 proc testWork(_arg: ^U8);
219219 begin
220- lock(ADR(testLock));
220+ lock(testLock);
221221 INC(testCount);
222- unlock(ADR(testLock));
222+ unlock(testLock);
223223 end;
224224
225225 proc testArg(i: INT): INT;
@@ -242,7 +242,7 @@
242242 if (alive < 1) or (alive > LEN(wave)) then Rt.stop("Thread: alive is 1 to 1000"); end;
243243 started := 0;
244244 while started < n do
245- lock(ADR(testLock));
245+ lock(testLock);
246246 k := 0;
247247 while (k < alive) and (started + k < n) do
248248 t, err := spawn(testWork, nil);
@@ -254,7 +254,7 @@
254254 wave[k] := t;
255255 INC(k);
256256 end;
257- unlock(ADR(testLock));
257+ unlock(testLock);
258258 for i := 0 to k - 1 do join(wave[i]); end;
259259 started := started + k;
260260 end;

site/content/advanced/15-atomics.txt (history) modified +2 -2

@@ -135,10 +135,10 @@
135135 var i: INT;
136136 begin
137137 for i := 1 to 20000 do
138- Lock.take(ADR(lock));
138+ Lock.take(lock);
139139 INC(deposits);
140140 INC(balance, 3);
141- Lock.give(ADR(lock));
141+ Lock.give(lock);
142142 end;
143143 end;
144144

site/content/tour/15-threads.txt (history) modified +2 -2

@@ -60,10 +60,10 @@
6060 var i: INT;
6161 begin
6262 for i := 1 to 10000 do
63- Thread.lock(ADR(m));
63+ Thread.lock(m);
6464 balance := balance + 5;
6565 INC(deposits);
66- Thread.unlock(ADR(m));
66+ Thread.unlock(m);
6767 end;
6868 end;
6969

test/doc/Thread.expect (history) modified +2 -2

@@ -46,10 +46,10 @@
4646 too, since pthread_kill is tgkill with C's id for the thread: a C
4747 thread that hasn't noted its id yet is waited for.
4848
49-proc lock*(m: ^Mutex);
49+proc lock*(var m: Mutex);
5050 Takes m, sleeping while another thread holds it.
5151
52-proc unlock*(m: ^Mutex);
52+proc unlock*(var m: Mutex);
5353 Gives up m, which the caller holds, waking a thread that waits
5454 for it.
5555

test/run/Spawn.raz (history) modified +2 -2

@@ -18,9 +18,9 @@
1818 begin
1919 list := nil;
2020 for i := 1 to Rounds do
21- Thread.lock(ADR(m));
21+ Thread.lock(m);
2222 INC(count);
23- Thread.unlock(ADR(m));
23+ Thread.unlock(m);
2424 _ := XADD(ADR(atomic), 1);
2525 NEW(p, Heap.heap);
2626 p^.next := list;

test/run/SpawnC.raz (history) modified +2 -2

@@ -19,9 +19,9 @@
1919 begin
2020 list := nil;
2121 for i := 1 to Rounds do
22- Thread.lock(ADR(m));
22+ Thread.lock(m);
2323 INC(count);
24- Thread.unlock(ADR(m));
24+ Thread.unlock(m);
2525 _ := XADD(ADR(atomic), 1);
2626 NEW(p, Heap.heap);
2727 p^.next := list;