Scoop API

scoop:scoop.concurrent:0.1.0

src/coroutine_local_queue.scoop

Selected production source · src/coroutine_local_queue.scoop

1package scoop.concurrent
3private const val LOCAL_CAPACITY: Long = 256L
5private const val POSITION_MASK: Long = 0x7fffffffffffffffL
7// Only the owning worker publishes slots. A steal reservation keeps its slots
8// unavailable for reuse until copying and clearing have both completed.
9internal 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_MASK
16 private fun advance(index: Long, count: Long): Long = (index + count) and POSITION_MASK
18 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 = held
26 }
27 return LOCAL_CAPACITY - used
28 }
30 internal fun tryPut(work: CoroutineSubmission): Boolean {
31 val end = tail.load(MemoryOrder.Relaxed)
32 if (available(end) <= 0L)
33 return false
34 slots[end and (LOCAL_CAPACITY - 1L)] = Some(work)
35 tail.store(advance(end, 1L), MemoryOrder.Release)
36 return true
37 }
39 internal fun take(): CoroutineSubmission? {
40 var current = head.load(MemoryOrder.Relaxed)
41 while (true) {
42 if (current == tail.load(MemoryOrder.Relaxed))
43 return None
44 if (
45 head.compareAndSet(
46 current,
47 advance(current, 1L),
48 MemoryOrder.AcqRel,
49 MemoryOrder.Relaxed
50 )
51 ) {
52 val index = current and (LOCAL_CAPACITY - 1L)
53 val work = slots[index]!!
54 slots[index] = None
55 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 = 0L
65 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 += 1L
71 }
72 case None -> break
73 }
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 while
81 // 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 = 0L
87 while (true) {
88 val current = head.load(MemoryOrder.Acquire)
89 if (!reservation.compareAndSet(-1L, current, MemoryOrder.AcqRel, MemoryOrder.Relaxed))
90 return None
91 val end = tail.load(MemoryOrder.Acquire)
92 var count = (distance(current, end) + 1L) / 2L
93 if (count > room + 1L)
94 count = room + 1L
95 if (count > limit.toLong())
96 count = limit.toLong()
97 if (count == 0L) {
98 reservation.store(-1L, MemoryOrder.Release)
99 return None
100 }
101 if (
102 !head.compareAndSet(
103 current,
104 advance(current, count),
105 MemoryOrder.AcqRel,
106 MemoryOrder.Relaxed
107 )
108 ) {
109 reservation.store(-1L, MemoryOrder.Release)
110 continue
111 }
112 val firstIndex = current and (LOCAL_CAPACITY - 1L)
113 val first = slots[firstIndex]!!
114 slots[firstIndex] = None
115 var copied = 1L
116 var targetCursor = targetEnd
117 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] = None
121 targetCursor = advance(targetCursor, 1L)
122 copied += 1L
123 }
124 reservation.store(-1L, MemoryOrder.Release)
125 if (count > 1L)
126 target.tail.store(targetCursor, MemoryOrder.Release)
127 return Some(first)
128 }
129 }