SyncVar
structure
signature SYNC_VAR
structure SyncVar
: SYNC_VAR
The 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.
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 iv1 and iv2 are the same I-variable.
type 'a mvar
mVar ()
mVarInit 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, newV)
sameMVar (mv1, mv2)
true
, if mv1 and mv2 are the same M-variable.
CML
I-variables provide a useful mechanism for implementing the reply communication in request/reply protocols (in cases where the server does not care if the reply is accepted). They may also be used to implement incremental data structures and streams; for example, the Multicast structure uses I-variables to implement its multicast channels.
A disciplined use of M-variables can provide an atomic read-modify-write operation.
Last Modified &date;
Comments to John Reppy.
Copyright © 1998 Bell Labs, Lucent Technologies