Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/jvm/src/scheduling/WorkQueue.kt (Kotlin)

Code before:
        val nextIndex = producerIndex.value and MASK
        /*
         * If current element is not null then we're racing with a really slow consumer that committed the consumer index,
         * but hasn't yet nulled out the slot, effectively preventing us from using it.
         * Such situations are very rare in practise (although possible) and we decided to give up a progress guarantee
         * to have a stronger invariant "add to queue with bufferSize == 0 is always successful".
         * This algorithm can still be wait-free for add, but if and only if tasks are not reusable, otherwise
         * nulling out the buffer wouldn't be possible.
         */
        while (buffer[nextIndex] != null) {
            Thread.yield()
        }
        buffer.lazySet(nextIndex, task)
        producerIndex.incrementAndGet()
        return null
    }

    /**
     * Tries stealing from this queue into the [stolenTaskRef] argument.
     *
     * Returns [NOTHING_TO_STEAL] if queue has nothing to steal, [TASK_STOLEN] if at least task was stolen
     * or positive value of how many nanoseconds should pass until the head of this queue will be available to steal.
     */
    fun trySteal(stolenTaskRef: ObjectRef<Task?>): Long {
        val task  = pollBuffer()
        if (task != null) {
            stolenTaskRef.element = task
            return TASK_STOLEN
        }
        return tryStealLastScheduled(stolenTaskRef, blockingOnly = false)
    }

    fun tryStealBlocking(stolenTaskRef: ObjectRef<Task?>): Long {
        var start = consumerIndex.value
        val end = producerIndex.value

        while (start != end) {
            val index = start and MASK
            if (blockingTasksInBuffer.value == 0) break
            val value = buffer[index]
            if (value != null && value.isBlocking && buffer.compareAndSet(index, value, null)) {
                blockingTasksInBuffer.decrementAndGet()
                stolenTaskRef.element = value
                return TASK_STOLEN
            } else {
                ++start
            }
        }
        return tryStealLastScheduled(stolenTaskRef, blockingOnly = true)
    }


Review comment (suggestion):
The names no longer reflect who's the receiver and who's doing the action. The semantics of "I steal (from myself) (into this bag that was given to me)" is not really intuitive!
* `tryGift`? Semantically clear, but "stealing" is a well-established term.
* `tryStealFromThisInto`? Wordy but much less confusing.
* Make it a separate static function, like `trySteal(victim, stolenTaskRef)`.
* ...

Code after:
        if (task.isBlocking) blockingTasksInBuffer.incrementAndGet()
        if (bufferSize == BUFFER_CAPACITY - 1) return task
        val nextIndex = producerIndex.value and MASK
        /*
         * If current element is not null then we're racing with a really slow consumer that committed the consumer index,
         * but hasn't yet nulled out the slot, effectively preventing us from using it.
         * Such situations are very rare in practise (although possible) and we decided to give up a progress guarantee
         * to have a stronger invariant "add to queue with bufferSize == 0 is always successful".
         * This algorithm can still be wait-free for add, but if and only if tasks are not reusable, otherwise
         * nulling out the buffer wouldn't be possible.
         */
        while (buffer[nextIndex] != null) {
            Thread.yield()
        }
        buffer.lazySet(nextIndex, task)
        producerIndex.incrementAndGet()
        return null
    }

    /**
     * Tries stealing from this queue into the [stolenTaskRef] argument.
     *
     * Returns [NOTHING_TO_STEAL] if queue has nothing to steal, [TASK_STOLEN] if at least task was stolen
     * or positive value of how many nanoseconds should pass until the head of this queue will be available to steal.
     */
    fun trySteal(stolenTaskRef: ObjectRef<Task?>): Long {
        val task  = pollBuffer()
        if (task != null) {
            stolenTaskRef.element = task
            return TASK_STOLEN
        }
        return tryStealLastScheduled(stolenTaskRef, blockingOnly = false)
    }

    fun tryStealBlocking(stolenTaskRef: ObjectRef<Task?>): Long {
        var start = consumerIndex.value
        val end = producerIndex.value

        while (start != end && blockingTasksInBuffer.value > 0) {
            stolenTaskRef.element = tryExtractBlockingTask(start++) ?: continue
            return TASK_STOLEN
        }
        return tryStealLastScheduled(stolenTaskRef, blockingOnly = true)
    }

    // Polls for blocking task, invoked only by the owner
    fun pollBlocking(): Task? {
        while (true) { // Poll the slot
            val lastScheduled = lastScheduledTask.value ?: break
            if (!lastScheduled.isBlocking) break
            if (lastScheduledTask.compareAndSet(lastScheduled, null)) {