ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/SequenceLock.java
(Generate patch)

Comparing jsr166/src/jsr166e/SequenceLock.java (file contents):
Revision 1.1 by dl, Fri Jul 15 13:14:47 2011 UTC vs.
Revision 1.2 by dl, Fri Jul 15 15:04:14 2011 UTC

# Line 19 | Line 19 | import java.io.IOException;
19   * A reentrant mutual exclusion {@link Lock} in which each lock
20   * acquisition or release advances a sequence number.  When the
21   * sequence number (accessible using {@link #getSequence()}) is odd,
22 < * the lock is held. When it is even (i.e., {@code lock.getSequence()
22 > * the lock is held. When it is even (i.e., ({@code lock.getSequence()
23   * & 1L) == 0L}), the lock is released. Method {@link
24   * #awaitAvailability} can be used to await availability of the lock,
25   * returning its current sequence number. Sequence numbers are of type
# Line 31 | Line 31 | import java.io.IOException;
31   * possibly platform-specific, value is used.
32   *
33   * <p>Except for the lack of support for specified fairness policies,
34 < * a SequenceLock can be used in the same way as {@link
35 < * ReentrantLock}, and has a nearly identical API. SequenceLocks may
36 < * be preferable in contexts in which multiple threads invoke
37 < * read-only methods much more frequently than fully locked methods.
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   *
40   * <p> Methods {@code awaitAvailability} and {@code getSequence} can
41   * be used together to define (partially) optimistic read-only methods
# Line 121 | Line 122 | public class SequenceLock implements Loc
122                  getExclusiveOwnerThread() == Thread.currentThread();
123          }
124          
125 <        public final boolean tryAcquire(long acquires) {
125 >      public final boolean tryAcquire(long acquires) {
126              Thread current = Thread.currentThread();
127              long c = getState();
128              if ((c & 1L) == 0L) {
# Line 159 | Line 160 | public class SequenceLock implements Loc
160              return true;
161          }
162  
163 <        public final Condition newCondition() { return new ConditionObject(); }
163 >        public final Condition newCondition() {
164 >            throw new UnsupportedOperationException();
165 >        }
166  
167          // Other methods in support of SequenceLock
168  
# Line 198 | Line 201 | public class SequenceLock implements Loc
201          }
202  
203          final Thread getOwner() {
204 <            return (getState() & 1L) != 0L ? null : getExclusiveOwnerThread();
204 >            return (getState() & 1L) == 0L ? null : getExclusiveOwnerThread();
205          }
206  
207          final long getHoldCount() {
# Line 437 | Line 440 | public class SequenceLock implements Loc
440      public void unlock()              { sync.release(1); }
441  
442      /**
443 <     * Returns a {@link Condition} instance for use with this
444 <     * {@link Lock} instance.
442 <     *
443 <     * <p>The returned {@link Condition} instance supports the same
444 <     * usages as do the {@link Object} monitor methods ({@link
445 <     * Object#wait() wait}, {@link Object#notify notify}, and {@link
446 <     * Object#notifyAll notifyAll}) when used with the built-in
447 <     * monitor lock.
448 <     *
449 <     * <ul>
450 <     *
451 <     * <li>If this lock is not held when any of the {@link Condition}
452 <     * {@linkplain Condition#await() waiting} or {@linkplain
453 <     * Condition#signal signalling} methods are called, then an {@link
454 <     * IllegalMonitorStateException} is thrown.
455 <     *
456 <     * <li>When the condition {@linkplain Condition#await() waiting}
457 <     * methods are called the lock is released and, before they
458 <     * return, the lock is reacquired and the lock hold count restored
459 <     * to what it was when the method was called.
460 <     *
461 <     * <li>If a thread is {@linkplain Thread#interrupt interrupted}
462 <     * while waiting then the wait will terminate, an {@link
463 <     * InterruptedException} will be thrown, and the thread's
464 <     * interrupted status will be cleared.
465 <     *
466 <     * <li> Waiting threads are signalled in FIFO order.
467 <     *
468 <     * <li>The ordering of lock reacquisition for threads returning
469 <     * from waiting methods is the same as for threads initially
470 <     * acquiring the lock.
471 <     * </ul>
443 >     * Throws UnsupportedOperationException. SequenceLocks
444 >     * do not support Condition objects.
445       *
446 <     * @return the Condition object
446 >     * @throws UnsupportedOperationException
447       */
448 <    public Condition newCondition()   { return sync.newCondition(); }
448 >    public Condition newCondition()   {
449 >        throw new UnsupportedOperationException();
450 >    }
451  
452      /**
453       * Queries the number of holds on this lock by the current thread.
# Line 580 | Line 555 | public class SequenceLock implements Loc
555      }
556  
557      /**
583     * Queries whether any threads are waiting on the given condition
584     * associated with this lock. Note that because timeouts and
585     * interrupts may occur at any time, a {@code true} return does
586     * not guarantee that a future {@code signal} will awaken any
587     * threads.  This method is designed primarily for use in
588     * monitoring of the system state.
589     *
590     * @param condition the condition
591     * @return {@code true} if there are any waiting threads
592     * @throws IllegalMonitorStateException if this lock is not held
593     * @throws IllegalArgumentException if the given condition is
594     *         not associated with this lock
595     * @throws NullPointerException if the condition is null
596     */
597    public boolean hasWaiters(Condition condition) {
598        if (condition == null)
599            throw new NullPointerException();
600        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
601            throw new IllegalArgumentException("not owner");
602        return sync.hasWaiters((AbstractQueuedLongSynchronizer.ConditionObject)condition);
603    }
604
605    /**
606     * Returns an estimate of the number of threads waiting on the
607     * given condition associated with this lock. Note that because
608     * timeouts and interrupts may occur at any time, the estimate
609     * serves only as an upper bound on the actual number of waiters.
610     * This method is designed for use in monitoring of the system
611     * state, not for synchronization control.
612     *
613     * @param condition the condition
614     * @return the estimated number of waiting threads
615     * @throws IllegalMonitorStateException if this lock is not held
616     * @throws IllegalArgumentException if the given condition is
617     *         not associated with this lock
618     * @throws NullPointerException if the condition is null
619     */
620    public int getWaitQueueLength(Condition condition) {
621        if (condition == null)
622            throw new NullPointerException();
623        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
624            throw new IllegalArgumentException("not owner");
625        return sync.getWaitQueueLength((AbstractQueuedLongSynchronizer.ConditionObject)condition);
626    }
627
628    /**
629     * Returns a collection containing those threads that may be
630     * waiting on the given condition associated with this lock.
631     * Because the actual set of threads may change dynamically while
632     * constructing this result, the returned collection is only a
633     * best-effort estimate. The elements of the returned collection
634     * are in no particular order.  This method is designed to
635     * facilitate construction of subclasses that provide more
636     * extensive condition monitoring facilities.
637     *
638     * @param condition the condition
639     * @return the collection of threads
640     * @throws IllegalMonitorStateException if this lock is not held
641     * @throws IllegalArgumentException if the given condition is
642     *         not associated with this lock
643     * @throws NullPointerException if the condition is null
644     */
645    protected Collection<Thread> getWaitingThreads(Condition condition) {
646        if (condition == null)
647            throw new NullPointerException();
648        if (!(condition instanceof AbstractQueuedLongSynchronizer.ConditionObject))
649            throw new IllegalArgumentException("not owner");
650        return sync.getWaitingThreads((AbstractQueuedLongSynchronizer.ConditionObject)condition);
651    }
652
653    /**
558       * Returns a string identifying this lock, as well as its lock state.
559       * The state, in brackets, includes either the String {@code "Unlocked"}
560       * or the String {@code "Locked by"} followed by the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines