author | wenzelm |
Mon, 24 Oct 2016 12:16:12 +0200 | |
changeset 64370 | 865b39487b5d |
parent 56694 | c4e77643aad6 |
child 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 |
||
56685 | 13 |
object Synchronized |
14 |
{ |
|
15 |
def apply[A](init: A): Synchronized[A] = new Synchronized(init) |
|
16 |
} |
|
17 |
||
18 |
||
19 |
final class Synchronized[A] private(init: A) |
|
20 |
{ |
|
56692 | 21 |
/* state variable */ |
22 |
||
56685 | 23 |
private var state: A = init |
56692 | 24 |
|
56687 | 25 |
def value: A = synchronized { state } |
56692 | 26 |
override def toString: String = value.toString |
27 |
||
28 |
||
29 |
/* synchronized access */ |
|
30 |
||
31 |
def timed_access[B](time_limit: A => Option[Time], f: A => Option[(B, A)]): Option[B] = |
|
32 |
synchronized { |
|
33 |
def check(x: A): Option[B] = |
|
34 |
f(x) match { |
|
35 |
case None => None |
|
36 |
case Some((y, x1)) => |
|
37 |
state = x1 |
|
38 |
notifyAll() |
|
39 |
Some(y) |
|
40 |
} |
|
41 |
@tailrec def try_change(): Option[B] = |
|
42 |
{ |
|
43 |
val x = state |
|
44 |
check(x) match { |
|
45 |
case None => |
|
46 |
time_limit(x) match { |
|
47 |
case Some(t) => |
|
48 |
val timeout = (t - Time.now()).ms |
|
49 |
if (timeout > 0L) { |
|
50 |
wait(timeout) |
|
51 |
check(state) |
|
52 |
} |
|
53 |
else None |
|
54 |
case None => |
|
55 |
wait() |
|
56 |
try_change() |
|
57 |
} |
|
58 |
case some => some |
|
59 |
} |
|
60 |
} |
|
61 |
try_change() |
|
62 |
} |
|
63 |
||
64 |
def guarded_access[B](f: A => Option[(B, A)]): B = |
|
65 |
timed_access(_ => None, f).get |
|
66 |
||
67 |
||
68 |
/* unconditional change */ |
|
69 |
||
56694
c4e77643aad6
proper signaling after each state update (NB: ML version does this uniformly via timed_access);
wenzelm
parents:
56692
diff
changeset
|
70 |
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
|
71 |
|
56687 | 72 |
def change_result[B](f: A => (B, A)): B = synchronized { |
56685 | 73 |
val (result, new_state) = f(state) |
74 |
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
|
75 |
notifyAll() |
56685 | 76 |
result |
77 |
} |
|
78 |
} |