blob: 865d0187f6e341e14d0ede2cfcb6b9713c472bda (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.concurrent
/** This class ...
*
* @author Martin Odersky
* @version 1.0, 10/03/2003
*/
class Channel[A] {
class LinkedList[A] {
var elem: A = _
var next: LinkedList[A] = null
}
private var written = new LinkedList[A] // FIFO buffer, realized through
private var lastWritten = written // aliasing of a linked list
private var nreaders = 0
/**
* @param x ...
*/
def write(x: A) = synchronized {
lastWritten.elem = x
lastWritten.next = new LinkedList[A]
lastWritten = lastWritten.next
if (nreaders > 0) notify()
}
def read: A = synchronized {
while (null == written.next) {
nreaders += 1; wait(); nreaders -= 1
}
val x = written.elem
written = written.next
x
}
}
|