author | wenzelm |
Thu, 04 Apr 2013 18:25:47 +0200 | |
changeset 51619 | 95b7da3430d4 |
parent 29564 | f8b933a62151 |
child 52583 | 0a7240d88e09 |
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 |
|
28579 | 4 |
Message exchange via mailbox, with non-blocking send (due to unbounded |
5 |
queueing) and potentially blocking receive. |
|
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 |
28579 | 13 |
val receive: 'a T -> 'a |
28139 | 14 |
val receive_timeout: Time.time -> 'a T -> 'a option |
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 |
|
28576
dc4aae271c41
simplified implementation using Synchronized.var;
wenzelm
parents:
28443
diff
changeset
|
20 |
datatype 'a T = Mailbox of 'a Queue.T Synchronized.var; |
28579 | 21 |
|
28576
dc4aae271c41
simplified implementation using Synchronized.var;
wenzelm
parents:
28443
diff
changeset
|
22 |
fun create () = Mailbox (Synchronized.var "mailbox" Queue.empty); |
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
23 |
|
28579 | 24 |
fun send (Mailbox mailbox) msg = |
25 |
Synchronized.change mailbox (Queue.enqueue msg); |
|
28139 | 26 |
|
28579 | 27 |
fun receive (Mailbox mailbox) = |
28 |
Synchronized.guarded_access mailbox (try Queue.dequeue); |
|
29 |
||
28576
dc4aae271c41
simplified implementation using Synchronized.var;
wenzelm
parents:
28443
diff
changeset
|
30 |
fun receive_timeout timeout (Mailbox mailbox) = |
28579 | 31 |
Synchronized.timed_access mailbox |
32 |
(fn _ => SOME (Time.+ (Time.now (), timeout))) (try Queue.dequeue); |
|
28135
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
33 |
|
4f6f0496e93c
Concurrent message exchange via mailbox -- with unbounded queueing.
wenzelm
parents:
diff
changeset
|
34 |
end; |