fun enqueue()

in integration-test/lincheck/src/main/org/jetbrains/lincheck_test/datastructures/FAAQueue.kt [32:59]


    fun enqueue(x: T) {
        while (true) {
            var tail = tail.get()
//            Commenting the code below leads to a bug.
//            val tNext = tail.next.get()
//            if (tNext != null) {
//                this.tail.compareAndSet(tail, tNext)
//                continue
//            }
            val enqueueIndex = tail.enqIdx.getAndIncrement()
            if (enqueueIndex >= SEGMENT_SIZE) {
                val nextTail = Segment(x)
                tail = this.tail.get()
                val nextTailLink = tail.next.get()
                if (nextTailLink == null) {
                    if (this.tail.get().next.compareAndSet(null, nextTail)) {
                        return
                    }
                } else {
                    this.tail.compareAndSet(tail, nextTailLink)
                }
            } else {
                if (tail.elements.compareAndSet(enqueueIndex, null, x)) {
                    return
                }
            }
        }
    }