2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-02-26 01:47:37 +00:00
|
|
|
|
2016-02-19 20:29:54 +00:00
|
|
|
#include <thread>
|
2014-02-26 01:47:37 +00:00
|
|
|
#include <atomic>
|
2016-02-11 00:56:01 +00:00
|
|
|
#include <string>
|
2014-02-26 01:47:37 +00:00
|
|
|
|
|
|
|
#include "rocksdb/env.h"
|
2015-07-01 23:13:49 +00:00
|
|
|
#include "port/port.h"
|
2014-02-26 01:47:37 +00:00
|
|
|
#include "util/autovector.h"
|
2016-02-11 00:56:01 +00:00
|
|
|
#include "util/sync_point.h"
|
2014-02-26 01:47:37 +00:00
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
2016-02-11 00:56:01 +00:00
|
|
|
#include "util/thread_local.h"
|
2014-02-26 01:47:37 +00:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class ThreadLocalTest : public testing::Test {
|
2014-02-26 01:47:37 +00:00
|
|
|
public:
|
|
|
|
ThreadLocalTest() : env_(Env::Default()) {}
|
|
|
|
|
|
|
|
Env* env_;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Params {
|
2014-10-31 18:59:54 +00:00
|
|
|
Params(port::Mutex* m, port::CondVar* c, int* u, int n,
|
2014-02-26 01:47:37 +00:00
|
|
|
UnrefHandler handler = nullptr)
|
|
|
|
: mu(m),
|
|
|
|
cv(c),
|
2014-10-31 18:59:54 +00:00
|
|
|
unref(u),
|
2014-02-26 01:47:37 +00:00
|
|
|
total(n),
|
|
|
|
started(0),
|
|
|
|
completed(0),
|
|
|
|
doWrite(false),
|
|
|
|
tls1(handler),
|
|
|
|
tls2(nullptr) {}
|
|
|
|
|
|
|
|
port::Mutex* mu;
|
|
|
|
port::CondVar* cv;
|
|
|
|
int* unref;
|
|
|
|
int total;
|
|
|
|
int started;
|
|
|
|
int completed;
|
|
|
|
bool doWrite;
|
|
|
|
ThreadLocalPtr tls1;
|
|
|
|
ThreadLocalPtr* tls2;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IDChecker : public ThreadLocalPtr {
|
2017-02-02 22:02:41 +00:00
|
|
|
public:
|
|
|
|
static uint32_t PeekId() {
|
|
|
|
return TEST_PeekId();
|
|
|
|
}
|
2014-02-26 01:47:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-07-09 00:50:51 +00:00
|
|
|
// Suppress false positive clang analyzer warnings.
|
|
|
|
#ifndef __clang_analyzer__
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, UniqueIdTest) {
|
2014-02-26 01:47:37 +00:00
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 0u);
|
2014-02-26 01:47:37 +00:00
|
|
|
// New ThreadLocal instance bumps id by 1
|
|
|
|
{
|
2017-06-26 22:13:35 +00:00
|
|
|
// Id used 0
|
2017-06-03 00:12:39 +00:00
|
|
|
Params p1(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 1u);
|
|
|
|
// Id used 1
|
2017-06-03 00:12:39 +00:00
|
|
|
Params p2(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 2u);
|
|
|
|
// Id used 2
|
2017-06-03 00:12:39 +00:00
|
|
|
Params p3(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 3u);
|
|
|
|
// Id used 3
|
2017-06-03 00:12:39 +00:00
|
|
|
Params p4(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 4u);
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|
2017-06-26 22:13:35 +00:00
|
|
|
// id 3, 2, 1, 0 are in the free queue in order
|
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 0u);
|
2014-02-26 01:47:37 +00:00
|
|
|
|
2017-06-26 22:13:35 +00:00
|
|
|
// pick up 0
|
2014-02-28 01:55:10 +00:00
|
|
|
Params p1(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 1u);
|
|
|
|
// pick up 1
|
2014-02-28 01:55:10 +00:00
|
|
|
Params* p2 = new Params(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 2u);
|
|
|
|
// pick up 2
|
2014-02-28 01:55:10 +00:00
|
|
|
Params p3(&mu, &cv, nullptr, 1u);
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 3u);
|
|
|
|
// return up 1
|
2014-02-26 01:47:37 +00:00
|
|
|
delete p2;
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 1u);
|
|
|
|
// Now we have 3, 1 in queue
|
|
|
|
// pick up 1
|
|
|
|
Params p4(&mu, &cv, nullptr, 1u);
|
2014-02-28 01:55:10 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 3u);
|
2014-02-26 01:47:37 +00:00
|
|
|
// pick up 3
|
2014-02-28 01:55:10 +00:00
|
|
|
Params p5(&mu, &cv, nullptr, 1u);
|
2014-02-26 01:47:37 +00:00
|
|
|
// next new id
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 4u);
|
2014-02-26 01:47:37 +00:00
|
|
|
// After exit, id sequence in queue:
|
2017-06-26 22:13:35 +00:00
|
|
|
// 3, 1, 2, 0
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|
2016-07-09 00:50:51 +00:00
|
|
|
#endif // __clang_analyzer__
|
2014-02-26 01:47:37 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, SequentialReadWriteTest) {
|
2017-06-26 22:13:35 +00:00
|
|
|
// global id list carries over 3, 1, 2, 0
|
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 0u);
|
2014-02-26 01:47:37 +00:00
|
|
|
|
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
Params p(&mu, &cv, nullptr, 1);
|
|
|
|
ThreadLocalPtr tls2;
|
|
|
|
p.tls2 = &tls2;
|
|
|
|
|
|
|
|
auto func = [](void* ptr) {
|
2014-10-31 18:59:54 +00:00
|
|
|
auto& params = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
ASSERT_TRUE(params.tls1.Get() == nullptr);
|
|
|
|
params.tls1.Reset(reinterpret_cast<int*>(1));
|
|
|
|
ASSERT_TRUE(params.tls1.Get() == reinterpret_cast<int*>(1));
|
|
|
|
params.tls1.Reset(reinterpret_cast<int*>(2));
|
|
|
|
ASSERT_TRUE(params.tls1.Get() == reinterpret_cast<int*>(2));
|
|
|
|
|
|
|
|
ASSERT_TRUE(params.tls2->Get() == nullptr);
|
|
|
|
params.tls2->Reset(reinterpret_cast<int*>(1));
|
|
|
|
ASSERT_TRUE(params.tls2->Get() == reinterpret_cast<int*>(1));
|
|
|
|
params.tls2->Reset(reinterpret_cast<int*>(2));
|
|
|
|
ASSERT_TRUE(params.tls2->Get() == reinterpret_cast<int*>(2));
|
|
|
|
|
|
|
|
params.mu->Lock();
|
|
|
|
++(params.completed);
|
|
|
|
params.cv->SignalAll();
|
|
|
|
params.mu->Unlock();
|
2014-02-26 01:47:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int iter = 0; iter < 1024; ++iter) {
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 1u);
|
2014-02-26 01:47:37 +00:00
|
|
|
// Another new thread, read/write should not see value from previous thread
|
|
|
|
env_->StartThread(func, static_cast<void*>(&p));
|
|
|
|
mu.Lock();
|
|
|
|
while (p.completed != iter + 1) {
|
|
|
|
cv.Wait();
|
|
|
|
}
|
|
|
|
mu.Unlock();
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 1u);
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, ConcurrentReadWriteTest) {
|
2017-06-26 22:13:35 +00:00
|
|
|
// global id list carries over 3, 1, 2, 0
|
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 0u);
|
2014-02-26 01:47:37 +00:00
|
|
|
|
|
|
|
ThreadLocalPtr tls2;
|
|
|
|
port::Mutex mu1;
|
|
|
|
port::CondVar cv1(&mu1);
|
2014-04-01 23:26:07 +00:00
|
|
|
Params p1(&mu1, &cv1, nullptr, 16);
|
2014-02-26 01:47:37 +00:00
|
|
|
p1.tls2 = &tls2;
|
|
|
|
|
|
|
|
port::Mutex mu2;
|
|
|
|
port::CondVar cv2(&mu2);
|
2014-04-01 23:26:07 +00:00
|
|
|
Params p2(&mu2, &cv2, nullptr, 16);
|
2014-02-26 01:47:37 +00:00
|
|
|
p2.doWrite = true;
|
|
|
|
p2.tls2 = &tls2;
|
|
|
|
|
|
|
|
auto func = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
2015-11-20 23:31:47 +00:00
|
|
|
// Size_T switches size along with the ptr size
|
|
|
|
// we want to cast to.
|
|
|
|
size_t own = ++(p.started);
|
2014-02-26 01:47:37 +00:00
|
|
|
p.cv->SignalAll();
|
|
|
|
while (p.started != p.total) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
|
|
|
|
// Let write threads write a different value from the read threads
|
|
|
|
if (p.doWrite) {
|
|
|
|
own += 8192;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_TRUE(p.tls1.Get() == nullptr);
|
|
|
|
ASSERT_TRUE(p.tls2->Get() == nullptr);
|
|
|
|
|
|
|
|
auto* env = Env::Default();
|
|
|
|
auto start = env->NowMicros();
|
|
|
|
|
2015-11-20 23:31:47 +00:00
|
|
|
p.tls1.Reset(reinterpret_cast<size_t*>(own));
|
|
|
|
p.tls2->Reset(reinterpret_cast<size_t*>(own + 1));
|
2014-02-26 01:47:37 +00:00
|
|
|
// Loop for 1 second
|
|
|
|
while (env->NowMicros() - start < 1000 * 1000) {
|
|
|
|
for (int iter = 0; iter < 100000; ++iter) {
|
2015-11-20 23:31:47 +00:00
|
|
|
ASSERT_TRUE(p.tls1.Get() == reinterpret_cast<size_t*>(own));
|
|
|
|
ASSERT_TRUE(p.tls2->Get() == reinterpret_cast<size_t*>(own + 1));
|
2014-02-26 01:47:37 +00:00
|
|
|
if (p.doWrite) {
|
2015-11-20 23:31:47 +00:00
|
|
|
p.tls1.Reset(reinterpret_cast<size_t*>(own));
|
|
|
|
p.tls2->Reset(reinterpret_cast<size_t*>(own + 1));
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.completed);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initiate 2 instnaces: one keeps writing and one keeps reading.
|
|
|
|
// The read instance should not see data from the write instance.
|
|
|
|
// Each thread local copy of the value are also different from each
|
|
|
|
// other.
|
|
|
|
for (int th = 0; th < p1.total; ++th) {
|
|
|
|
env_->StartThread(func, static_cast<void*>(&p1));
|
|
|
|
}
|
|
|
|
for (int th = 0; th < p2.total; ++th) {
|
|
|
|
env_->StartThread(func, static_cast<void*>(&p2));
|
|
|
|
}
|
|
|
|
|
|
|
|
mu1.Lock();
|
|
|
|
while (p1.completed != p1.total) {
|
|
|
|
cv1.Wait();
|
|
|
|
}
|
|
|
|
mu1.Unlock();
|
|
|
|
|
|
|
|
mu2.Lock();
|
|
|
|
while (p2.completed != p2.total) {
|
|
|
|
cv2.Wait();
|
|
|
|
}
|
|
|
|
mu2.Unlock();
|
|
|
|
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 3u);
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, Unref) {
|
2017-06-26 22:13:35 +00:00
|
|
|
ASSERT_EQ(IDChecker::PeekId(), 0u);
|
2014-02-26 01:47:37 +00:00
|
|
|
|
|
|
|
auto unref = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
p.mu->Lock();
|
|
|
|
++(*p.unref);
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Case 0: no unref triggered if ThreadLocalPtr is never accessed
|
|
|
|
auto func0 = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.started);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
while (p.started != p.total) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int th = 1; th <= 128; th += th) {
|
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
int unref_count = 0;
|
|
|
|
Params p(&mu, &cv, &unref_count, th, unref);
|
|
|
|
|
|
|
|
for (int i = 0; i < p.total; ++i) {
|
|
|
|
env_->StartThread(func0, static_cast<void*>(&p));
|
|
|
|
}
|
|
|
|
env_->WaitForJoin();
|
|
|
|
ASSERT_EQ(unref_count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 1: unref triggered by thread exit
|
|
|
|
auto func1 = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.started);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
while (p.started != p.total) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
|
|
|
|
ASSERT_TRUE(p.tls1.Get() == nullptr);
|
|
|
|
ASSERT_TRUE(p.tls2->Get() == nullptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int th = 1; th <= 128; th += th) {
|
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
int unref_count = 0;
|
|
|
|
ThreadLocalPtr tls2(unref);
|
|
|
|
Params p(&mu, &cv, &unref_count, th, unref);
|
|
|
|
p.tls2 = &tls2;
|
|
|
|
|
|
|
|
for (int i = 0; i < p.total; ++i) {
|
|
|
|
env_->StartThread(func1, static_cast<void*>(&p));
|
|
|
|
}
|
|
|
|
|
|
|
|
env_->WaitForJoin();
|
|
|
|
|
|
|
|
// N threads x 2 ThreadLocal instance cleanup on thread exit
|
|
|
|
ASSERT_EQ(unref_count, 2 * p.total);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: unref triggered by ThreadLocal instance destruction
|
|
|
|
auto func2 = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.started);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
while (p.started != p.total) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
|
|
|
|
ASSERT_TRUE(p.tls1.Get() == nullptr);
|
|
|
|
ASSERT_TRUE(p.tls2->Get() == nullptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.completed);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
|
|
|
|
// Waiting for instruction to exit thread
|
|
|
|
while (p.completed != 0) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int th = 1; th <= 128; th += th) {
|
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
int unref_count = 0;
|
|
|
|
Params p(&mu, &cv, &unref_count, th, unref);
|
|
|
|
p.tls2 = new ThreadLocalPtr(unref);
|
|
|
|
|
|
|
|
for (int i = 0; i < p.total; ++i) {
|
|
|
|
env_->StartThread(func2, static_cast<void*>(&p));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all threads to finish using Params
|
|
|
|
mu.Lock();
|
|
|
|
while (p.completed != p.total) {
|
|
|
|
cv.Wait();
|
|
|
|
}
|
|
|
|
mu.Unlock();
|
|
|
|
|
|
|
|
// Now destroy one ThreadLocal instance
|
|
|
|
delete p.tls2;
|
|
|
|
p.tls2 = nullptr;
|
|
|
|
// instance destroy for N threads
|
|
|
|
ASSERT_EQ(unref_count, p.total);
|
|
|
|
|
|
|
|
// Signal to exit
|
|
|
|
mu.Lock();
|
|
|
|
p.completed = 0;
|
|
|
|
cv.SignalAll();
|
|
|
|
mu.Unlock();
|
|
|
|
env_->WaitForJoin();
|
|
|
|
// additional N threads exit unref for the left instance
|
|
|
|
ASSERT_EQ(unref_count, 2 * p.total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, Swap) {
|
2014-02-26 01:47:37 +00:00
|
|
|
ThreadLocalPtr tls;
|
|
|
|
tls.Reset(reinterpret_cast<void*>(1));
|
|
|
|
ASSERT_EQ(reinterpret_cast<int64_t>(tls.Swap(nullptr)), 1);
|
|
|
|
ASSERT_TRUE(tls.Swap(reinterpret_cast<void*>(2)) == nullptr);
|
|
|
|
ASSERT_EQ(reinterpret_cast<int64_t>(tls.Get()), 2);
|
|
|
|
ASSERT_EQ(reinterpret_cast<int64_t>(tls.Swap(reinterpret_cast<void*>(3))), 2);
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, Scrape) {
|
2014-02-26 01:47:37 +00:00
|
|
|
auto unref = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
p.mu->Lock();
|
|
|
|
++(*p.unref);
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
auto func = [](void* ptr) {
|
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
|
|
|
|
ASSERT_TRUE(p.tls1.Get() == nullptr);
|
|
|
|
ASSERT_TRUE(p.tls2->Get() == nullptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
|
|
|
|
p.tls1.Reset(ptr);
|
|
|
|
p.tls2->Reset(ptr);
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.completed);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
|
|
|
|
// Waiting for instruction to exit thread
|
|
|
|
while (p.completed != 0) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int th = 1; th <= 128; th += th) {
|
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
int unref_count = 0;
|
|
|
|
Params p(&mu, &cv, &unref_count, th, unref);
|
|
|
|
p.tls2 = new ThreadLocalPtr(unref);
|
|
|
|
|
|
|
|
for (int i = 0; i < p.total; ++i) {
|
|
|
|
env_->StartThread(func, static_cast<void*>(&p));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all threads to finish using Params
|
|
|
|
mu.Lock();
|
|
|
|
while (p.completed != p.total) {
|
|
|
|
cv.Wait();
|
|
|
|
}
|
|
|
|
mu.Unlock();
|
|
|
|
|
|
|
|
ASSERT_EQ(unref_count, 0);
|
|
|
|
|
|
|
|
// Scrape all thread local data. No unref at thread
|
|
|
|
// exit or ThreadLocalPtr destruction
|
|
|
|
autovector<void*> ptrs;
|
2014-03-07 22:43:22 +00:00
|
|
|
p.tls1.Scrape(&ptrs, nullptr);
|
|
|
|
p.tls2->Scrape(&ptrs, nullptr);
|
2014-02-26 01:47:37 +00:00
|
|
|
delete p.tls2;
|
|
|
|
// Signal to exit
|
|
|
|
mu.Lock();
|
|
|
|
p.completed = 0;
|
|
|
|
cv.SignalAll();
|
|
|
|
mu.Unlock();
|
|
|
|
env_->WaitForJoin();
|
|
|
|
|
|
|
|
ASSERT_EQ(unref_count, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:37:39 +00:00
|
|
|
TEST_F(ThreadLocalTest, Fold) {
|
|
|
|
auto unref = [](void* ptr) {
|
|
|
|
delete static_cast<std::atomic<int64_t>*>(ptr);
|
|
|
|
};
|
2016-08-23 20:34:56 +00:00
|
|
|
static const int kNumThreads = 16;
|
|
|
|
static const int kItersPerThread = 10;
|
2016-08-22 22:37:39 +00:00
|
|
|
port::Mutex mu;
|
|
|
|
port::CondVar cv(&mu);
|
|
|
|
Params params(&mu, &cv, nullptr, kNumThreads, unref);
|
2016-08-23 20:34:56 +00:00
|
|
|
auto func = [](void* ptr) {
|
2016-08-22 22:37:39 +00:00
|
|
|
auto& p = *static_cast<Params*>(ptr);
|
|
|
|
ASSERT_TRUE(p.tls1.Get() == nullptr);
|
|
|
|
p.tls1.Reset(new std::atomic<int64_t>(0));
|
|
|
|
|
|
|
|
for (int i = 0; i < kItersPerThread; ++i) {
|
|
|
|
static_cast<std::atomic<int64_t>*>(p.tls1.Get())->fetch_add(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu->Lock();
|
|
|
|
++(p.completed);
|
|
|
|
p.cv->SignalAll();
|
|
|
|
|
|
|
|
// Waiting for instruction to exit thread
|
|
|
|
while (p.completed != 0) {
|
|
|
|
p.cv->Wait();
|
|
|
|
}
|
|
|
|
p.mu->Unlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int th = 0; th < params.total; ++th) {
|
|
|
|
env_->StartThread(func, static_cast<void*>(¶ms));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all threads to finish using Params
|
|
|
|
mu.Lock();
|
|
|
|
while (params.completed != params.total) {
|
|
|
|
cv.Wait();
|
|
|
|
}
|
|
|
|
mu.Unlock();
|
|
|
|
|
|
|
|
// Verify Fold() behavior
|
|
|
|
int64_t sum = 0;
|
|
|
|
params.tls1.Fold(
|
|
|
|
[](void* ptr, void* res) {
|
|
|
|
auto sum_ptr = static_cast<int64_t*>(res);
|
|
|
|
*sum_ptr += static_cast<std::atomic<int64_t>*>(ptr)->load();
|
|
|
|
},
|
|
|
|
&sum);
|
|
|
|
ASSERT_EQ(sum, kNumThreads * kItersPerThread);
|
|
|
|
|
|
|
|
// Signal to exit
|
|
|
|
mu.Lock();
|
|
|
|
params.completed = 0;
|
|
|
|
cv.SignalAll();
|
|
|
|
mu.Unlock();
|
|
|
|
env_->WaitForJoin();
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(ThreadLocalTest, CompareAndSwap) {
|
2014-03-07 22:43:22 +00:00
|
|
|
ThreadLocalPtr tls;
|
|
|
|
ASSERT_TRUE(tls.Swap(reinterpret_cast<void*>(1)) == nullptr);
|
|
|
|
void* expected = reinterpret_cast<void*>(1);
|
|
|
|
// Swap in 2
|
|
|
|
ASSERT_TRUE(tls.CompareAndSwap(reinterpret_cast<void*>(2), expected));
|
|
|
|
expected = reinterpret_cast<void*>(100);
|
|
|
|
// Fail Swap, still 2
|
|
|
|
ASSERT_TRUE(!tls.CompareAndSwap(reinterpret_cast<void*>(2), expected));
|
|
|
|
ASSERT_EQ(expected, reinterpret_cast<void*>(2));
|
|
|
|
// Swap in 3
|
|
|
|
expected = reinterpret_cast<void*>(2);
|
|
|
|
ASSERT_TRUE(tls.CompareAndSwap(reinterpret_cast<void*>(3), expected));
|
|
|
|
ASSERT_EQ(tls.Get(), reinterpret_cast<void*>(3));
|
|
|
|
}
|
|
|
|
|
2016-02-11 00:56:01 +00:00
|
|
|
namespace {
|
|
|
|
|
2018-02-22 20:36:41 +00:00
|
|
|
void* AccessThreadLocal(void* arg) {
|
2016-02-11 00:56:01 +00:00
|
|
|
TEST_SYNC_POINT("AccessThreadLocal:Start");
|
|
|
|
ThreadLocalPtr tlp;
|
|
|
|
tlp.Reset(new std::string("hello RocksDB"));
|
|
|
|
TEST_SYNC_POINT("AccessThreadLocal:End");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// The following test is disabled as it requires manual steps to run it
|
|
|
|
// correctly.
|
|
|
|
//
|
|
|
|
// Currently we have no way to acess SyncPoint w/o ASAN error when the
|
|
|
|
// child thread dies after the main thread dies. So if you manually enable
|
|
|
|
// this test and only see an ASAN error on SyncPoint, it means you pass the
|
|
|
|
// test.
|
|
|
|
TEST_F(ThreadLocalTest, DISABLED_MainThreadDiesFirst) {
|
|
|
|
rocksdb::SyncPoint::GetInstance()->LoadDependency(
|
|
|
|
{{"AccessThreadLocal:Start", "MainThreadDiesFirst:End"},
|
|
|
|
{"PosixEnv::~PosixEnv():End", "AccessThreadLocal:End"}});
|
|
|
|
|
|
|
|
// Triggers the initialization of singletons.
|
|
|
|
Env::Default();
|
2016-02-19 20:29:54 +00:00
|
|
|
|
2016-02-19 21:47:19 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
2016-02-19 20:29:54 +00:00
|
|
|
try {
|
2016-02-19 21:47:19 +00:00
|
|
|
#endif // ROCKSDB_LITE
|
2017-02-06 22:43:55 +00:00
|
|
|
rocksdb::port::Thread th(&AccessThreadLocal, nullptr);
|
2016-02-19 20:29:54 +00:00
|
|
|
th.detach();
|
|
|
|
TEST_SYNC_POINT("MainThreadDiesFirst:End");
|
2016-02-19 21:47:19 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
2016-02-19 20:29:54 +00:00
|
|
|
} catch (const std::system_error& ex) {
|
|
|
|
std::cerr << "Start thread: " << ex.code() << std::endl;
|
2017-07-17 04:23:33 +00:00
|
|
|
FAIL();
|
2016-02-19 20:29:54 +00:00
|
|
|
}
|
2016-02-19 21:47:19 +00:00
|
|
|
#endif // ROCKSDB_LITE
|
2016-02-11 00:56:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:47:37 +00:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2014-02-26 01:47:37 +00:00
|
|
|
}
|