1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
import sun.misc.Unsafe; |
9 |
import java.lang.reflect.*; |
10 |
|
11 |
/** |
12 |
* Package-private native methods for classes introduced in JSR166 |
13 |
*/ |
14 |
final class JSR166Support { |
15 |
private static native void registerNatives(); |
16 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
17 |
private static int addressSize = unsafe.addressSize(); |
18 |
static { |
19 |
registerNatives(); |
20 |
} |
21 |
|
22 |
/** |
23 |
* Native implementation of trylock entry in Locks.attempt. |
24 |
*/ |
25 |
static native boolean tryLockEnter(Object o); |
26 |
|
27 |
/** |
28 |
* Native implementation of trylock exit in Locks.attempt. |
29 |
*/ |
30 |
static native void tryLockExit(Object o); |
31 |
|
32 |
/** |
33 |
* Native implementation of Locks.newConditionFor(obj).wait() |
34 |
*/ |
35 |
static native void conditionWait(Object obj, |
36 |
Object cond |
37 |
) throws InterruptedException; |
38 |
|
39 |
|
40 |
/** |
41 |
* Native implementation of Locks.newConditionFor(obj).waitNanos() |
42 |
*/ |
43 |
static native long conditionRelWait(Object obj, |
44 |
Object cond, |
45 |
long nanos) throws InterruptedException; |
46 |
|
47 |
/** |
48 |
* Native implementation of Locks.newConditionFor(obj).waitUntil() |
49 |
*/ |
50 |
static native boolean conditionAbsWait(Object obj, |
51 |
Object cond, |
52 |
long deadline) throws InterruptedException; |
53 |
|
54 |
/** |
55 |
* Native implementation of Locks.newConditionFor(obj).signal() |
56 |
*/ |
57 |
static native void conditionNotify(Object base, Object cond); |
58 |
|
59 |
/** |
60 |
* Native implementation of Locks.newConditionFor(obj).signalAll() |
61 |
*/ |
62 |
static native void conditionNotifyAll(Object base, Object cond); |
63 |
|
64 |
/** |
65 |
* Native implementation of TimeUnit.nanoTime |
66 |
*/ |
67 |
static native long currentTimeNanos(); |
68 |
|
69 |
/** |
70 |
* Native implementation of thread-blocking primitive used in |
71 |
* ReentrantLock (and possibly elsewhere). Block current thread |
72 |
* until a balancing unpark occurs, or the thread is interrupted, |
73 |
* or if isAbsolute is false, the relative time in nanoseconds |
74 |
* elapses, or if true, the time of day in millisecs since epoch |
75 |
* elapses, or if a balancing unpark has already been |
76 |
* issued, or just spuriously. |
77 |
* @param isAbsolute true if time represents a deadline, false if a timeout. |
78 |
* @param time the deadline or timeout. If zero and isAbsolute is |
79 |
* false, means to wait forever. |
80 |
*/ |
81 |
static native void park(boolean isAbsolute, long time); |
82 |
|
83 |
/** |
84 |
* Native implementation of thread-unblocking primitive used in |
85 |
* ReentrantLock (and possibly elsewhere). Unblock the given |
86 |
* thread blocked on park, or, if it is not blocked, cause the |
87 |
* subsequent call to park not to block. |
88 |
* @param thread the thread to unpark (no-op if null). |
89 |
*/ |
90 |
static native void unpark(Object thread); |
91 |
|
92 |
/** |
93 |
* Implementation of Locks.mightBeLocked. |
94 |
*/ |
95 |
static boolean mightBeLocked(Object x) { |
96 |
// This is actually done non-natively via unsafe, but is |
97 |
// highly dependent on JVM object layout details. |
98 |
boolean l = (addressSize == 4)? |
99 |
((unsafe.getInt(x, 0L) & 3) == 1) : |
100 |
((unsafe.getLong(x, 0L) & 3) == 1); |
101 |
unsafe.loadLoadBarrier(); |
102 |
return l; |
103 |
} |
104 |
|
105 |
} |