~ubuntu-branches/debian/sid/scala/sid

« back to all changes in this revision

Viewing changes to src/library/scala/concurrent/MailBox.scala

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Mehdi Dogguy, Lucas Satabin, Frank S. Thomas, Emmanuel Bourg
  • Date: 2015-06-05 23:52:59 UTC
  • mfrom: (1.2.11)
  • Revision ID: package-import@ubuntu.com-20150605235259-wk00vgk83dh8o19g
Tags: 2.10.5-1
* Team upload.

[ Mehdi Dogguy ]
* New upstream release (Closes: #744278).

[ Lucas Satabin ]
* Update patches
* Update the clean target
* Update paths of elements to install
* Update watch file

[ Frank S. Thomas ]
* Remove myself from Uploaders.

[ Emmanuel Bourg ]
* The package has been adopted by the Java Team (Closes: #754935)
* Patched the build to avoid downloading libraries from the Internet
* Replaced the minified JavaScript files with unobfuscated ones
* No longer build scala-partest.jar until diffutils is packaged or replaced
* debian/watch: Fixed the versions matched (x.y.z instead of x.y.z..z)
* debian/rules:
  - Added the missing get-orig-source target (Closes: #724704)
  - Improved the clean target
* debian/control:
  - Build depend on scala (>= 2.10) and bnd
  - Use canonical URLs for the Vcs-* fields
  - Standards-Version updated to 3.9.6 (no changes)
* Switch to debhelper level 9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*                     __                                               *\
2
 
**     ________ ___   / /  ___     Scala API                            **
3
 
**    / __/ __// _ | / /  / _ |    (c) 2003-2011, LAMP/EPFL             **
4
 
**  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **
5
 
** /____/\___/_/ |_/____/_/ | |                                         **
6
 
**                          |/                                          **
7
 
\*                                                                      */
8
 
 
9
 
 
10
 
 
11
 
package scala.concurrent
12
 
 
13
 
/** This class ...
14
 
 *
15
 
 *  @author  Martin Odersky
16
 
 *  @version 1.0, 12/03/2003
17
 
 */
18
 
//class MailBox with Monitor with LinkedListQueueCreator {
19
 
@deprecated("use actors instead", "2.8.0")
20
 
class MailBox extends AnyRef with ListQueueCreator {
21
 
 
22
 
  type Message = AnyRef
23
 
 
24
 
  private abstract class PreReceiver {
25
 
    var msg: Message = null
26
 
    def isDefinedAt(msg: Message): Boolean
27
 
  }
28
 
 
29
 
  private class Receiver[A](receiver: PartialFunction[Message, A]) extends PreReceiver {
30
 
 
31
 
    def isDefinedAt(msg: Message) = receiver.isDefinedAt(msg)
32
 
 
33
 
    def receive(): A = synchronized {
34
 
      while (msg eq null) wait()
35
 
      receiver(msg)
36
 
    }
37
 
 
38
 
    def receiveWithin(msec: Long): A = synchronized {
39
 
      if (msg eq null) wait(msec)
40
 
      receiver(if (msg ne null) msg else TIMEOUT)
41
 
    }
42
 
  }
43
 
 
44
 
  private val messageQueue = queueCreate[Message]
45
 
  private val receiverQueue = queueCreate[PreReceiver]
46
 
 
47
 
  /** Unconsumed messages. */
48
 
  private var sent = messageQueue.make
49
 
 
50
 
  /** Pending receivers. */
51
 
  private var receivers = receiverQueue.make
52
 
 
53
 
  /**
54
 
  * Check whether the receiver can be applied to an unconsumed message.
55
 
  * If yes, the message is extracted and associated with the receiver.
56
 
  * Otherwise the receiver is appended to the list of pending receivers.
57
 
  */
58
 
  private def scanSentMsgs[A](receiver: Receiver[A]): Unit = synchronized {
59
 
    messageQueue.extractFirst(sent, msg => receiver.isDefinedAt(msg)) match {
60
 
      case None =>
61
 
        receivers = receiverQueue.append(receivers, receiver)
62
 
      case Some((msg, withoutMsg)) =>
63
 
        sent = withoutMsg
64
 
        receiver.msg = msg
65
 
    }
66
 
  }
67
 
 
68
 
  /**
69
 
  * First check whether a pending receiver is applicable to the sent
70
 
  * message. If yes, the receiver is notified. Otherwise the message
71
 
  * is appended to the linked list of sent messages.
72
 
  */
73
 
  def send(msg: Message): Unit = synchronized {
74
 
    receiverQueue.extractFirst(receivers, r => r.isDefinedAt(msg)) match {
75
 
      case None =>
76
 
        sent = messageQueue.append(sent, msg)
77
 
      case Some((receiver, withoutReceiver)) =>
78
 
        receivers = withoutReceiver
79
 
        receiver.msg = msg
80
 
        receiver synchronized { receiver.notify() }
81
 
    }
82
 
  }
83
 
 
84
 
  /**
85
 
  * Block until there is a message in the mailbox for which the processor
86
 
  * <code>f</code> is defined.
87
 
  */
88
 
  def receive[A](f: PartialFunction[Message, A]): A = {
89
 
    val r = new Receiver(f)
90
 
    scanSentMsgs(r)
91
 
    r.receive()
92
 
  }
93
 
 
94
 
  /**
95
 
  * Block until there is a message in the mailbox for which the processor
96
 
  * <code>f</code> is defined or the timeout is over.
97
 
  */
98
 
  def receiveWithin[A](msec: Long)(f: PartialFunction[Message, A]): A = {
99
 
    val r = new Receiver(f)
100
 
    scanSentMsgs(r)
101
 
    r.receiveWithin(msec)
102
 
  }
103
 
 
104
 
}
105
 
 
106
 
 
107
 
 
108
 
/**
109
 
* Module for dealing with queues.
110
 
*/
111
 
@deprecated("use actors instead", "2.8.0")
112
 
trait QueueModule[A] {
113
 
  /** Type of queues. */
114
 
  type T
115
 
  /** Create an empty queue. */
116
 
  def make: T
117
 
  /** Append an element to a queue. */
118
 
  def append(l: T, x: A): T
119
 
  /** Extract an element satisfying a predicate from a queue. */
120
 
  def extractFirst(l: T, p: A => Boolean): Option[(A, T)]
121
 
}
122
 
 
123
 
/** Inefficient but simple queue module creator. */
124
 
@deprecated("use actors instead", "2.8.0")
125
 
trait ListQueueCreator {
126
 
  def queueCreate[A]: QueueModule[A] = new QueueModule[A] {
127
 
    type T = List[A]
128
 
    def make: T = Nil
129
 
    def append(l: T, x: A): T = l ::: x :: Nil
130
 
    def extractFirst(l: T, p: A => Boolean): Option[(A, T)] =
131
 
      l match {
132
 
        case Nil => None
133
 
        case head :: tail =>
134
 
          if (p(head))
135
 
            Some((head, tail))
136
 
          else
137
 
            extractFirst(tail, p) match {
138
 
              case None => None
139
 
              case Some((x, without_x)) => Some((x, head :: without_x))
140
 
            }
141
 
      }
142
 
  }
143
 
}
144
 
 
145
 
/** Efficient queue module creator based on linked lists. */
146
 
@deprecated("use actors instead", "2.8.0")
147
 
trait LinkedListQueueCreator {
148
 
  import scala.collection.mutable.LinkedList
149
 
  def queueCreate[A >: Null <: AnyRef]: QueueModule[A] = new QueueModule[A] {
150
 
    type T = (LinkedList[A], LinkedList[A]) // fst = the list, snd = last elem
151
 
    def make: T = {
152
 
      val l = new LinkedList[A](null, null)
153
 
      (l, l)
154
 
    }
155
 
    def append(l: T, x: A): T = {
156
 
      val atTail = new LinkedList(x, null)
157
 
      l._2 append atTail;
158
 
      (l._1, atTail)
159
 
    }
160
 
    def extractFirst(l: T, p: A => Boolean): Option[(A, T)] = {
161
 
      var xs = l._1
162
 
      var xs1 = xs.next
163
 
      while ((xs1 ne null) && !p(xs1.elem)) {
164
 
        xs = xs1
165
 
        xs1 = xs1.next
166
 
      }
167
 
      if (xs1 ne null) {
168
 
        xs.next = xs1.next
169
 
        if (xs.next eq null)
170
 
          Some((xs1.elem, (l._1, xs)))
171
 
        else
172
 
          Some((xs1.elem, l))
173
 
      }
174
 
      else
175
 
        None
176
 
    }
177
 
  }
178
 
}
179