<commit_before>package com.github.davidmoten.rx2;

import java.util.concurrent.atomic.AtomicReference;

import com.github.davidmoten.guavamini.Preconditions;

public final class MpscQueue<T> {

    private final AtomicReference<Node<T>> head = new AtomicReference<Node<T>>();
    private final AtomicReference<Boolean> tailSet = new AtomicReference<Boolean>(false);

    // mutable
    private Node<T> tail;

    private static final class Node<T> {

        // mutable

        T value;
        Node<T> next;

        Node(T value) {
            this.value = value;
        }
    }

    public void push(T value) {
        // performs one volatile read and two CAS operations per call to this method
        // (under contention can be higher)

        Preconditions.checkNotNull(value, "cannot push a null value");
        Node<T> node = new Node<T>(value);
        while (true) {
            Node<T> h = head.get();
            if (head.compareAndSet(h, node)) {
                h.next = node;
                if (tailSet.compareAndSet(false, true)) {
                    // don't mind that this is not instantly visible to poll() because
                    // in reactive operators the push is always followed by a drain that
                    // forces a poll which will be ordered after this assignment (so this
                    // assignment will be visible)
                    tail = h;
                }
                break;
            }
        }
    }

    public T poll() {
        // performs one volatile write only when reading the end of the queue

        if (tail == null) {
            return null;
        } else {
            Node<T> temp = tail;
            Node<T> next = temp.next;
            if (next == null) {
                tail = null;
                tailSet.set(false);
            } else {
                tail = next;
            }
            T v = temp.value;
            // help gc including prevent nepotism
            temp.value = null;
            return v;
        }
    }

}<commit_msg>test dev<commit_after>package com.github.davidmoten.rx2;

import java.util.concurrent.atomic.AtomicReference;

public final class MpscQueue<T> {

    private final AtomicReference<Node<T>> head = new AtomicReference<Node<T>>();
    private final AtomicReference<Boolean> tailSet = new AtomicReference<Boolean>(false);

    // mutable
    private Node<T> tail;

    private static final class Node<T> {

        // mutable

        T value;
        Node<T> next;

        Node(T value) {
            this.value = value;
        }
    }

    public void push(T value) {
        // performs one volatile read and two CAS operations per call to this method
        // (under contention can be higher)

        Node<T> node = new Node<T>(value);
        while (true) {
            Node<T> h = head.get();
            if (head.compareAndSet(h, node)) {
                h.next = node;
                if (tailSet.weakCompareAndSet(false, true)) {
                    // don't mind that this is not instantly visible to poll() because
                    // in reactive operators the push is always followed by a drain that
                    // forces a poll which will be ordered after this assignment (so this
                    // assignment will be visible)
                    tail = h;
                }
                break;
            }
        }
    }

    public T poll() {
        // performs one lazy set only when reading the end of the queue

        if (tail == null) {
            return null;
        } else {
            Node<T> temp = tail;
            Node<T> next = temp.next;
            if (next == null) {
                tail = null;
                tailSet.lazySet(false);
            } else {
                tail = next;
            }
            T v = temp.value;
            // help gc including prevent nepotism
            temp.value = null;
            temp.next = null;
            return v;
        }
    }

}
{'fraction_non_alphanumeric': 0.08967596081386586, 'fraction_numerical': 0.0005023863350916855, 'mean_word_length': 1.4399509803921569, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3135254', 'n_tokens_mistral': 1081, 'n_tokens_neox': 1018, 'n_words': 346}