Commit 6226a991 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

Remove the old SpinLock implementations

Summary: They aren't actually needed as the primary implementation is supported on all platforms.

Reviewed By: yfeldblum

Differential Revision: D4882687

fbshipit-source-id: 7208c5d3c1f35b29b0cabb6a20fe030fbf10b131
parent 6c4a55d0
...@@ -76,7 +76,6 @@ nobase_follyinclude_HEADERS = \ ...@@ -76,7 +76,6 @@ nobase_follyinclude_HEADERS = \
detail/Sleeper.h \ detail/Sleeper.h \
detail/SlowFingerprint.h \ detail/SlowFingerprint.h \
detail/SocketFastOpen.h \ detail/SocketFastOpen.h \
detail/SpinLockImpl.h \
detail/StaticSingletonManager.h \ detail/StaticSingletonManager.h \
detail/Stats.h \ detail/Stats.h \
detail/ThreadLocalDetail.h \ detail/ThreadLocalDetail.h \
......
...@@ -34,19 +34,29 @@ ...@@ -34,19 +34,29 @@
#include <type_traits> #include <type_traits>
#include <folly/detail/SpinLockImpl.h> #include <folly/Portability.h>
#include <folly/SmallLocks.h>
namespace folly { namespace folly {
#if __x86_64__ class SpinLock {
typedef SpinLockMslImpl SpinLock; public:
#elif __APPLE__ FOLLY_ALWAYS_INLINE SpinLock() {
typedef SpinLockAppleImpl SpinLock; lock_.init();
#elif FOLLY_HAVE_PTHREAD_SPINLOCK_T }
typedef SpinLockPthreadImpl SpinLock; FOLLY_ALWAYS_INLINE void lock() const {
#else lock_.lock();
typedef SpinLockPthreadMutexImpl SpinLock; }
#endif FOLLY_ALWAYS_INLINE void unlock() const {
lock_.unlock();
}
FOLLY_ALWAYS_INLINE bool try_lock() const {
return lock_.try_lock();
}
private:
mutable folly::MicroSpinLock lock_;
};
template <typename LOCK> template <typename LOCK>
class SpinLockGuardImpl : private boost::noncopyable { class SpinLockGuardImpl : private boost::noncopyable {
......
/*
* Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
/*
* This class provides a few spin lock implementations, depending on the
* platform. folly/SpinLock.h will select one of these as the folly::SpinLock
* implementation.
*
* The main reason we keep these separated out here is so that we can run unit
* tests for all supported spin lock implementations, even though only one will
* be selected as the actual folly::SpinLock implemenatation for any given
* platform.
*/
#include <boost/noncopyable.hpp>
#include <folly/Portability.h>
#if __x86_64__
#include <folly/SmallLocks.h>
namespace folly {
class SpinLockMslImpl {
public:
FOLLY_ALWAYS_INLINE SpinLockMslImpl() {
lock_.init();
}
FOLLY_ALWAYS_INLINE void lock() const {
lock_.lock();
}
FOLLY_ALWAYS_INLINE void unlock() const {
lock_.unlock();
}
FOLLY_ALWAYS_INLINE bool try_lock() const {
return lock_.try_lock();
}
private:
mutable folly::MicroSpinLock lock_;
};
}
#endif // __x86_64__
#if __APPLE__
#include <libkern/OSAtomic.h>
namespace folly {
class SpinLockAppleImpl {
public:
FOLLY_ALWAYS_INLINE SpinLockAppleImpl() : lock_(0) {}
FOLLY_ALWAYS_INLINE void lock() const {
OSSpinLockLock(&lock_);
}
FOLLY_ALWAYS_INLINE void unlock() const {
OSSpinLockUnlock(&lock_);
}
FOLLY_ALWAYS_INLINE bool try_lock() const {
return OSSpinLockTry(&lock_);
}
private:
mutable OSSpinLock lock_;
};
}
#endif // __APPLE__
#include <pthread.h>
#include <folly/Exception.h>
#if FOLLY_HAVE_PTHREAD_SPINLOCK_T
// Apple and Android systems don't have pthread_spinlock_t, so we can't support
// this version on those platforms.
namespace folly {
class SpinLockPthreadImpl {
public:
FOLLY_ALWAYS_INLINE SpinLockPthreadImpl() {
int rc = pthread_spin_init(&lock_, PTHREAD_PROCESS_PRIVATE);
checkPosixError(rc, "failed to initialize spinlock");
}
FOLLY_ALWAYS_INLINE ~SpinLockPthreadImpl() {
pthread_spin_destroy(&lock_);
}
void lock() const {
int rc = pthread_spin_lock(&lock_);
checkPosixError(rc, "error locking spinlock");
}
FOLLY_ALWAYS_INLINE void unlock() const {
int rc = pthread_spin_unlock(&lock_);
checkPosixError(rc, "error unlocking spinlock");
}
FOLLY_ALWAYS_INLINE bool try_lock() const {
int rc = pthread_spin_trylock(&lock_);
if (rc == 0) {
return true;
} else if (rc == EBUSY) {
return false;
}
throwSystemErrorExplicit(rc, "spinlock trylock error");
}
private:
mutable pthread_spinlock_t lock_;
};
}
#endif // FOLLY_HAVE_PTHREAD_SPINLOCK_T
namespace folly {
class SpinLockPthreadMutexImpl {
public:
FOLLY_ALWAYS_INLINE SpinLockPthreadMutexImpl() {
int rc = pthread_mutex_init(&lock_, nullptr);
checkPosixError(rc, "failed to initialize mutex");
}
FOLLY_ALWAYS_INLINE ~SpinLockPthreadMutexImpl() {
pthread_mutex_destroy(&lock_);
}
void lock() const {
int rc = pthread_mutex_lock(&lock_);
checkPosixError(rc, "error locking mutex");
}
FOLLY_ALWAYS_INLINE void unlock() const {
int rc = pthread_mutex_unlock(&lock_);
checkPosixError(rc, "error unlocking mutex");
}
FOLLY_ALWAYS_INLINE bool try_lock() const {
int rc = pthread_mutex_trylock(&lock_);
if (rc == 0) {
return true;
} else if (rc == EBUSY) {
return false;
}
throwSystemErrorExplicit(rc, "mutex trylock error");
}
private:
mutable pthread_mutex_t lock_;
};
}
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <stdexcept> #include <stdexcept>
#include <utility> #include <utility>
#include <folly/Exception.h>
#include <folly/FileUtil.h> #include <folly/FileUtil.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <folly/io/async/EventHandler.h> #include <folly/io/async/EventHandler.h>
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <folly/SpinLock.h> #include <folly/SpinLock.h>
#include <folly/Random.h> #include <folly/Random.h>
...@@ -132,36 +133,9 @@ void trylockTest() { ...@@ -132,36 +133,9 @@ void trylockTest() {
} // unnamed namespace } // unnamed namespace
#if __x86_64__ TEST(SpinLock, Correctness) {
TEST(SpinLock, MslCorrectness) { correctnessTest<folly::SpinLock>();
correctnessTest<folly::SpinLockMslImpl>();
}
TEST(SpinLock, MslTryLock) {
trylockTest<folly::SpinLockMslImpl>();
}
#endif
#if __APPLE__
TEST(SpinLock, AppleCorrectness) {
correctnessTest<folly::SpinLockAppleImpl>();
}
TEST(SpinLock, AppleTryLock) {
trylockTest<folly::SpinLockAppleImpl>();
}
#endif
#if FOLLY_HAVE_PTHREAD_SPINLOCK_T
TEST(SpinLock, PthreadCorrectness) {
correctnessTest<folly::SpinLockPthreadImpl>();
}
TEST(SpinLock, PthreadTryLock) {
trylockTest<folly::SpinLockPthreadImpl>();
}
#endif
TEST(SpinLock, MutexCorrectness) {
correctnessTest<folly::SpinLockPthreadMutexImpl>();
} }
TEST(SpinLock, MutexTryLock) { TEST(SpinLock, TryLock) {
trylockTest<folly::SpinLockPthreadMutexImpl>(); trylockTest<folly::SpinLock>();
} }
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