Commit c17ed205 authored by Maged Michael's avatar Maged Michael Committed by Facebook GitHub Bot

SharedMutex: Change SharedMutexPolicyDefault and change default spin and yield counts

Summary:
Change SharedMutexPolicyDefault to include max_spin_count and max_soft_yield_count instead of bool block_immediately.

Change the default spin and yield counts from 1000 and 1000 to 2 and 1, respectively.

Reviewed By: ot

Differential Revision: D29559594

fbshipit-source-id: 93a3bdf43c20f456031265daf7b76ab40e3dcbdf
parent b9176fc5
......@@ -252,7 +252,8 @@ struct SharedMutexToken {
};
struct SharedMutexPolicyDefault {
static constexpr bool block_immediately = false;
static constexpr uint32_t max_spin_count = 2;
static constexpr uint32_t max_soft_yield_count = 1;
static constexpr bool track_thread_id = false;
static constexpr bool skip_annotate_rwlock = false;
};
......@@ -336,7 +337,8 @@ class SharedMutexImpl : std::conditional_t<
shared_mutex_detail::ThreadIdOwnershipTracker,
shared_mutex_detail::NopOwnershipTracker> {
private:
static constexpr bool BlockImmediately = Policy::block_immediately;
static constexpr uint32_t MaxSpinCount = Policy::max_spin_count;
static constexpr uint32_t MaxSoftYieldCount = Policy::max_soft_yield_count;
static constexpr bool AnnotateForThreadSanitizer =
kIsSanitizeThread && !ReaderPriority && !Policy::skip_annotate_rwlock;
static constexpr bool TrackThreadId = Policy::track_thread_id;
......@@ -909,11 +911,8 @@ class SharedMutexImpl : std::conditional_t<
static constexpr uint32_t kNumSharedToStartDeferring = 2;
// The typical number of spins that a thread will wait for a state
// transition. There is no bound on the number of threads that can wait
// for a writer, so we are pretty conservative here to limit the chance
// that we are starving the writer of CPU. Each spin is 6 or 7 nanos,
// almost all of which is in the pause instruction.
static constexpr uint32_t kMaxSpinCount = !BlockImmediately ? 1000 : 2;
// transition.
static constexpr uint32_t kMaxSpinCount = MaxSpinCount;
// The maximum number of soft yields before falling back to futex.
// If the preemption heuristic is activated we will fall back before
......@@ -921,7 +920,7 @@ class SharedMutexImpl : std::conditional_t<
// to getrusage, with checks of the goal at each step). Soft yields
// aren't compatible with deterministic execution under test (unlike
// futexWaitUntil, which has a capricious but deterministic back end).
static constexpr uint32_t kMaxSoftYieldCount = !BlockImmediately ? 1000 : 0;
static constexpr uint32_t kMaxSoftYieldCount = MaxSoftYieldCount;
// If AccessSpreader assigns indexes from 0..k*n-1 on a system where some
// level of the memory hierarchy is symmetrically divided into k pieces
......@@ -1093,12 +1092,12 @@ class SharedMutexImpl : std::conditional_t<
if ((state & goal) == 0) {
return true;
}
asm_volatile_pause();
++spinCount;
if (UNLIKELY(spinCount >= kMaxSpinCount)) {
if (UNLIKELY(spinCount == kMaxSpinCount)) {
return ctx.canBlock() &&
yieldWaitForZeroBits(state, goal, waitMask, ctx);
}
asm_volatile_pause();
++spinCount;
}
}
......
......@@ -39,7 +39,8 @@ using namespace std;
using namespace std::chrono;
struct DSharedMutexPolicy : SharedMutexPolicyDefault {
static constexpr bool block_immediately = true;
static constexpr uint32_t max_spin_count = 0;
static constexpr uint32_t max_soft_yield_count = 0;
};
using DSched = DeterministicSchedule;
using DSharedMutexReadPriority =
......
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