View Javadoc

    1   /*
    2    * Copyright 2010 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.util.internal;
    17
    18  import java.util.concurrent.TimeUnit;
    19  import java.util.concurrent.locks.AbstractQueuedSynchronizer;
    20  import java.util.concurrent.locks.Condition;
    21  import java.util.concurrent.locks.Lock;
    22
    23  /**
    24   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    25   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    26   * @version $Rev: 2115 $, $Date: 2010-02-01 15:21:49 +0900 (Mon, 01 Feb 2010) $
    27   */
    28  public final class NonReentrantLock extends AbstractQueuedSynchronizer
    29                               implements Lock {
    30
    31      private static final long serialVersionUID = -833780837233068610L;
    32
    33      private Thread owner;
    34
    35      public void lock() {
    36          acquire(1);
    37      }
    38
    39      public void lockInterruptibly() throws InterruptedException {
    40          acquireInterruptibly(1);
    41      }
    42
    43      public boolean tryLock() {
    44          return tryAcquire(1);
    45      }
    46
    47      public boolean tryLock(long time, TimeUnit unit)
    48              throws InterruptedException {
    49          return tryAcquireNanos(1, unit.toNanos(time));
    50      }
    51
    52      public void unlock() {
    53          release(1);
    54      }
    55
    56      public boolean isHeldByCurrentThread() {
    57          return isHeldExclusively();
    58      }
    59
    60      public Condition newCondition() {
    61          return new ConditionObject();
    62      }
    63
    64      @Override
    65      protected final boolean tryAcquire(int acquires) {
    66          if (compareAndSetState(0, 1)) {
    67              owner = Thread.currentThread();
    68              return true;
    69          }
    70          return false;
    71      }
    72
    73      @Override
    74      protected final boolean tryRelease(int releases) {
    75          if (Thread.currentThread() != owner) {
    76              throw new IllegalMonitorStateException();
    77          }
    78          owner = null;
    79          setState(0);
    80          return true;
    81      }
    82
    83      @Override
    84      protected final boolean isHeldExclusively() {
    85          return getState() != 0 && owner == Thread.currentThread();
    86      }
    87  }