2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * N.B. You most likely do _not_ want to use SpinLock or any other
19 * kind of spinlock. Use std::mutex instead.
21 * In short, spinlocks in preemptive multi-tasking operating systems
22 * have serious problems and fast mutexes like std::mutex are almost
23 * certainly the better choice, because letting the OS scheduler put a
24 * thread to sleep is better for system responsiveness and throughput
25 * than wasting a timeslice repeatedly querying a lock held by a
26 * thread that's blocked, and you can't prevent userspace
29 * Spinlocks in an operating system kernel make much more sense than
30 * they do in userspace.
35 #include <type_traits>
37 #include <folly/detail/SpinLockImpl.h>
42 typedef SpinLockMslImpl SpinLock;
44 typedef SpinLockAppleImpl SpinLock;
45 #elif FOLLY_HAVE_PTHREAD_SPINLOCK_T
46 typedef SpinLockPthreadImpl SpinLock;
48 typedef SpinLockPthreadMutexImpl SpinLock;
51 template <typename LOCK>
52 class SpinLockGuardImpl : private boost::noncopyable {
54 FOLLY_ALWAYS_INLINE explicit SpinLockGuardImpl(LOCK& lock) :
58 FOLLY_ALWAYS_INLINE ~SpinLockGuardImpl() {
65 typedef SpinLockGuardImpl<SpinLock> SpinLockGuard;