src/Pure/Concurrent/synchronized.scala
author wenzelm
Fri, 15 Nov 2024 23:25:18 +0100
changeset 81459 570b4652d143
parent 75393 87ebf5a50283
permissions -rw-r--r--
more NEWS;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56685
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Concurrent/synchronized.scala
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     3
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     4
Synchronized variables.
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     5
*/
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     6
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     7
package isabelle
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     8
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
     9
56692
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    10
import scala.annotation.tailrec
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    11
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 64370
diff changeset
    13
object Synchronized {
56685
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    14
  def apply[A](init: A): Synchronized[A] = new Synchronized(init)
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    15
}
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    16
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    17
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 64370
diff changeset
    18
final class Synchronized[A] private(init: A) {
56692
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    19
  /* state variable */
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    20
56685
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    21
  private var state: A = init
56692
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    22
56687
7fb98325722a tuned signature in accordance to ML version;
wenzelm
parents: 56685
diff changeset
    23
  def value: A = synchronized { state }
56692
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    24
  override def toString: String = value.toString
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    25
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    26
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    27
  /* synchronized access */
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    28
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    29
  def timed_access[B](time_limit: A => Option[Time], f: A => Option[(B, A)]): Option[B] =
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    30
    synchronized {
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    31
      def check(x: A): Option[B] =
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    32
        f(x) match {
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    33
          case None => None
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    34
          case Some((y, x1)) =>
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    35
            state = x1
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    36
            notifyAll()
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    37
            Some(y)
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    38
        }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 64370
diff changeset
    39
      @tailrec def try_change(): Option[B] = {
56692
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    40
        val x = state
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    41
        check(x) match {
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    42
          case None =>
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    43
            time_limit(x) match {
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    44
              case Some(t) =>
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    45
                val timeout = (t - Time.now()).ms
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    46
                if (timeout > 0L) {
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    47
                  wait(timeout)
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    48
                  check(state)
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    49
                }
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    50
                else None
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    51
              case None =>
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    52
                wait()
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    53
                try_change()
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    54
            }
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    55
          case some => some
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    56
        }
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    57
      }
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    58
      try_change()
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    59
    }
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    60
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    61
  def guarded_access[B](f: A => Option[(B, A)]): B =
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    62
    timed_access(_ => None, f).get
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    63
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    64
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    65
  /* unconditional change */
8219a65b24e3 synchronized access, similar to ML version;
wenzelm
parents: 56687
diff changeset
    66
56694
c4e77643aad6 proper signaling after each state update (NB: ML version does this uniformly via timed_access);
wenzelm
parents: 56692
diff changeset
    67
  def change(f: A => A): Unit = synchronized { state = f(state); notifyAll() }
c4e77643aad6 proper signaling after each state update (NB: ML version does this uniformly via timed_access);
wenzelm
parents: 56692
diff changeset
    68
56687
7fb98325722a tuned signature in accordance to ML version;
wenzelm
parents: 56685
diff changeset
    69
  def change_result[B](f: A => (B, A)): B = synchronized {
56685
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    70
    val (result, new_state) = f(state)
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    71
    state = new_state
56694
c4e77643aad6 proper signaling after each state update (NB: ML version does this uniformly via timed_access);
wenzelm
parents: 56692
diff changeset
    72
    notifyAll()
56685
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    73
    result
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    74
  }
535d59d4ed12 more uniform synchronized variables;
wenzelm
parents:
diff changeset
    75
}