src/Pure/System/invoke_scala.ML
author wenzelm
Tue, 18 Oct 2016 16:03:30 +0200
changeset 64304 96bc94c87a81
parent 62505 9e2a65912111
child 66166 c88d1c36c9c3
permissions -rw-r--r--
clarified modules;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/System/invoke_scala.ML
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     3
49173
fa01a202399c eliminated potentially confusing terminology of Scala "layer";
wenzelm
parents: 46774
diff changeset
     4
JVM method invocation service via Isabelle/Scala.
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     5
*)
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     6
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     7
signature INVOKE_SCALA =
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
     8
sig
43749
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
     9
  val method: string -> string -> string
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    10
  val promise_method: string -> string -> string future
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    11
  exception Null
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    12
end;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    13
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    14
structure Invoke_Scala: INVOKE_SCALA =
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    15
struct
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    16
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    17
val _ = Session.protocol_handler "isabelle.Invoke_Scala";
43749
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    18
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    19
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    20
(* pending promises *)
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    21
52537
4b5941730bd8 more uniform Counter in ML and Scala;
wenzelm
parents: 52111
diff changeset
    22
val new_id = string_of_int o Counter.make ();
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    23
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    24
val promises =
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    25
  Synchronized.var "Invoke_Scala.promises" (Symtab.empty: string future Symtab.table);
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    26
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    27
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    28
(* method invocation *)
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    29
43749
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    30
fun promise_method name arg =
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    31
  let
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    32
    val id = new_id ();
56333
38f1422ef473 support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
wenzelm
parents: 52537
diff changeset
    33
    fun abort () = Output.protocol_message (Markup.cancel_scala id) [];
44298
b8f8488704e2 Future.promise: explicit abort operation (like uninterruptible future job);
wenzelm
parents: 43761
diff changeset
    34
    val promise = Future.promise abort : string future;
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    35
    val _ = Synchronized.change promises (Symtab.update (id, promise));
56333
38f1422ef473 support bulk messages consisting of small string segments, which are more healthy to the Poly/ML RTS and might prevent spurious GC crashes such as MTGCProcessMarkPointers::ScanAddressesInObject;
wenzelm
parents: 52537
diff changeset
    36
    val _ = Output.protocol_message (Markup.invoke_scala name id) [arg];
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    37
  in promise end;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    38
43749
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    39
fun method name arg = Future.join (promise_method name arg);
5ca34f21cb44 tuned signature;
wenzelm
parents: 43748
diff changeset
    40
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    41
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    42
(* fulfill *)
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    43
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    44
exception Null;
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    45
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    46
fun fulfill id tag res =
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    47
  let
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    48
    val result =
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    49
      (case tag of
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    50
        "0" => Exn.Exn Null
43761
e72ba84ae58f tuned signature -- corresponding to Scala version;
wenzelm
parents: 43749
diff changeset
    51
      | "1" => Exn.Res res
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    52
      | "2" => Exn.Exn (ERROR res)
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    53
      | "3" => Exn.Exn (Fail res)
49470
ee564db2649b more management of Invoke_Scala tasks;
wenzelm
parents: 49173
diff changeset
    54
      | "4" => Exn.Exn Exn.Interrupt
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    55
      | _ => raise Fail "Bad tag");
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    56
    val promise =
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    57
      Synchronized.change_result promises
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    58
        (fn tab => (the (Symtab.lookup tab id), Symtab.delete id tab));
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    59
    val _ = Future.fulfill_result promise result;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    60
  in () end;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    61
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    62
val _ =
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    63
  Isabelle_Process.protocol_command "Invoke_Scala.fulfill"
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    64
    (fn [id, tag, res] =>
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    65
      fulfill id tag res
62505
9e2a65912111 clarified modules;
wenzelm
parents: 56333
diff changeset
    66
        handle exn => if Exn.is_interrupt exn then () else Exn.reraise exn);
52111
1fd184eaa310 explicit management of Session.Protocol_Handlers, with protocol state and functions;
wenzelm
parents: 50682
diff changeset
    67
43748
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    68
end;
c70bd78ec83c JVM method invocation service via Scala layer;
wenzelm
parents:
diff changeset
    69