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
iPut
and mPut
).
type 'a ivar
iVar ()
iPut (iv, x)
Put
exception is raised.
iGet iv
iGetEvt iv
iGet
operation on iv.
val iGetPoll : 'a ivar -> 'a option
iGet
. If the corresponding blocking form would block, then it returns NONE
; otherwise it returns SOME
of the variable's contents.
sameIVar (iv1, iv2)
true
, if iv1 and iv2 are the same I-variable.
type 'a mvar
ivar
values, M-structured variables may be updated multiple times. Like I-variables, however, they may only be written if they are empty.
mVar ()
mVarInit x
mPut (mv, x)
Put
exception is raised.
mTake mv
mTakeEvt mv
mTake
operation on mv.
mGet mv
let val x = mTake mv in mPut(mv, x); x end
mGetEvt mv
mGet
operation on mv.
val mTakePoll : 'a mvar -> 'a option
val mGetPoll : 'a mvar -> 'a option
mTake
and mGet
(respectively). If the corresponding blocking form would block, then they return NONE
; otherwise they return SOME
of the variable's contents.
mSwap (mv, newV)
let val x = mTake mv in mPut(mv, newV); x endexcept that
mSwap
is executed atomically.
mSwapEvt (mv, newV)
mSwap
operation on mv and 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 © 1991-2003 John Reppy