
com.hazelcast.concurrent.semaphore.SemaphoreProxy Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. 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.hazelcast.concurrent.semaphore;
import com.hazelcast.concurrent.semaphore.operations.AcquireOperation;
import com.hazelcast.concurrent.semaphore.operations.AvailableOperation;
import com.hazelcast.concurrent.semaphore.operations.DrainOperation;
import com.hazelcast.concurrent.semaphore.operations.InitOperation;
import com.hazelcast.concurrent.semaphore.operations.ReduceOperation;
import com.hazelcast.concurrent.semaphore.operations.ReleaseOperation;
import com.hazelcast.core.ISemaphore;
import com.hazelcast.spi.AbstractDistributedObject;
import com.hazelcast.spi.InternalCompletableFuture;
import com.hazelcast.spi.NodeEngine;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.OperationService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import static com.hazelcast.util.ExceptionUtil.rethrowAllowInterrupted;
import static com.hazelcast.util.Preconditions.checkNotNegative;
public class SemaphoreProxy extends AbstractDistributedObject implements ISemaphore {
private final String name;
private final int partitionId;
public SemaphoreProxy(String name, SemaphoreService service, NodeEngine nodeEngine) {
super(nodeEngine, service);
this.name = name;
this.partitionId = nodeEngine.getPartitionService().getPartitionId(getNameAsPartitionAwareData());
}
@Override
public String getName() {
return name;
}
@Override
public boolean init(int permits) {
checkNotNegative(permits, "permits can't be negative");
InitOperation operation = new InitOperation(name, permits);
InternalCompletableFuture future = invoke(operation);
return future.getSafely();
}
@Override
public void acquire() throws InterruptedException {
acquire(1);
}
@Override
public void acquire(int permits) throws InterruptedException {
checkNotNegative(permits, "permits can't be negative");
try {
AcquireOperation operation = new AcquireOperation(name, permits, -1);
InternalCompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy