Add a default timeout parameter to HHWheelTimer.
[folly.git] / folly / PicoSpinLock.h
1 /*
2  * Copyright 2015 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 #pragma once
18
19 /*
20  * @author Keith Adams <kma@fb.com>
21  * @author Jordan DeLong <delong.j@fb.com>
22  */
23
24 #include <array>
25 #include <cinttypes>
26 #include <type_traits>
27 #include <cstdlib>
28 #include <pthread.h>
29 #include <mutex>
30 #include <atomic>
31
32 #include <glog/logging.h>
33 #include <folly/detail/Sleeper.h>
34 #include <folly/Portability.h>
35
36 #if !FOLLY_X64 && !FOLLY_A64
37 # error "PicoSpinLock.h is currently x64 and aarch64 only."
38 #endif
39
40 namespace folly {
41
42 /*
43  * Spin lock on a single bit in an integral type.  You can use this
44  * with 16, 32, or 64-bit integral types.
45  *
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.
50  *
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.
54  */
55 template<class IntType, int Bit = sizeof(IntType) * 8 - 1>
56 struct PicoSpinLock {
57   // Internally we deal with the unsigned version of the type.
58   typedef typename std::make_unsigned<IntType>::type UIntType;
59
60   static_assert(std::is_integral<IntType>::value,
61                 "PicoSpinLock needs an integral type");
62   static_assert(sizeof(IntType) == 2 || sizeof(IntType) == 4 ||
63                   sizeof(IntType) == 8,
64                 "PicoSpinLock can't work on integers smaller than 2 bytes");
65
66  public:
67   static const UIntType kLockBitMask_ = UIntType(1) << Bit;
68   UIntType lock_;
69
70   /*
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
74    * getData() == 0.
75    *
76    * (This doesn't use a constructor because we want to be a POD.)
77    */
78   void init(IntType initialValue = 0) {
79     CHECK(!(initialValue & kLockBitMask_));
80     lock_ = initialValue;
81   }
82
83   /*
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.
87    *
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.)
91    */
92   IntType getData() const {
93     return static_cast<IntType>(lock_ & ~kLockBitMask_);
94   }
95
96   /*
97    * Set the value of the other bits in our integer.
98    *
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.
101    */
102   void setData(IntType w) {
103     CHECK(!(w & kLockBitMask_));
104     lock_ = (lock_ & kLockBitMask_) | w;
105   }
106
107   /*
108    * Try to get the lock without blocking: returns whether or not we
109    * got it.
110    */
111   bool try_lock() const {
112     bool ret = false;
113
114 #if FOLLY_X64
115 #define FB_DOBTS(size)                                  \
116   asm volatile("lock; bts" #size " %1, (%2); setnc %0"  \
117                : "=r" (ret)                             \
118                : "i" (Bit),                             \
119                  "r" (&lock_)                           \
120                : "memory", "flags")
121
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;
126     }
127
128 #undef FB_DOBTS
129 #elif FOLLY_A64
130     ret = __atomic_fetch_or(&lock_, 1 << Bit, __ATOMIC_SEQ_CST);
131 #else
132 #error "x86 aarch64 only"
133 #endif
134
135     return ret;
136   }
137
138   /*
139    * Block until we can acquire the lock.  Uses Sleeper to wait.
140    */
141   void lock() const {
142     detail::Sleeper sleeper;
143     while (!try_lock()) {
144       sleeper.wait();
145     }
146   }
147
148   /*
149    * Release the lock, without changing the value of the rest of the
150    * integer.
151    */
152   void unlock() const {
153 #if FOLLY_X64
154 #define FB_DOBTR(size)                          \
155   asm volatile("lock; btr" #size " %0, (%1)"    \
156                :                                \
157                : "i" (Bit),                     \
158                  "r" (&lock_)                   \
159                : "memory", "flags")
160
161
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;
168     }
169
170 #undef FB_DOBTR
171 #elif FOLLY_A64
172     __atomic_fetch_and(&lock_, ~(1 << Bit), __ATOMIC_SEQ_CST);
173 #else
174 # error "x64 aarch64 only"
175 #endif
176   }
177 };
178
179 }