| 13 | import sun.misc.Unsafe; |
| 14 | |
| 15 | public class LockSupport { |
| 16 | private LockSupport() { |
| 17 | // can't construct |
| 18 | } |
| 19 | |
| 20 | private static final Unsafe unsafe; |
| 21 | private static final long parkBlockerOffset; |
| 22 | |
| 23 | static { |
| 24 | unsafe = Unsafe.getUnsafe(); |
| 25 | try { |
| 26 | parkBlockerOffset = unsafe.objectFieldOffset(Thread.class.getDeclaredField("parkBlocker")); |
| 27 | } catch (Exception ex) { |
| 28 | throw new Error(ex); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | public static void unpark(Thread thread) { |
| 33 | if (thread != null) { |
| 34 | unsafe.unpark(thread); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public static void park(Object blocker) { |
| 39 | doParkNanos(blocker, 0L); |
| 40 | } |
| 41 | |
| 42 | public static void parkNanos(Object blocker, long nanos) { |
| 43 | if (nanos <= 0) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | doParkNanos(blocker, nanos); |
| 48 | } |
| 49 | |
| 50 | private static void doParkNanos(Object blocker, long nanos) { |
| 51 | Thread t = Thread.currentThread(); |
| 52 | unsafe.putObject(t, parkBlockerOffset, blocker); |
| 53 | unsafe.park(false, nanos); |
| 54 | unsafe.putObject(t, parkBlockerOffset, null); |
| 55 | } |
| 56 | |
| 57 | public static void parkUntil(Object blocker, long deadline) { |
| 58 | Thread t = Thread.currentThread(); |
| 59 | unsafe.putObject(t, parkBlockerOffset, blocker); |
| 60 | unsafe.park(true, deadline); |
| 61 | unsafe.putObject(t, parkBlockerOffset, null); |
| 62 | } |
| 63 | |
| 64 | public static Object getBlocker(Thread t) { |
| 65 | if (t == null) { |
| 66 | throw new NullPointerException(); |
| 67 | } |
| 68 | |
| 69 | return unsafe.getObjectVolatile(t, parkBlockerOffset); |
| 70 | } |
| 71 | |
| 72 | public static void park() { |
nothing calls this directly
no test coverage detected