在hotspot/src/share/vm/utilities/exceptions.hpp
中,有一些代码:
// The THREAD & TRAPS macros facilitate the declaration of functions that throw exceptions.
// Convention: Use the TRAPS macro as the last argument of such a function; e.g.:
//
// int this_function_may_trap(int x, float y, TRAPS)
#define THREAD __the_thread__
#define TRAPS Thread* THREAD
THREAD
宏仅用于抛出异常:
// The THROW... macros should be used to throw an exception. They require a THREAD variable to be
// visible within the scope containing the THROW. Usually this is achieved by declaring the function
// with a TRAPS argument.
#define THREAD_AND_LOCATION THREAD, __FILE__, __LINE__
#define THROW_OOP(e) \
{ Exceptions::_throw_oop(THREAD_AND_LOCATION, e); return; }
#define THROW_HANDLE(e) \
{ Exceptions::_throw(THREAD_AND_LOCATION, e); return; }
// the real place to use __the_thread__ macro
void Exceptions::_throw(Thread* thread, const char* file, int line, Handle h_exception, const char* message) {......}
但是,我使用grep
来搜索所有OpenJDK8的代码,但却发现这里只有一个地方有这个__the_thread__
宏。
有人能告诉我真正的宏定义了什么吗?
答案 0 :(得分:2)
__the_thread__
这里是变量的名称,或者更确切地说是可能引发异常的VM函数的参数。它总是通过THREAD
或TRAPS
宏访问,这就是为什么没有其他引用,并且变量的真实名称无关紧要。
例如,定义
jint find_field_offset(jobject field, int must_be_static, TRAPS)
由预处理器扩展为
jint find_field_offset(jobject field, int must_be_static, Thread* __the_thread__)