1
2
3
4
5
6
7 package gov.bnl.gums.configuration;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public class ReadWriteLock {
25 private Log log = LogFactory.getLog(ReadWriteLock.class);
26 private int[] lock = new int[2];
27 private String name;
28
29 public ReadWriteLock(String name) {
30 this.name = name;
31 }
32
33
34
35
36 public synchronized boolean isReadLocked() {
37 return lock[1] != 0;
38 }
39
40
41
42
43 public synchronized boolean isWriteLocked() {
44 return lock[0] != 0;
45 }
46
47
48
49
50 public synchronized void obtainReadLock() {
51 log.trace("Trying to read on " + name + ": " + lock[0] + ", " + lock[1]);
52
53 while (lock[1] != 0) {
54 try {
55 log.trace("Waiting read on " + name + ": " + lock[0] + ", " + lock[1]);
56 wait();
57 } catch (InterruptedException e) {
58 log.error("Wait was interrupted: " + e.getMessage(), e);
59 }
60 }
61
62
63 lock[0]++;
64 log.trace("Locked read on " + name + ": " + lock[0] + ", " + lock[1]);
65 notifyAll();
66 }
67
68
69
70
71 public synchronized void obtainWriteLock() {
72 log.trace("Trying to write on " + name + ": " + lock[0] + ", " + lock[1]);
73
74
75 while (lock[1] != 0) {
76 try {
77 log.trace("Waiting write on " + name + ": " + lock[0] + ", " + lock[1]);
78 wait();
79 } catch (InterruptedException e) {
80 log.error("Wait was interrupted: " + e.getMessage(), e);
81 }
82 }
83 lock[1] = 1;
84 log.trace("Locked write on " + name + ": " + lock[0] + ", " + lock[1]);
85 notifyAll();
86
87
88 while (lock[0] != 0) {
89 try {
90 wait();
91 } catch (InterruptedException e) {
92 log.error("Wait was interrupted: " + e.getMessage(), e);
93 }
94 }
95 }
96
97
98
99
100 public synchronized void releaseReadLock() {
101 log.trace("Unlocking read on " + name + ": " + lock[0] + ", " + lock[1]);
102 lock[0]--;
103 log.trace("Unocked read on " + name + ": " + lock[0] + ", " + lock[1]);
104 notifyAll();
105 }
106
107
108
109
110 public synchronized void releaseWriteLock() {
111 log.trace("Unlocking write on " + name + ": " + lock[0] + ", " + lock[1]);
112 lock[1]=0;
113 log.trace("Unocked write on " + name + ": " + lock[0] + ", " + lock[1]);
114 notifyAll();
115 }
116
117 }