src/Pure/Concurrent/unsynchronized.ML
author wenzelm
Wed, 10 Jan 2024 13:10:20 +0100
changeset 79463 7d10708bbc32
parent 78720 909dc00766a0
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62508
d0b68218ea55 discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents: 61925
diff changeset
     1
(*  Title:      Pure/Concurrent/unsynchronized.ML
32737
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
     3
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
     4
Raw ML references as unsynchronized state variables.
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
     5
*)
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
     6
62935
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
     7
signature UNSYNCHRONIZED =
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
     8
sig
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
     9
  datatype ref = datatype ref
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    10
  val := : 'a ref * 'a -> unit
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    11
  val ! : 'a ref -> 'a
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    12
  val change: 'a ref -> ('a -> 'a) -> unit
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    13
  val change_result: 'a ref -> ('a -> 'b * 'a) -> 'b
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    14
  val inc: int ref -> int
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    15
  val dec: int ref -> int
73696
03e134d5f867 reactive "sledgehammer_filter": statically correct, but untested (no proof_file);
wenzelm
parents: 62935
diff changeset
    16
  val add: int ref -> int -> int
62935
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    17
  val setmp: 'a ref -> 'a -> ('b -> 'c) -> 'b -> 'c
77998
cad50a7c8091 support for cached evaluation via weak_ref;
wenzelm
parents: 77996
diff changeset
    18
  type 'a weak_ref = 'a ref option ref
cad50a7c8091 support for cached evaluation via weak_ref;
wenzelm
parents: 77996
diff changeset
    19
  val weak_ref: 'a -> 'a weak_ref
78001
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    20
  val weak_peek: 'a weak_ref -> 'a option
78019
82b09fd28504 more operations;
wenzelm
parents: 78002
diff changeset
    21
  val weak_active: 'a weak_ref -> bool
62935
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    22
end;
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    23
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    24
structure Unsynchronized: UNSYNCHRONIZED =
32737
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    25
struct
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    26
78001
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    27
(* regular references *)
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    28
32737
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    29
datatype ref = datatype ref;
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    30
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    31
val op := = op :=;
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    32
val ! = !;
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    33
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    34
fun change r f = r := f (! r);
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    35
fun change_result r f = let val (x, y) = f (! r) in r := y; x end;
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    36
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    37
fun inc i = (i := ! i + (1: int); ! i);
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    38
fun dec i = (i := ! i - (1: int); ! i);
73696
03e134d5f867 reactive "sledgehammer_filter": statically correct, but untested (no proof_file);
wenzelm
parents: 62935
diff changeset
    39
fun add i n = (i := ! i + (n: int); ! i);
32737
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    40
39616
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    41
fun setmp flag value f x =
78720
909dc00766a0 clarified signature;
wenzelm
parents: 78716
diff changeset
    42
  Thread_Attributes.uninterruptible_body (fn run =>
39616
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    43
    let
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    44
      val orig_value = ! flag;
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    45
      val _ = flag := value;
78716
97dfba4405e3 tuned signature;
wenzelm
parents: 78715
diff changeset
    46
      val result = Exn.capture0 (run f) x;
39616
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    47
      val _ = flag := orig_value;
78720
909dc00766a0 clarified signature;
wenzelm
parents: 78716
diff changeset
    48
    in Exn.release result end);
39616
8052101883c3 renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents: 38799
diff changeset
    49
78001
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    50
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    51
(* weak references *)
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    52
78002
1aa20895464e tuned comments;
wenzelm
parents: 78001
diff changeset
    53
(*see also $ML_SOURCES/basis/Weak.sml*)
1aa20895464e tuned comments;
wenzelm
parents: 78001
diff changeset
    54
77998
cad50a7c8091 support for cached evaluation via weak_ref;
wenzelm
parents: 77996
diff changeset
    55
type 'a weak_ref = 'a ref option ref;
78001
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    56
77998
cad50a7c8091 support for cached evaluation via weak_ref;
wenzelm
parents: 77996
diff changeset
    57
fun weak_ref a = Weak.weak (SOME (ref a));
cad50a7c8091 support for cached evaluation via weak_ref;
wenzelm
parents: 77996
diff changeset
    58
78001
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    59
fun weak_peek (ref (SOME (ref a))) = SOME a
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    60
  | weak_peek _ = NONE;
e5c146904c90 clarified signature;
wenzelm
parents: 77998
diff changeset
    61
78019
82b09fd28504 more operations;
wenzelm
parents: 78002
diff changeset
    62
fun weak_active (ref (SOME (ref _))) = true
82b09fd28504 more operations;
wenzelm
parents: 78002
diff changeset
    63
  | weak_active _ = false;
82b09fd28504 more operations;
wenzelm
parents: 78002
diff changeset
    64
32737
76fa673eee8b Raw ML references as unsynchronized state variables.
wenzelm
parents:
diff changeset
    65
end;
62818
2733b240bfea clarified modules;
wenzelm
parents: 62508
diff changeset
    66
62935
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    67
ML_Name_Space.forget_val "ref";
3c7a35c12e03 proper signature for structure;
wenzelm
parents: 62923
diff changeset
    68
ML_Name_Space.forget_type "ref";