-
Notifications
You must be signed in to change notification settings - Fork 866
/
OModificationLock.java
executable file
·123 lines (106 loc) · 4.21 KB
/
OModificationLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * 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.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.common.concur.lock;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* This lock is intended to be used inside of storage to request lock on any data modifications. Writes can be prohibited from one
* thread, but then allowed from other thread.
*
*
* @author Andrey Lomakin (a.lomakin-at-orientechnologies.com)
* @since 15.06.12
*/
public class OModificationLock {
private final AtomicInteger vetos = new AtomicInteger();
private volatile boolean throwException = false;
private final ConcurrentLinkedQueue<Thread> waiters = new ConcurrentLinkedQueue<Thread>();
private final OReadersWriterSpinLock lock = new OReadersWriterSpinLock();
/**
* Tells the lock that thread is going to perform data modifications in storage. This method allows to perform several data
* modifications in parallel.
*/
public void requestModificationLock() {
lock.acquireReadLock();
if (vetos.get() == 0)
return;
if (throwException) {
lock.releaseReadLock();
throw new OModificationOperationProhibitedException("Modification requests are prohibited");
}
boolean wasInterrupted = false;
Thread thread = Thread.currentThread();
waiters.add(thread);
while (vetos.get() > 0) {
LockSupport.park(this);
if (Thread.interrupted())
wasInterrupted = true;
}
waiters.remove(thread);
if (wasInterrupted)
thread.interrupt();
}
/**
* Tells the lock that thread is finished to perform to perform modifications in storage.
*/
public void releaseModificationLock() {
lock.releaseReadLock();
}
/**
* After this method finished it's execution, all threads that are going to perform data modifications in storage should wait till
* {@link #allowModifications()} method will be called. This method will wait till all ongoing modifications will be finished.
*/
public void prohibitModifications() {
prohibitModifications(false);
}
/**
* After this method finished it's execution, all threads that are going to perform data modifications in storage should wait till
* {@link #allowModifications()} method will be called. This method will wait till all ongoing modifications will be finished.
*
* @param throwException
* If <code>true</code> {@link OModificationOperationProhibitedException} exception will be thrown on
* {@link #requestModificationLock()} call.
*/
public void prohibitModifications(boolean throwException) {
lock.acquireWriteLock();
try {
if (vetos.get() == 0)
this.throwException = throwException;
vetos.incrementAndGet();
} finally {
lock.releaseWriteLock();
}
}
/**
* After this method finished execution all threads that are waiting to perform data modifications in storage will be awaken and
* will be allowed to continue their execution.
*/
public void allowModifications() {
final int currentVetos = vetos.decrementAndGet();
if (currentVetos < 0)
throw new IllegalStateException("Bad state of modification lock. "
+ "Modifications were prohibited less times than they will be allowed.");
for (Thread thread : waiters)
LockSupport.unpark(thread);
}
}