cdd121e710dba6f9c0888c71ae48b8de5f226f09
[folly.git] / folly / detail / Sleeper.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 <cinttypes>
25 #include <ctime>
26
27 #include <folly/Portability.h>
28
29 namespace folly {
30
31 //////////////////////////////////////////////////////////////////////
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
42     uint32_t spinCount;
43
44   public:
45     Sleeper() : spinCount(0) {}
46
47     void wait() {
48       if (spinCount < kMaxActiveSpin) {
49         ++spinCount;
50         asm_volatile_pause();
51       } else {
52         /*
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).
56          */
57         struct timespec ts = { 0, 500000 };
58         nanosleep(&ts, nullptr);
59       }
60     }
61   };
62
63 }
64 }