File tree Expand file tree Collapse file tree 3 files changed +40
-12
lines changed
tests/run/suspend-strawman-2 Expand file tree Collapse file tree 3 files changed +40
-12
lines changed Original file line number Diff line number Diff line change @@ -65,6 +65,7 @@ object Async:
6565
6666 end Impl
6767
68+ /** An implementation of Async that blocks the running thread when waiting */
6869 private class Blocking (val scheduler : Scheduler = Scheduler ) extends Async :
6970
7071 def root = Cancellable .empty
@@ -73,19 +74,21 @@ object Async:
7374
7475 private var hasResumed = false
7576
76- def await [T ](src : Source [T ]): T = synchronized :
77- src.poll() match
78- case Some (x) => x
79- case None =>
80- var result : Option [T ] = None
81- src.onComplete: x =>
82- synchronized :
83- result = Some (x)
84- notify()
85- true
77+ def await [T ](src : Source [T ]): T =
78+ src.poll().getOrElse:
79+ var result : Option [T ] = None
80+ src.onComplete: x =>
81+ synchronized :
82+ result = Some (x)
83+ notify()
84+ true
85+ synchronized :
8686 while result.isEmpty do wait()
8787 result.get
8888
89+ /** Execute asynchronous computation `body` on currently running thread.
90+ * The thread will suspend when the computation waits.
91+ */
8992 def blocking [T ](body : Async ?=> T , scheduler : Scheduler = Scheduler ): T =
9093 body(using Blocking ())
9194
Original file line number Diff line number Diff line change 1+ // scalajs: --skip
2+
13import concurrent .*
24import fiberRuntime .boundary .setName
35
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