PageRenderTime 34ms CodeModel.GetById 7ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/base/DistributedSemaphore.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 124 lines | 90 code | 19 blank | 15 comment | 15 complexity | 41e657dbacd292d7a5402eb00d01c19b MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.impl.base;
 18
 19import com.hazelcast.nio.Address;
 20import com.hazelcast.nio.DataSerializable;
 21
 22import java.io.DataInput;
 23import java.io.DataOutput;
 24import java.io.IOException;
 25import java.util.HashMap;
 26import java.util.Map;
 27
 28public class DistributedSemaphore implements DataSerializable {
 29
 30    Map<Address, Integer> attachedPermits = new HashMap<Address, Integer>();
 31    int available;
 32
 33    public DistributedSemaphore() {
 34    }
 35
 36    public DistributedSemaphore(int initialPermits) {
 37        available = initialPermits;
 38    }
 39
 40    public void readData(DataInput in) throws IOException {
 41        Address address;
 42        attachedPermits.clear();
 43        available = in.readInt();
 44        int entries = in.readInt();
 45        while (entries-- > 0) {
 46            (address = new Address()).readData(in);
 47            attachedPermits.put(address, in.readInt());
 48        }
 49    }
 50
 51    public void writeData(DataOutput out) throws IOException {
 52        out.writeInt(available);
 53        out.writeInt(attachedPermits.size());
 54        for (Map.Entry<Address, Integer> entry : attachedPermits.entrySet()) {
 55            entry.getKey().writeData(out);
 56            out.writeInt(entry.getValue());
 57        }
 58    }
 59
 60    public void attachDetach(Integer permitsDelta, Address address) {
 61        if (permitsDelta != 0 && address != null) {
 62            int newValue = permitsDelta + getAttached(address);
 63            if (newValue != 0) {
 64                attachedPermits.put(address, newValue);
 65            } else {
 66                attachedPermits.remove(address);
 67            }
 68        }
 69    }
 70
 71    public int drain() {
 72        int drained = available;
 73        available = 0;
 74        return drained;
 75    }
 76
 77    public int getAttached() {
 78        int total = 0;
 79        for (Integer permits : attachedPermits.values()) {
 80            total += permits;
 81        }
 82        return total;
 83    }
 84
 85    public int getAttached(Address address) {
 86        return attachedPermits.containsKey(address) ? attachedPermits.get(address) : 0;
 87    }
 88
 89    public int getAvailable() {
 90        return available;
 91    }
 92
 93    public void reduce(int permits) {
 94        available -= permits;
 95    }
 96
 97    public void release(int permits, Address address) {
 98        available += permits;
 99        attachDetach(permits, address);
100    }
101
102    public boolean tryAcquire(int permits, Address address) {
103        if (available >= permits) {
104            available -= permits;
105            attachDetach(permits, address);
106            return true;
107        }
108        return false;
109    }
110
111    public boolean onDisconnect(Address deadAddress) {
112        Integer attached = attachedPermits.remove(deadAddress);
113        if (attached != null && attached != 0) {
114            available += attached;
115            return true;
116        }
117        return false;
118    }
119
120    @Override
121    public String toString() {
122        return String.format("Semahpore{available=%d, global attached=%d}", available, getAttached());
123    }
124}