summaryrefslogtreecommitdiff
path: root/util/env_test.cc
diff options
context:
space:
mode:
authorcostan <costan@google.com>2019-03-11 13:04:53 -0700
committerChris Mumford <cmumford@google.com>2019-03-11 13:41:25 -0700
commit7d8e41e49b8fddda66a2c5f0a6a47f1a916e8d26 (patch)
tree0a5556aaf20ba27bd6ea0ab3792d1366e60b2f81 /util/env_test.cc
parentdd906262fd364c08a652dfa914f9995f6b7608a9 (diff)
downloadleveldb-7d8e41e49b8fddda66a2c5f0a6a47f1a916e8d26.tar.gz
leveldb: Replace AtomicPointer with std::atomic.
This CL removes AtomicPointer from leveldb's port interface. Its usage is replaced with std::atomic<> from the C++11 standard library. AtomicPointer was used to wrap flags, numbers, and pointers, so its instances are replaced with std::atomic<bool>, std::atomic<int>, std::atomic<size_t> and std::atomic<Node*>. This CL does not revise the memory ordering. AtomicPointer's methods are replaced mechanically with their std::atomic equivalents, even when the underlying usage is incorrect. (Example: DBImpl::has_imm_ is written using release stores, even though it is always read using relaxed ordering.) Revising the memory ordering is left for future CLs. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237865146
Diffstat (limited to 'util/env_test.cc')
-rw-r--r--util/env_test.cc57
1 files changed, 31 insertions, 26 deletions
diff --git a/util/env_test.cc b/util/env_test.cc
index 070109b..b204089 100644
--- a/util/env_test.cc
+++ b/util/env_test.cc
@@ -5,6 +5,7 @@
#include "leveldb/env.h"
#include <algorithm>
+#include <atomic>
#include "port/port.h"
#include "port/thread_annotations.h"
@@ -24,10 +25,15 @@ class EnvTest {
EnvTest() : env_(Env::Default()) { }
};
-static void SetBool(void* ptr) {
- reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
+namespace {
+
+static void SetAtomicBool(void* atomic_bool_ptr) {
+ std::atomic<bool>* atomic_bool =
+ reinterpret_cast<std::atomic<bool>*>(atomic_bool_ptr);
+ atomic_bool->store(true, std::memory_order_relaxed);
}
+} // namespace
TEST(EnvTest, ReadWrite) {
Random rnd(test::RandomSeed());
@@ -77,42 +83,41 @@ TEST(EnvTest, ReadWrite) {
}
TEST(EnvTest, RunImmediately) {
- port::AtomicPointer called(nullptr);
- env_->Schedule(&SetBool, &called);
+ std::atomic<bool> called(false);
+ env_->Schedule(&SetAtomicBool, &called);
env_->SleepForMicroseconds(kDelayMicros);
- ASSERT_TRUE(called.NoBarrier_Load() != nullptr);
+ ASSERT_TRUE(called.load(std::memory_order_relaxed));
}
TEST(EnvTest, RunMany) {
- port::AtomicPointer last_id(nullptr);
+ std::atomic<int> last_id(0);
- struct CB {
- port::AtomicPointer* last_id_ptr; // Pointer to shared slot
- uintptr_t id; // Order# for the execution of this callback
+ struct Callback {
+ std::atomic<int>* const last_id_ptr_; // Pointer to shared state.
+ const int id_; // Order# for the execution of this callback.
- CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
+ Callback(std::atomic<int>* last_id_ptr, int id)
+ : last_id_ptr_(last_id_ptr), id_(id) { }
- static void Run(void* v) {
- CB* cb = reinterpret_cast<CB*>(v);
- void* cur = cb->last_id_ptr->NoBarrier_Load();
- ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
- cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
+ static void Run(void* arg) {
+ Callback* callback = reinterpret_cast<Callback*>(arg);
+ int current_id = callback->last_id_ptr_->load(std::memory_order_relaxed);
+ ASSERT_EQ(callback->id_ - 1, current_id);
+ callback->last_id_ptr_->store(callback->id_, std::memory_order_relaxed);
}
};
- // Schedule in different order than start time
- CB cb1(&last_id, 1);
- CB cb2(&last_id, 2);
- CB cb3(&last_id, 3);
- CB cb4(&last_id, 4);
- env_->Schedule(&CB::Run, &cb1);
- env_->Schedule(&CB::Run, &cb2);
- env_->Schedule(&CB::Run, &cb3);
- env_->Schedule(&CB::Run, &cb4);
+ Callback callback1(&last_id, 1);
+ Callback callback2(&last_id, 2);
+ Callback callback3(&last_id, 3);
+ Callback callback4(&last_id, 4);
+ env_->Schedule(&Callback::Run, &callback1);
+ env_->Schedule(&Callback::Run, &callback2);
+ env_->Schedule(&Callback::Run, &callback3);
+ env_->Schedule(&Callback::Run, &callback4);
env_->SleepForMicroseconds(kDelayMicros);
- void* cur = last_id.Acquire_Load();
- ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
+ ASSERT_EQ(4, last_id.load(std::memory_order_relaxed));
}
struct State {