author | wenzelm |
Mon, 29 Aug 2022 19:26:27 +0200 | |
changeset 76019 | f3d8da992445 |
parent 75393 | 87ebf5a50283 |
child 78533 | dd0501accda8 |
permissions | -rw-r--r-- |
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 | 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 | 21 |
def fork[A](name: String = "", daemon: Boolean = false)( |
22 |
consume: A => Boolean, |
|
75393 | 23 |
finish: () => Unit = () => () |
24 |
): Consumer_Thread[A] = { |
|
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 | 35 |
} |
56695
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
36 |
|
56698 | 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 | 41 |
finish: () => Unit |
42 |
) { |
|
71142 | 43 |
/* thread */ |
44 |
||
56698 | 45 |
private var active = true |
71142 | 46 |
private val mailbox = Mailbox[Option[Request]] |
56696 | 47 |
|
71692 | 48 |
private val thread = Isabelle_Thread.fork(name = name, daemon = daemon) { main_loop(Nil) } |
74253 | 49 |
def is_active(): Boolean = active && thread.isAlive |
74254 | 50 |
def check_thread(): Boolean = Thread.currentThread == thread |
56708 | 51 |
|
56701 | 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 | 54 |
"Consumer thread failure: " + quote(thread.getName) + "\n" + Exn.message(exn)) |
56701 | 55 |
|
56 |
private def robust_finish(): Unit = |
|
57 |
try { finish() } catch { case exn: Throwable => failure(exn) } |
|
58 |
||
71142 | 59 |
|
60 |
/* requests */ |
|
61 |
||
75393 | 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 | 64 |
if (acknowledge) Some(Synchronized(None)) else None |
65 |
||
75393 | 66 |
def await(): Unit = { |
71142 | 67 |
for (a <- ack) { |
68 |
Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) })) |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
||
75393 | 73 |
private def request(req: Request): Unit = { |
71142 | 74 |
synchronized { |
74253 | 75 |
if (is_active()) mailbox.send(Some(req)) |
71142 | 76 |
else error("Consumer thread not active: " + quote(thread.getName)) |
77 |
} |
|
74252 | 78 |
req.await() |
71142 | 79 |
} |
80 |
||
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 | 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 | 93 |
for { |
94 |
(Some(req), Some(res)) <- reqs.map(Some(_)).zipAll(results.map(Some(_)), None, None) |
|
95 |
} { |
|
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
96 |
(req.ack, res) match { |
74251 | 97 |
case (Some(a), _) => a.change(_ => Some(res)) |
98 |
case (None, Exn.Res(_)) => |
|
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 | 107 |
|
108 |
/* main methods */ |
|
109 |
||
74253 | 110 |
assert(is_active()) |
56696 | 111 |
|
73340 | 112 |
def send(arg: A): Unit = request(new Request(arg)) |
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 | 115 |
def shutdown(): Unit = { |
74253 | 116 |
synchronized { if (is_active()) { active = false; mailbox.send(None) } } |
74140 | 117 |
thread.join() |
56696 | 118 |
} |
56695
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
119 |
} |