X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSpinLock.h;h=b87e58a6ed074415bd230dd21157f06dbb74078e;hb=c1a1a5e6039c13c55597fa942ce1c01ecfe2f6a3;hp=76cab06a5e608613bae8e0758895675d204afe21;hpb=275ca94d04e44f28cfa411668eb1c1dd8db90b80;p=folly.git diff --git a/folly/SpinLock.h b/folly/SpinLock.h index 76cab06a..b87e58a6 100644 --- a/folly/SpinLock.h +++ b/folly/SpinLock.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 Facebook, Inc. + * 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. @@ -14,21 +14,49 @@ * limitations under the License. */ +/* + * N.B. You most likely do _not_ want to use SpinLock or any other + * kind of spinlock. Use std::mutex instead. + * + * In short, spinlocks in preemptive multi-tasking operating systems + * have serious problems and fast mutexes like std::mutex are almost + * certainly the better choice, because letting the OS scheduler put a + * thread to sleep is better for system responsiveness and throughput + * than wasting a timeslice repeatedly querying a lock held by a + * thread that's blocked, and you can't prevent userspace + * programs blocking. + * + * Spinlocks in an operating system kernel make much more sense than + * they do in userspace. + */ + #pragma once -#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 {