SyncVar
structureThe SyncVar structure provides Id-style synchronous variables (or memory cells). These variables have two states: empty and full. An attempt to read a value from an empty variable blocks the calling thread until there is a value available. An attempt to put a value into a variable that is full results in the Put exception being raised. There are two kinds of synchronous variables: I-variables are write-once, while M-variables are mutable.
signature SYNC_VAR
structure SyncVar
: SYNC_VAR
exception Put
type 'a ivar
val iVar : unit -> 'a ivar
val iPut : ('a ivar * 'a) -> unit
val iGet : 'a ivar -> 'a
val iGetEvt : 'a ivar -> 'a event
val iGetPoll : 'a ivar -> 'a option
val sameIVar : ('a ivar * 'a ivar) -> bool
type 'a mvar
val mVar : unit -> 'a mvar
val mVarInit : 'a -> 'a mvar
val mPut : ('a mvar * 'a) -> unit
val mTake : 'a mvar -> 'a
val mTakeEvt : 'a mvar -> 'a event
val mGet : 'a mvar -> 'a
val mGetEvt : 'a mvar -> 'a event
val mTakePoll : 'a mvar -> 'a option
val mGetPoll : 'a mvar -> 'a option
val mSwap : ('a mvar * 'a) -> 'a
val mSwapEvt : ('a mvar * 'a) -> 'a event
val sameMVar : ('a mvar * 'a mvar) -> bool
exception Put
type 'a ivar
iVar ()
iPut (iv, x)
iGet iv
iGetEvt iv
val iGetPoll
sameIVar (iv1, iv2)
true
, if the iv1 and iv2 are the same I-variable.
type 'a mvar
mVar ()
mVar x
mPut (mv, x)
mTake mv
mTakeEvt mv
mGet mv
let val x = mTake mv in mPut(mv, x); x end
mGetEvt mv
val mTakePoll
val mGetPoll
mSwap (mv, newV)
let val x = mTake mv in mPut(mv, newV); x endexcept that
mSwap
is executed atomically.
mSwapEvt mv
sameMVar (mv1, mv2)
true
, if mv1 and mv2 are the same M-variable.
I-variables provide a useful mechanism for implementing the reply communication in request/reply protocols. They may also be used to implement incremental data structures and streams.
A disciplined use of M-variables can provide an atomic read-modify-write operation.
CML
Last Modified November 16, 1995
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies