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 withState() and getState() 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 withState() and getState() methods.

Example usage:

 
sync := SynchronizedState(ActorPool(), Mutable#)
msg  := "That's cool, dude!"

val  := sync.getState |Mutable state -> Int| {
    state.buf.writeChars(msg)
    return state.buf.size
}

class Mutable {
    Buf buf := Buf()
}
getState

Source

Obj? getState(|Obj->Obj? func)

Calls the given func synchronously, passing in the State object and returning the func's response.

The given func should be immutable.

lock

Source

const Synchronized lock

The lock object should you need to synchronize on the state.

makeWithFactory

Source

new makeWithFactory(ActorPool actorPool, |->Obj? stateFactory)

The given (immutable) factory func is used to create the state object inside it's thread.

makeWithType

Source

new makeWithType(ActorPool actorPool, Type stateType)

The given state type must have a public no-args ctor as per Type.make.

withState

Source

Future withState(|Obj->Obj? func)

Calls the given func asynchronously, passing in the State object.

The given func should be immutable.