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 static com.sleepycat.je.latch.LatchStatDefinition.LATCH_CONTENTION;
 import static com.sleepycat.je.latch.LatchStatDefinition.LATCH_NOWAIT_SUCCESS;
 import static com.sleepycat.je.latch.LatchStatDefinition.LATCH_NOWAIT_UNSUCCESS;
 import static com.sleepycat.je.latch.LatchStatDefinition.LATCH_NO_WAITERS;
 import static com.sleepycat.je.latch.LatchStatDefinition.LATCH_RELEASES;
 import static com.sleepycat.je.latch.LatchStatDefinition.LATCH_SELF_OWNED;
 
 
 
 public class Latch {
 
     /*
      * Required because getOwner() is protected (for unknown reasons) and can't
      * be accessed except by a subclass of ReentrantLock.
      */
     @SuppressWarnings("serial")
     private static class JEReentrantLock extends ReentrantLock {
         JEReentrantLock(boolean fair) {
             super(fair);
         }
 
         @Override
         protected Thread getOwner() {
             return super.getOwner();
         }
     }
 
     private final JEReentrantLock lock;
     private String name;
     private final StatGroup stats;
     private final IntStat nAcquiresNoWaiters;
     private final IntStat nAcquiresSelfOwned;
     private final IntStat nAcquiresWithContention;
     private final IntStat nAcquiresNoWaitSuccessful;
     private final IntStat nAcquiresNoWaitUnsuccessful;
     private final IntStat nReleases;
 
     public Latch(String name) {
          = new JEReentrantLock(false /* fair */);
         this. = name;
 
          = new StatGroup(.,
                               .);
          = new IntStat();
          = new IntStat();
          = new IntStat(,
                                                   );
          = new IntStat();
     }

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

    
Used by com.sleepycat.je.utilint.TimingLatch.
 
     public String getName() {
         return ;
     }

    
Acquire a latch for exclusive/write access.

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. When the method returns, the latch is held for exclusive access.

Throws:
com.sleepycat.je.EnvironmentFailureException if the latch is already held by the calling thread.
 
     public void acquire() {
         try {
             if (.isHeldByCurrentThread()) {
                 .increment();
                 throw EnvironmentFailureException.unexpectedState
                     ("Latch already held: " + );
             }
            if (.isLocked()) {
                .increment();
            } else {
                .increment();
            }
            .lock();
            assert noteLatch(); // intentional side effect;
        } finally {
            assert EnvironmentImpl.maybeForceYield();
        }
    }

    
Acquire a latch for exclusive/write access, but do not 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 acquireNoWait() {
        try {
            if (.isHeldByCurrentThread()) {
                .increment();
                throw EnvironmentFailureException.unexpectedState
                    ("Latch already held: " + );
            }
            boolean ret = .tryLock();
            if (ret) {
                assert noteLatch();
                .increment();
            } else {
                .increment();
            }
            return ret;
        } finally {
            assert EnvironmentImpl.maybeForceYield();
        }
    }

    
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() {
        doRelease(false);
    }

    
Release the latch. If there are other thread(s) waiting for the latch, they are woken up and granted the latch.

Throws:
com.sleepycat.je.EnvironmentFailureException if the latch is not currently held.
    public void release() {
        if (doRelease(true)) {
            throw EnvironmentFailureException.unexpectedState
                ("Latch not held: " + );
        }
    }

    
Do the work of releasing the latch. Wake up any waiters.

Returns:
true if this latch was not owned by the caller.
    private boolean doRelease(boolean checkHeld) {
        try {
            if (!.isHeldByCurrentThread()) {
                return true;
            }
            .unlock();
            .increment();
            assert unNoteLatch(checkHeld); // intentional side effect.
        } catch (IllegalMonitorStateException IMSE) {
            return true;
        }
        return false;
    }

    
Return true if the current thread holds this latch.

Returns:
true if we hold this latch. False otherwise.
    public boolean isOwner() {
        return .isHeldByCurrentThread();
    }

    
Used only for unit tests.

Returns:
the thread that currently holds the latch for exclusive access.
    public Thread owner() {
        return .getOwner();
    }

    
Return the number of threads waiting.

Returns:
the number of threads waiting for the latch.
    public int nWaiters() {
        return .getQueueLength();
    }

    

Returns:
a LatchStats object with information about this latch.
    public StatGroup getLatchStats() {
        return ;
    }
    public void clear() {
        .clear();
    }

    
Formats a latch owner and waiters.
    @Override
    public String toString() {
        return .toString();
    }

    
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(boolean checkHeld) {
        /* Only return a false status if we are checking for latch ownership.*/
        if (checkHeld) {
            return ..unNoteLatch(this);
        } else {
            ..unNoteLatch(this);
            return true;
        }
    }
New to GrepCode? Check out our FAQ X