Skip to content

Commit 6cbf532

Browse files
committed
implemented select.exit and select.shutdown
1 parent 09a0eca commit 6cbf532

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

src/main/scala/gopher/channels/Channel.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import scala.reflect.api._
1414
trait Channel[A] extends InputOutput[A]
1515
{
1616

17-
1817
def close(): Unit
1918

2019
}

src/main/scala/gopher/channels/CurrentFlowTermination.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@ object CurrentFlowTermination
3636
c.Expr[Unit](q"implicitly[FlowTermination[Any]].doThrow(${e})")
3737
}
3838

39+
def shutdownImpl(c:Context)(): c.Expr[Unit] =
40+
{
41+
import c.universe._
42+
exitImpl[Unit](c)(c.Expr[Unit](q"()"))
43+
}
44+
3945

4046
}

src/main/scala/gopher/channels/SelectorFactory.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ class SelectFactory(val api: GopherAPI)
4646
*/
4747
def loop[A]: SelectorBuilder[A] = new SelectorBuilder[A] with SelectFactoryApi {}
4848

49+
/**
50+
* afold - asynchronious version of fold.
51+
* {{{
52+
* select.afold((0,1)) { (s,(x,y)) =>
53+
* s match {
54+
* case x: out.write => (y,x+y)
55+
* case q: quit.read => select.exit((x,y))
56+
* }
57+
* }
58+
* }}}
59+
*/
4960
def afold[S](s:S)(op:(S,Any)=>S):Future[S] = macro FoldSelectorBuilderImpl.afold[S]
5061

5162
def fold[S](s:S)(op:(S,Any)=>S):S = macro FoldSelectorBuilderImpl.fold[S]
@@ -58,6 +69,11 @@ class SelectFactory(val api: GopherAPI)
5869
def amap[B](f:PartialFunction[Any,B]):Input[B] =
5970
macro SelectorBuilder.inputImpl[B]
6071

72+
//
73+
74+
def exit[A](a:A):A = macro CurrentFlowTermination.exitImpl[A]
75+
76+
def shutdown():Unit = macro CurrentFlowTermination.shutdownImpl
6177

6278
}
6379

src/test/scala/example/FibonaccyFoldSuite.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ import gopher.tags._
1717
object FibonaccyFold {
1818

1919
import scala.concurrent.ExecutionContext.Implicits.global
20+
import CommonTestObjects.gopherApi._
2021

2122
def fibonacci(c: Output[Long], quit: Input[Int]): Future[(Long,Long)] =
22-
gopherApi.select.afold((0L,1L)) { case ((x,y),s) =>
23+
select.afold((0L,1L)) { case ((x,y),s) =>
2324
s match {
2425
case x: c.write => (y, x+y)
2526
case q: quit.read =>
26-
implicitly[FlowTermination[(Long,Long)]].doExit((x,y))
27+
select.exit((x,y))
2728
}
2829
}
2930

3031
def run(n:Int, acceptor: Long => Unit ): Future[(Long,Long)] =
3132
{
32-
val c = gopherApi.makeChannel[Long](1);
33-
val quit = gopherApi.makeChannel[Int](1);
33+
val c = makeChannel[Long](1);
34+
val quit = makeChannel[Int](1);
3435
val r = c.map{ x =>
3536
//Console.println(s"${x} ")
3637
acceptor(x)}.atake(n) flatMap ( _ => (quit awrite 0) )
3738
fibonacci(c,quit)
3839
}
3940

40-
def gopherApi = CommonTestObjects.gopherApi
4141

4242
}
4343

src/test/scala/gopher/channels/FlowTerminationSuite.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.scalatest._
55
import scala.concurrent._
66
import scala.concurrent.duration._
77
import gopher._
8+
import scala.language.postfixOps
89

910
import scala.concurrent.ExecutionContext.Implicits.global
1011

@@ -59,9 +60,7 @@ class FlowTerminationSuite extends FunSuite
5960
var sum = 0
6061
val f = select.forever{
6162
case x: channel.read => sum += x
62-
CurrentFlowTermination.exit(())
63-
//TODO: implement syntax
64-
//select.shutdown()
63+
select.shutdown()
6564
}
6665
val f2 = channel.awrite(1)
6766
Await.result(f, 1 second)

0 commit comments

Comments
 (0)