1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
28
29
30
31
32
33
34
35
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
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 }