C++ 作用域和线程局部变量如何在(V8';s)C++;?

C++ 作用域和线程局部变量如何在(V8';s)C++;?,c++,v8,embedded-v8,C++,V8,Embedded V8,我对V8的示波器的工作方式很感兴趣 堆栈上的范围对象如何在堆栈上找到其他范围对象和上下文 深入研究手提镜的工作原理,我发现它们依赖于当地人。这让我想知道C++中的这些工作,我已经找到了实现,但仍然不知道我在做什么。 api.cc--HandleScope查找当前隔离 HandleScope::HandleScope() { i::Isolate* isolate = i::Isolate::Current(); API_ENTRY_CHECK(isolate, "HandleScope:

我对V8的示波器的工作方式很感兴趣

堆栈上的范围对象如何在堆栈上找到其他范围对象和上下文

深入研究手提镜的工作原理,我发现它们依赖于当地人。这让我想知道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++;
}
consolate.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;
  }
  static inline void* GetExistingThreadLocal(LocalStorageKey key) {
    void* result = reinterpret_cast<void*>(
        InternalGetExistingThreadLocal(static_cast<intptr_t>(key)));
    ASSERT(result == GetThreadLocal(key));
    return result;
  }
//返回当前线程在其中运行的隔离。
内联(静态隔离*当前(){
const Thread::LocalStorageKey=isolate_key();
隔离*隔离=重新解释(
线程::GetExistingThreadLocal(键));
如果(!隔离){
确保故障隔离();
隔离=重新解释铸件(
线程::GetExistingThreadLocal(键));
}
断言(隔离!=NULL);
返回隔离;
}
h——调用一个低级方法来检索本地线程

  // 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;
  }
  static inline void* GetExistingThreadLocal(LocalStorageKey key) {
    void* result = reinterpret_cast<void*>(
        InternalGetExistingThreadLocal(static_cast<intptr_t>(key)));
    ASSERT(result == GetThreadLocal(key));
    return result;
  }
静态内联void*GetExistingThreadLocal(LocalStorageKey){
无效*结果=重新解释(
InternalGetExistingThreadLocal(静态_转换(键));
断言(result==GetThreadLocal(key));
返回结果;
}
platform-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));
}
inline intptr\u t InternalGetExistingThreadLocal(intptr\u t索引){
常量intptr_t ktibinlinetlskoffset=0xE10;
常数intptr_t ktibextratlsofset=0xF94;
const intptr_t kMaxInlineSlots=64;
const intptr_t kMaxSlots=kMaxInlineSlots+1024;

断言(0您可以将
InternalGetExistingThreadLocal
作为
TlsGetValue
WinAPI调用的内联版本进行查看

在用户模式下的Windows上,段寄存器允许代码访问包含线程特定信息的线程信息块(TIB),例如线程本地存储结构

TIB布局以及TLS在TIB中的存储方式在DDK中公开(有关TIB布局的快速概述,请参阅)


鉴于此知识和能力,可通过
\uu readfsdword(offs)
(相当于读取
dword ptr fs:[offs]
)从TIB读取数据您可以直接高效地访问TLS,而无需调用
TlsGetValue

这是一种非常有趣的方法。我想知道它是否比C++11的thread_local(线程本地)有任何优势——除了在较旧的编译器中可用之外。