Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*-
   * See the file LICENSE for redistribution information.
   *
   * Copyright (c) 2002, 2013 Oracle and/or its affiliates.  All rights reserved.
   *
   */
  
  package com.sleepycat.je.latch;
  
 import java.util.List;
 
Simple thread-based non-transactional reader-writer/shared-exclusive latch. Latches provide simple exclusive or shared transient locks on objects. Latches are expected to be held for short, defined periods of time. No deadlock detection is provided so it is the caller's responsibility to sequence latch acquisition in an ordered fashion to avoid deadlocks.
 
 @SuppressWarnings("serial")
 public class SharedLatch
     extends ReentrantReadWriteLock {
 
     private String name;
     private boolean noteLatch;
     private List<Threadreaders;

    
If true, this shared latch is only ever latched exclusively. Used for BINs.
 
     private boolean exclusiveOnly;
 
     public SharedLatch(String name) {
         super(false /* fair */);
         assert ( = Collections.synchronizedList
                 (new ArrayList<Thread>())) != null;
          = false;
         this. = name;
     }

    
Set the latch name, used for latches in objects instantiated from the log.
 
     public void setName(String name) {
         this. = name;
     }

    
Indicate whether this latch should be tracked in the debugging LatchSupport.latchTable. Always return true so this can be called under an assert.
 
     public boolean setNoteLatch(boolean noteLatch) {
         this. = noteLatch;
         return true;
     }

    
Indicate whether this latch can only be set exclusively (not shared). Used for BIN latches that are Shared, but should only be latched exclusively.
 
     public void setExclusiveOnly(boolean exclusiveOnly) {
         this. = exclusiveOnly;
     }

    
Acquire a latch for exclusive/write access. If the thread already holds the latch for shared access, it cannot be upgraded and EnvironmentFailureException will be thrown. Wait for the latch if some other thread is holding it. If there are threads waiting for access, they will be granted the latch on a FIFO basis if fair latches are enabled (as of JE 5.0 they are never enabled). When the method returns, the latch is held for exclusive access.

Throws:
com.sleepycat.je.EnvironmentFailureException if the latch is already held by the current thread for shared access.
 
     public void acquireExclusive() {
         try {
             if (isWriteLockedByCurrentThread()) {
                 throw EnvironmentFailureException.unexpectedState
                     ("Latch already held: " + );
             }
 
             writeLock().lock();
 
             assert ( ? noteLatch() : true);// intentional side effect;
         } finally {
             assert EnvironmentImpl.maybeForceYield();
        }
    }

    
Probe a latch for exclusive access, but don't block if it's not available.

Returns:
true if the latch was acquired, false if it is not available.
Throws:
com.sleepycat.je.EnvironmentFailureException if the latch is already held by the calling thread.
    public boolean acquireExclusiveNoWait() {
        try {
            if (isWriteLockedByCurrentThread()) {
                throw EnvironmentFailureException.unexpectedState
                    ("Latch already held: " + );
            }
            boolean ret = writeLock().tryLock();
            /* Intentional side effect. */
            assert (( & ret) ? noteLatch() : true);
            return ret;
        } finally {
            assert EnvironmentImpl.maybeForceYield();
        }
    }

    
Acquire a latch for shared/read access. Nesting is allowed, that is, the latch may be acquired more than once by the same thread.
    public void acquireShared() {
        if () {
            acquireExclusive();
            return;
        }
        try {
            boolean assertionsEnabled = false;
            assert assertionsEnabled = true;
            if (assertionsEnabled) {
                if (.add(Thread.currentThread())) {
                    readLock().lock();
                } else {
                    /* Already latched, do nothing. */
                }
            } else {
                readLock().lock();
            }
            assert ( ?  noteLatch() : true);// intentional side effect
        } finally {
            assert EnvironmentImpl.maybeForceYield();
        }
    }

    
Release an exclusive or shared latch. If there are other thread(s) waiting for the latch, they are woken up and granted the latch.
    public void release() {
        try {
            if (isWriteLockedByCurrentThread()) {
                writeLock().unlock();
                /* Intentional side effect. */
                assert ( ? unNoteLatch() : true);
                return;
            }
            if () {
                return;
            }
            boolean assertionsEnabled = false;
            assert assertionsEnabled = true;
            if (assertionsEnabled) {
                if (.remove(Thread.currentThread())) {
                    readLock().unlock();
                } else {
                    throw EnvironmentFailureException.unexpectedState
                        ("Latch not held: " + );
                }
            } else {
                /*
                 * There's no way to tell if a readlock is held by the current
                 * thread so just try unlocking it.
                 */
                readLock().unlock();
            }
            /* Intentional side effect. */
            assert ( ? unNoteLatch() : true);
        } catch (IllegalMonitorStateException IMSE) {
            IMSE.printStackTrace();
            return;
        }
    }

    
Release the latch. If there are other thread(s) waiting for the latch, one is woken up and granted the latch. If the latch was not owned by the caller, just return.
    public void releaseIfOwner() {
        if (isWriteLockedByCurrentThread()) {
            writeLock().unlock();
            assert ( ? unNoteLatch() : true);
            return;
        }
        if () {
            return;
        }
        assert (getReadLockCount() > 0);
        boolean assertionsEnabled = false;
        assert assertionsEnabled = true;
        if (assertionsEnabled) {
            if (.contains(Thread.currentThread())) {
                readLock().unlock();
                .remove(Thread.currentThread());
                assert ( ? unNoteLatch() : true);
            }
        } else {
            /*
             * There's no way to tell if a readlock is held by the current
             * thread so just try unlocking it.
             */
            readLock().unlock();
        }
    }

    
Return true if this thread is an owner, reader, or write.
    public boolean isOwner() {
        boolean assertionsEnabled = false;
        assert assertionsEnabled = true;
        if (assertionsEnabled && !) {
            return .contains(Thread.currentThread()) ||
                isWriteLockedByCurrentThread();
        } else {
            return isWriteLockedByCurrentThread();
        }
    }

    
Only call under the assert system. This records latching by thread.
    private boolean noteLatch() {
        return ..noteLatch(this);
    }

    
Only call under the assert system. This records latching by thread.
    private boolean unNoteLatch() {
        return ..unNoteLatch(this);
    }
New to GrepCode? Check out our FAQ X