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.
20 * @author Keith Adams <kma@fb.com>
21 * @author Jordan DeLong <delong.j@fb.com>
26 #include <folly/portability/Asm.h>
27 #include <folly/portability/Time.h>
31 //////////////////////////////////////////////////////////////////////
36 * A helper object for the contended case. Starts off with eager
37 * spinning, and falls back to sleeping for small quantums.
40 static const uint32_t kMaxActiveSpin = 4000;
45 Sleeper() : spinCount(0) {}
48 if (spinCount < kMaxActiveSpin) {
53 * Always sleep 0.5ms, assuming this will make the kernel put
54 * us down for whatever its minimum timer resolution is (in
55 * linux this varies by kernel version from 1ms to 10ms).
57 struct timespec ts = { 0, 500000 };
58 nanosleep(&ts, nullptr);