scoop:scoop.core:0.1.0
src/coroutine_listeners.scoop
Selected production source · src/coroutine_listeners.scoop
1
// Short state transitions only; callbacks and continuation resumes run unlocked.2
internal class CoroutineLock {3
private val held: AtomicBoolean = AtomicBoolean(false)5
private fun acquire() {6
while (!held.compareAndSet(false, true, MemoryOrder.Acquire, MemoryOrder.Relaxed)) {}7
}9
private fun unlock() {10
held.store(false, MemoryOrder.Release)11
}13
internal fun <T> withLock(block: () -> T): T {14
acquire()15
try {16
return block()17
}18
finally {19
unlock()20
}21
}22
}24
internal class CoroutineListeners(private val lock: CoroutineLock = CoroutineLock()) {25
private var entries: ArrayList<CoroutineRegistration>? = None27
internal fun add(action: () -> Unit): CoroutineRegistration =28
lock.withLock { addLocked(action) }30
// The owning Job can update its state and listeners in one critical section.31
internal fun addLocked(action: () -> Unit): CoroutineRegistration {32
val entry = CoroutineRegistration(this, action)33
if (coroutineAbsent(entries))34
entries = Some(ArrayList<CoroutineRegistration>())35
entries!!.add(entry)36
return entry37
}39
internal fun select(entry: CoroutineRegistration): (() -> Unit)? =40
lock.withLock { selectLocked(entry) }42
private fun selectLocked(entry: CoroutineRegistration): (() -> Unit)? {43
val callback = entry.takeActionLocked()44
when (entries) {45
case None -> {}46
case Some(val values) -> if (!coroutineAbsent(callback)) {47
var index = 0L48
while (index < values.size) {49
if (values[index] === entry) {50
values.removeAt(index)51
break52
}53
index += 1L54
}55
}56
}57
return callback58
}60
internal fun take(): ArrayList<CoroutineRegistration>? = lock.withLock { takeLocked() }62
internal fun takeLocked(): ArrayList<CoroutineRegistration>? {63
val result = entries64
entries = None65
return result66
}68
internal fun clear() {69
lock.withLock { clearLocked() }70
}72
internal fun clearLocked() {73
when (takeLocked()) {74
case Some(val values) -> for (entry in values)75
entry.takeActionLocked()76
case None -> {}77
}78
}79
}81
internal class CoroutineRegistration(82
private val owner: CoroutineListeners,83
action: () -> Unit84
): Disposable {85
private var action: (() -> Unit)? = Some(action)87
internal fun takeActionLocked(): (() -> Unit)? {88
val result = action89
action = None90
return result91
}93
internal fun fire() {94
when (owner.select(this)) {95
case Some(val invoke) -> invoke()96
case None -> {}97
}98
}100
public override fun dispose() {101
owner.select(this)102
}103
}105
internal fun notifyCoroutineListeners(entries: ArrayList<CoroutineRegistration>?) {106
if (coroutineAbsent(entries))107
return108
var failure: Throwable? = None109
for (entry in entries!!) {110
try {111
entry.fire()112
}113
catch (error: Throwable) {114
if (coroutineAbsent(failure))115
failure = Some(error)116
}117
}118
when (failure) {119
case Some(val error) -> throw error120
case None -> {}121
}122
}124
internal fun <T> coroutineAbsent(value: T?): Boolean =125
when (value) {126
case None -> true127
case Some(_) -> false128
}