proxygen
Sleeper.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 #pragma once
18 
19 /*
20  * @author Keith Adams <kma@fb.com>
21  * @author Jordan DeLong <delong.j@fb.com>
22  */
23 
24 #include <cstdint>
25 
26 #include <folly/portability/Asm.h>
27 #include <folly/portability/Time.h>
28 
29 namespace folly {
30 
32 
33 namespace detail {
34 
35 /*
36  * A helper object for the contended case. Starts off with eager
37  * spinning, and falls back to sleeping for small quantums.
38  */
39 class Sleeper {
40  static const uint32_t kMaxActiveSpin = 4000;
41 
43 
44  public:
45  Sleeper() noexcept : spinCount(0) {}
46 
47  static void sleep() noexcept {
48  /*
49  * Always sleep 0.5ms, assuming this will make the kernel put
50  * us down for whatever its minimum timer resolution is (in
51  * linux this varies by kernel version from 1ms to 10ms).
52  */
53  struct timespec ts = {0, 500000};
54  nanosleep(&ts, nullptr);
55  }
56 
57  void wait() noexcept {
58  if (spinCount < kMaxActiveSpin) {
59  ++spinCount;
61  } else {
62  sleep();
63  }
64  }
65 };
66 
67 } // namespace detail
68 } // namespace folly
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
void wait() noexcept
Definition: Sleeper.h:57
Sleeper() noexcept
Definition: Sleeper.h:45
static const uint32_t kMaxActiveSpin
Definition: Sleeper.h:40
static void sleep() noexcept
Definition: Sleeper.h:47
uint32_t spinCount
Definition: Sleeper.h:42
void asm_volatile_pause()
Definition: Asm.h:37