2 * Copyright 2015 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.
20 * @author Keith Adams <kma@fb.com>
21 * @author Jordan DeLong <delong.j@fb.com>
26 #include <type_traits>
32 #include <glog/logging.h>
33 #include <folly/detail/Sleeper.h>
34 #include <folly/Portability.h>
36 #if !FOLLY_X64 && !FOLLY_A64
37 # error "PicoSpinLock.h is currently x64 and aarch64 only."
43 * Spin lock on a single bit in an integral type. You can use this
44 * with 16, 32, or 64-bit integral types.
46 * This is useful if you want a small lock and already have an int
47 * with a bit in it that you aren't using. But note that it can't be
48 * as small as MicroSpinLock (1 byte), if you don't already have a
49 * convenient int with an unused bit lying around to put it on.
51 * To construct these, either use init() or zero initialize. We don't
52 * have a real constructor because we want this to be a POD type so we
53 * can put it into packed structs.
55 template<class IntType, int Bit = sizeof(IntType) * 8 - 1>
57 // Internally we deal with the unsigned version of the type.
58 typedef typename std::make_unsigned<IntType>::type UIntType;
60 static_assert(std::is_integral<IntType>::value,
61 "PicoSpinLock needs an integral type");
62 static_assert(sizeof(IntType) == 2 || sizeof(IntType) == 4 ||
64 "PicoSpinLock can't work on integers smaller than 2 bytes");
67 static const UIntType kLockBitMask_ = UIntType(1) << Bit;
71 * You must call this function before using this class, if you
72 * default constructed it. If you zero-initialized it you can
73 * assume the PicoSpinLock is in a valid unlocked state with
76 * (This doesn't use a constructor because we want to be a POD.)
78 void init(IntType initialValue = 0) {
79 CHECK(!(initialValue & kLockBitMask_));
84 * Returns the value of the integer we using for our lock, except
85 * with the bit we are using as a lock cleared, regardless of
86 * whether the lock is held.
88 * It is 'safe' to call this without holding the lock. (As in: you
89 * get the same guarantees for simultaneous accesses to an integer
90 * as you normally get.)
92 IntType getData() const {
93 return static_cast<IntType>(lock_ & ~kLockBitMask_);
97 * Set the value of the other bits in our integer.
99 * Don't use this when you aren't holding the lock, unless it can be
100 * guaranteed that no other threads may be trying to use this.
102 void setData(IntType w) {
103 CHECK(!(w & kLockBitMask_));
104 lock_ = (lock_ & kLockBitMask_) | w;
108 * Try to get the lock without blocking: returns whether or not we
111 bool try_lock() const {
115 #define FB_DOBTS(size) \
116 asm volatile("lock; bts" #size " %1, (%2); setnc %0" \
122 switch (sizeof(IntType)) {
123 case 2: FB_DOBTS(w); break;
124 case 4: FB_DOBTS(l); break;
125 case 8: FB_DOBTS(q); break;
130 ret = __atomic_fetch_or(&lock_, 1 << Bit, __ATOMIC_SEQ_CST);
132 #error "x86 aarch64 only"
139 * Block until we can acquire the lock. Uses Sleeper to wait.
142 detail::Sleeper sleeper;
143 while (!try_lock()) {
149 * Release the lock, without changing the value of the rest of the
152 void unlock() const {
154 #define FB_DOBTR(size) \
155 asm volatile("lock; btr" #size " %0, (%1)" \
162 // Reads and writes can not be reordered wrt locked instructions,
163 // so we don't need a memory fence here.
164 switch (sizeof(IntType)) {
165 case 2: FB_DOBTR(w); break;
166 case 4: FB_DOBTR(l); break;
167 case 8: FB_DOBTR(q); break;
172 __atomic_fetch_and(&lock_, ~(1 << Bit), __ATOMIC_SEQ_CST);
174 # error "x64 aarch64 only"