Skip to content

Commit 089dfa3

Browse files
committed
LazyVals: support debug mode.
Helps to spot usage of unsafe that would lead to undefined behaviour.
1 parent e00c826 commit 089dfa3

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/dotty/runtime/LazyVals.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@ object LazyVals {
1010

1111
final val BITS_PER_LAZY_VAL = 2L
1212
final val LAZY_VAL_MASK = 3L
13+
final val debug = false
1314

14-
@inline def STATE(cur: Long, ord: Int) = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK
15+
@inline def STATE(cur: Long, ord: Int) = {
16+
val r = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK
17+
if (debug)
18+
println(s"STATE($cur, $ord) = $r")
19+
r
20+
}
1521
@inline def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int) = {
22+
if (debug)
23+
println(s"CAS($t, $offset, $e, $v, $ord)")
1624
val mask = ~(LAZY_VAL_MASK << ord * BITS_PER_LAZY_VAL)
1725
val n = (e & mask) | (v << (ord * BITS_PER_LAZY_VAL))
1826
compareAndSet(t, offset, e, n)
1927
}
2028
@inline def setFlag(t: Object, offset: Long, v: Int, ord: Int) = {
29+
if (debug)
30+
println(s"setFlag($t, $offset, $v, $ord)")
2131
var retry = true
2232
while (retry) {
2333
val cur = get(t, offset)
@@ -35,6 +45,8 @@ object LazyVals {
3545
}
3646
}
3747
@inline def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int) = {
48+
if (debug)
49+
println(s"wait4Notification($t, $offset, $cur, $ord)")
3850
var retry = true
3951
while (retry) {
4052
val cur = get(t, offset)
@@ -68,7 +80,12 @@ object LazyVals {
6880
monitors(id)
6981
}
7082

71-
@inline def getOffset(clz: Class[_], name: String) = unsafe.objectFieldOffset(clz.getDeclaredField(name))
83+
@inline def getOffset(clz: Class[_], name: String) = {
84+
val r = unsafe.objectFieldOffset(clz.getDeclaredField(name))
85+
if (debug)
86+
println(s"getOffset($clz, $name) = $r")
87+
r
88+
}
7289

7390
object Names {
7491
final val state = "STATE"

0 commit comments

Comments
 (0)