From fc0cd8934ac2fdbd0c0be69d7804283812012184 Mon Sep 17 00:00:00 2001 From: Sean Cannella Date: Fri, 17 Oct 2014 13:38:30 -0700 Subject: [PATCH] Support NotificationQueue on iOS/Android Summary: This is the last of the changes to make folly and thrift portable. The old thrift SpinLock implementation is exactly we want here so port this into folly. If someone later makes efficeint versions of MicroSpinLock for platforms other than x64 they can easily be plugged in without changing the calling code. Test Plan: compiled on multiple platforms Reviewed By: alandau@fb.com Subscribers: folly-diffs@, ldbrandy, trunkagent, alandau, bmatheny, njormrod, mshneer, shikong, benyluo, kmdent, fma, ranjeeth, subodh FB internal diff: D1621717 Tasks: 5183325 --- folly/io/PortableSpinLock.h | 120 +++++++++++++++++++++++++++++ folly/io/async/NotificationQueue.h | 40 ++++++---- 2 files changed, 145 insertions(+), 15 deletions(-) create mode 100644 folly/io/PortableSpinLock.h diff --git a/folly/io/PortableSpinLock.h b/folly/io/PortableSpinLock.h new file mode 100644 index 00000000..619c202b --- /dev/null +++ b/folly/io/PortableSpinLock.h @@ -0,0 +1,120 @@ +/* + * Copyright 2014 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 + +#include + +// This is a wrapper SpinLock implementation that works around the +// x64 limitation of the base folly MicroSpinLock. If that is available, this +// simply thinly wraps it. Otherwise, it uses the simplest analog available on +// iOS (or 32-bit Mac) or, failing that, POSIX (on Android et. al.) + +#if __x86_64__ +#include + +namespace folly { namespace io { + +class PortableSpinLock { + public: + FOLLY_ALWAYS_INLINE PortableSpinLock() { + lock_.init(); + } + FOLLY_ALWAYS_INLINE void lock() const { + lock_.lock(); + } + FOLLY_ALWAYS_INLINE void unlock() const { + lock_.unlock(); + } + FOLLY_ALWAYS_INLINE bool trylock() const { + return lock_.try_lock(); + } + private: + mutable folly::MicroSpinLock lock_; +}; + +}} + +#elif __APPLE__ +#include + +namespace folly { namespace io { + +class PortableSpinLock { + public: + FOLLY_ALWAYS_INLINE PortableSpinLock() : lock_(0) {} + FOLLY_ALWAYS_INLINE void lock() const { + OSSpinLockLock(&lock_); + } + FOLLY_ALWAYS_INLINE void unlock() const { + OSSpinLockUnlock(&lock_); + } + FOLLY_ALWAYS_INLINE bool trylock() const { + return OSSpinLockTry(&lock_); + } + private: + mutable OSSpinLock lock_; +}; + +}} + +#else +#include + +namespace folly { namespace io { + +class PortableSpinLock { + public: + FOLLY_ALWAYS_INLINE PortableSpinLock() { + pthread_mutex_init(&lock_, nullptr); + } + void lock() const { + int rc = pthread_mutex_lock(&lock_); + CHECK_EQ(0, rc); + } + FOLLY_ALWAYS_INLINE void unlock() const { + int rc = pthread_mutex_unlock(&lock_); + CHECK_EQ(0, rc); + } + FOLLY_ALWAYS_INLINE bool trylock() const { + int rc = pthread_mutex_trylock(&lock_); + CHECK_GE(rc, 0); + return rc == 0; + } + private: + mutable pthread_mutex_t lock_; +}; + +}} + +#endif + +namespace folly { namespace io { + +class PortableSpinLockGuard : private boost::noncopyable { + public: + FOLLY_ALWAYS_INLINE explicit PortableSpinLockGuard(PortableSpinLock& lock) : + lock_(lock) { + lock_.lock(); + } + FOLLY_ALWAYS_INLINE ~PortableSpinLockGuard() { + lock_.unlock(); + } + private: + PortableSpinLock& lock_; +}; + +}} diff --git a/folly/io/async/NotificationQueue.h b/folly/io/async/NotificationQueue.h index c06f3e84..f817fbdc 100644 --- a/folly/io/async/NotificationQueue.h +++ b/folly/io/async/NotificationQueue.h @@ -19,17 +19,21 @@ #include #include +#include #include -#include #include #include #include -#include #include #include #include +#if __linux__ && !__ANDROID__ +#define FOLLY_HAVE_EVENTFD +#include +#endif + namespace folly { /** @@ -169,8 +173,10 @@ class NotificationQueue { }; enum class FdType { + PIPE, +#ifdef FOLLY_HAVE_EVENTFD EVENTFD, - PIPE +#endif }; /** @@ -189,17 +195,20 @@ class NotificationQueue { * mostly for testing purposes. */ explicit NotificationQueue(uint32_t maxSize = 0, - FdType fdType = FdType::EVENTFD) +#ifdef FOLLY_HAVE_EVENTFD + FdType fdType = FdType::EVENTFD) +#else + FdType fdType = FdType::PIPE) +#endif : eventfd_(-1), pipeFds_{-1, -1}, advisoryMaxQueueSize_(maxSize), pid_(getpid()), queue_() { - spinlock_.init(); - RequestContext::getStaticContext(); +#ifdef FOLLY_HAVE_EVENTFD if (fdType == FdType::EVENTFD) { eventfd_ = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE); if (eventfd_ == -1) { @@ -216,6 +225,7 @@ class NotificationQueue { } } } +#endif if (fdType == FdType::PIPE) { if (pipe(pipeFds_)) { folly::throwSystemError("Failed to create pipe for NotificationQueue", @@ -342,7 +352,7 @@ class NotificationQueue { try { - folly::MSLGuard g(spinlock_); + folly::io::PortableSpinLockGuard g(spinlock_); if (UNLIKELY(queue_.empty())) { return false; @@ -366,7 +376,7 @@ class NotificationQueue { } int size() { - folly::MSLGuard g(spinlock_); + folly::io::PortableSpinLockGuard g(spinlock_); return queue_.size(); } @@ -393,7 +403,7 @@ class NotificationQueue { NotificationQueue& operator=(NotificationQueue const &) = delete; inline bool checkQueueSize(size_t maxSize, bool throws=true) const { - DCHECK(0 == spinlock_.try_lock()); + DCHECK(0 == spinlock_.trylock()); if (maxSize > 0 && queue_.size() >= maxSize) { if (throws) { throw std::overflow_error("unable to add message to NotificationQueue: " @@ -461,7 +471,7 @@ class NotificationQueue { checkPid(); bool signal = false; { - folly::MSLGuard g(spinlock_); + folly::io::PortableSpinLockGuard g(spinlock_); if (!checkQueueSize(maxSize, throws)) { return false; } @@ -485,7 +495,7 @@ class NotificationQueue { checkPid(); bool signal = false; { - folly::MSLGuard g(spinlock_); + folly::io::PortableSpinLockGuard g(spinlock_); if (!checkQueueSize(maxSize, throws)) { return false; } @@ -507,7 +517,7 @@ class NotificationQueue { bool signal = false; size_t numAdded = 0; { - folly::MSLGuard g(spinlock_); + folly::io::PortableSpinLockGuard g(spinlock_); while (first != last) { queue_.push_back(std::make_pair(*first, RequestContext::saveContext())); ++first; @@ -522,7 +532,7 @@ class NotificationQueue { } } - mutable folly::MicroSpinLock spinlock_; + mutable folly::io::PortableSpinLock spinlock_; int eventfd_; int pipeFds_[2]; // to fallback to on older/non-linux systems uint32_t advisoryMaxQueueSize_; @@ -675,7 +685,7 @@ void NotificationQueue::Consumer::init( queue_ = queue; { - folly::MSLGuard g(queue_->spinlock_); + folly::io::PortableSpinLockGuard g(queue_->spinlock_); queue_->numConsumers_++; } queue_->signalEvent(); @@ -695,7 +705,7 @@ void NotificationQueue::Consumer::stopConsuming() { } { - folly::MSLGuard g(queue_->spinlock_); + folly::io::PortableSpinLockGuard g(queue_->spinlock_); queue_->numConsumers_--; setActive(false); } -- 2.34.1