src/Pure/Concurrent/synchronized.scala
changeset 75393 87ebf5a50283
parent 64370 865b39487b5d
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
     8 
     8 
     9 
     9 
    10 import scala.annotation.tailrec
    10 import scala.annotation.tailrec
    11 
    11 
    12 
    12 
    13 object Synchronized
    13 object Synchronized {
    14 {
       
    15   def apply[A](init: A): Synchronized[A] = new Synchronized(init)
    14   def apply[A](init: A): Synchronized[A] = new Synchronized(init)
    16 }
    15 }
    17 
    16 
    18 
    17 
    19 final class Synchronized[A] private(init: A)
    18 final class Synchronized[A] private(init: A) {
    20 {
       
    21   /* state variable */
    19   /* state variable */
    22 
    20 
    23   private var state: A = init
    21   private var state: A = init
    24 
    22 
    25   def value: A = synchronized { state }
    23   def value: A = synchronized { state }
    36           case Some((y, x1)) =>
    34           case Some((y, x1)) =>
    37             state = x1
    35             state = x1
    38             notifyAll()
    36             notifyAll()
    39             Some(y)
    37             Some(y)
    40         }
    38         }
    41       @tailrec def try_change(): Option[B] =
    39       @tailrec def try_change(): Option[B] = {
    42       {
       
    43         val x = state
    40         val x = state
    44         check(x) match {
    41         check(x) match {
    45           case None =>
    42           case None =>
    46             time_limit(x) match {
    43             time_limit(x) match {
    47               case Some(t) =>
    44               case Some(t) =>