minor tweak to MicroLock slow path
[folly.git] / folly / MicroLock.cpp
1 /*
2  * Copyright 2016 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 #include <folly/MicroLock.h>
18 #include <thread>
19
20 namespace folly {
21
22 void MicroLockCore::lockSlowPath(uint32_t oldWord,
23                                  detail::Futex<>* wordPtr,
24                                  uint32_t slotHeldBit,
25                                  unsigned maxSpins,
26                                  unsigned maxYields) {
27   unsigned newWord;
28   unsigned spins = 0;
29   uint32_t slotWaitBit = slotHeldBit << 1;
30
31 retry:
32   if ((oldWord & slotHeldBit) != 0) {
33     ++spins;
34     if (spins > maxSpins + maxYields) {
35       // Somebody appears to have the lock.  Block waiting for the
36       // holder to unlock the lock.  We set heldbit(slot) so that the
37       // lock holder knows to FUTEX_WAKE us.
38       newWord = oldWord | slotWaitBit;
39       if (newWord != oldWord) {
40         if (!wordPtr->compare_exchange_weak(oldWord,
41                                             newWord,
42                                             std::memory_order_relaxed,
43                                             std::memory_order_relaxed)) {
44           goto retry;
45         }
46       }
47       (void)wordPtr->futexWait(newWord, slotHeldBit);
48     } else if (spins > maxSpins) {
49       // sched_yield(), but more portable
50       std::this_thread::yield();
51     } else {
52       folly::asm_pause();
53     }
54     oldWord = wordPtr->load(std::memory_order_relaxed);
55     goto retry;
56   }
57
58   newWord = oldWord | slotHeldBit;
59   if (!wordPtr->compare_exchange_weak(oldWord,
60                                       newWord,
61                                       std::memory_order_acquire,
62                                       std::memory_order_relaxed)) {
63     goto retry;
64   }
65 }
66 }