File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed
tests/run/suspend-strawman-2 Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change 11package fiberRuntime
22
3+ object util :
4+ inline val logging = false
5+ inline def log (inline msg : String ) =
6+ if logging then println(msg)
7+
8+ private val rand = new java.util.Random
9+
10+ def sleepABit () =
11+ Thread .sleep(rand.nextInt(100 ))
12+
13+ val threadName = new ThreadLocal [String ]
14+ end util
15+ import util .*
16+
317/** A delimited contination, which can be invoked with `resume` */
418class Suspension :
519 private var hasResumed = false
@@ -8,22 +22,31 @@ class Suspension:
822 notify()
923 def suspend (): Unit = synchronized :
1024 if ! hasResumed then
25+ log(s " suspended ${threadName.get()}" )
1126 wait()
1227
1328def suspend [T , R ](body : Suspension => Unit ): Unit =
29+ sleepABit()
30+ log(s " suspending ${threadName.get()}" )
1431 val susp = Suspension ()
1532 body(susp)
33+ sleepABit()
1634 susp.suspend()
1735
1836object boundary :
1937 final class Label [- T ]()
2038
21- def setName (name : String ) = ()
39+ def setName (name : String ) =
40+ log(s " started $name, ${Thread .currentThread.getId()}" )
41+ sleepABit()
42+ threadName.set(name)
2243
2344 def apply [T ](body : Label [T ] ?=> Unit ): Unit =
2445 new Thread :
2546 override def run () =
26- body(using Label [T ]())
47+ sleepABit()
48+ try body(using Label [T ]())
49+ finally log(s " finished ${threadName.get()} ${Thread .currentThread.getId()}" )
2750 .start()
2851
2952
You can’t perform that action at this time.
0 commit comments