|
| 1 | +package dotty.runtime |
| 2 | + |
| 3 | +import scala.annotation.tailrec |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper methods used in thread-safe lazy vals. |
| 7 | + */ |
| 8 | +object LazyVals { |
| 9 | + private val unsafe = scala.concurrent.util.Unsafe.instance |
| 10 | + |
| 11 | + final val BITS_PER_LAZY_VAL = 2 |
| 12 | + final val LAZY_VAL_MASK = 3 |
| 13 | + |
| 14 | + @inline def STATE(cur: Long, ord: Long) = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK |
| 15 | + @inline def CAS(t: Object, offset: Long, e: Long, v: Long, ord: Int) = { |
| 16 | + val mask = ~(LAZY_VAL_MASK << ord * BITS_PER_LAZY_VAL) |
| 17 | + val n = (e & mask) | (v << (ord * BITS_PER_LAZY_VAL)) |
| 18 | + compareAndSet(t, offset, e, n) |
| 19 | + } |
| 20 | + @inline def setFlag(t: Object, offset: Long, v: Int, ord: Int) = { |
| 21 | + var retry = true |
| 22 | + while (retry) { |
| 23 | + val cur = get(t, offset) |
| 24 | + if (STATE(cur, ord) == 1) retry = CAS(t, offset, cur, v, ord) |
| 25 | + else { |
| 26 | + // cur == 2, somebody is waiting on monitor |
| 27 | + if (CAS(t, offset, cur, v, ord)) { |
| 28 | + val monitor = getMonitor(t, ord) |
| 29 | + monitor.synchronized { |
| 30 | + monitor.notifyAll() |
| 31 | + } |
| 32 | + retry = false |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + @inline def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int) = { |
| 38 | + var retry = true |
| 39 | + while (retry) { |
| 40 | + val cur = get(t, offset) |
| 41 | + val state = STATE(cur, ord) |
| 42 | + if (state == 1) CAS(t, offset, cur, 2, ord) |
| 43 | + else if (state == 2) { |
| 44 | + val monitor = getMonitor(t, ord) |
| 45 | + monitor.synchronized { |
| 46 | + monitor.wait() |
| 47 | + } |
| 48 | + } |
| 49 | + else retry = false |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @inline def compareAndSet(t: Object, off: Long, e: Long, v: Long) = unsafe.compareAndSwapLong(t, off, e, v) |
| 54 | + @inline def get(t: Object, off: Long) = unsafe.getLongVolatile(t, off) |
| 55 | + |
| 56 | + val processors: Int = java.lang.Runtime.getRuntime.availableProcessors() |
| 57 | + val base: Int = 8 * processors * processors |
| 58 | + val monitors: Array[Object] = (0 to base).map { |
| 59 | + x => new Object() |
| 60 | + }.toArray |
| 61 | + |
| 62 | + @inline def getMonitor(obj: Object, fieldId: Int = 0) = { |
| 63 | + var id = (java.lang.System.identityHashCode(obj) + fieldId) % base |
| 64 | + if (id < 0) id += base |
| 65 | + monitors(id) |
| 66 | + } |
| 67 | + |
| 68 | + @inline def getOffset(obj: Object, name: String) = unsafe.objectFieldOffset(obj.getClass.getDeclaredField(name)) |
| 69 | + |
| 70 | + object Names { |
| 71 | + final val state = "STATE" |
| 72 | + final val cas = "CAS" |
| 73 | + final val setFlag = "setFlag" |
| 74 | + final val wait4Notification = "wait4Notification" |
| 75 | + final val compareAndSet = "compareAndSet" |
| 76 | + final val get = "get" |
| 77 | + final val getOffset = "getOffset" |
| 78 | + } |
| 79 | +} |
0 commit comments