summaryrefslogtreecommitdiff
path: root/docs/examples/computeserver.scala
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/computeserver.scala')
-rw-r--r--docs/examples/computeserver.scala42
1 files changed, 42 insertions, 0 deletions
diff --git a/docs/examples/computeserver.scala b/docs/examples/computeserver.scala
new file mode 100644
index 0000000000..acc4a0b93e
--- /dev/null
+++ b/docs/examples/computeserver.scala
@@ -0,0 +1,42 @@
+package examples;
+
+import concurrent._, concurrent.ops._;
+
+class ComputeServer(n: Int) {
+
+ private trait Job {
+ type t;
+ def task: t;
+ def ret(x: t): Unit;
+ }
+
+ private val openJobs = new Channel[Job]();
+
+ private def processor(i: Int): Unit = {
+ while (true) {
+ val job = openJobs.read;
+ Console.println("read a job");
+ job.ret(job.task)
+ }
+ }
+
+ def future[a](p: => a): () => a = {
+ val reply = new SyncVar[a]();
+ openJobs.write{
+ new Job {
+ type t = a;
+ def task = p;
+ def ret(x: a) = reply.set(x);
+ }
+ }
+ () => reply.get
+ }
+
+ spawn(replicate(0, n) { processor })
+}
+
+object computeserver with Application {
+ val server = new ComputeServer(1);
+ val f = server.future(42);
+ Console.println(f())
+}