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/execution/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 namespace { 13 14 thread_local int thread_id = 0; 15 16 std::atomic<int> next_thread_id{1}; 17 18 } // namespace 19 20 // static TryGetCurrent()21ThreadId ThreadId::TryGetCurrent() { 22 return thread_id == 0 ? Invalid() : ThreadId(thread_id); 23 } 24 25 // static GetCurrentThreadId()26int ThreadId::GetCurrentThreadId() { 27 if (thread_id == 0) { 28 thread_id = next_thread_id.fetch_add(1); 29 CHECK_LE(1, thread_id); 30 } 31 return thread_id; 32 } 33 34 } // namespace internal 35 } // namespace v8 36