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.
17 #include <folly/MicroLock.h>
21 void MicroLockCore::lockSlowPath(uint32_t oldWord,
22 detail::Futex<>* wordPtr,
28 uint32_t slotWaitBit = slotHeldBit << 1;
31 if ((oldWord & slotHeldBit) != 0) {
33 if (spins > maxSpins + maxYields) {
34 // Somebody appears to have the lock. Block waiting for the
35 // holder to unlock the lock. We set heldbit(slot) so that the
36 // lock holder knows to FUTEX_WAKE us.
37 newWord = oldWord | slotWaitBit;
38 if (newWord != oldWord) {
39 if (!wordPtr->compare_exchange_weak(oldWord,
41 std::memory_order_relaxed,
42 std::memory_order_relaxed)) {
46 (void)wordPtr->futexWait(newWord, slotHeldBit);
47 } else if (spins > maxSpins) {
50 oldWord = wordPtr->load(std::memory_order_relaxed);
54 newWord = oldWord | slotHeldBit;
55 if (!wordPtr->compare_exchange_weak(oldWord,
57 std::memory_order_relaxed,
58 std::memory_order_relaxed)) {
62 // Locks are traditionally memory barriers, so we emit a full fence
63 // even though we were happy using relaxed atomics for the
65 std::atomic_thread_fence(std::memory_order_seq_cst);