raz / sbcl / lib / Lock.raz

22 lines · 438 B · raw · history · last change cfa2bc20

1(* A lock for short sections: a thread that finds it taken tries again,
2 giving up the processor now and then. Zero is free. *)
3module Lock;
4import Sys;
5
6type Lock* = record held: INT; end;
7
8proc take*(l: ^Lock);
9var tries: INT;
10begin
11 while not CAS(ADR(l^.held), 0, 1) do
12 INC(tries);
13 if tries mod 64 = 0 then Sys.yield; end;
14 end;
15end;
16
17proc give*(l: ^Lock);
18begin
19 _ := XCHG(ADR(l^.held), 0);
20end;
21
22end Lock.