Download 1 Introduction

Transcript
declare A L in
A = {NewArray 1 10 ˜5}
L = {NewLock}
proc {Switch A}
{For {Array.low A} {Array.high
proc {$ I}
X in
X = {Get A I}
case X<0 then {Put A I ˜X}
elsecase X == 0 then {Put
{Delay 100}
end}
end
proc {Zero A}
{For {Array.low A} {Array.high
proc {$ I} {Put A I 0} {Delay
end
A} 1
A I zero} else skip end
A} 1
100} end}
Figure 30: Locking
E then S end
where E is an expression and the construct blocks until E is determined. If E is not
a lock, then a type error is raised. {NewLock L} allocates a new lock L. {IsLock
E} returns true i E is a lock.
lock
Arrays. Oz has arrays as primary chunk type. Operation on arrays are dened in
module Array [2]. To create an array the procedure {NewArray +L +H I A} is
used where \A? is the resulting array, L is the lower-bound index, H is the higherbound index, and I is the initial value. As a simple illustration of locks consider the
program in Figure 30.
Assume that we want to perform the procedures Zero and Switch each atomically but in an arbitrary order. To do this we can use locks as in the following
example:
local X Y in
thread {Delay 100} lock L then {Zero A} end X = unit end
thread lock L then {Switch A} end Y = X end
{Wait Y} {For 1 10 1 proc {$ I} {Browse {Get A I}} end}
end
By Switching the delay statement above between the the rst and the second threads,
we observe that all the elements of the array either will get the value zero or 0. We
have no mixed values.
Write an example of an atomic transaction on multiple objects.
51