Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
98
Task/Atomic-updates/Oz/atomic-updates-1.oz
Normal file
98
Task/Atomic-updates/Oz/atomic-updates-1.oz
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
declare
|
||||
%%
|
||||
%% INIT
|
||||
%%
|
||||
NBuckets = 100
|
||||
StartVal = 50
|
||||
ExpectedSum = NBuckets * StartVal
|
||||
|
||||
%% Makes a tuple and calls Fun for every field
|
||||
fun {Make Label N Fun}
|
||||
R = {Tuple.make Label N}
|
||||
in
|
||||
for I in 1..N do R.I = {Fun} end
|
||||
R
|
||||
end
|
||||
|
||||
Buckets = {Make buckets NBuckets fun {$} {Cell.new StartVal} end}
|
||||
Locks = {Make locks NBuckets Lock.new}
|
||||
LockList = {Record.toList Locks}
|
||||
|
||||
%%
|
||||
%% DISPLAY
|
||||
%%
|
||||
proc {Display}
|
||||
Snapshot = {WithLocks LockList
|
||||
fun {$}
|
||||
{Record.map Buckets Cell.access}
|
||||
end
|
||||
}
|
||||
Sum = {Record.foldL Snapshot Number.'+' 0}
|
||||
in
|
||||
{Print Snapshot}
|
||||
{System.showInfo " sum: "#Sum}
|
||||
Sum = ExpectedSum %% assert
|
||||
end
|
||||
|
||||
%% Calls Fun with multiple locks locked and returns the result of Fun.
|
||||
fun {WithLocks Ls Fun}
|
||||
case Ls of L|Lr then
|
||||
lock L then
|
||||
{WithLocks Lr Fun}
|
||||
end
|
||||
[] nil then {Fun}
|
||||
end
|
||||
end
|
||||
|
||||
%%
|
||||
%% MANIPULATE
|
||||
%%
|
||||
proc {Smooth I J}
|
||||
Diff = @(Buckets.I) - @(Buckets.J) %% reading without lock: by design
|
||||
Amount = Diff div 4
|
||||
in
|
||||
{Transfer I J Amount}
|
||||
end
|
||||
|
||||
proc {Roughen I J}
|
||||
Amount = @(Buckets.I) div 3 %% reading without lock: by design
|
||||
in
|
||||
{Transfer I J Amount}
|
||||
end
|
||||
|
||||
%% Atomically transfer an amount from From to To.
|
||||
%% Negative amounts are allowed;
|
||||
%% will never make a bucket negative.
|
||||
proc {Transfer From To Amount}
|
||||
if From \= To then
|
||||
%% lock in order (to avoid deadlocks)
|
||||
Smaller = {Min From To}
|
||||
Bigger = {Max From To}
|
||||
in
|
||||
lock Locks.Smaller then
|
||||
lock Locks.Bigger then
|
||||
FromBucket = Buckets.From
|
||||
ToBucket = Buckets.To
|
||||
NewFromValue = @FromBucket - Amount
|
||||
NewToValue = @ToBucket + Amount
|
||||
in
|
||||
if NewFromValue >= 0 andthen NewToValue >= 0 then
|
||||
FromBucket := NewFromValue
|
||||
ToBucket := NewToValue
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
%% Returns a random bucket index.
|
||||
fun {Pick}
|
||||
{OS.rand} mod NBuckets + 1
|
||||
end
|
||||
in
|
||||
%%
|
||||
%% START
|
||||
%%
|
||||
thread for do {Smooth {Pick} {Pick}} end end
|
||||
thread for do {Roughen {Pick} {Pick}} end end
|
||||
for do {Display} {Time.delay 50} end
|
||||
7
Task/Atomic-updates/Oz/atomic-updates-2.oz
Normal file
7
Task/Atomic-updates/Oz/atomic-updates-2.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
buckets(50 50 50 50 50 50 50 50 50 50 ,,,) sum: 5000
|
||||
buckets(24 68 58 43 78 85 43 66 14 48 ,,,) sum: 5000
|
||||
buckets(36 33 59 38 39 23 55 51 43 45 ,,,) sum: 5000
|
||||
buckets(64 32 62 26 50 82 38 70 16 43 ,,,) sum: 5000
|
||||
buckets(51 51 49 50 51 51 51 49 49 49 ,,,) sum: 5000
|
||||
buckets(43 28 27 60 77 41 36 48 72 70 ,,,) sum: 5000
|
||||
...
|
||||
Loading…
Add table
Add a link
Reference in a new issue