@@ -22,6 +22,7 @@ object LazyVals {
22
22
val processors = java.lang.Runtime .getRuntime.nn.availableProcessors()
23
23
8 * processors * processors
24
24
}
25
+
25
26
private [this ] val monitors : Array [Object ] =
26
27
Array .tabulate(base)(_ => new Object )
27
28
@@ -37,6 +38,41 @@ object LazyVals {
37
38
38
39
/* ------------- Start of public API ------------- */
39
40
41
+ /**
42
+ * Used to indicate the state of a lazy val that is being
43
+ * evaluated and of which other threads await the result.
44
+ */
45
+ final class Waiting :
46
+ private var done = false
47
+
48
+ /**
49
+ * Wakes up waiting threads. Called on completion of the evaluation
50
+ * of lazy val's right-hand side.
51
+ */
52
+ def release (): Unit = synchronized {
53
+ done = true
54
+ notifyAll()
55
+ }
56
+
57
+ /**
58
+ * Awaits the completion of the evaluation of lazy val's right-hand side.
59
+ */
60
+ def awaitRelease (): Unit = synchronized {
61
+ while ! done do wait()
62
+ }
63
+
64
+ /**
65
+ * Used to indicate the state of a lazy val that is currently being
66
+ * evaluated with no other thread awaiting its result.
67
+ */
68
+ object Evaluating
69
+
70
+ /**
71
+ * Used to indicate the state of a lazy val that has been evaluated to
72
+ * `null`.
73
+ */
74
+ object NULL
75
+
40
76
final val BITS_PER_LAZY_VAL = 2L
41
77
42
78
def STATE (cur : Long , ord : Int ): Long = {
@@ -54,6 +90,12 @@ object LazyVals {
54
90
unsafe.compareAndSwapLong(t, offset, e, n)
55
91
}
56
92
93
+ def objCAS (t : Object , offset : Long , exp : Object , n : Object ): Boolean = {
94
+ if (debug)
95
+ println(s " objCAS( $t, $exp, $n) " )
96
+ unsafe.compareAndSwapObject(t, offset, exp, n)
97
+ }
98
+
57
99
def setFlag (t : Object , offset : Long , v : Int , ord : Int ): Unit = {
58
100
if (debug)
59
101
println(s " setFlag( $t, $offset, $v, $ord) " )
@@ -106,6 +148,13 @@ object LazyVals {
106
148
r
107
149
}
108
150
151
+ def getStaticOffset (clz : Class [_], name : String ): Long = {
152
+ val r = unsafe.staticFieldOffset(clz.getDeclaredField(name))
153
+ if (debug)
154
+ println(s " getStaticOffset( $clz, $name) = $r" )
155
+ r
156
+ }
157
+
109
158
def getOffsetStatic (field : java.lang.reflect.Field ) =
110
159
val r = unsafe.objectFieldOffset(field)
111
160
if (debug)
@@ -114,11 +163,18 @@ object LazyVals {
114
163
115
164
116
165
object Names {
166
+ final val waiting = " Waiting"
167
+ final val evaluating = " Evaluating"
168
+ final val nullValued = " NULL"
169
+ final val waitingAwaitRelease = " awaitRelease"
170
+ final val waitingRelease = " release"
117
171
final val state = " STATE"
118
172
final val cas = " CAS"
173
+ final val objCas = " objCAS"
119
174
final val setFlag = " setFlag"
120
175
final val wait4Notification = " wait4Notification"
121
176
final val get = " get"
122
177
final val getOffset = " getOffset"
178
+ final val getStaticOffset = " getStaticOffset"
123
179
}
124
180
}
0 commit comments