author | wenzelm |
Wed, 10 Jan 2024 13:10:20 +0100 | |
changeset 79463 | 7d10708bbc32 |
parent 62826 | eb94e570c1a4 |
permissions | -rw-r--r-- |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Concurrent/mailbox.ML |
28140 | 2 |
Author: Makarius |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
3 |
|
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
4 |
Message exchange via mailbox, with multiple senders (non-blocking, |
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
5 |
unbounded buffering) and single receiver (bulk messages). |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
6 |
*) |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
7 |
|
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
8 |
signature MAILBOX = |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
9 |
sig |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
10 |
type 'a T |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
11 |
val create: unit -> 'a T |
28170 | 12 |
val send: 'a T -> 'a -> unit |
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
13 |
val receive: Time.time option -> 'a T -> 'a list |
52583 | 14 |
val await_empty: 'a T -> unit |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
15 |
end; |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
16 |
|
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
17 |
structure Mailbox: MAILBOX = |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
18 |
struct |
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
19 |
|
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
20 |
datatype 'a T = Mailbox of 'a list Synchronized.var; |
28579 | 21 |
|
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
22 |
fun create () = Mailbox (Synchronized.var "mailbox" []); |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
23 |
|
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
24 |
fun send (Mailbox mailbox) msg = Synchronized.change mailbox (cons msg); |
28139 | 25 |
|
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
26 |
fun receive timeout (Mailbox mailbox) = |
28579 | 27 |
Synchronized.timed_access mailbox |
62826 | 28 |
(fn _ => Option.map (fn t => Time.now () + t) timeout) |
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
29 |
(fn [] => NONE | msgs => SOME (msgs, [])) |
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
30 |
|> these |> rev; |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
31 |
|
52583 | 32 |
fun await_empty (Mailbox mailbox) = |
57417
29fe9bac501b
more tight Mailbox: single list is sufficient for single receiver, reverse outside critical section;
wenzelm
parents:
52583
diff
changeset
|
33 |
Synchronized.guarded_access mailbox (fn [] => SOME ((), []) | _ => NONE); |
52583 | 34 |
|
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
35 |
end; |