Skip to content

Commit

Permalink
Use thread_local for storing ThreadId
Browse files Browse the repository at this point in the history
Change-Id: Ifc754fb81089aed4cb79b1f6c4aab0cb73a2a5d7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2537690
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71176}
  • Loading branch information
ulan authored and Commit Bot committed Nov 13, 2020
1 parent 05f41a5 commit dacc2fe
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 12 deletions.
6 changes: 0 additions & 6 deletions src/base/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,7 @@ class V8_BASE_EXPORT Thread {
static LocalStorageKey CreateThreadLocalKey();
static void DeleteThreadLocalKey(LocalStorageKey key);
static void* GetThreadLocal(LocalStorageKey key);
static int GetThreadLocalInt(LocalStorageKey key) {
return static_cast<int>(reinterpret_cast<intptr_t>(GetThreadLocal(key)));
}
static void SetThreadLocal(LocalStorageKey key, void* value);
static void SetThreadLocalInt(LocalStorageKey key, int value) {
SetThreadLocal(key, reinterpret_cast<void*>(static_cast<intptr_t>(value)));
}
static bool HasThreadLocal(LocalStorageKey key) {
return GetThreadLocal(key) != nullptr;
}
Expand Down
7 changes: 1 addition & 6 deletions src/execution/thread-id.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,22 @@ namespace internal {

namespace {

DEFINE_LAZY_LEAKY_OBJECT_GETTER(base::Thread::LocalStorageKey, GetThreadIdKey,
base::Thread::CreateThreadLocalKey())
thread_local int thread_id = 0;

std::atomic<int> next_thread_id{1};

} // namespace

// static
ThreadId ThreadId::TryGetCurrent() {
int thread_id = base::Thread::GetThreadLocalInt(*GetThreadIdKey());
return thread_id == 0 ? Invalid() : ThreadId(thread_id);
}

// static
int ThreadId::GetCurrentThreadId() {
auto key = *GetThreadIdKey();
int thread_id = base::Thread::GetThreadLocalInt(key);
if (thread_id == 0) {
thread_id = next_thread_id.fetch_add(1);
CHECK_LE(1, thread_id);
base::Thread::SetThreadLocalInt(key, thread_id);
}
return thread_id;
}
Expand Down

1 comment on commit dacc2fe

@alii
Copy link

@alii alii commented on dacc2fe May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RIP fibers. End of an era πŸš€

Please sign in to comment.