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