scoop:scoop.concurrent:0.1.0
src/coroutine_local_queue.scoop
Selected production source · src/coroutine_local_queue.scoop
1
package scoop.concurrent3
private const val LOCAL_CAPACITY: Long = 256L5
private const val POSITION_MASK: Long = 0x7fffffffffffffffL7
// Only the owning worker publishes slots. A steal reservation keeps its slots8
// unavailable for reuse until copying and clearing have both completed.9
internal class CoroutineLocalQueue {10
private val slots = MutableArray<CoroutineSubmission?>(LOCAL_CAPACITY) { _ -> None }11
private val head = AtomicLong(0L)12
private val tail = AtomicLong(0L)13
private val reservation = AtomicLong(-1L)15
private fun distance(start: Long, end: Long): Long = (end - start) and POSITION_MASK16
private fun advance(index: Long, count: Long): Long = (index + count) and POSITION_MASK18
private fun available(end: Long): Long {19
val current = head.load(MemoryOrder.Acquire)20
val reserved = reservation.load(MemoryOrder.Acquire)21
var used = distance(current, end)22
if (reserved != -1L) {23
val held = distance(reserved, end)24
if (held > used)25
used = held26
}27
return LOCAL_CAPACITY - used28
}30
internal fun tryPut(work: CoroutineSubmission): Boolean {31
val end = tail.load(MemoryOrder.Relaxed)32
if (available(end) <= 0L)33
return false34
slots[end and (LOCAL_CAPACITY - 1L)] = Some(work)35
tail.store(advance(end, 1L), MemoryOrder.Release)36
return true37
}39
internal fun take(): CoroutineSubmission? {40
var current = head.load(MemoryOrder.Relaxed)41
while (true) {42
if (current == tail.load(MemoryOrder.Relaxed))43
return None44
if (45
head.compareAndSet(46
current,47
advance(current, 1L),48
MemoryOrder.AcqRel,49
MemoryOrder.Relaxed50
)51
) {52
val index = current and (LOCAL_CAPACITY - 1L)53
val work = slots[index]!!54
slots[index] = None55
return Some(work)56
}57
current = head.load(MemoryOrder.Relaxed)58
}59
}61
internal fun refill(inbox: CoroutineQueue): Int {62
var end = tail.load(MemoryOrder.Relaxed)63
val room = available(end)64
var count = 0L65
while (count < room) {66
when (inbox.take()) {67
case Some(val work) -> {68
slots[end and (LOCAL_CAPACITY - 1L)] = Some(work)69
end = advance(end, 1L)70
count += 1L71
}72
case None -> break73
}74
}75
if (count > 0L)76
tail.store(end, MemoryOrder.Release)77
return count.toInt()78
}80
// The caller owns target. Its available-space snapshot remains valid while81
// reserving this source, because nobody else can publish into target.82
internal fun stealInto(target: CoroutineLocalQueue, limit: Int): CoroutineSubmission? {83
val targetEnd = target.tail.load(MemoryOrder.Relaxed)84
var room = target.available(targetEnd)85
if (room < 0L)86
room = 0L87
while (true) {88
val current = head.load(MemoryOrder.Acquire)89
if (!reservation.compareAndSet(-1L, current, MemoryOrder.AcqRel, MemoryOrder.Relaxed))90
return None91
val end = tail.load(MemoryOrder.Acquire)92
var count = (distance(current, end) + 1L) / 2L93
if (count > room + 1L)94
count = room + 1L95
if (count > limit.toLong())96
count = limit.toLong()97
if (count == 0L) {98
reservation.store(-1L, MemoryOrder.Release)99
return None100
}101
if (102
!head.compareAndSet(103
current,104
advance(current, count),105
MemoryOrder.AcqRel,106
MemoryOrder.Relaxed107
)108
) {109
reservation.store(-1L, MemoryOrder.Release)110
continue111
}112
val firstIndex = current and (LOCAL_CAPACITY - 1L)113
val first = slots[firstIndex]!!114
slots[firstIndex] = None115
var copied = 1L116
var targetCursor = targetEnd117
while (copied < count) {118
val index = advance(current, copied) and (LOCAL_CAPACITY - 1L)119
target.slots[targetCursor and (LOCAL_CAPACITY - 1L)] = slots[index]120
slots[index] = None121
targetCursor = advance(targetCursor, 1L)122
copied += 1L123
}124
reservation.store(-1L, MemoryOrder.Release)125
if (count > 1L)126
target.tail.store(targetCursor, MemoryOrder.Release)127
return Some(first)128
}129
}130
}