Skip to content

Commit 13f5174

Browse files
committed
Add logging and timing randomization
1 parent f9675a5 commit 13f5174

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package 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.nextLong(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` */
418
class 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

1328
def 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

1836
object 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

0 commit comments

Comments
 (0)