Scoop API

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.
2internal 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 }
24internal class CoroutineListeners(private val lock: CoroutineLock = CoroutineLock()) {
25 private var entries: ArrayList<CoroutineRegistration>? = None
27 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 entry
37 }
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 = 0L
48 while (index < values.size) {
49 if (values[index] === entry) {
50 values.removeAt(index)
51 break
52 }
53 index += 1L
54 }
55 }
56 }
57 return callback
58 }
60 internal fun take(): ArrayList<CoroutineRegistration>? = lock.withLock { takeLocked() }
62 internal fun takeLocked(): ArrayList<CoroutineRegistration>? {
63 val result = entries
64 entries = None
65 return result
66 }
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 }
81internal class CoroutineRegistration(
82 private val owner: CoroutineListeners,
83 action: () -> Unit
84): Disposable {
85 private var action: (() -> Unit)? = Some(action)
87 internal fun takeActionLocked(): (() -> Unit)? {
88 val result = action
89 action = None
90 return result
91 }
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 }
105internal fun notifyCoroutineListeners(entries: ArrayList<CoroutineRegistration>?) {
106 if (coroutineAbsent(entries))
107 return
108 var failure: Throwable? = None
109 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 error
120 case None -> {}
121 }
124internal fun <T> coroutineAbsent(value: T?): Boolean =
125 when (value) {
126 case None -> true
127 case Some(_) -> false
128 }