const classafConcurrent::SynchronizedState
sys::Obj afConcurrent::SynchronizedState
Provides synchronized
access to a (non- const
) mutable state object.
SynchronizedState
creates the state object in its own thread and provides access to it via the sync()
and async()
methods. Note that by their nature, these methods are immutable boundaries. Meaning that while data in the State object can be mutable, data passed in and out of these these boundaries can not be.
SynchronizedState
is designed to be scope safe, that is you cannot accidently call methods on your State object outside of the sync()
and async()
methods.
Example usage:
sync := SynchronizedState(ActorPool(), Mutable#) msg := "That's cool, dude!" val := sync.sync |Mutable state -> Int| { state.buf.writeChars(msg) return state.buf.size } class Mutable { Buf buf := Buf() }
Note SynchronizedState
overrides trap()
to make convenience calls to sync()
allowing dynamic calls:
sync := SynchronizedState(ActorPool(), Mutable#) size := sync->buf->size//--> returns size of Mutable.buf
- async
Calls the given func asynchronously, passing in the State object.
The given func should be immutable.
- asyncLater
Future asyncLater(Duration duration, |Obj->Obj? func)
Calls the given func asynchronously, passing in the State object.
The given func should be immutable.
- lock
const Synchronized lock
The
lock
object should you need tosynchronize
on the state.- makeWithFactory
new makeWithFactory(ActorPool actorPool, |->Obj? stateFactory)
Creates a
SynchronizedState
instance.The given (immutable) factory func is used to create the state object inside it's thread.
- makeWithType
new makeWithType(ActorPool actorPool, Type stateType)
Creates a
SynchronizedState
instance.The given state type must have a public no-args ctor as per Type.make.
- sync
Calls the given func synchronously, passing in the State object and returning the func's response.
The given func should be immutable.
- toStr
virtual override Str toStr()
Returns a string representation the state.
- trap
virtual override Obj? trap(Str name, Obj?[]? args := null)
Routes trap() to the enclosed state object. Allows convenience calls for calling
sync()
and returning state:sync := SynchronizedState(ActorPool(), Buf#) size := sync->size
//--> returns size of Buf