Skip to content

Commit 3e59c57

Browse files
committed
~extract method
1 parent 59c84ec commit 3e59c57

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

kotlinx-coroutines-core/jvm/src/scheduling/WorkQueue.kt

+14-7
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,25 @@ internal class WorkQueue {
150150
var end = producerIndex.value
151151

152152
while (start != end) {
153-
--end
154-
val index = end and MASK
155-
if (blockingTasksInBuffer.value == 0) break
156-
val value = buffer[index]
157-
if (value != null && value.isBlocking && buffer.compareAndSet(index, value, null)) {
158-
blockingTasksInBuffer.decrementAndGet()
159-
return value
153+
val task = tryExtractBlockingTask(--end)
154+
if (task != null) {
155+
return task
160156
}
161157
}
162158
return null
163159
}
164160

161+
private fun tryExtractBlockingTask(index: Int): Task? {
162+
if (blockingTasksInBuffer.value == 0) return null
163+
val arrayIndex = index and MASK
164+
val value = buffer[arrayIndex]
165+
if (value != null && value.isBlocking && buffer.compareAndSet(arrayIndex, value, null)) {
166+
blockingTasksInBuffer.decrementAndGet()
167+
return value
168+
}
169+
return null
170+
}
171+
165172
fun offloadAllWorkTo(globalQueue: GlobalQueue) {
166173
lastScheduledTask.getAndSet(null)?.let { globalQueue.addLast(it) }
167174
while (pollTo(globalQueue)) {

0 commit comments

Comments
 (0)