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.
19 * This class provides a few spin lock implementations, depending on the
20 * platform. folly/SpinLock.h will select one of these as the folly::SpinLock
23 * The main reason we keep these separated out here is so that we can run unit
24 * tests for all supported spin lock implementations, even though only one will
25 * be selected as the actual folly::SpinLock implemenatation for any given
29 #include <boost/noncopyable.hpp>
30 #include <folly/Portability.h>
33 #include <folly/SmallLocks.h>
37 class SpinLockMslImpl {
39 FOLLY_ALWAYS_INLINE SpinLockMslImpl() {
42 FOLLY_ALWAYS_INLINE void lock() const {
45 FOLLY_ALWAYS_INLINE void unlock() const {
48 FOLLY_ALWAYS_INLINE bool trylock() const {
49 return lock_.try_lock();
52 mutable folly::MicroSpinLock lock_;
60 #include <libkern/OSAtomic.h>
64 class SpinLockAppleImpl {
66 FOLLY_ALWAYS_INLINE SpinLockAppleImpl() : lock_(0) {}
67 FOLLY_ALWAYS_INLINE void lock() const {
68 OSSpinLockLock(&lock_);
70 FOLLY_ALWAYS_INLINE void unlock() const {
71 OSSpinLockUnlock(&lock_);
73 FOLLY_ALWAYS_INLINE bool trylock() const {
74 return OSSpinLockTry(&lock_);
77 mutable OSSpinLock lock_;
85 #include <folly/Exception.h>
87 #if FOLLY_HAVE_PTHREAD_SPINLOCK_T
89 // Apple and Android systems don't have pthread_spinlock_t, so we can't support
90 // this version on those platforms.
93 class SpinLockPthreadImpl {
95 FOLLY_ALWAYS_INLINE SpinLockPthreadImpl() {
96 int rc = pthread_spin_init(&lock_, PTHREAD_PROCESS_PRIVATE);
97 checkPosixError(rc, "failed to initialize spinlock");
99 FOLLY_ALWAYS_INLINE ~SpinLockPthreadImpl() {
100 pthread_spin_destroy(&lock_);
103 int rc = pthread_spin_lock(&lock_);
104 checkPosixError(rc, "error locking spinlock");
106 FOLLY_ALWAYS_INLINE void unlock() const {
107 int rc = pthread_spin_unlock(&lock_);
108 checkPosixError(rc, "error unlocking spinlock");
110 FOLLY_ALWAYS_INLINE bool trylock() const {
111 int rc = pthread_spin_trylock(&lock_);
114 } else if (rc == EBUSY) {
117 throwSystemErrorExplicit(rc, "spinlock trylock error");
120 mutable pthread_spinlock_t lock_;
125 #endif // FOLLY_HAVE_PTHREAD_SPINLOCK_T
129 class SpinLockPthreadMutexImpl {
131 FOLLY_ALWAYS_INLINE SpinLockPthreadMutexImpl() {
132 int rc = pthread_mutex_init(&lock_, nullptr);
133 checkPosixError(rc, "failed to initialize mutex");
135 FOLLY_ALWAYS_INLINE ~SpinLockPthreadMutexImpl() {
136 pthread_mutex_destroy(&lock_);
139 int rc = pthread_mutex_lock(&lock_);
140 checkPosixError(rc, "error locking mutex");
142 FOLLY_ALWAYS_INLINE void unlock() const {
143 int rc = pthread_mutex_unlock(&lock_);
144 checkPosixError(rc, "error unlocking mutex");
146 FOLLY_ALWAYS_INLINE bool trylock() const {
147 int rc = pthread_mutex_trylock(&lock_);
150 } else if (rc == EBUSY) {
153 throwSystemErrorExplicit(rc, "mutex trylock error");
156 mutable pthread_mutex_t lock_;