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