V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
thread-id.cc
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/thread-id.h"
6 #include "src/base/lazy-instance.h"
7 #include "src/base/platform/platform.h"
8 
9 namespace v8 {
10 namespace internal {
11 
12 base::Atomic32 ThreadId::highest_thread_id_ = 0;
13 
14 namespace {
15 
16 struct LocalStorageKeyAllocator {
17  static void Construct(void* storage_ptr_arg) {
18  auto storage_ptr =
19  reinterpret_cast<base::Thread::LocalStorageKey*>(storage_ptr_arg);
20  *storage_ptr = base::Thread::CreateThreadLocalKey();
21  }
22 };
23 
24 static base::LazyInstance<base::Thread::LocalStorageKey,
25  LocalStorageKeyAllocator>::type thread_id_key =
26  LAZY_INSTANCE_INITIALIZER;
27 
28 } // namespace
29 
30 // static
31 ThreadId ThreadId::TryGetCurrent() {
32  int thread_id = base::Thread::GetThreadLocalInt(thread_id_key.Get());
33  return thread_id == 0 ? Invalid() : ThreadId(thread_id);
34 }
35 
36 // static
37 int ThreadId::GetCurrentThreadId() {
38  int thread_id = base::Thread::GetThreadLocalInt(thread_id_key.Get());
39  if (thread_id == 0) {
40  thread_id = AllocateThreadId();
41  base::Thread::SetThreadLocalInt(thread_id_key.Get(), thread_id);
42  }
43  return thread_id;
44 }
45 
46 } // namespace internal
47 } // namespace v8
Definition: libplatform.h:13