]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/thread/SpinLock.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / thread / SpinLock.java
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
23 /**
24  * <p>This spin lock is a lock designed to protect VERY short sections
25  * of critical code.  Threads attempting to take the lock will spin
26  * forever until the lock is available, thus it is important that
27  * the code protected by this lock is extremely simple and non
28  * blocking. The reason for this lock is that it prevents a thread
29  * from giving up a CPU core when contending for the lock.</p>
30  * <pre>
31  * try(SpinLock.Lock lock = spinlock.lock())
32  * {
33  *   // something very quick and non blocking
34  * }
35  * </pre>
36  * <p>Further analysis however, shows that spin locks behave really
37  * bad under heavy contention and where the number of threads
38  * exceeds the number of cores, which are common scenarios for a
39  * server, so this class was removed from usage, preferring
40  * standard locks instead.</p>
41  * @deprecated Do not use it anymore, prefer normal locks
42  */
43 @Deprecated
44 public class SpinLock
45 {
46     private final AtomicReference<Thread> _lock = new AtomicReference<>(null);
47     private final Lock _unlock = new Lock();
48
49     public Lock lock()
50     {
51         Thread thread = Thread.currentThread();
52         while(true)
53         {
54             if (!_lock.compareAndSet(null,thread))
55             {
56                 if (_lock.get()==thread)
57                     throw new IllegalStateException("SpinLock is not reentrant");
58                 continue;
59             }
60             return _unlock;
61         }
62     }
63
64     public boolean isLocked()
65     {
66         return _lock.get()!=null;
67     }
68
69     public boolean isLockedThread()
70     {
71         return _lock.get()==Thread.currentThread();
72     }
73
74     public class Lock implements AutoCloseable
75     {
76         @Override
77         public void close()
78         {
79             _lock.set(null);
80         }
81     }
82 }