作用域和线程局部变量如何在 (V8) C++中工作?
How do scopes and thread locals work in (V8's) C++?
我对V8的作用域是如何工作的很感兴趣。
堆栈上的作用域对象如何在堆栈上找到其他作用域对象和上下文?
深入研究HandleScopes的工作方式,我发现它们依赖于线程本地程序。这让我想知道这些在C++中是如何工作的,我已经找到了实现,但仍然不知道发生了什么。
api.cc--HandleScope查找当前隔离
HandleScope::HandleScope() {
i::Isolate* isolate = i::Isolate::Current();
API_ENTRY_CHECK(isolate, "HandleScope::HandleScope");
v8::ImplementationUtilities::HandleScopeData* current =
isolate->handle_scope_data();
isolate_ = isolate;
prev_next_ = current->next;
prev_limit_ = current->limit;
is_closed_ = false;
current->level++;
}
isolate.cc--静态方法将当前隔离作为线程本地
// Returns the isolate inside which the current thread is running.
INLINE(static Isolate* Current()) {
const Thread::LocalStorageKey key = isolate_key();
Isolate* isolate = reinterpret_cast<Isolate*>(
Thread::GetExistingThreadLocal(key));
if (!isolate) {
EnsureDefaultIsolate();
isolate = reinterpret_cast<Isolate*>(
Thread::GetExistingThreadLocal(key));
}
ASSERT(isolate != NULL);
return isolate;
}
platform.h--调用一个低级别方法来检索线程本地
static inline void* GetExistingThreadLocal(LocalStorageKey key) {
void* result = reinterpret_cast<void*>(
InternalGetExistingThreadLocal(static_cast<intptr_t>(key)));
ASSERT(result == GetThreadLocal(key));
return result;
}
平台-tls-win32.h——神奇的发生了
inline intptr_t InternalGetExistingThreadLocal(intptr_t index) {
const intptr_t kTibInlineTlsOffset = 0xE10;
const intptr_t kTibExtraTlsOffset = 0xF94;
const intptr_t kMaxInlineSlots = 64;
const intptr_t kMaxSlots = kMaxInlineSlots + 1024;
ASSERT(0 <= index && index < kMaxSlots);
if (index < kMaxInlineSlots) {
return static_cast<intptr_t>(__readfsdword(kTibInlineTlsOffset +
kPointerSize * index));
}
intptr_t extra = static_cast<intptr_t>(__readfsdword(kTibExtraTlsOffset));
ASSERT(extra != 0);
return *reinterpret_cast<intptr_t*>(extra +
kPointerSize * (index - kMaxInlineSlots));
}
- 最后一种方法究竟是如何工作的
- 它怎么知道往哪里看
- 堆栈的结构是什么
您可以将InternalGetExistingThreadLocal
视为TlsGetValue
WinAPI调用的内联版本。
在Windows用户模式下,fs
段寄存器允许代码访问线程信息块(TIB),其中包含线程特定信息,例如线程本地存储结构。
TIB的布局以及TLS存储在TIB内部的方式在DDK中公开(请参阅http://en.wikipedia.org/wiki/Win32_Thread_Information_Block用于TIB布局的快速概览)。
给定通过__readfsdword(offs)
从TIB读取数据的这种知识和能力(相当于读取dword ptr fs:[offs]
),可以在不调用TlsGetValue
的情况下直接有效地访问TLS。
相关文章:
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 在 G++ v8.2.0 中使用 std::experimental::可选
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- v8::HandleScope::CreateHandle() # 中的致命错误无法在没有 HandleScope 的情
- 如何将节点 V8 字符串转换为 C++ 字符串
- 在 C++ 中嵌入 V8 会导致与 libcpp 相关的"unrecognized external symbol"错误
- 当我使用 V8 库中的 GetInternalField() 时出现分段错误
- 在 v8 JavaScript 中重复调用C++是否有巨大的开销?
- 我可以使用谷歌 v8 在回调函数中获取 JavaScript 函数C++源文本吗?
- 与纯 V8 相比,NodeJS 是否有任何性能缺陷或显著开销?
- V8垃圾收集器回调,用于测量GC活动
- 如何在 C++ 中从 V8 调用 Javascript 函数
- 如何将 v8::FunctionCallbackInfo<v8::Value> 数组从一个隔离复制到另一个隔离?
- 使用导入的函数从嵌入式v8调用webassembly
- v8 源代码中 ArrayMap 函数的回调 fn 参数是什么?
- 如何在 v8 Javascript 中的多个函数中使用相同的上下文?
- 如何使用 v8 本机插件将 C++ 数组交付到 Node.js
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- v8 不再支持 v8::Value::ToNumber 了吗?
- 如何将wchar_t转换为 V8 字符串?