Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014 Ben Manes. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.benmanes.caffeine;
import static java.util.Objects.requireNonNull;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A lock-free unbounded queue based on linked nodes that supports concurrent producers and is
* restricted to a single consumer. This queue orders elements FIFO (first-in-first-out). The
* head of the queue is that element that has been on the queue the longest time. The
* tail of the queue is that element that has been on the queue the shortest time. New
* elements are inserted at the tail of the queue, and the queue retrieval operations obtain
* elements at the head of the queue. Like most other concurrent collection implementations, this
* class does not permit the use of {@code null} elements.
*
* A {@code SingleConsumerQueue} is an appropriate choice when many producer threads will share
* access to a common collection and a single consumer thread drains it. This collection is useful
* in scenarios such as implementing flat combining, actors, or lock amortization.
*
* This implementation employs combination to transfer elements between threads that are producing
* concurrently. This approach avoids contention on the queue by combining colliding operations
* that have identical semantics. When a pair of producers collide, the task of performing the
* combined set of operations is delegated to one of the threads and the other thread optionally
* waits for its operation to be completed. This decision of whether to wait for completion is
* determined by constructing either a linearizable or optimistic queue.
*
* Iterators are weakly consistent, returning elements reflecting the state of the queue at
* some point at or since the creation of the iterator. They do not throw {@link
* java.util.ConcurrentModificationException}, and may proceed concurrently with other operations.
* Elements contained in the queue since the creation of the iterator will be returned exactly once.
*
* Beware that it is the responsibility of the caller to ensure that a consumer has exclusive read
* access to the queue. This implementation does not include fail-fast behavior to guard
* against incorrect consumer usage.
*
* Beware that, unlike in most collections, the {@code size} method is NOT a
* constant-time operation. Because of the asynchronous nature of these queues, determining the
* current number of elements requires a traversal of the elements, and so may report inaccurate
* results if this collection is modified during traversal.
*
* Warning: This class is scheduled for removal in version 3.0.0.
*
* @author [email protected] (Ben Manes)
* @param the type of elements held in this collection
* @deprecated Scheduled for removal in version 3.0.0
*/
@Deprecated
@SuppressWarnings("NullAway")
public final class SingleConsumerQueue extends SCQHeader.HeadAndTailRef
implements Queue, Serializable {
/*
* The queue is represented as a singly-linked list with an atomic head and tail reference. It is
* based on the non-intrusive multi-producer / single-consumer node queue described by
* Dmitriy Vyukov [1].
*
* The backoff strategy of combining operations with identical semantics is based on inverting
* the elimination technique [2]. Elimination allows pairs of operations with reverse semantics,
* like pushes and pops on a stack, to complete without any central coordination and therefore
* substantially aids scalability. The approach of applying elimination and reversing its
* semantics was explored in [3, 4]. Unlike other approaches, this implementation does not use
* opcodes or a background thread.
*
* This implementation borrows optimizations from {@link java.util.concurrent.Exchanger} for
* choosing an arena location and awaiting a match [5].
*
* [1] Non-intrusive MPSC node-based queue
* http://www.1024cores.net/home/lock-free-algorithms/queues/non-intrusive-mpsc-node-based-queue
* [2] A Scalable Lock-free Stack Algorithm
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.156.8728
* [3] Using Elimination to Implement Scalable and Lock-Free FIFO Queues
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.108.6422
* [4] A Dynamic Elimination-Combining Stack Algorithm
* http://www.cs.bgu.ac.il/~hendlerd/papers/DECS.pdf
* [5] A Scalable Elimination-based Exchange Channel
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.59.7396
*/
/** The number of CPUs */
static final int NCPU = Runtime.getRuntime().availableProcessors();
/** The number of slots in the elimination array. */
static final int ARENA_LENGTH = ceilingPowerOfTwo((NCPU + 1) / 2);
/** The mask value for indexing into the arena. */
static final int ARENA_MASK = ARENA_LENGTH - 1;
/** The factory for creating an optimistic node. */
static final Function, ?> OPTIMISIC = Node