Skip to content

Commit

Permalink
better names for SingletonThreadLocal internals
Browse files Browse the repository at this point in the history
Summary: Possibly not the ideal names, but also possibly a little more helpful than the current names.

Reviewed By: luciang

Differential Revision: D53601849

fbshipit-source-id: 78be10d2f61e51e1b4bb7c5c3c66fef12de9d155
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Feb 10, 2024
1 parent 61fd22c commit 6d79e8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
26 changes: 13 additions & 13 deletions folly/SingletonThreadLocal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ namespace folly {

namespace detail {

FOLLY_NOINLINE SingletonThreadLocalState::Wrapper::Wrapper() noexcept {}
FOLLY_NOINLINE SingletonThreadLocalState::Tracking::Tracking() noexcept {}

FOLLY_NOINLINE SingletonThreadLocalState::Wrapper::~Wrapper() {
FOLLY_NOINLINE SingletonThreadLocalState::Tracking::~Tracking() {
for (auto& kvp : caches) {
kvp.first->cache = nullptr;
kvp.first->object = nullptr;
}
}

FOLLY_NOINLINE void SingletonThreadLocalState::LocalLifetime::destroy(
Wrapper& wrapper) noexcept {
auto& lifetimes = wrapper.lifetimes[this];
Tracking& tracking) noexcept {
auto& lifetimes = tracking.lifetimes[this];
for (auto cache : lifetimes) {
auto const it = wrapper.caches.find(cache);
auto const it = tracking.caches.find(cache);
if (!--it->second) {
wrapper.caches.erase(it);
cache->cache = nullptr;
tracking.caches.erase(it);
cache->object = nullptr;
}
}
wrapper.lifetimes.erase(this);
tracking.lifetimes.erase(this);
}

FOLLY_NOINLINE void SingletonThreadLocalState::LocalLifetime::track(
LocalCache& cache, Wrapper& wrapper, void* object) noexcept {
cache.cache = object;
auto const inserted = wrapper.lifetimes[this].insert(&cache);
wrapper.caches[&cache] += inserted.second;
LocalCache& cache, Tracking& tracking, void* object) noexcept {
cache.object = object;
auto const inserted = tracking.lifetimes[this].insert(&cache);
tracking.caches[&cache] += inserted.second;
}

} // namespace detail
Expand Down
16 changes: 8 additions & 8 deletions folly/SingletonThreadLocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace detail {

struct SingletonThreadLocalState {
struct LocalCache {
void* cache; // type-erased pointer to the object field of wrapper, below
void* object; // type-erased pointer to the object field of wrapper, below
};
static_assert( // pod avoids tls-init guard var and tls-fini ub use-after-dtor
std::is_standard_layout<LocalCache>::value &&
Expand All @@ -44,7 +44,7 @@ struct SingletonThreadLocalState {

struct LocalLifetime;

struct Wrapper {
struct Tracking {
using LocalCacheSet = std::unordered_set<LocalCache*>;

// per-cache refcounts, the number of lifetimes tracking that cache
Expand All @@ -53,13 +53,13 @@ struct SingletonThreadLocalState {
// per-lifetime cache tracking; 1-M lifetimes may track 1-N caches
std::unordered_map<LocalLifetime*, LocalCacheSet> lifetimes;

Wrapper() noexcept;
~Wrapper();
Tracking() noexcept;
~Tracking();
};

struct LocalLifetime {
void destroy(Wrapper& wrapper) noexcept;
void track(LocalCache& cache, Wrapper& wrapper, void* object) noexcept;
void destroy(Tracking& tracking) noexcept;
void track(LocalCache& cache, Tracking& tracking, void* object) noexcept;
};
};

Expand Down Expand Up @@ -117,7 +117,7 @@ class SingletonThreadLocal {
// keep as first field in first base, to save 1 instr in the fast path
Object object{Make{}()};
};
struct Wrapper : ObjectWrapper, State::Wrapper {
struct Wrapper : ObjectWrapper, State::Tracking {
/* implicit */ operator T&() { return ObjectWrapper::object; }
};

Expand Down Expand Up @@ -152,7 +152,7 @@ class SingletonThreadLocal {
return getWrapper();
}
static thread_local LocalCache cache;
auto* object = static_cast<Object*>(cache.cache);
auto* object = static_cast<Object*>(cache.object);
return FOLLY_LIKELY(!!object) ? *object : getSlow(cache).object;
}

Expand Down

0 comments on commit 6d79e8b

Please sign in to comment.