From 6226a99184a7b700f3038cf3d65879da8c2f83d3 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Fri, 14 Apr 2017 11:14:45 -0700 Subject: [PATCH] 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 --- folly/Makefile.am | 1 - folly/SpinLock.h | 30 ++++-- folly/detail/SpinLockImpl.h | 159 ----------------------------- folly/io/async/NotificationQueue.h | 1 + folly/test/SpinLockTest.cpp | 36 +------ 5 files changed, 26 insertions(+), 201 deletions(-) delete mode 100644 folly/detail/SpinLockImpl.h diff --git a/folly/Makefile.am b/folly/Makefile.am index 0e6467b2..b2dbcfaf 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -76,7 +76,6 @@ nobase_follyinclude_HEADERS = \ detail/Sleeper.h \ detail/SlowFingerprint.h \ detail/SocketFastOpen.h \ - detail/SpinLockImpl.h \ detail/StaticSingletonManager.h \ detail/Stats.h \ detail/ThreadLocalDetail.h \ diff --git a/folly/SpinLock.h b/folly/SpinLock.h index ef35a485..b87e58a6 100644 --- a/folly/SpinLock.h +++ b/folly/SpinLock.h @@ -34,19 +34,29 @@ #include -#include +#include +#include namespace folly { -#if __x86_64__ -typedef SpinLockMslImpl SpinLock; -#elif __APPLE__ -typedef SpinLockAppleImpl SpinLock; -#elif FOLLY_HAVE_PTHREAD_SPINLOCK_T -typedef SpinLockPthreadImpl SpinLock; -#else -typedef SpinLockPthreadMutexImpl SpinLock; -#endif +class SpinLock { + public: + FOLLY_ALWAYS_INLINE SpinLock() { + 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_; +}; template class SpinLockGuardImpl : private boost::noncopyable { diff --git a/folly/detail/SpinLockImpl.h b/folly/detail/SpinLockImpl.h deleted file mode 100644 index 22ba7535..00000000 --- a/folly/detail/SpinLockImpl.h +++ /dev/null @@ -1,159 +0,0 @@ -/* - * 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 -#include - -#if __x86_64__ -#include - -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 - -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 -#include - -#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_; -}; - -} diff --git a/folly/io/async/NotificationQueue.h b/folly/io/async/NotificationQueue.h index 28a2878e..3018a5d9 100644 --- a/folly/io/async/NotificationQueue.h +++ b/folly/io/async/NotificationQueue.h @@ -25,6 +25,7 @@ #include #include +#include #include #include #include diff --git a/folly/test/SpinLockTest.cpp b/folly/test/SpinLockTest.cpp index 029e6625..b2387ea7 100644 --- a/folly/test/SpinLockTest.cpp +++ b/folly/test/SpinLockTest.cpp @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include @@ -132,36 +133,9 @@ void trylockTest() { } // unnamed namespace -#if __x86_64__ -TEST(SpinLock, MslCorrectness) { - correctnessTest(); -} -TEST(SpinLock, MslTryLock) { - trylockTest(); -} -#endif - -#if __APPLE__ -TEST(SpinLock, AppleCorrectness) { - correctnessTest(); -} -TEST(SpinLock, AppleTryLock) { - trylockTest(); -} -#endif - -#if FOLLY_HAVE_PTHREAD_SPINLOCK_T -TEST(SpinLock, PthreadCorrectness) { - correctnessTest(); -} -TEST(SpinLock, PthreadTryLock) { - trylockTest(); -} -#endif - -TEST(SpinLock, MutexCorrectness) { - correctnessTest(); +TEST(SpinLock, Correctness) { + correctnessTest(); } -TEST(SpinLock, MutexTryLock) { - trylockTest(); +TEST(SpinLock, TryLock) { + trylockTest(); } -- 2.34.1