View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util.thread;
20  
21  import java.util.concurrent.atomic.AtomicReference;
22  import java.util.concurrent.locks.ReentrantLock;
23  
24  /**
25   * <p>This is a lock designed to protect VERY short sections of
26   * critical code.  Threads attempting to take the lock will wait
27   * until the lock is available, thus it is important that
28   * the code protected by this lock is extremely simple and non
29   * blocking.</p>
30   * <pre>
31   * try(SpinLock.Lock lock = locker.lock())
32   * {
33   *   // something very quick and non blocking
34   * }
35   * </pre>
36   */
37  public class Locker
38  {
39      private static final boolean SPIN = Boolean.getBoolean(Locker.class.getName() + ".spin");
40  
41      private final boolean _spin;
42      private final ReentrantLock _lock = new ReentrantLock();
43      private final AtomicReference<Thread> _spinLockState = new AtomicReference<>(null);
44      private final Lock _unlock = new Lock();
45  
46      public Locker()
47      {
48          this(SPIN);
49      }
50  
51      public Locker(boolean spin)
52      {
53          this._spin = spin;
54      }
55  
56      public Lock lock()
57      {
58          if (_spin)
59              spinLock();
60          else
61              concLock();
62          return _unlock;
63      }
64  
65      private void spinLock()
66      {
67          Thread current = Thread.currentThread();
68          while (true)
69          {
70              // Using test-and-test-and-set for better performance.
71              Thread locker = _spinLockState.get();
72              if (locker != null || !_spinLockState.compareAndSet(null, current))
73              {
74                  if (locker == current)
75                      throw new IllegalStateException("Locker is not reentrant");
76                  continue;
77              }
78              return;
79          }
80      }
81  
82      private void concLock()
83      {
84          if (_lock.isHeldByCurrentThread())
85              throw new IllegalStateException("Locker is not reentrant");
86          _lock.lock();
87      }
88  
89      public boolean isLocked()
90      {
91          if (_spin)
92              return _spinLockState.get() != null;
93          else
94              return _lock.isLocked();
95      }
96  
97      public class Lock implements AutoCloseable
98      {
99          @Override
100         public void close()
101         {
102             if (_spin)
103                 _spinLockState.set(null);
104             else
105                 _lock.unlock();
106         }
107     }
108 }