author | wenzelm |
Fri, 15 Nov 2024 23:25:18 +0100 | |
changeset 81459 | 570b4652d143 |
parent 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
56685 | 1 |
/* Title: Pure/Concurrent/synchronized.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Synchronized variables. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
56692 | 10 |
import scala.annotation.tailrec |
11 |
||
12 |
||
75393 | 13 |
object Synchronized { |
56685 | 14 |
def apply[A](init: A): Synchronized[A] = new Synchronized(init) |
15 |
} |
|
16 |
||
17 |
||
75393 | 18 |
final class Synchronized[A] private(init: A) { |
56692 | 19 |
/* state variable */ |
20 |
||
56685 | 21 |
private var state: A = init |
56692 | 22 |
|
56687 | 23 |
def value: A = synchronized { state } |
56692 | 24 |
override def toString: String = value.toString |
25 |
||
26 |
||
27 |
/* synchronized access */ |
|
28 |
||
29 |
def timed_access[B](time_limit: A => Option[Time], f: A => Option[(B, A)]): Option[B] = |
|
30 |
synchronized { |
|
31 |
def check(x: A): Option[B] = |
|
32 |
f(x) match { |
|
33 |
case None => None |
|
34 |
case Some((y, x1)) => |
|
35 |
state = x1 |
|
36 |
notifyAll() |
|
37 |
Some(y) |
|
38 |
} |
|
75393 | 39 |
@tailrec def try_change(): Option[B] = { |
56692 | 40 |
val x = state |
41 |
check(x) match { |
|
42 |
case None => |
|
43 |
time_limit(x) match { |
|
44 |
case Some(t) => |
|
45 |
val timeout = (t - Time.now()).ms |
|
46 |
if (timeout > 0L) { |
|
47 |
wait(timeout) |
|
48 |
check(state) |
|
49 |
} |
|
50 |
else None |
|
51 |
case None => |
|
52 |
wait() |
|
53 |
try_change() |
|
54 |
} |
|
55 |
case some => some |
|
56 |
} |
|
57 |
} |
|
58 |
try_change() |
|
59 |
} |
|
60 |
||
61 |
def guarded_access[B](f: A => Option[(B, A)]): B = |
|
62 |
timed_access(_ => None, f).get |
|
63 |
||
64 |
||
65 |
/* unconditional change */ |
|
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 | 69 |
def change_result[B](f: A => (B, A)): B = synchronized { |
56685 | 70 |
val (result, new_state) = f(state) |
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 | 73 |
result |
74 |
} |
|
75 |
} |