src/Pure/Concurrent/consumer_thread.scala
author wenzelm
Mon, 29 Aug 2022 19:26:27 +0200
changeset 76019 f3d8da992445
parent 75393 87ebf5a50283
child 78533 dd0501accda8
permissions -rw-r--r--
provide cvc5-1.0.2 (inactive);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56695
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Concurrent/consumer_thread.scala
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     3
56702
f96ad2b19c38 support for requests with explicit acknowledgment (and exception propagation);
wenzelm
parents: 56701
diff changeset
     4
Consumer thread with unbounded queueing of requests, and optional
f96ad2b19c38 support for requests with explicit acknowledgment (and exception propagation);
wenzelm
parents: 56701
diff changeset
     5
acknowledgment.
56695
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     6
*/
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     7
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     8
package isabelle
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
     9
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
    10
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
    11
import scala.annotation.tailrec
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
    12
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
    13
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    14
object Consumer_Thread {
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    15
  def fork_bulk[A](name: String = "", daemon: Boolean = false)(
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    16
      bulk: A => Boolean,
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    17
      consume: List[A] => (List[Exn.Result[Unit]], Boolean),
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    18
      finish: () => Unit = () => ()): Consumer_Thread[A] =
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    19
    new Consumer_Thread[A](name, daemon, bulk, consume, finish)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    20
56698
e0655270d3f3 allow more control of main loop;
wenzelm
parents: 56696
diff changeset
    21
  def fork[A](name: String = "", daemon: Boolean = false)(
e0655270d3f3 allow more control of main loop;
wenzelm
parents: 56696
diff changeset
    22
      consume: A => Boolean,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    23
      finish: () => Unit = () => ()
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    24
    ): Consumer_Thread[A] = {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    25
    def consume_single(args: List[A]): (List[Exn.Result[Unit]], Boolean) = {
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    26
      assert(args.length == 1)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    27
      Exn.capture { consume(args.head) } match {
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    28
        case Exn.Res(continue) => (List(Exn.Res(())), continue)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    29
        case Exn.Exn(exn) => (List(Exn.Exn(exn)), true)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    30
      }
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    31
    }
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    32
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    33
    fork_bulk(name = name, daemon = daemon)(_ => false, consume_single, finish = finish)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    34
  }
56696
ff782c5450bf more robust shutdown;
wenzelm
parents: 56695
diff changeset
    35
}
56695
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
    36
56698
e0655270d3f3 allow more control of main loop;
wenzelm
parents: 56696
diff changeset
    37
final class Consumer_Thread[A] private(
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    38
  name: String, daemon: Boolean,
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    39
  bulk: A => Boolean,
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    40
  consume: List[A] => (List[Exn.Result[Unit]], Boolean),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    41
  finish: () => Unit
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    42
) {
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    43
  /* thread */
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    44
56698
e0655270d3f3 allow more control of main loop;
wenzelm
parents: 56696
diff changeset
    45
  private var active = true
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    46
  private val mailbox = Mailbox[Option[Request]]
56696
ff782c5450bf more robust shutdown;
wenzelm
parents: 56695
diff changeset
    47
71692
f8e52c0152fe clarified names;
wenzelm
parents: 71685
diff changeset
    48
  private val thread = Isabelle_Thread.fork(name = name, daemon = daemon) { main_loop(Nil) }
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 74252
diff changeset
    49
  def is_active(): Boolean = active && thread.isAlive
74254
53e1a14e2ef1 tuned signature;
wenzelm
parents: 74253
diff changeset
    50
  def check_thread(): Boolean = Thread.currentThread == thread
56708
d39148de6eee misc tuning;
wenzelm
parents: 56702
diff changeset
    51
56701
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
    52
  private def failure(exn: Throwable): Unit =
56782
433cf57550fa more systematic Isabelle output, like in classic Isabelle/ML (without markup);
wenzelm
parents: 56718
diff changeset
    53
    Output.error_message(
56708
d39148de6eee misc tuning;
wenzelm
parents: 56702
diff changeset
    54
      "Consumer thread failure: " + quote(thread.getName) + "\n" + Exn.message(exn))
56701
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
    55
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
    56
  private def robust_finish(): Unit =
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
    57
    try { finish() } catch { case exn: Throwable => failure(exn) }
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
    58
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    59
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    60
  /* requests */
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    62
  private class Request(val arg: A, acknowledge: Boolean = false) {
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    63
    val ack: Option[Synchronized[Option[Exn.Result[Unit]]]] =
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    64
      if (acknowledge) Some(Synchronized(None)) else None
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    65
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    66
    def await(): Unit = {
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    67
      for (a <- ack) {
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    68
        Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) }))
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    69
      }
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    70
    }
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    71
  }
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    72
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    73
  private def request(req: Request): Unit = {
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    74
    synchronized {
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 74252
diff changeset
    75
      if (is_active()) mailbox.send(Some(req))
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    76
      else error("Consumer thread not active: " + quote(thread.getName))
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    77
    }
74252
3300847d75ae tuned signature;
wenzelm
parents: 74251
diff changeset
    78
    req.await()
71142
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    79
  }
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    80
d6688677a784 clarified signature -- more explicit types;
wenzelm
parents: 66094
diff changeset
    81
  @tailrec private def main_loop(msgs: List[Option[Request]]): Unit =
