Commit 0ae0ccc8 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

use relaxed_atomic in BufferedRandomDevice

Differential Revision: D31661218

fbshipit-source-id: 48535870ab2b2f86b0d4e9286365fe906110dc3e
parent e5d88948
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
#include <folly/Random.h> #include <folly/Random.h>
#include <array> #include <array>
#include <atomic>
#include <mutex> #include <mutex>
#include <random> #include <random>
...@@ -30,6 +29,7 @@ ...@@ -30,6 +29,7 @@
#include <folly/portability/Config.h> #include <folly/portability/Config.h>
#include <folly/portability/SysTime.h> #include <folly/portability/SysTime.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
#include <folly/synchronization/RelaxedAtomic.h>
#include <glog/logging.h> #include <glog/logging.h>
...@@ -92,15 +92,12 @@ class BufferedRandomDevice { ...@@ -92,15 +92,12 @@ class BufferedRandomDevice {
public: public:
static constexpr size_t kDefaultBufferSize = 128; static constexpr size_t kDefaultBufferSize = 128;
static void notifyNewGlobalEpoch() { static void notifyNewGlobalEpoch() { ++globalEpoch_; }
globalEpoch_.fetch_add(1, std::memory_order_relaxed);
}
explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize); explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize);
void get(void* data, size_t size) { void get(void* data, size_t size) {
auto const globalEpoch = globalEpoch_.load(std::memory_order_relaxed); if (LIKELY(epoch_ == globalEpoch_ && size <= remaining())) {
if (LIKELY(globalEpoch == epoch_ && size <= remaining())) {
memcpy(data, ptr_, size); memcpy(data, ptr_, size);
ptr_ += size; ptr_ += size;
} else { } else {
...@@ -115,7 +112,7 @@ class BufferedRandomDevice { ...@@ -115,7 +112,7 @@ class BufferedRandomDevice {
return size_t(buffer_.get() + bufferSize_ - ptr_); return size_t(buffer_.get() + bufferSize_ - ptr_);
} }
static std::atomic<size_t> globalEpoch_; static relaxed_atomic<size_t> globalEpoch_;
size_t epoch_{size_t(-1)}; // refill on first use size_t epoch_{size_t(-1)}; // refill on first use
const size_t bufferSize_; const size_t bufferSize_;
...@@ -123,7 +120,7 @@ class BufferedRandomDevice { ...@@ -123,7 +120,7 @@ class BufferedRandomDevice {
unsigned char* ptr_; unsigned char* ptr_;
}; };
std::atomic<size_t> BufferedRandomDevice::globalEpoch_{0}; relaxed_atomic<size_t> BufferedRandomDevice::globalEpoch_{0};
struct RandomTag {}; struct RandomTag {};
BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize) BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
...@@ -145,8 +142,7 @@ BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize) ...@@ -145,8 +142,7 @@ BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize)
} }
void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) {
auto const globalEpoch = globalEpoch_.load(std::memory_order_relaxed); if (epoch_ != globalEpoch_) {
if (globalEpoch != epoch_) {
epoch_ = globalEpoch_; epoch_ = globalEpoch_;
ptr_ = buffer_.get() + bufferSize_; ptr_ = buffer_.get() + bufferSize_;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment