Line data Source code
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 791137 : DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::Thread::LocalStorageKey, GetThreadIdKey,
17 : base::Thread::CreateThreadLocalKey());
18 :
19 : } // namespace
20 :
21 : // static
22 1499 : ThreadId ThreadId::TryGetCurrent() {
23 1499 : int thread_id = base::Thread::GetThreadLocalInt(*GetThreadIdKey());
24 3491 : return thread_id == 0 ? Invalid() : ThreadId(thread_id);
25 : }
26 :
27 : // static
28 662594 : int ThreadId::GetCurrentThreadId() {
29 662594 : int thread_id = base::Thread::GetThreadLocalInt(*GetThreadIdKey());
30 662589 : if (thread_id == 0) {
31 : thread_id = AllocateThreadId();
32 67561 : base::Thread::SetThreadLocalInt(*GetThreadIdKey(), thread_id);
33 : }
34 662591 : return thread_id;
35 : }
36 :
37 : } // namespace internal
38 : } // namespace v8
|