summaryrefslogtreecommitdiff
path: root/sources/examples/ComputeServer.scala
blob: c3ea4907adde19980e7262d44ffd5430cc80c406 (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
package examples;

import concurrent._, concurrent.ops._;

class ComputeServer(n: Int) {
  private trait Job {
    type t;
    def task: t;
    def return(x: t): Unit;
  }

  private val openJobs = new Channel[Job]();

  private def processor(i: Int): Unit = {
    while (True) {
      val job = openJobs.read;
      job.return(job.task)
    }
  }
  def future[a](def p: a): () => a = {
    val reply = new SyncVar[a]();
    openJobs.write{
      new Job {
	type t = a;
	def task = p;
	def return(x: a) = reply.set(x);
      }
    }
    () => reply.get
  }
  replicate(1,n){processor}
}