Java源码-0x05-宏定义

1 宏定义

宏定义着实太多,甚至有的嵌套地太深,只能遇到了就记录一下

当然了,很多宏的定义都是跨平台的,所以以linux+gcc的环境为例

1.1 JNIEXPORT

1.2 JNICALL

1
#define JNICALL

1.3 JVM_ENTRY

1
2
3
4
5
6
7
#define JVM_ENTRY(result_type, header)                               \
extern "C" { \
result_type JNICALL header { \
JavaThread* thread=JavaThread::thread_from_jni_environment(env); \
ThreadInVMfromNative __tiv(thread); \
debug_only(VMNativeEntryWrapper __vew;) \
VM_ENTRY_BASE(result_type, header, thread)

1.4 JVM_END

1
#define JVM_END } }

2 示例

2.1 JNIEXPORT和JNICALL

1
2
JNIEXPORT void JNICALL
JVM_MonitorWait(JNIEnv *env, jobject obj, jlong ms);

替换为

1
2
__attribute__((visibility("default")))
void JVM_MonitorWait(JNIEnv *env, jobject obj, jlong ms);

2.2 JVM_ENTRY和JVM_END

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
JVMWrapper("JVM_MonitorWait");
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
JavaThreadInObjectWaitState jtiows(thread, ms != 0);
if (JvmtiExport::should_post_monitor_wait()) {
JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms);

// The current thread already owns the monitor and it has not yet
// been added to the wait queue so the current thread cannot be
// made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
// event handler cannot accidentally consume an unpark() meant for
// the ParkEvent associated with this ObjectMonitor.
}
ObjectSynchronizer::wait(obj, ms, CHECK);
JVM_END

替换为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
extern "C" {
void JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
{
JavaThread* thread=JavaThread::thread_from_jni_environment(env);
ThreadInVMfromNative __tiv(thread);
debug_only(VMNativeEntryWrapper __vew;)
VM_ENTRY_BASE(result_type, header, thread)
JVMWrapper("JVM_MonitorWait");
Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
JavaThreadInObjectWaitState jtiows(thread, ms != 0);
if (JvmtiExport::should_post_monitor_wait())
{
JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms);

// The current thread already owns the monitor and it has not yet
// been added to the wait queue so the current thread cannot be
// made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
// event handler cannot accidentally consume an unpark() meant for
// the ParkEvent associated with this ObjectMonitor.
}
ObjectSynchronizer::wait(obj, ms, CHECK);
}
}

Java源码-0x05-宏定义
https://bannirui.github.io/2024/03/10/Java源码-0x05-宏定义/
作者
dingrui
发布于
2024年3月10日
许可协议