31 |
|
* possibly platform-specific, value is used. |
32 |
|
* |
33 |
|
* <p>Except for the lack of support for specified fairness policies, |
34 |
< |
* or {link Condition} objects, a SequenceLock can be used in the same |
34 |
> |
* or {@link Condition} objects, a SequenceLock can be used in the same |
35 |
|
* way as {@link ReentrantLock}, and has a nearly identical |
36 |
|
* API. SequenceLocks may be preferable in contexts in which multiple |
37 |
|
* threads invoke read-only methods much more frequently than fully |
38 |
|
* locked methods. |
39 |
< |
* |
39 |
> |
* |
40 |
|
* <p> Methods {@code awaitAvailability} and {@code getSequence} can |
41 |
|
* be used together to define (partially) optimistic read-only methods |
42 |
|
* that are usually more efficient than ReadWriteLocks when they |
121 |
|
return (getState() & 1L) != 0L && |
122 |
|
getExclusiveOwnerThread() == Thread.currentThread(); |
123 |
|
} |
124 |
< |
|
124 |
> |
|
125 |
|
public final boolean tryAcquire(long acquires) { |
126 |
|
Thread current = Thread.currentThread(); |
127 |
|
long c = getState(); |
138 |
|
} |
139 |
|
return false; |
140 |
|
} |
141 |
< |
|
141 |
> |
|
142 |
|
public final boolean tryRelease(long releases) { |
143 |
|
if (Thread.currentThread() != getExclusiveOwnerThread()) |
144 |
|
throw new IllegalMonitorStateException(); |
151 |
|
} |
152 |
|
|
153 |
|
public final long tryAcquireShared(long unused) { |
154 |
< |
return ((getState() & 1L) == 0L || |
155 |
< |
getExclusiveOwnerThread() == Thread.currentThread())? |
154 |
> |
return ((getState() & 1L) == 0L || |
155 |
> |
getExclusiveOwnerThread() == Thread.currentThread())? |
156 |
|
1L : -1L; // must return long |
157 |
|
} |
158 |
|
|
160 |
|
return true; |
161 |
|
} |
162 |
|
|
163 |
< |
public final Condition newCondition() { |
163 |
> |
public final Condition newCondition() { |
164 |
|
throw new UnsupportedOperationException(); |
165 |
|
} |
166 |
|
|
169 |
|
final long getSequence() { |
170 |
|
return getState(); |
171 |
|
} |
172 |
< |
|
172 |
> |
|
173 |
|
final void lock() { |
174 |
|
int k = spins; |
175 |
|
while (!tryAcquire(1)) { |
180 |
|
--k; |
181 |
|
} |
182 |
|
} |
183 |
< |
|
183 |
> |
|
184 |
|
final long awaitAvailability() { |
185 |
|
long s; |
186 |
|
int k = spins; |
218 |
|
|
219 |
|
private final Sync sync; |
220 |
|
|
221 |
< |
/** |
221 |
> |
/** |
222 |
|
* The default spin value for constructor. Future versions of this |
223 |
|
* class might choose platform-specific values. Currently, except |
224 |
|
* on uniprocessors, it is set to a small value that ovecomes near |
225 |
|
* misses between releases and acquires. |
226 |
|
*/ |
227 |
< |
static final int DEFAULT_SPINS = |
227 |
> |
static final int DEFAULT_SPINS = |
228 |
|
Runtime.getRuntime().availableProcessors() > 1 ? 64 : 0; |
229 |
|
|
230 |
|
/** |
236 |
|
|
237 |
|
/** |
238 |
|
* Creates an instance of {@code SequenceLock} that |
239 |
< |
* will retry attempts to lock or await release |
239 |
> |
* will retry attempts to lock or await release |
240 |
|
* at least the given number times before blocking. |
241 |
|
*/ |
242 |
|
public SequenceLock(int spins) { sync = new Sync(spins); } |
261 |
|
* @return the current sequence number |
262 |
|
*/ |
263 |
|
public long awaitAvailability() { return sync.awaitAvailability(); } |
264 |
< |
|
264 |
> |
|
265 |
|
/** |
266 |
|
* Acquires the lock. |
267 |
|
* |
334 |
|
* |
335 |
|
* <p>Acquires the lock if it is not held by another thread and |
336 |
|
* returns immediately with the value {@code true}, setting the |
337 |
< |
* lock hold count to one. |
337 |
> |
* lock hold count to one. |
338 |
|
* |
339 |
|
* <p> If the current thread already holds this lock then the hold |
340 |
|
* count is incremented by one and the method returns {@code true}. |
445 |
|
* |
446 |
|
* @throws UnsupportedOperationException |
447 |
|
*/ |
448 |
< |
public Condition newCondition() { |
448 |
> |
public Condition newCondition() { |
449 |
|
throw new UnsupportedOperationException(); |
450 |
|
} |
451 |
|
|
456 |
|
* matched by an unlock action. |
457 |
|
* |
458 |
|
* <p>The hold count information is typically only used for testing and |
459 |
< |
* debugging purposes. |
459 |
> |
* debugging purposes. |
460 |
|
* |
461 |
|
* @return the number of holds on this lock by the current thread, |
462 |
|
* or zero if this lock is not held by the current thread |
570 |
|
} |
571 |
|
|
572 |
|
} |
573 |
< |
|
573 |
> |
|