Skip to content

Commit

Permalink
outline ThreadEntry::LocalLifetime
Browse files Browse the repository at this point in the history
Summary: These functions are cold so there is no need for them to be inline.

Differential Revision: D60009883

fbshipit-source-id: 03b09ffd77d0523f0d250a68d7f9b9c40c4f1b84
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jul 21, 2024
1 parent a6f3952 commit 94806c0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
30 changes: 30 additions & 0 deletions third-party/folly/src/folly/detail/ThreadLocalDetail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ constexpr auto kBigGrowthFactor = 1.7;
namespace folly {
namespace threadlocal_detail {

struct ThreadEntry::LocalSet {
using Map = std::unordered_map<LocalLifetime const*, LocalCache*>;
Synchronized<Map> tracking;
};

FOLLY_NOINLINE ThreadEntry::LocalSet* ThreadEntry::newLocalSet() {
return new ThreadEntry::LocalSet();
}

FOLLY_NOINLINE ThreadEntry::LocalLifetime::LocalLifetime(
LocalSet& set, LocalCache& cache) noexcept
: caches{set} {
DCHECK(!cache.poison);
auto tracking = caches.tracking.wlock();
auto inserted = tracking->emplace(this, &cache).second;
DCHECK(inserted);
}

FOLLY_NOINLINE ThreadEntry::LocalLifetime::~LocalLifetime() {
auto tracking = caches.tracking.wlock();
auto it = tracking->find(this);
DCHECK(it != tracking->end());
DCHECK(it->second);
auto& cache = *it->second;
tracking->erase(it);
DCHECK(!cache.poison);
cache = {};
cache.poison = true;
}

bool ThreadEntrySet::basicSanity() const {
return //
threadEntries.size() == entryToVectorSlot.size() &&
Expand Down
28 changes: 5 additions & 23 deletions third-party/folly/src/folly/detail/ThreadLocalDetail.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,13 @@ struct ThreadEntry {

struct LocalLifetime;

struct LocalSet {
using Map = std::unordered_map<LocalLifetime const*, LocalCache*>;
Synchronized<Map> tracking;
};
struct LocalSet;
static LocalSet* newLocalSet();

struct LocalLifetime {
LocalSet& caches;
explicit LocalLifetime(LocalSet& set, LocalCache& cache) noexcept
: caches{set} {
DCHECK(!cache.poison);
auto tracking = caches.tracking.wlock();
auto inserted = tracking->emplace(this, &cache).second;
DCHECK(inserted);
}
~LocalLifetime() {
auto tracking = caches.tracking.wlock();
auto it = tracking->find(this);
DCHECK(it != tracking->end());
DCHECK(it->second);
auto& cache = *it->second;
tracking->erase(it);
DCHECK(!cache.poison);
cache = {};
cache.poison = true;
}
explicit LocalLifetime(LocalSet& set, LocalCache& cache) noexcept;
~LocalLifetime();
};

ElementWrapper* elements{nullptr};
Expand Down Expand Up @@ -775,7 +757,7 @@ struct FOLLY_EXPORT StaticMeta final : StaticMetaBase {
int ret = pthread_setspecific(key, threadEntry);
checkPosixError(ret, "pthread_setspecific failed");

threadEntry->caches = new ThreadEntry::LocalSet();
threadEntry->caches = ThreadEntry::newLocalSet();
}
return threadEntry;
}
Expand Down

0 comments on commit 94806c0

Please sign in to comment.