PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 145 lines | 108 code | 22 blank | 15 comment | 32 complexity | 24b69723f322d74a3b65436c045ca89b 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.util.Clock;
 21
 22import java.io.Serializable;
 23
 24public class DistributedLock implements Serializable {
 25    Address lockAddress = null;
 26    int lockThreadId = -1;
 27    int lockCount;
 28    long lockAcquireTime = -1L;
 29
 30    public DistributedLock() {
 31    }
 32
 33    public DistributedLock(DistributedLock copy) {
 34        this(copy.lockAddress, copy.lockThreadId, copy.lockCount, copy.lockAcquireTime);
 35    }
 36
 37    public DistributedLock(Address address, int threadId) {
 38        this(address, threadId, 1);
 39    }
 40
 41    public DistributedLock(Address lockAddress, int lockThreadId, int lockCount) {
 42        this(lockAddress, lockThreadId, lockCount, Clock.currentTimeMillis());
 43    }
 44
 45    private DistributedLock(final Address lockAddress, final int lockThreadId,
 46                            final int lockCount, final long lockAcquireTime) {
 47        this.lockAcquireTime = lockAcquireTime;
 48        this.lockAddress = lockAddress;
 49        this.lockCount = lockCount;
 50        this.lockThreadId = lockThreadId;
 51    }
 52
 53    public boolean isLocked() {
 54        return lockCount > 0;
 55    }
 56
 57    public boolean isLockedBy(Address address, int threadId) {
 58        return (lockThreadId == threadId && address != null && address.equals(lockAddress));
 59    }
 60
 61    public boolean lock(Address address, int threadId) {
 62        if (lockCount == 0) {
 63            lockAddress = address;
 64            lockThreadId = threadId;
 65            lockCount = 1;
 66            lockAcquireTime = Clock.currentTimeMillis();
 67            return true;
 68        } else if (isLockedBy(address, threadId)) {
 69            lockCount = 1;
 70            return true;
 71        }
 72        return false;
 73    }
 74
 75    public boolean unlock(Address address, int threadId) {
 76        if (lockCount == 0) {
 77            return false;
 78        } else {
 79            if (isLockedBy(address, threadId)) {
 80                lockCount--;
 81                if (lockCount == 0) {
 82                    lockAddress = null;
 83                    lockThreadId = -1;
 84                    lockAcquireTime = -1L;
 85                }
 86                return true;
 87            }
 88        }
 89        return false;
 90    }
 91
 92    public boolean testLock(int threadId, Address address) {
 93        return lockCount == 0 || getLockThreadId() == threadId && getLockAddress().equals(address);
 94    }
 95
 96    public void clear() {
 97        lockThreadId = -1;
 98        lockCount = 0;
 99        lockAddress = null;
100    }
101
102    public Address getLockAddress() {
103        return lockAddress;
104    }
105
106    public int getLockThreadId() {
107        return lockThreadId;
108    }
109
110    public int getLockCount() {
111        return lockCount;
112    }
113
114    public long getAcquireTime() {
115        return lockAcquireTime;
116    }
117
118    @Override
119    public boolean equals(Object o) {
120        if (this == o) return true;
121        if (o == null || getClass() != o.getClass()) return false;
122        DistributedLock that = (DistributedLock) o;
123        if (lockCount != that.lockCount) return false;
124        if (lockThreadId != that.lockThreadId) return false;
125        if (lockAddress != null ? !lockAddress.equals(that.lockAddress) : that.lockAddress != null) return false;
126        return true;
127    }
128
129    @Override
130    public int hashCode() {
131        int result = lockAddress != null ? lockAddress.hashCode() : 0;
132        result = 31 * result + lockThreadId;
133        result = 31 * result + lockCount;
134        return result;
135    }
136
137    @Override
138    public String toString() {
139        return "Lock{" +
140                "lockAddress=" + lockAddress +
141                ", lockThreadId=" + lockThreadId +
142                ", lockCount=" + lockCount +
143                '}';
144    }
145}