From 357d6f9d9d4fb51fdcbf4f027c232a1f781ed5a0 Mon Sep 17 00:00:00 2001 From: PPLong <61926575+PPLong222@users.noreply.github.com> Date: Wed, 23 Apr 2025 18:29:42 -0400 Subject: [PATCH] Simplify full-queue check to use single margin --- .../common/src/internal/LockFreeTaskQueue.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/internal/LockFreeTaskQueue.kt b/kotlinx-coroutines-core/common/src/internal/LockFreeTaskQueue.kt index b056bfd33a..349be30c07 100644 --- a/kotlinx-coroutines-core/common/src/internal/LockFreeTaskQueue.kt +++ b/kotlinx-coroutines-core/common/src/internal/LockFreeTaskQueue.kt @@ -102,9 +102,8 @@ internal class LockFreeTaskQueueCore( if (state and (FROZEN_MASK or CLOSED_MASK) != 0L) return state.addFailReason() // cannot add state.withState { head, tail -> val mask = this.mask // manually move instance field to local for performance - // If queue is Single-Consumer then there could be one element beyond head that we cannot overwrite, - // so we check for full queue with an extra margin of one element - if ((tail + 2) and mask == head and mask) return ADD_FROZEN // overfull, so do freeze & copy + // Reserve one margin element to determine when the queue is full + if ((tail + 1) and mask == head and mask) return ADD_FROZEN // overfull, so do freeze & copy // If queue is Multi-Consumer then the consumer could still have not cleared element // despite the above check for one free slot. if (!singleConsumer && array[tail and mask].value != null) {