ru.primetalk.synapse.concurrent.SafeVariable.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of synapse-grid-concurrent Show documentation
Show all versions of synapse-grid-concurrent Show documentation
SynapseGrid is a framework for constructing reactive real-time immutable data flow systems. -core contains everything to run a single-threaded system, -akka contains everything to run systemsover Akka actors, -slf4j - enables logging, -examples - a few test systems.
///////////////////////////////////////////////////////////////
// © ООО «Праймтолк», 2011-2013 //
// Все права принадлежат компании ООО «Праймтолк». //
///////////////////////////////////////////////////////////////
/**
* SynapseGrid
* © Primetalk Ltd., 2013.
* All rights reserved.
* Authors: A.Zhizhelev, A.Nehaev, P. Popov
*
* Created: 22.09.13, zhizhelev
*/
package ru.primetalk.synapse.concurrent
import scala.concurrent.Lock
/** A variable with synchronized access*/
class SafeVariable[T](initialValue:T) {
private var value = initialValue
val lock = new Lock
def update(u:T=>T) {
this.synchronized{
value = u(value)
}
}
def update2[T2](u:T=>(T, T2)):T2 =
this.synchronized{
val (newValue, result) = u(value)
// println(s"s=$value; lock="+lock.available)
value = newValue
result
}
def get:T = this.synchronized{ value }
override def toString = "SV("+value+"; "+lock.available+")"
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy