proxygen
PicoSpinLock.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 /*
18  * N.B. You most likely do _not_ want to use PicoSpinLock or any other
19  * kind of spinlock. Consider MicroLock instead.
20  *
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
27  * programs blocking.
28  *
29  * Spinlocks in an operating system kernel make much more sense than
30  * they do in userspace.
31  */
32 
33 #pragma once
34 #define FOLLY_PICO_SPIN_LOCK_H_
35 
36 /*
37  * @author Keith Adams <kma@fb.com>
38  * @author Jordan DeLong <delong.j@fb.com>
39  */
40 
41 #include <array>
42 #include <atomic>
43 #include <cinttypes>
44 #include <cstdlib>
45 #include <mutex>
46 #include <type_traits>
47 
48 #include <glog/logging.h>
49 
50 #include <folly/Portability.h>
52 
53 #if !FOLLY_X64 && !FOLLY_AARCH64 && !FOLLY_PPC64
54 #error "PicoSpinLock.h is currently x64, aarch64 and ppc64 only."
55 #endif
56 
57 namespace folly {
58 
59 /*
60  * Spin lock on a single bit in an integral type. You can use this
61  * with 16, 32, or 64-bit integral types.
62  *
63  * This is useful if you want a small lock and already have an int
64  * with a bit in it that you aren't using. But note that it can't be
65  * as small as MicroSpinLock (1 byte), if you don't already have a
66  * convenient int with an unused bit lying around to put it on.
67  *
68  * To construct these, either use init() or zero initialize. We don't
69  * have a real constructor because we want this to be a POD type so we
70  * can put it into packed structs.
71  */
72 template <class IntType, int Bit = sizeof(IntType) * 8 - 1>
73 struct PicoSpinLock {
74  // Internally we deal with the unsigned version of the type.
76 
77  static_assert(
79  "PicoSpinLock needs an integral type");
80  static_assert(
81  sizeof(IntType) == 2 || sizeof(IntType) == 4 || sizeof(IntType) == 8,
82  "PicoSpinLock can't work on integers smaller than 2 bytes");
83 
84  public:
85  static const UIntType kLockBitMask_ = UIntType(1) << Bit;
86  mutable UIntType lock_;
87 
88  /*
89  * You must call this function before using this class, if you
90  * default constructed it. If you zero-initialized it you can
91  * assume the PicoSpinLock is in a valid unlocked state with
92  * getData() == 0.
93  *
94  * (This doesn't use a constructor because we want to be a POD.)
95  */
96  void init(IntType initialValue = 0) {
97  CHECK(!(initialValue & kLockBitMask_));
98  reinterpret_cast<std::atomic<UIntType>*>(&lock_)->store(
99  UIntType(initialValue), std::memory_order_release);
100  }
101 
102  /*
103  * Returns the value of the integer we using for our lock, except
104  * with the bit we are using as a lock cleared, regardless of
105  * whether the lock is held.
106  *
107  * It is 'safe' to call this without holding the lock. (As in: you
108  * get the same guarantees for simultaneous accesses to an integer
109  * as you normally get.)
110  */
111  IntType getData() const {
112  auto res = reinterpret_cast<std::atomic<UIntType>*>(&lock_)->load(
113  std::memory_order_relaxed) &
114  ~kLockBitMask_;
115  return res;
116  }
117 
118  /*
119  * Set the value of the other bits in our integer.
120  *
121  * Don't use this when you aren't holding the lock, unless it can be
122  * guaranteed that no other threads may be trying to use this.
123  */
124  void setData(IntType w) {
125  CHECK(!(w & kLockBitMask_));
126  auto l = reinterpret_cast<std::atomic<UIntType>*>(&lock_);
127  l->store(
128  (l->load(std::memory_order_relaxed) & kLockBitMask_) | w,
129  std::memory_order_relaxed);
130  }
131 
132  /*
133  * Try to get the lock without blocking: returns whether or not we
134  * got it.
135  */
136  bool try_lock() const {
137  bool ret = false;
138 
139 #if defined(FOLLY_SANITIZE_THREAD)
140  // TODO: Might be able to fully move to std::atomic when gcc emits lock btr:
141  // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49244
142  ret =
143  !(reinterpret_cast<std::atomic<UIntType>*>(&lock_)->fetch_or(
144  kLockBitMask_, std::memory_order_acquire) &
145  kLockBitMask_);
146 #elif _MSC_VER
147  switch (sizeof(IntType)) {
148  case 2:
149  // There is no _interlockedbittestandset16 for some reason :(
150  ret = _InterlockedOr16((volatile short*)&lock_, (short)kLockBitMask_) &
151  kLockBitMask_;
152  break;
153  case 4:
154  ret = _interlockedbittestandset((volatile long*)&lock_, Bit);
155  break;
156  case 8:
157  ret = _interlockedbittestandset64((volatile long long*)&lock_, Bit);
158  break;
159  }
160 #elif FOLLY_X64
161 #define FB_DOBTS(size) \
162  asm volatile("lock; bts" #size " %1, (%2); setnc %0" \
163  : "=r"(ret) \
164  : "i"(Bit), "r"(&lock_) \
165  : "memory", "flags")
166 
167  switch (sizeof(IntType)) {
168  case 2:
169  FB_DOBTS(w);
170  break;
171  case 4:
172  FB_DOBTS(l);
173  break;
174  case 8:
175  FB_DOBTS(q);
176  break;
177  }
178 
179 #undef FB_DOBTS
180 #elif FOLLY_AARCH64
181  using SIntType = typename std::make_signed<UIntType>::type;
182  auto const lock = reinterpret_cast<SIntType*>(&lock_);
183  auto const mask = static_cast<SIntType>(kLockBitMask_);
184  return !(mask & __atomic_fetch_or(lock, mask, __ATOMIC_ACQUIRE));
185 #elif FOLLY_PPC64
186 #define FB_DOBTS(size) \
187  asm volatile( \
188  "\teieio\n" \
189  "\tl" #size \
190  "arx 14,0,%[lockPtr]\n" \
191  "\tli 15,1\n" \
192  "\tsldi 15,15,%[bit]\n" \
193  "\tand. 16,15,14\n" \
194  "\tbne 0f\n" \
195  "\tor 14,14,15\n" \
196  "\tst" #size \
197  "cx. 14,0,%[lockPtr]\n" \
198  "\tbne 0f\n" \
199  "\tori %[output],%[output],1\n" \
200  "\tisync\n" \
201  "0:\n" \
202  : [output] "+r"(ret) \
203  : [lockPtr] "r"(&lock_), [bit] "i"(Bit) \
204  : "cr0", "memory", "r14", "r15", "r16")
205 
206  switch (sizeof(IntType)) {
207  case 2:
208  FB_DOBTS(h);
209  break;
210  case 4:
211  FB_DOBTS(w);
212  break;
213  case 8:
214  FB_DOBTS(d);
215  break;
216  }
217 
218 #undef FB_DOBTS
219 #else
220 #error "x86 aarch64 ppc64 only"
221 #endif
222 
223  return ret;
224  }
225 
226  /*
227  * Block until we can acquire the lock. Uses Sleeper to wait.
228  */
229  void lock() const {
230  detail::Sleeper sleeper;
231  while (!try_lock()) {
232  sleeper.wait();
233  }
234  }
235 
236  /*
237  * Release the lock, without changing the value of the rest of the
238  * integer.
239  */
240  void unlock() const {
241 #ifdef _MSC_VER
242  switch (sizeof(IntType)) {
243  case 2:
244  // There is no _interlockedbittestandreset16 for some reason :(
245  _InterlockedAnd16((volatile short*)&lock_, (short)~kLockBitMask_);
246  break;
247  case 4:
248  _interlockedbittestandreset((volatile long*)&lock_, Bit);
249  break;
250  case 8:
251  _interlockedbittestandreset64((volatile long long*)&lock_, Bit);
252  break;
253  }
254 #elif FOLLY_X64
255 #define FB_DOBTR(size) \
256  asm volatile("lock; btr" #size " %0, (%1)" \
257  : \
258  : "i"(Bit), "r"(&lock_) \
259  : "memory", "flags")
260 
261  // Reads and writes can not be reordered wrt locked instructions,
262  // so we don't need a memory fence here.
263  switch (sizeof(IntType)) {
264  case 2:
265  FB_DOBTR(w);
266  break;
267  case 4:
268  FB_DOBTR(l);
269  break;
270  case 8:
271  FB_DOBTR(q);
272  break;
273  }
274 
275 #undef FB_DOBTR
276 #elif FOLLY_AARCH64
277  using SIntType = typename std::make_signed<UIntType>::type;
278  auto const lock = reinterpret_cast<SIntType*>(&lock_);
279  auto const mask = static_cast<SIntType>(kLockBitMask_);
280  __atomic_fetch_and(lock, ~mask, __ATOMIC_RELEASE);
281 #elif FOLLY_PPC64
282 #define FB_DOBTR(size) \
283  asm volatile( \
284  "\teieio\n" \
285  "0: l" #size \
286  "arx 14,0,%[lockPtr]\n" \
287  "\tli 15,1\n" \
288  "\tsldi 15,15,%[bit]\n" \
289  "\txor 14,14,15\n" \
290  "\tst" #size \
291  "cx. 14,0,%[lockPtr]\n" \
292  "\tbne 0b\n" \
293  "\tisync\n" \
294  : \
295  : [lockPtr] "r"(&lock_), [bit] "i"(Bit) \
296  : "cr0", "memory", "r14", "r15")
297 
298  switch (sizeof(IntType)) {
299  case 2:
300  FB_DOBTR(h);
301  break;
302  case 4:
303  FB_DOBTR(w);
304  break;
305  case 8:
306  FB_DOBTR(d);
307  break;
308  }
309 
310 #undef FB_DOBTR
311 #else
312 #error "x64 aarch64 ppc64 only"
313 #endif
314  }
315 };
316 
317 } // namespace folly
*than *hazptr_holder h
Definition: Hazptr.h:116
IntType getData() const
Definition: PicoSpinLock.h:111
PskType type
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void init(IntType initialValue=0)
Definition: PicoSpinLock.h:96
def load()
Definition: deadlock.py:441
void wait() noexcept
Definition: Sleeper.h:57
void unlock() const
Definition: PicoSpinLock.h:240
bool try_lock() const
Definition: PicoSpinLock.h:136
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
void lock() const
Definition: PicoSpinLock.h:229
static const char *const value
Definition: Conv.cpp:50
void setData(IntType w)
Definition: PicoSpinLock.h:124
std::make_unsigned< IntType >::type UIntType
Definition: PicoSpinLock.h:75