src/Pure/Concurrent/mailbox.scala
author wenzelm
Fri, 25 Apr 2014 13:55:50 +0200
changeset 56719 80eb2192516a
parent 56693 0423c957b081
child 57417 29fe9bac501b
permissions -rw-r--r--
simplified change_buffer (again, see 937826d702d5): no thread, just timer, rely on asynchronous commands_changed.post;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56693
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Concurrent/mailbox.scala
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     2
    Module:     PIDE
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     4
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     5
Message exchange via mailbox, with non-blocking send (due to unbounded
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     6
queueing) and potentially blocking receive.
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     7
*/
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     8
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
     9
package isabelle
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    10
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    11
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    12
import scala.collection.immutable.Queue
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    13
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    14
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    15
object Mailbox
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    16
{
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    17
  def apply[A]: Mailbox[A] = new Mailbox[A]()
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    18
}
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    19
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    20
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    21
class Mailbox[A] private()
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    22
{
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    23
  private val mailbox = Synchronized(Queue.empty[A])
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    24
  override def toString: String = mailbox.value.mkString("Mailbox(", ",", ")")
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    25
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    26
  def send(msg: A): Unit =
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    27
    mailbox.change(_.enqueue(msg))
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    28
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    29
  def receive: A =
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    30
    mailbox.guarded_access(_.dequeueOption)
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    31
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    32
  def receive_timeout(timeout: Time): Option[A] =
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    33
    mailbox.timed_access(_ => Some(Time.now() + timeout), _.dequeueOption)
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    34
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    35
  def await_empty: Unit =
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    36
    mailbox.guarded_access(queue => if (queue.isEmpty) Some(((), queue)) else None)
0423c957b081 added Mailbox, as in ML;
wenzelm
parents:
diff changeset
    37
}