2014-02-26 02:47:37 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "util/thread_local.h"
|
|
|
|
#include "util/mutexlock.h"
|
2014-02-27 20:38:55 +01:00
|
|
|
#include "port/likely.h"
|
2015-01-12 18:59:36 +01:00
|
|
|
#include <stdlib.h>
|
2014-04-23 03:38:10 +02:00
|
|
|
|
2014-02-26 02:47:37 +01:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2014-04-23 03:38:10 +02:00
|
|
|
port::Mutex ThreadLocalPtr::StaticMeta::mutex_;
|
2015-08-11 22:30:49 +02:00
|
|
|
#if ROCKSDB_SUPPORT_THREAD_LOCAL
|
2014-02-26 02:47:37 +01:00
|
|
|
__thread ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::tls_ = nullptr;
|
|
|
|
#endif
|
|
|
|
|
2015-07-02 01:13:49 +02:00
|
|
|
// Windows doesn't support a per-thread destructor with its
|
|
|
|
// TLS primitives. So, we build it manually by inserting a
|
|
|
|
// function to be called on each thread's exit.
|
|
|
|
// See http://www.codeproject.com/Articles/8113/Thread-Local-Storage-The-C-Way
|
|
|
|
// and http://www.nynaeve.net/?p=183
|
|
|
|
//
|
2015-07-13 21:11:05 +02:00
|
|
|
// really we do this to have clear conscience since using TLS with thread-pools
|
|
|
|
// is iffy
|
|
|
|
// although OK within a request. But otherwise, threads have no identity in its
|
|
|
|
// modern use.
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
// This runs on windows only called from the System Loader
|
|
|
|
#ifdef OS_WIN
|
|
|
|
|
|
|
|
// Windows cleanup routine is invoked from a System Loader with a different
|
2015-07-13 21:11:05 +02:00
|
|
|
// signature so we can not directly hookup the original OnThreadExit which is
|
|
|
|
// private member
|
|
|
|
// so we make StaticMeta class share with the us the address of the function so
|
|
|
|
// we can invoke it.
|
2015-07-02 01:13:49 +02:00
|
|
|
namespace wintlscleanup {
|
|
|
|
|
|
|
|
// This is set to OnThreadExit in StaticMeta singleton constructor
|
2015-07-13 21:11:05 +02:00
|
|
|
UnrefHandler thread_local_inclass_routine = nullptr;
|
2015-07-02 01:13:49 +02:00
|
|
|
pthread_key_t thread_local_key = -1;
|
|
|
|
|
|
|
|
// Static callback function to call with each thread termination.
|
|
|
|
void NTAPI WinOnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
|
2015-07-08 01:58:20 +02:00
|
|
|
// We decided to punt on PROCESS_EXIT
|
|
|
|
if (DLL_THREAD_DETACH == reason) {
|
|
|
|
if (thread_local_key != -1 && thread_local_inclass_routine != nullptr) {
|
|
|
|
void* tls = pthread_getspecific(thread_local_key);
|
2015-07-13 21:11:05 +02:00
|
|
|
if (tls != nullptr) {
|
2015-07-08 01:58:20 +02:00
|
|
|
thread_local_inclass_routine(tls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-02 01:13:49 +02:00
|
|
|
}
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
} // wintlscleanup
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#ifdef _WIN64
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#pragma comment(linker, "/include:_tls_used")
|
|
|
|
#pragma comment(linker, "/include:p_thread_callback_on_exit")
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
#else // _WIN64
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#pragma comment(linker, "/INCLUDE:__tls_used")
|
|
|
|
#pragma comment(linker, "/INCLUDE:_p_thread_callback_on_exit")
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#endif // _WIN64
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
// extern "C" suppresses C++ name mangling so we know the symbol name for the
|
|
|
|
// linker /INCLUDE:symbol pragma above.
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
// The linker must not discard thread_callback_on_exit. (We force a reference
|
|
|
|
// to this variable with a linker /include:symbol pragma to ensure that.) If
|
|
|
|
// this variable is discarded, the OnThreadExit function will never be called.
|
|
|
|
#ifdef _WIN64
|
|
|
|
|
|
|
|
// .CRT section is merged with .rdata on x64 so it must be constant data.
|
|
|
|
#pragma const_seg(".CRT$XLB")
|
|
|
|
// When defining a const variable, it must have external linkage to be sure the
|
|
|
|
// linker doesn't discard it.
|
|
|
|
extern const PIMAGE_TLS_CALLBACK p_thread_callback_on_exit;
|
2015-07-13 21:11:05 +02:00
|
|
|
const PIMAGE_TLS_CALLBACK p_thread_callback_on_exit =
|
|
|
|
wintlscleanup::WinOnThreadExit;
|
2015-07-02 01:13:49 +02:00
|
|
|
// Reset the default section.
|
|
|
|
#pragma const_seg()
|
|
|
|
|
|
|
|
#else // _WIN64
|
|
|
|
|
|
|
|
#pragma data_seg(".CRT$XLB")
|
|
|
|
PIMAGE_TLS_CALLBACK p_thread_callback_on_exit = wintlscleanup::WinOnThreadExit;
|
|
|
|
// Reset the default section.
|
|
|
|
#pragma data_seg()
|
|
|
|
|
|
|
|
#endif // _WIN64
|
|
|
|
|
|
|
|
} // extern "C"
|
|
|
|
|
2015-07-13 21:11:05 +02:00
|
|
|
#endif // OS_WIN
|
2015-07-02 01:13:49 +02:00
|
|
|
|
2014-04-23 06:13:34 +02:00
|
|
|
ThreadLocalPtr::StaticMeta* ThreadLocalPtr::Instance() {
|
|
|
|
static ThreadLocalPtr::StaticMeta inst;
|
|
|
|
return &inst;
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::OnThreadExit(void* ptr) {
|
|
|
|
auto* tls = static_cast<ThreadData*>(ptr);
|
|
|
|
assert(tls != nullptr);
|
|
|
|
|
|
|
|
auto* inst = Instance();
|
|
|
|
pthread_setspecific(inst->pthread_key_, nullptr);
|
|
|
|
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
inst->RemoveThreadData(tls);
|
|
|
|
// Unref stored pointers of current thread from all instances
|
|
|
|
uint32_t id = 0;
|
|
|
|
for (auto& e : tls->entries) {
|
2015-01-27 05:23:15 +01:00
|
|
|
void* raw = e.ptr.load();
|
2014-02-26 02:47:37 +01:00
|
|
|
if (raw != nullptr) {
|
|
|
|
auto unref = inst->GetHandler(id);
|
|
|
|
if (unref != nullptr) {
|
|
|
|
unref(raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++id;
|
|
|
|
}
|
|
|
|
// Delete thread local structure no matter if it is Mac platform
|
|
|
|
delete tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalPtr::StaticMeta::StaticMeta() : next_instance_id_(0) {
|
|
|
|
if (pthread_key_create(&pthread_key_, &OnThreadExit) != 0) {
|
2014-12-04 22:35:31 +01:00
|
|
|
abort();
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
2015-08-06 00:45:21 +02:00
|
|
|
|
|
|
|
// OnThreadExit is not getting called on the main thread.
|
|
|
|
// Call through the static destructor mechanism to avoid memory leak.
|
2015-08-07 19:47:05 +02:00
|
|
|
//
|
|
|
|
// Caveats: ~A() will be invoked _after_ ~StaticMeta for the global
|
|
|
|
// singleton (destructors are invoked in reverse order of constructor
|
|
|
|
// _completion_); the latter must not mutate internal members. This
|
|
|
|
// cleanup mechanism inherently relies on use-after-release of the
|
|
|
|
// StaticMeta, and is brittle with respect to compiler-specific handling
|
|
|
|
// of memory backing destructed statically-scoped objects. Perhaps
|
|
|
|
// registering with atexit(3) would be more robust.
|
|
|
|
//
|
2015-08-11 22:30:49 +02:00
|
|
|
// This is not required on Windows.
|
|
|
|
#if !defined(OS_WIN)
|
2015-08-06 00:45:21 +02:00
|
|
|
static struct A {
|
|
|
|
~A() {
|
2015-08-11 22:30:49 +02:00
|
|
|
#if !(ROCKSDB_SUPPORT_THREAD_LOCAL)
|
2015-08-07 19:47:05 +02:00
|
|
|
ThreadData* tls_ =
|
|
|
|
static_cast<ThreadData*>(pthread_getspecific(Instance()->pthread_key_));
|
2015-08-11 22:30:49 +02:00
|
|
|
#endif
|
2015-08-06 00:45:21 +02:00
|
|
|
if (tls_) {
|
|
|
|
OnThreadExit(tls_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} a;
|
2015-08-11 22:30:49 +02:00
|
|
|
#endif // !defined(OS_WIN)
|
2015-08-06 00:45:21 +02:00
|
|
|
|
2014-02-26 02:47:37 +01:00
|
|
|
head_.next = &head_;
|
|
|
|
head_.prev = &head_;
|
2015-07-02 01:13:49 +02:00
|
|
|
|
|
|
|
#ifdef OS_WIN
|
2015-07-13 21:11:05 +02:00
|
|
|
// Share with Windows its cleanup routine and the key
|
2015-07-02 01:13:49 +02:00
|
|
|
wintlscleanup::thread_local_inclass_routine = OnThreadExit;
|
|
|
|
wintlscleanup::thread_local_key = pthread_key_;
|
|
|
|
#endif
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::AddThreadData(ThreadLocalPtr::ThreadData* d) {
|
2014-04-23 03:38:10 +02:00
|
|
|
mutex_.AssertHeld();
|
2014-02-26 02:47:37 +01:00
|
|
|
d->next = &head_;
|
|
|
|
d->prev = head_.prev;
|
|
|
|
head_.prev->next = d;
|
|
|
|
head_.prev = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::RemoveThreadData(
|
|
|
|
ThreadLocalPtr::ThreadData* d) {
|
2014-04-23 03:38:10 +02:00
|
|
|
mutex_.AssertHeld();
|
2014-02-26 02:47:37 +01:00
|
|
|
d->next->prev = d->prev;
|
|
|
|
d->prev->next = d->next;
|
|
|
|
d->next = d->prev = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalPtr::ThreadData* ThreadLocalPtr::StaticMeta::GetThreadLocal() {
|
2015-08-11 22:30:49 +02:00
|
|
|
#if !(ROCKSDB_SUPPORT_THREAD_LOCAL)
|
2015-07-13 21:11:05 +02:00
|
|
|
// Make this local variable name look like a member variable so that we
|
|
|
|
// can share all the code below
|
2014-02-26 02:47:37 +01:00
|
|
|
ThreadData* tls_ =
|
|
|
|
static_cast<ThreadData*>(pthread_getspecific(Instance()->pthread_key_));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (UNLIKELY(tls_ == nullptr)) {
|
|
|
|
auto* inst = Instance();
|
|
|
|
tls_ = new ThreadData();
|
|
|
|
{
|
|
|
|
// Register it in the global chain, needs to be done before thread exit
|
|
|
|
// handler registration
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
inst->AddThreadData(tls_);
|
|
|
|
}
|
|
|
|
// Even it is not OS_MACOSX, need to register value for pthread_key_ so that
|
|
|
|
// its exit handler will be triggered.
|
|
|
|
if (pthread_setspecific(inst->pthread_key_, tls_) != 0) {
|
|
|
|
{
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
inst->RemoveThreadData(tls_);
|
|
|
|
}
|
|
|
|
delete tls_;
|
2014-12-04 22:35:31 +01:00
|
|
|
abort();
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tls_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* ThreadLocalPtr::StaticMeta::Get(uint32_t id) const {
|
|
|
|
auto* tls = GetThreadLocal();
|
|
|
|
if (UNLIKELY(id >= tls->entries.size())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-01-27 05:23:15 +01:00
|
|
|
return tls->entries[id].ptr.load(std::memory_order_acquire);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::Reset(uint32_t id, void* ptr) {
|
|
|
|
auto* tls = GetThreadLocal();
|
|
|
|
if (UNLIKELY(id >= tls->entries.size())) {
|
|
|
|
// Need mutex to protect entries access within ReclaimId
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
tls->entries.resize(id + 1);
|
|
|
|
}
|
2015-01-27 05:23:15 +01:00
|
|
|
tls->entries[id].ptr.store(ptr, std::memory_order_release);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void* ThreadLocalPtr::StaticMeta::Swap(uint32_t id, void* ptr) {
|
|
|
|
auto* tls = GetThreadLocal();
|
|
|
|
if (UNLIKELY(id >= tls->entries.size())) {
|
|
|
|
// Need mutex to protect entries access within ReclaimId
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
tls->entries.resize(id + 1);
|
|
|
|
}
|
2015-01-27 05:23:15 +01:00
|
|
|
return tls->entries[id].ptr.exchange(ptr, std::memory_order_acquire);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 23:43:22 +01:00
|
|
|
bool ThreadLocalPtr::StaticMeta::CompareAndSwap(uint32_t id, void* ptr,
|
|
|
|
void*& expected) {
|
|
|
|
auto* tls = GetThreadLocal();
|
|
|
|
if (UNLIKELY(id >= tls->entries.size())) {
|
|
|
|
// Need mutex to protect entries access within ReclaimId
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-03-07 23:43:22 +01:00
|
|
|
tls->entries.resize(id + 1);
|
|
|
|
}
|
2015-01-27 05:23:15 +01:00
|
|
|
return tls->entries[id].ptr.compare_exchange_strong(
|
|
|
|
expected, ptr, std::memory_order_release, std::memory_order_relaxed);
|
2014-03-07 23:43:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::Scrape(uint32_t id, autovector<void*>* ptrs,
|
|
|
|
void* const replacement) {
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
for (ThreadData* t = head_.next; t != &head_; t = t->next) {
|
|
|
|
if (id < t->entries.size()) {
|
|
|
|
void* ptr =
|
2015-01-27 05:23:15 +01:00
|
|
|
t->entries[id].ptr.exchange(replacement, std::memory_order_acquire);
|
2014-02-26 02:47:37 +01:00
|
|
|
if (ptr != nullptr) {
|
|
|
|
ptrs->push_back(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::SetHandler(uint32_t id, UnrefHandler handler) {
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
handler_map_[id] = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnrefHandler ThreadLocalPtr::StaticMeta::GetHandler(uint32_t id) {
|
2014-04-23 03:38:10 +02:00
|
|
|
mutex_.AssertHeld();
|
2014-02-26 02:47:37 +01:00
|
|
|
auto iter = handler_map_.find(id);
|
|
|
|
if (iter == handler_map_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ThreadLocalPtr::StaticMeta::GetId() {
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
if (free_instance_ids_.empty()) {
|
|
|
|
return next_instance_id_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t id = free_instance_ids_.back();
|
|
|
|
free_instance_ids_.pop_back();
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ThreadLocalPtr::StaticMeta::PeekId() const {
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
if (!free_instance_ids_.empty()) {
|
|
|
|
return free_instance_ids_.back();
|
|
|
|
}
|
|
|
|
return next_instance_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::StaticMeta::ReclaimId(uint32_t id) {
|
|
|
|
// This id is not used, go through all thread local data and release
|
|
|
|
// corresponding value
|
2014-04-23 03:38:10 +02:00
|
|
|
MutexLock l(&mutex_);
|
2014-02-26 02:47:37 +01:00
|
|
|
auto unref = GetHandler(id);
|
|
|
|
for (ThreadData* t = head_.next; t != &head_; t = t->next) {
|
|
|
|
if (id < t->entries.size()) {
|
2015-01-27 05:23:15 +01:00
|
|
|
void* ptr = t->entries[id].ptr.exchange(nullptr);
|
2014-02-26 02:47:37 +01:00
|
|
|
if (ptr != nullptr && unref != nullptr) {
|
|
|
|
unref(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handler_map_[id] = nullptr;
|
|
|
|
free_instance_ids_.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalPtr::ThreadLocalPtr(UnrefHandler handler)
|
2014-04-23 06:13:34 +02:00
|
|
|
: id_(Instance()->GetId()) {
|
2014-02-26 02:47:37 +01:00
|
|
|
if (handler != nullptr) {
|
2014-04-23 06:13:34 +02:00
|
|
|
Instance()->SetHandler(id_, handler);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadLocalPtr::~ThreadLocalPtr() {
|
2014-04-23 06:13:34 +02:00
|
|
|
Instance()->ReclaimId(id_);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void* ThreadLocalPtr::Get() const {
|
2014-04-23 06:13:34 +02:00
|
|
|
return Instance()->Get(id_);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::Reset(void* ptr) {
|
2014-04-23 06:13:34 +02:00
|
|
|
Instance()->Reset(id_, ptr);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void* ThreadLocalPtr::Swap(void* ptr) {
|
2014-04-23 06:13:34 +02:00
|
|
|
return Instance()->Swap(id_, ptr);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 23:43:22 +01:00
|
|
|
bool ThreadLocalPtr::CompareAndSwap(void* ptr, void*& expected) {
|
2014-04-23 06:13:34 +02:00
|
|
|
return Instance()->CompareAndSwap(id_, ptr, expected);
|
2014-03-07 23:43:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadLocalPtr::Scrape(autovector<void*>* ptrs, void* const replacement) {
|
2014-04-23 06:13:34 +02:00
|
|
|
Instance()->Scrape(id_, ptrs, replacement);
|
2014-02-26 02:47:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|