Commit d7736cff authored by Dan Melnic's avatar Dan Melnic Committed by Facebook GitHub Bot

Allow changing the Sleeper sleep interval

Summary: Allow changing the Sleeper sleep interval

Reviewed By: yfeldblum

Differential Revision: D23591682

fbshipit-source-id: 81b55fa699362d62e436c599c38046d15fecee37
parent 0a1b471e
......@@ -928,7 +928,7 @@ bool spin(Waiter& waiter, std::uint32_t& sig, std::uint32_t mode) {
if (spins < kMaxSpins) {
asm_volatile_pause();
} else {
Sleeper::sleep();
std::this_thread::sleep_for(folly::detail::Sleeper::kMinYieldingSleep);
}
}
}
......
......@@ -331,7 +331,6 @@ class shared_head_only_list {
}
Node* pop_all_lock() noexcept {
folly::detail::Sleeper s;
while (true) {
auto oldval = head();
auto lockbit = oldval & kLockBit;
......@@ -349,7 +348,7 @@ class shared_head_only_list {
return reinterpret_cast<Node*>(ptrval);
}
}
s.sleep();
std::this_thread::sleep_for(folly::detail::Sleeper::kMinYieldingSleep);
}
}
}; // shared_head_only_list
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/synchronization/detail/Sleeper.h>
namespace folly {
namespace detail {
constexpr std::chrono::nanoseconds Sleeper::kMinYieldingSleep;
}
} // namespace folly
......@@ -21,10 +21,11 @@
* @author Jordan DeLong <delong.j@fb.com>
*/
#include <chrono>
#include <cstdint>
#include <thread>
#include <folly/portability/Asm.h>
#include <folly/portability/Time.h>
namespace folly {
......@@ -37,29 +38,27 @@ namespace detail {
* spinning, and falls back to sleeping for small quantums.
*/
class Sleeper {
static const uint32_t kMaxActiveSpin = 4000;
const std::chrono::nanoseconds delta;
uint32_t spinCount;
static constexpr uint32_t kMaxActiveSpin = 4000;
public:
Sleeper() noexcept : spinCount(0) {}
static constexpr std::chrono::nanoseconds kMinYieldingSleep =
std::chrono::microseconds(500);
static void sleep() noexcept {
/*
* Always sleep 0.5ms, assuming this will make the kernel put
* us down for whatever its minimum timer resolution is (in
* linux this varies by kernel version from 1ms to 10ms).
*/
struct timespec ts = {0, 500000};
nanosleep(&ts, nullptr);
}
constexpr Sleeper() noexcept : delta(kMinYieldingSleep), spinCount(0) {}
explicit Sleeper(std::chrono::nanoseconds d) noexcept
: delta(d), spinCount(0) {}
void wait() noexcept {
if (spinCount < kMaxActiveSpin) {
++spinCount;
asm_volatile_pause();
} else {
sleep();
/* sleep override */
std::this_thread::sleep_for(delta);
}
}
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment