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) { ...@@ -928,7 +928,7 @@ bool spin(Waiter& waiter, std::uint32_t& sig, std::uint32_t mode) {
if (spins < kMaxSpins) { if (spins < kMaxSpins) {
asm_volatile_pause(); asm_volatile_pause();
} else { } else {
Sleeper::sleep(); std::this_thread::sleep_for(folly::detail::Sleeper::kMinYieldingSleep);
} }
} }
} }
......
...@@ -331,7 +331,6 @@ class shared_head_only_list { ...@@ -331,7 +331,6 @@ class shared_head_only_list {
} }
Node* pop_all_lock() noexcept { Node* pop_all_lock() noexcept {
folly::detail::Sleeper s;
while (true) { while (true) {
auto oldval = head(); auto oldval = head();
auto lockbit = oldval & kLockBit; auto lockbit = oldval & kLockBit;
...@@ -349,7 +348,7 @@ class shared_head_only_list { ...@@ -349,7 +348,7 @@ class shared_head_only_list {
return reinterpret_cast<Node*>(ptrval); return reinterpret_cast<Node*>(ptrval);
} }
} }
s.sleep(); std::this_thread::sleep_for(folly::detail::Sleeper::kMinYieldingSleep);
} }
} }
}; // shared_head_only_list }; // 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 @@ ...@@ -21,10 +21,11 @@
* @author Jordan DeLong <delong.j@fb.com> * @author Jordan DeLong <delong.j@fb.com>
*/ */
#include <chrono>
#include <cstdint> #include <cstdint>
#include <thread>
#include <folly/portability/Asm.h> #include <folly/portability/Asm.h>
#include <folly/portability/Time.h>
namespace folly { namespace folly {
...@@ -37,29 +38,27 @@ namespace detail { ...@@ -37,29 +38,27 @@ namespace detail {
* spinning, and falls back to sleeping for small quantums. * spinning, and falls back to sleeping for small quantums.
*/ */
class Sleeper { class Sleeper {
static const uint32_t kMaxActiveSpin = 4000; const std::chrono::nanoseconds delta;
uint32_t spinCount; uint32_t spinCount;
static constexpr uint32_t kMaxActiveSpin = 4000;
public: public:
Sleeper() noexcept : spinCount(0) {} static constexpr std::chrono::nanoseconds kMinYieldingSleep =
std::chrono::microseconds(500);
static void sleep() noexcept { constexpr Sleeper() noexcept : delta(kMinYieldingSleep), spinCount(0) {}
/*
* Always sleep 0.5ms, assuming this will make the kernel put explicit Sleeper(std::chrono::nanoseconds d) noexcept
* us down for whatever its minimum timer resolution is (in : delta(d), spinCount(0) {}
* linux this varies by kernel version from 1ms to 10ms).
*/
struct timespec ts = {0, 500000};
nanosleep(&ts, nullptr);
}
void wait() noexcept { void wait() noexcept {
if (spinCount < kMaxActiveSpin) { if (spinCount < kMaxActiveSpin) {
++spinCount; ++spinCount;
asm_volatile_pause(); asm_volatile_pause();
} else { } 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