author | wenzelm |
Tue, 29 Aug 2023 12:53:28 +0200 | |
changeset 78592 | fdfe9b91d96e |
parent 78534 | 879e1ba3868b |
child 78864 | 2024a2298d7a |
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), |
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
18 |
timeout: Option[Time] = None, |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
19 |
finish: () => Unit = () => ()): Consumer_Thread[A] = |
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
20 |
new Consumer_Thread[A](name, daemon, bulk, consume, timeout, finish) |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
21 |
|
56698 | 22 |
def fork[A](name: String = "", daemon: Boolean = false)( |
23 |
consume: A => Boolean, |
|
75393 | 24 |
finish: () => Unit = () => () |
25 |
): Consumer_Thread[A] = { |
|
26 |
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
|
27 |
assert(args.length == 1) |
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
28 |
Exn.capture { consume(args.head) } match { |
78533 | 29 |
case Exn.Res(cont) => (List(Exn.Res(())), cont) |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
30 |
case Exn.Exn(exn) => (List(Exn.Exn(exn)), true) |
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 |
|
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
34 |
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
|
35 |
} |
56696 | 36 |
} |
56695
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
37 |
|
56698 | 38 |
final class Consumer_Thread[A] private( |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
39 |
name: String, daemon: Boolean, |
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
40 |
bulk: A => Boolean, |
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
41 |
consume: List[A] => (List[Exn.Result[Unit]], Boolean), |
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
42 |
timeout: Option[Time] = None, |
75393 | 43 |
finish: () => Unit |
44 |
) { |
|
71142 | 45 |
/* thread */ |
46 |
||
56698 | 47 |
private var active = true |
71142 | 48 |
private val mailbox = Mailbox[Option[Request]] |
56696 | 49 |
|
71692 | 50 |
private val thread = Isabelle_Thread.fork(name = name, daemon = daemon) { main_loop(Nil) } |
74253 | 51 |
def is_active(): Boolean = active && thread.isAlive |
74254 | 52 |
def check_thread(): Boolean = Thread.currentThread == thread |
56708 | 53 |
|
56701 | 54 |
private def failure(exn: Throwable): Unit = |
56782
433cf57550fa
more systematic Isabelle output, like in classic Isabelle/ML (without markup);
wenzelm
parents:
56718
diff
changeset
|
55 |
Output.error_message( |
56708 | 56 |
"Consumer thread failure: " + quote(thread.getName) + "\n" + Exn.message(exn)) |
56701 | 57 |
|
58 |
private def robust_finish(): Unit = |
|
59 |
try { finish() } catch { case exn: Throwable => failure(exn) } |
|
60 |
||
71142 | 61 |
|
62 |
/* requests */ |
|
63 |
||
75393 | 64 |
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
|
65 |
val ack: Option[Synchronized[Option[Exn.Result[Unit]]]] = |
71142 | 66 |
if (acknowledge) Some(Synchronized(None)) else None |
67 |
||
75393 | 68 |
def await(): Unit = { |
71142 | 69 |
for (a <- ack) { |
70 |
Exn.release(a.guarded_access({ case None => None case res => Some((res.get, res)) })) |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
75393 | 75 |
private def request(req: Request): Unit = { |
71142 | 76 |
synchronized { |
74253 | 77 |
if (is_active()) mailbox.send(Some(req)) |
71142 | 78 |
else error("Consumer thread not active: " + quote(thread.getName)) |
79 |
} |
|
74252 | 80 |
req.await() |
71142 | 81 |
} |
82 |
||
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
83 |
private def process(msgs: List[Option[Request]]): (List[Option[Request]], Boolean) = { |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
84 |
val reqs = |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
85 |
proper_list(msgs.takeWhile(msg => msg.isDefined && bulk(msg.get.arg))) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
86 |
.getOrElse(msgs.take(1)) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
87 |
.map(_.get) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
88 |
val rest = msgs.drop(reqs.length) |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
89 |
|
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
90 |
val (results, cont) = consume(reqs.map(_.arg)) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
91 |
for { |
78592 | 92 |
case (Some(req), Some(res)) <- reqs.map(Some(_)).zipAll(results.map(Some(_)), None, None) |
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
93 |
} { |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
94 |
(req.ack, res) match { |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
95 |
case (Some(a), _) => a.change(_ => Some(res)) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
96 |
case (None, Exn.Res(_)) => |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
97 |
case (None, Exn.Exn(exn)) => failure(exn) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
98 |
} |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
99 |
} |
71143
5ea3ed3c52b3
support for bulk operations: consume mailbox content in batches;
wenzelm
parents:
71142
diff
changeset
|
100 |
|
78534
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
101 |
(rest, cont) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
102 |
} |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
103 |
|
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
104 |
@tailrec private def main_loop(buffer: List[Option[Request]]): Unit = |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
105 |
proper_list(buffer).getOrElse(mailbox.receive(timeout = timeout)) match { |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
106 |
case None :: _ => robust_finish() |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
107 |
case msgs => |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
108 |
val (rest, cont) = process(msgs) |
879e1ba3868b
clarified main_loop: support timeout, which results in consume(Nil);
wenzelm
parents:
78533
diff
changeset
|
109 |
if (cont) main_loop(rest) else robust_finish() |
56695
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
110 |
} |
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
111 |
|
56701 | 112 |
|
113 |
/* main methods */ |
|
114 |
||
74253 | 115 |
assert(is_active()) |
56696 | 116 |
|
73340 | 117 |
def send(arg: A): Unit = request(new Request(arg)) |
118 |
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
|
119 |
|
75393 | 120 |
def shutdown(): Unit = { |
74253 | 121 |
synchronized { if (is_active()) { active = false; mailbox.send(None) } } |
74140 | 122 |
thread.join() |
56696 | 123 |
} |
56695
963732291084
consumer thread with unbounded queueing of requests (similar to Message_Channel in ML);
wenzelm
parents:
diff
changeset
|
124 |
} |