57417
29fe9bac501b more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents: 56782
diff changeset
    82
    msgs match {
71144
d6b9dead8c8d tuned signature;
wenzelm
parents: 71143
diff changeset
    83
      case Nil => main_loop(mailbox.receive())
57417
29fe9bac501b more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents: 56782
diff changeset
    84
      case None :: _ => robust_finish()
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    85
      case _ =>
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    86
        val reqs =
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    87
          proper_list(msgs.takeWhile(msg => msg.isDefined && bulk(msg.get.arg)))
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    88
            .getOrElse(msgs.take(1))
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    89
            .map(_.get)
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    90
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    91
        val (results, continue) = consume(reqs.map(_.arg))
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    92
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    93
        for {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    94
          (Some(req), Some(res)) <- reqs.map(Some(_)).zipAll(results.map(Some(_)), None, None)
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
    95
        } {
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
    96
          (req.ack, res) match {
74251
wenzelm
parents: 74140
diff changeset
    97
            case (Some(a), _) => a.change(_ => Some(res))
wenzelm
parents: 74140
diff changeset
    98
            case (None, Exn.Res(_)) =>
wenzelm
parents: 74140
diff changeset
    99
            case (None, Exn.Exn(exn)) => failure(exn)
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
   100
          }
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
   101
        }
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
   102
71231
dafa5fce70f1 clarified messages streaming (again, amending 5ea3ed3c52b3): avoid too many small messages stacking up, e.g. when loading HOL-Analysis.Analysis.thy into Isabelle/jEdit;
wenzelm
parents: 71144
diff changeset
   103
        if (continue) main_loop(msgs.drop(reqs.length))
71143
5ea3ed3c52b3 support for bulk operations: consume mailbox content in batches;
wenzelm
parents: 71142
diff changeset
   104
        else robust_finish()
56695
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
   105
    }
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
   106
56701
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
   107
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
   108
  /* main methods */
ac5b66fa2a56 more robust thread: continue after failure;
wenzelm
parents: 56698
diff changeset
   109
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 74252
diff changeset
   110
  assert(is_active())
56696
ff782c5450bf more robust shutdown;
wenzelm
parents: 56695
diff changeset
   111
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71692
diff changeset
   112
  def send(arg: A): Unit = request(new Request(arg))
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 71692
diff changeset
   113
  def send_wait(arg: A): Unit = request(new Request(arg, acknowledge = true))
56702
f96ad2b19c38 support for requests with explicit acknowledgment (and exception propagation);
wenzelm
parents: 56701
diff changeset
   114
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74254
diff changeset
   115
  def shutdown(): Unit = {
74253
45dc9de1bd33 tuned signature;
wenzelm
parents: 74252
diff changeset
   116
    synchronized { if (is_active()) { active = false; mailbox.send(None) } }
74140
8a5e02ef975c clarified signature;
wenzelm
parents: 73340
diff changeset
   117
    thread.join()
56696
ff782c5450bf more robust shutdown;
wenzelm
parents: 56695
diff changeset
   118
  }
56695
963732291084 consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff changeset
   119
}