src/Pure/Concurrent/thread_attributes.ML
author wenzelm
Tue, 26 Sep 2023 12:30:08 +0200
changeset 78715 9506b852ebdf
parent 78713 a44ac17ae227
child 78720 909dc00766a0
permissions -rw-r--r--
clarified signature: distinction of unmanaged vs. managed interrupts (not implemented yet);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/thread_attributes.ML
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     3
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     4
Thread attributes for interrupt handling.
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     5
*)
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     6
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     7
signature THREAD_ATTRIBUTES =
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
     8
sig
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
     9
  type attributes
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    10
  val get_attributes: unit -> attributes
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    11
  val set_attributes: attributes -> unit
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    12
  val convert_attributes: attributes -> Thread.Thread.threadAttribute list
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    13
  val no_interrupts: attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    14
  val test_interrupts: attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    15
  val public_interrupts: attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    16
  val private_interrupts: attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    17
  val sync_interrupts: attributes -> attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    18
  val safe_interrupts: attributes -> attributes
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    19
  val with_attributes: attributes -> (attributes -> 'a) -> 'a
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    20
  val uninterruptible: ((('c -> 'd) -> 'c -> 'd) -> 'a -> 'b) -> 'a -> 'b
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    21
end;
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    22
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    23
structure Thread_Attributes: THREAD_ATTRIBUTES =
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    24
struct
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    25
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    26
abstype attributes = Attributes of Word.word
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    27
with
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    28
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    29
(* thread attributes *)
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    30
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    31
val thread_attributes = 0w7;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    32
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    33
val broadcast = 0w1;
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    34
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    35
val interrupt_state = 0w6;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    36
val interrupt_defer = 0w0;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    37
val interrupt_synch = 0w2;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    38
val interrupt_asynch = 0w4;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    39
val interrupt_asynch_once = 0w6;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    40
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    41
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    42
(* access thread flags *)
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    43
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    44
val thread_flags = 0w1;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    45
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    46
fun load_word () : word =
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    47
  RunCall.loadWord (RunCall.unsafeCast (Thread.Thread.self ()), thread_flags);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    48
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    49
fun get_attributes () = Attributes (Word.andb (load_word (), thread_attributes));
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    50
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    51
fun set_attributes (Attributes w) =
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    52
  let
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    53
    val w' = Word.orb (Word.andb (load_word (), Word.notb thread_attributes), w);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    54
    val _ = RunCall.storeWord (RunCall.unsafeCast (Thread.Thread.self ()), thread_flags, w');
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    55
  in
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    56
    if Word.andb (w', interrupt_asynch) = interrupt_asynch
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    57
    then Thread.Thread.testInterrupt () else ()
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    58
  end;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    59
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    60
fun convert_attributes (Attributes w) =
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    61
  [Thread.Thread.EnableBroadcastInterrupt (Word.andb (w, broadcast) = broadcast),
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    62
   Thread.Thread.InterruptState
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    63
      let val w' = Word.andb (w, interrupt_state) in
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    64
        if w' = interrupt_defer then Thread.Thread.InterruptDefer
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    65
        else if w' = interrupt_synch then Thread.Thread.InterruptSynch
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    66
        else if w' = interrupt_asynch then Thread.Thread.InterruptAsynch
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    67
        else Thread.Thread.InterruptAsynchOnce
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    68
      end];
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    69
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    70
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    71
(* common configurations *)
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    72
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    73
val no_interrupts = Attributes interrupt_defer;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    74
val test_interrupts = Attributes interrupt_synch;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    75
val public_interrupts = Attributes (Word.orb (broadcast, interrupt_asynch_once));
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    76
val private_interrupts = Attributes interrupt_asynch_once;
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    77
64557
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    78
fun sync_interrupts (Attributes w) =
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    79
  let
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    80
    val w' = Word.andb (w, interrupt_state);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    81
    val w'' = Word.andb (w, Word.notb interrupt_state);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    82
  in Attributes (if w' = interrupt_defer then w else Word.orb (w'', interrupt_synch)) end;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    83
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    84
fun safe_interrupts (Attributes w) =
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    85
  let
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    86
    val w' = Word.andb (w, interrupt_state);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    87
    val w'' = Word.andb (w, Word.notb interrupt_state);
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    88
  in Attributes (if w' = interrupt_asynch then Word.orb (w'', interrupt_asynch_once) else w) end;
37074e22e8be more tight thread attributes, based in internal word arithmetic instead of symbolic datatypes: measurable performance improvement;
wenzelm
parents: 62924
diff changeset
    89
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    90
fun with_attributes new_atts e =
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
    91
  let
64564
wenzelm
parents: 64557
diff changeset
    92
    val atts1 = safe_interrupts (get_attributes ());
wenzelm
parents: 64557
diff changeset
    93
    val atts2 = safe_interrupts new_atts;
wenzelm
parents: 64557
diff changeset
    94
  in
wenzelm
parents: 64557
diff changeset
    95
    if atts1 = atts2 then e atts1
wenzelm
parents: 64557
diff changeset
    96
    else
wenzelm
parents: 64557
diff changeset
    97
      let
78715
9506b852ebdf clarified signature: distinction of unmanaged vs. managed interrupts (not implemented yet);
wenzelm
parents: 78713
diff changeset
    98
        val result = Exn.capture0 (fn () => (set_attributes atts2; e atts1)) ();
64564
wenzelm
parents: 64557
diff changeset
    99
        val _ = set_attributes atts1;
wenzelm
parents: 64557
diff changeset
   100
      in Exn.release result end
wenzelm
parents: 64557
diff changeset
   101
  end;
wenzelm
parents: 64557
diff changeset
   102
wenzelm
parents: 64557
diff changeset
   103
end;
62923
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   104
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   105
fun uninterruptible f x =
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   106
  with_attributes no_interrupts (fn atts =>
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   107
    f (fn g => fn y => with_attributes atts (fn _ => g y)) x);
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   108
3a122e1e352a clarified bootstrap;
wenzelm
parents:
diff changeset
   109
end;