/** * Copyright 2016 Nikita Koksharov * * 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 org.redisson.api; import java.util.concurrent.TimeUnit; /** * Distributed and concurrent implementation of {@link java.util.concurrent.Semaphore}. * * <p>Works in non-fair mode. Therefore order of acquiring is unpredictable. * * @author Nikita Koksharov * */ public interface RSemaphore extends RExpirable, RSemaphoreAsync { /** * Acquires a permit from this semaphore, blocking until one is * available, or the thread is {@linkplain Thread#interrupt interrupted}. * * <p>Acquires a permit, if one is available and returns immediately, * reducing the number of available permits by one. * * <p>If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of two things happens: * <ul> * <li>Some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit; or * <li>Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. * </ul> * * @throws InterruptedException if the current thread is interrupted */ void acquire() throws InterruptedException; /** * Acquires the given number of permits from this semaphore, * blocking until all are available, * or the thread is {@linkplain Thread#interrupt interrupted}. * * <p>Acquires the given number of permits, if they are available, * and returns immediately, reducing the number of available permits * by the given amount. * * <p>If insufficient permits are available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of two things happens: * <ul> * <li>Some other thread invokes one of the {@link #release() release} * methods for this semaphore, the current thread is next to be assigned * permits and the number of available permits satisfies this request; or * <li>Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. * </ul> * * @param permits the number of permits to acquire * @throws InterruptedException if the current thread is interrupted * @throws IllegalArgumentException if {@code permits} is negative */ void acquire(int permits) throws InterruptedException; /** * Acquires a permit only if one is available at the * time of invocation. * * <p>Acquires a permit, if one is available and returns immediately, * with the value {@code true}, * reducing the number of available permits by one. * * <p>If no permit is available then this method will return * immediately with the value {@code false}. * * @return {@code true} if a permit was acquired and {@code false} * otherwise */ boolean tryAcquire(); /** * Acquires the given number of permits only if all are available at the * time of invocation. * * <p>Acquires a permits, if all are available and returns immediately, * with the value {@code true}, * reducing the number of available permits by given number of permitss. * * <p>If no permits are available then this method will return * immediately with the value {@code false}. * * @param permits the number of permits to acquire * @return {@code true} if a permit was acquired and {@code false} * otherwise */ boolean tryAcquire(int permits); /** * Acquires a permit from this semaphore, if one becomes available * within the given waiting time and the current thread has not * been {@linkplain Thread#interrupt interrupted}. * * <p>Acquires a permit, if one is available and returns immediately, * with the value {@code true}, * reducing the number of available permits by one. * * <p>If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of three things happens: * <ul> * <li>Some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit; or * <li>Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or * <li>The specified waiting time elapses. * </ul> * * <p>If a permit is acquired then the value {@code true} is returned. * * <p>If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param waitTime the maximum time to wait for a permit * @param unit the time unit of the {@code timeout} argument * @return {@code true} if a permit was acquired and {@code false} * if the waiting time elapsed before a permit was acquired * @throws InterruptedException if the current thread is interrupted */ boolean tryAcquire(long waitTime, TimeUnit unit) throws InterruptedException; /** * Acquires the given number of permits only if all are available * within the given waiting time and the current thread has not * been {@linkplain Thread#interrupt interrupted}. * * <p>Acquires a permits, if all are available and returns immediately, * with the value {@code true}, * reducing the number of available permits by one. * * <p>If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of three things happens: * <ul> * <li>Some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit; or * <li>Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or * <li>The specified waiting time elapses. * </ul> * * <p>If a permits is acquired then the value {@code true} is returned. * * <p>If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param permits amount * @param waitTime the maximum time to wait for a permit * @param unit the time unit of the {@code timeout} argument * @return {@code true} if a permit was acquired and {@code false} * if the waiting time elapsed before a permit was acquired * @throws InterruptedException if the current thread is interrupted */ boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException; /** * Releases a permit, returning it to the semaphore. * * <p>Releases a permit, increasing the number of available permits by * one. If any threads of Redisson client are trying to acquire a permit, * then one is selected and given the permit that was just released. * * <p>There is no requirement that a thread that releases a permit must * have acquired that permit by calling {@link #acquire}. * Correct usage of a semaphore is established by programming convention * in the application. */ void release(); /** * Releases the given number of permits, returning them to the semaphore. * * <p>Releases the given number of permits, increasing the number of available permits by * the given number of permits. If any threads of Redisson client are trying to * acquire a permits, then next threads is selected and tries to acquire the permits that was just released. * * <p>There is no requirement that a thread that releases a permits must * have acquired that permit by calling {@link #acquire}. * Correct usage of a semaphore is established by programming convention * in the application. * * @param permits amount */ void release(int permits); /** * Returns the current number of available permits. * * @return number of available permits */ int availablePermits(); /** * Acquires and returns all permits that are immediately available. * * @return the number of permits acquired */ int drainPermits(); /** * Use {@link #trySetPermits(int)} * * @param permits amount */ @Deprecated void setPermits(int permits); /** * Sets number of permits. * * @param permits - number of permits * @return <code>true</code> if permits has been set successfully, otherwise <code>false</code>. */ boolean trySetPermits(int permits); /** * Shrinks the number of available permits by the indicated * reduction. * * @param permits - reduction the number of permits to remove * @throws IllegalArgumentException if {@code reduction} is negative */ void reducePermits(int permits); }