1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.lock; |
8 |
| |
9 |
| import java.util.Collections; |
10 |
| import java.util.Set; |
11 |
| import java.util.concurrent.CopyOnWriteArraySet; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class LockMap |
20 |
| { |
21 |
| public static final int OWNER_ANY = 0; |
22 |
| public static final int OWNER_READ = 1; |
23 |
| public static final int OWNER_WRITE = 2; |
24 |
| |
25 |
| private Object writeOwner_ = null; |
26 |
| |
27 |
| |
28 |
| private final Set readOwnerList_ = new CopyOnWriteArraySet(); |
29 |
| |
30 |
180145
| public LockMap()
|
31 |
| { |
32 |
| } |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
16336973
| public boolean isOwner(Object caller, int ownership)
|
45 |
| { |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
16336973
| switch (ownership)
|
51 |
| { |
52 |
7971429
| case OWNER_ANY:
|
53 |
7971429
| return (writeOwner_ != null && caller.equals(writeOwner_) || readOwnerList_.contains(caller));
|
54 |
7350920
| case OWNER_READ:
|
55 |
7350970
| return (readOwnerList_.contains(caller));
|
56 |
1014574
| case OWNER_WRITE:
|
57 |
1014574
| return (writeOwner_ != null && caller.equals(writeOwner_));
|
58 |
0
| default:
|
59 |
0
| return false;
|
60 |
| } |
61 |
| } |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
6436240
| public void addReader(Object owner)
|
70 |
| { |
71 |
6436234
| readOwnerList_.add(owner);
|
72 |
| } |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
442250
| public void setWriterIfNotNull(Object owner)
|
80 |
| { |
81 |
442250
| synchronized (this)
|
82 |
| { |
83 |
442250
| if (writeOwner_ != null)
|
84 |
0
| throw new IllegalStateException("there is already a writer holding the lock: " + writeOwner_);
|
85 |
442250
| writeOwner_ = owner;
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
14006
| private Object setWriter(Object owner)
|
90 |
| { |
91 |
14006
| Object old;
|
92 |
14006
| synchronized (this)
|
93 |
| { |
94 |
14006
| old = writeOwner_;
|
95 |
14006
| writeOwner_ = owner;
|
96 |
| } |
97 |
14006
| return old;
|
98 |
| } |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
14006
| public boolean upgrade(Object owner) throws OwnerNotExistedException
|
108 |
| { |
109 |
14006
| boolean old_value = readOwnerList_.remove(owner);
|
110 |
14006
| if (!old_value)
|
111 |
0
| throw new OwnerNotExistedException("Can't upgrade lock. Read lock owner did not exist");
|
112 |
14006
| setWriter(owner);
|
113 |
14006
| return true;
|
114 |
| } |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
26278
| public Set readerOwners()
|
120 |
| { |
121 |
26278
| return Collections.unmodifiableSet(readOwnerList_);
|
122 |
| } |
123 |
| |
124 |
291
| public void releaseReaderOwners(LockStrategy lock)
|
125 |
| { |
126 |
291
| int size = readOwnerList_.size();
|
127 |
291
| for (int i = 0; i < size; i++)
|
128 |
5
| lock.readLock().unlock();
|
129 |
| } |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
251794
| public Object writerOwner()
|
135 |
| { |
136 |
251794
| return writeOwner_;
|
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
6422113
| public void removeReader(Object owner)
|
143 |
| { |
144 |
6422113
| readOwnerList_.remove(owner);
|
145 |
| } |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
456110
| public void removeWriter()
|
151 |
| { |
152 |
456110
| synchronized (this)
|
153 |
| { |
154 |
456110
| writeOwner_ = null;
|
155 |
| } |
156 |
| } |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
295
| public void removeAll()
|
162 |
| { |
163 |
295
| removeWriter();
|
164 |
295
| readOwnerList_.clear();
|
165 |
| } |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
78
| public String printInfo()
|
173 |
| { |
174 |
78
| StringBuffer buf = new StringBuffer(64);
|
175 |
78
| buf.append("Read lock owners: ").append(readOwnerList_).append('\n');
|
176 |
78
| buf.append("Write lock owner: ").append(writeOwner_).append('\n');
|
177 |
78
| return buf.toString();
|
178 |
| } |
179 |
| |
180 |
225564
| public boolean isReadLocked()
|
181 |
| { |
182 |
225564
| return !readOwnerList_.isEmpty();
|
183 |
| } |
184 |
| } |