Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ProcessState.*
- type ProcessState = Stop | Running | Idle
- object ProcessState {
- type Stop = 1
- val Stop: Stop = 1
- type Running = 2
- val Running: Running = 2
- type Idle = 3
- val Idle: Idle = 3
- }
- import java.util.concurrent.atomic.AtomicInteger
- opaque type AtomicProcessState = AtomicInteger
- object AtomicProcessState {
- inline def apply(initial: ProcessState): AtomicProcessState = AtomicInteger(initial)
- extension (self: AtomicProcessState) {
- inline def compareAndSet(expected: ProcessState, newValue: ProcessState): Boolean =
- self.compareAndSet(expected, newValue)
- inline def set(value: ProcessState): Unit =
- self.set(value)
- inline def get(): ProcessState =
- self.get().asInstanceOf[ProcessState]
- }
- }
- protected val state: AtomicProcessState = AtomicProcessState(ProcessState.Idle)
- private def tell(queue: ConcurrentLinkedQueue[T], message: T): Unit = {
- import ProcessState.*
- (state.get(): @switch) match {
- case Stop =>
- runtime.reportFailure(Undelivered(message))
- case Running =>
- queue.add(message)
- case Idle =>
- queue.add(message)
- continue()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement