jdk/src/share/classes/java/util/concurrent/locks/StampedLock.java
changeset 18576 7a5c231327af
parent 15648 6a38cf764825
child 19589 459db4bb1df0
equal deleted inserted replaced
18575:2ab0d0b3ecad 18576:7a5c231327af
   364      * Exclusively acquires the lock if it is available within the
   364      * Exclusively acquires the lock if it is available within the
   365      * given time and the current thread has not been interrupted.
   365      * given time and the current thread has not been interrupted.
   366      * Behavior under timeout and interruption matches that specified
   366      * Behavior under timeout and interruption matches that specified
   367      * for method {@link Lock#tryLock(long,TimeUnit)}.
   367      * for method {@link Lock#tryLock(long,TimeUnit)}.
   368      *
   368      *
       
   369      * @param time the maximum time to wait for the lock
       
   370      * @param unit the time unit of the {@code time} argument
   369      * @return a stamp that can be used to unlock or convert mode,
   371      * @return a stamp that can be used to unlock or convert mode,
   370      * or zero if the lock is not available
   372      * or zero if the lock is not available
   371      * @throws InterruptedException if the current thread is interrupted
   373      * @throws InterruptedException if the current thread is interrupted
   372      * before acquiring the lock
   374      * before acquiring the lock
   373      */
   375      */
   443      * Non-exclusively acquires the lock if it is available within the
   445      * Non-exclusively acquires the lock if it is available within the
   444      * given time and the current thread has not been interrupted.
   446      * given time and the current thread has not been interrupted.
   445      * Behavior under timeout and interruption matches that specified
   447      * Behavior under timeout and interruption matches that specified
   446      * for method {@link Lock#tryLock(long,TimeUnit)}.
   448      * for method {@link Lock#tryLock(long,TimeUnit)}.
   447      *
   449      *
       
   450      * @param time the maximum time to wait for the lock
       
   451      * @param unit the time unit of the {@code time} argument
   448      * @return a stamp that can be used to unlock or convert mode,
   452      * @return a stamp that can be used to unlock or convert mode,
   449      * or zero if the lock is not available
   453      * or zero if the lock is not available
   450      * @throws InterruptedException if the current thread is interrupted
   454      * @throws InterruptedException if the current thread is interrupted
   451      * before acquiring the lock
   455      * before acquiring the lock
   452      */
   456      */
   508      * stamp is zero. Always returns true if the stamp represents a
   512      * stamp is zero. Always returns true if the stamp represents a
   509      * currently held lock. Invoking this method with a value not
   513      * currently held lock. Invoking this method with a value not
   510      * obtained from {@link #tryOptimisticRead} or a locking method
   514      * obtained from {@link #tryOptimisticRead} or a locking method
   511      * for this lock has no defined effect or result.
   515      * for this lock has no defined effect or result.
   512      *
   516      *
   513      * @return true if the lock has not been exclusively acquired
   517      * @param stamp a stamp
       
   518      * @return {@code true} if the lock has not been exclusively acquired
   514      * since issuance of the given stamp; else false
   519      * since issuance of the given stamp; else false
   515      */
   520      */
   516     public boolean validate(long stamp) {
   521     public boolean validate(long stamp) {
   517         U.loadFence();
   522         U.loadFence();
   518         return (stamp & SBITS) == (state & SBITS);
   523         return (stamp & SBITS) == (state & SBITS);
   721     /**
   726     /**
   722      * Releases the write lock if it is held, without requiring a
   727      * Releases the write lock if it is held, without requiring a
   723      * stamp value. This method may be useful for recovery after
   728      * stamp value. This method may be useful for recovery after
   724      * errors.
   729      * errors.
   725      *
   730      *
   726      * @return true if the lock was held, else false
   731      * @return {@code true} if the lock was held, else false
   727      */
   732      */
   728     public boolean tryUnlockWrite() {
   733     public boolean tryUnlockWrite() {
   729         long s; WNode h;
   734         long s; WNode h;
   730         if (((s = state) & WBIT) != 0L) {
   735         if (((s = state) & WBIT) != 0L) {
   731             state = (s += WBIT) == 0L ? ORIGIN : s;
   736             state = (s += WBIT) == 0L ? ORIGIN : s;
   739     /**
   744     /**
   740      * Releases one hold of the read lock if it is held, without
   745      * Releases one hold of the read lock if it is held, without
   741      * requiring a stamp value. This method may be useful for recovery
   746      * requiring a stamp value. This method may be useful for recovery
   742      * after errors.
   747      * after errors.
   743      *
   748      *
   744      * @return true if the read lock was held, else false
   749      * @return {@code true} if the read lock was held, else false
   745      */
   750      */
   746     public boolean tryUnlockRead() {
   751     public boolean tryUnlockRead() {
   747         long s, m; WNode h;
   752         long s, m; WNode h;
   748         while ((m = (s = state) & ABITS) != 0L && m < WBIT) {
   753         while ((m = (s = state) & ABITS) != 0L && m < WBIT) {
   749             if (m < RFULL) {
   754             if (m < RFULL) {
   771             readers = RFULL + readerOverflow;
   776             readers = RFULL + readerOverflow;
   772         return (int) readers;
   777         return (int) readers;
   773     }
   778     }
   774 
   779 
   775     /**
   780     /**
   776      * Returns true if the lock is currently held exclusively.
   781      * Returns {@code true} if the lock is currently held exclusively.
   777      *
   782      *
   778      * @return true if the lock is currently held exclusively
   783      * @return {@code true} if the lock is currently held exclusively
   779      */
   784      */
   780     public boolean isWriteLocked() {
   785     public boolean isWriteLocked() {
   781         return (state & WBIT) != 0L;
   786         return (state & WBIT) != 0L;
   782     }
   787     }
   783 
   788 
   784     /**
   789     /**
   785      * Returns true if the lock is currently held non-exclusively.
   790      * Returns {@code true} if the lock is currently held non-exclusively.
   786      *
   791      *
   787      * @return true if the lock is currently held non-exclusively
   792      * @return {@code true} if the lock is currently held non-exclusively
   788      */
   793      */
   789     public boolean isReadLocked() {
   794     public boolean isReadLocked() {
   790         return (state & RBITS) != 0L;
   795         return (state & RBITS) != 0L;
   791     }
   796     }
   792 
   797