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