001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.modeshape.common.collection.ring;
018
019import java.util.concurrent.TimeoutException;
020
021/**
022 * A barrier that encapsulates a {@link Pointer} and that will {@link #waitFor(long) wait} to advance it until it is safe to do so
023 * and there are entries available to read. This is used within the {@link RingBuffer#addConsumer} method to provide each consumer
024 * with a valid {@link Pointer} that stays as close as possible behind the ring buffer's {@link Cursor}.
025 * 
026 * @see Cursor#newBarrier()
027 * @author Randall Hauch (rhauch@redhat.com)
028 */
029public interface PointerBarrier extends AutoCloseable {
030
031    /**
032     * Wait for the given position to be available for consumption.
033     * 
034     * @param position the sequence to wait for
035     * @return the position up to which is available, which may be larger than the requested position or a negative number if
036     *         there will never be any more positions
037     * @throws InterruptedException if the thread needs awaking on a condition variable.
038     * @throws TimeoutException if this blocking method times out
039     */
040    long waitFor( long position ) throws InterruptedException, TimeoutException;
041
042    /**
043     * Return whether this barrier has completed and should no longer be used.
044     * 
045     * @return true if this barrier is complete, or false otherwise
046     */
047    boolean isComplete();
048
049    /**
050     * Signal that this barrier is closed and should return -1 from {@link #waitFor(long)}.
051     */
052    @Override
053    public void close();
054}