Commit 7426f02a authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

remove folly::SpinLockGuard

Summary:
C++17 permits use of `std::unique_lock` instead of `folly::SpinLockGuard`; just use that.

C++11 permits use of `std::unique_lock<SpinLock>`.

Differential Revision: D27390136

fbshipit-source-id: bcc34a043644289523ddfdfbf47a260c133db186
parent e4fb76b6
...@@ -52,24 +52,4 @@ class SpinLock { ...@@ -52,24 +52,4 @@ class SpinLock {
mutable folly::MicroSpinLock lock_; mutable folly::MicroSpinLock lock_;
}; };
template <typename LOCK>
class SpinLockGuardImpl {
public:
FOLLY_ALWAYS_INLINE explicit SpinLockGuardImpl(LOCK& lock) noexcept(
noexcept(lock.lock()))
: lock_(lock) {
lock_.lock();
}
SpinLockGuardImpl(const SpinLockGuardImpl&) = delete;
SpinLockGuardImpl& operator=(const SpinLockGuardImpl&) = delete;
FOLLY_ALWAYS_INLINE ~SpinLockGuardImpl() { lock_.unlock(); }
private:
LOCK& lock_;
};
typedef SpinLockGuardImpl<SpinLock> SpinLockGuard;
} // namespace folly } // namespace folly
...@@ -561,7 +561,7 @@ void AsyncSSLSocket::attachSSLContext(const std::shared_ptr<SSLContext>& ctx) { ...@@ -561,7 +561,7 @@ void AsyncSSLSocket::attachSSLContext(const std::shared_ptr<SSLContext>& ctx) {
OpenSSLUtils::setSSLInitialCtx(ssl_.get(), sslCtx); OpenSSLUtils::setSSLInitialCtx(ssl_.get(), sslCtx);
// Detach sets the socket's context to the dummy context. Thus we must acquire // Detach sets the socket's context to the dummy context. Thus we must acquire
// this lock. // this lock.
SpinLockGuard guard(dummyCtxLock); std::unique_lock<SpinLock> guard(dummyCtxLock);
SSL_set_SSL_CTX(ssl_.get(), sslCtx); SSL_set_SSL_CTX(ssl_.get(), sslCtx);
} }
...@@ -587,7 +587,7 @@ void AsyncSSLSocket::detachSSLContext() { ...@@ -587,7 +587,7 @@ void AsyncSSLSocket::detachSSLContext() {
OpenSSLUtils::setSSLInitialCtx(ssl_.get(), nullptr); OpenSSLUtils::setSSLInitialCtx(ssl_.get(), nullptr);
} }
SpinLockGuard guard(dummyCtxLock); std::unique_lock<SpinLock> guard(dummyCtxLock);
if (nullptr == dummyCtx) { if (nullptr == dummyCtx) {
// We need to lazily initialize the dummy context so we don't // We need to lazily initialize the dummy context so we don't
// accidentally override any programmatic settings to openssl // accidentally override any programmatic settings to openssl
......
...@@ -423,7 +423,7 @@ class NotificationQueue { ...@@ -423,7 +423,7 @@ class NotificationQueue {
std::unique_ptr<Node> data; std::unique_ptr<Node> data;
{ {
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
if (UNLIKELY(queue_.empty())) { if (UNLIKELY(queue_.empty())) {
return false; return false;
...@@ -440,7 +440,7 @@ class NotificationQueue { ...@@ -440,7 +440,7 @@ class NotificationQueue {
} }
size_t size() const { size_t size() const {
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
return queue_.size(); return queue_.size();
} }
...@@ -548,12 +548,12 @@ class NotificationQueue { ...@@ -548,12 +548,12 @@ class NotificationQueue {
} }
void ensureSignal() const { void ensureSignal() const {
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
ensureSignalLocked(); ensureSignalLocked();
} }
void syncSignalAndQueue() { void syncSignalAndQueue() {
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
if (queue_.empty()) { if (queue_.empty()) {
drainSignalsLocked(); drainSignalsLocked();
...@@ -569,7 +569,7 @@ class NotificationQueue { ...@@ -569,7 +569,7 @@ class NotificationQueue {
{ {
auto data = std::make_unique<Node>( auto data = std::make_unique<Node>(
std::forward<MessageTT>(message), RequestContext::saveContext()); std::forward<MessageTT>(message), RequestContext::saveContext());
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
if (checkDraining(throws) || !checkQueueSize(maxSize, throws)) { if (checkDraining(throws) || !checkQueueSize(maxSize, throws)) {
return false; return false;
} }
...@@ -599,7 +599,7 @@ class NotificationQueue { ...@@ -599,7 +599,7 @@ class NotificationQueue {
q.push_back(*data.release()); q.push_back(*data.release());
++first; ++first;
} }
folly::SpinLockGuard g(spinlock_); std::unique_lock<SpinLock> g(spinlock_);
checkDraining(); checkDraining();
queue_.splice(queue_.end(), q); queue_.splice(queue_.end(), q);
if (numActiveConsumers_ < numConsumers_) { if (numActiveConsumers_ < numConsumers_) {
...@@ -781,7 +781,7 @@ void NotificationQueue<MessageT>::Consumer::init( ...@@ -781,7 +781,7 @@ void NotificationQueue<MessageT>::Consumer::init(
queue_ = queue; queue_ = queue;
{ {
folly::SpinLockGuard g(queue_->spinlock_); std::unique_lock<SpinLock> g(queue_->spinlock_);
queue_->numConsumers_++; queue_->numConsumers_++;
} }
queue_->ensureSignal(); queue_->ensureSignal();
...@@ -801,7 +801,7 @@ void NotificationQueue<MessageT>::Consumer::stopConsuming() { ...@@ -801,7 +801,7 @@ void NotificationQueue<MessageT>::Consumer::stopConsuming() {
} }
{ {
folly::SpinLockGuard g(queue_->spinlock_); std::unique_lock<SpinLock> g(queue_->spinlock_);
queue_->numConsumers_--; queue_->numConsumers_--;
setActive(false); setActive(false);
} }
...@@ -817,7 +817,7 @@ bool NotificationQueue<MessageT>::Consumer::consumeUntilDrained( ...@@ -817,7 +817,7 @@ bool NotificationQueue<MessageT>::Consumer::consumeUntilDrained(
size_t* numConsumed) noexcept { size_t* numConsumed) noexcept {
DestructorGuard dg(this); DestructorGuard dg(this);
{ {
folly::SpinLockGuard g(queue_->spinlock_); std::unique_lock<SpinLock> g(queue_->spinlock_);
if (queue_->draining_) { if (queue_->draining_) {
return false; return false;
} }
...@@ -825,7 +825,7 @@ bool NotificationQueue<MessageT>::Consumer::consumeUntilDrained( ...@@ -825,7 +825,7 @@ bool NotificationQueue<MessageT>::Consumer::consumeUntilDrained(
} }
consumeMessages(true, numConsumed); consumeMessages(true, numConsumed);
{ {
folly::SpinLockGuard g(queue_->spinlock_); std::unique_lock<SpinLock> g(queue_->spinlock_);
queue_->draining_ = false; queue_->draining_ = false;
} }
return true; return true;
...@@ -840,7 +840,7 @@ void NotificationQueue<MessageT>::SimpleConsumer::consume(F&& foreach) { ...@@ -840,7 +840,7 @@ void NotificationQueue<MessageT>::SimpleConsumer::consume(F&& foreach) {
std::unique_ptr<Node> data; std::unique_ptr<Node> data;
{ {
folly::SpinLockGuard g(queue_.spinlock_); std::unique_lock<SpinLock> g(queue_.spinlock_);
if (UNLIKELY(queue_.queue_.empty())) { if (UNLIKELY(queue_.queue_.empty())) {
return; return;
......
...@@ -40,7 +40,7 @@ DigestT DigestBuilder<DigestT>::build() { ...@@ -40,7 +40,7 @@ DigestT DigestBuilder<DigestT>::build() {
digestPtrs.reserve(cpuLocalBuffers_.size()); digestPtrs.reserve(cpuLocalBuffers_.size());
for (auto& cpuLocalBuffer : cpuLocalBuffers_) { for (auto& cpuLocalBuffer : cpuLocalBuffers_) {
SpinLockGuard g(cpuLocalBuffer.mutex); std::unique_lock<SpinLock> g(cpuLocalBuffer.mutex);
valuesVec.push_back(std::move(cpuLocalBuffer.buffer)); valuesVec.push_back(std::move(cpuLocalBuffer.buffer));
if (cpuLocalBuffer.digest) { if (cpuLocalBuffer.digest) {
digestPtrs.push_back(std::move(cpuLocalBuffer.digest)); digestPtrs.push_back(std::move(cpuLocalBuffer.digest));
......
...@@ -23,8 +23,6 @@ ...@@ -23,8 +23,6 @@
#include <folly/portability/GMock.h> #include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
using folly::SpinLockGuardImpl;
namespace { namespace {
template <typename LOCK> template <typename LOCK>
...@@ -41,7 +39,7 @@ void spinlockTestThread(LockedVal<LOCK>* v) { ...@@ -41,7 +39,7 @@ void spinlockTestThread(LockedVal<LOCK>* v) {
auto rng = folly::ThreadLocalPRNG(); auto rng = folly::ThreadLocalPRNG();
for (int i = 0; i < max; i++) { for (int i = 0; i < max; i++) {
folly::asm_volatile_pause(); folly::asm_volatile_pause();
SpinLockGuardImpl<LOCK> g(v->lock); std::unique_lock g(v->lock);
EXPECT_THAT(v->ar, testing::Each(testing::Eq(v->ar[0]))); EXPECT_THAT(v->ar, testing::Each(testing::Eq(v->ar[0])));
...@@ -64,7 +62,7 @@ void trylockTestThread(TryLockState<LOCK>* state, size_t count) { ...@@ -64,7 +62,7 @@ void trylockTestThread(TryLockState<LOCK>* state, size_t count) {
while (true) { while (true) {
folly::asm_volatile_pause(); folly::asm_volatile_pause();
bool ret = state->lock2.try_lock(); bool ret = state->lock2.try_lock();
SpinLockGuardImpl<LOCK> g(state->lock1); std::unique_lock g(state->lock1);
if (state->obtained >= count) { if (state->obtained >= count) {
if (ret) { if (ret) {
state->lock2.unlock(); state->lock2.unlock();
......
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