[K/N][New MM] Check if asserts are enabled in thread state asserts

This commit is contained in:
Ilya Matveev
2021-07-29 15:31:50 +07:00
committed by Space
parent 5ee87e126d
commit e45b13d582
3 changed files with 35 additions and 12 deletions
@@ -414,12 +414,18 @@ ALWAYS_INLINE void AssertThreadState(MemoryState* thread, std::initializer_list<
// Asserts that the current thread is in the the given state. // Asserts that the current thread is in the the given state.
ALWAYS_INLINE inline void AssertThreadState(ThreadState expected) noexcept { ALWAYS_INLINE inline void AssertThreadState(ThreadState expected) noexcept {
// Avoid redundant TLS access in GetMemoryState if runtime asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
AssertThreadState(mm::GetMemoryState(), expected); AssertThreadState(mm::GetMemoryState(), expected);
} }
}
ALWAYS_INLINE inline void AssertThreadState(std::initializer_list<ThreadState> expected) noexcept { ALWAYS_INLINE inline void AssertThreadState(std::initializer_list<ThreadState> expected) noexcept {
// Avoid redundant TLS access in GetMemoryState if runtime asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
AssertThreadState(mm::GetMemoryState(), expected); AssertThreadState(mm::GetMemoryState(), expected);
} }
}
// Scopely sets the given thread state for the given thread. // Scopely sets the given thread state for the given thread.
class ThreadStateGuard final : private MoveOnly { class ThreadStateGuard final : private MoveOnly {
@@ -33,12 +33,18 @@ ALWAYS_INLINE ThreadState kotlin::SwitchThreadState(MemoryState* thread, ThreadS
} }
ALWAYS_INLINE void kotlin::AssertThreadState(MemoryState* thread, ThreadState expected) noexcept { ALWAYS_INLINE void kotlin::AssertThreadState(MemoryState* thread, ThreadState expected) noexcept {
// Avoid redundant read in GetThreadData if runtime asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
AssertThreadState(thread->GetThreadData(), expected); AssertThreadState(thread->GetThreadData(), expected);
} }
}
ALWAYS_INLINE void kotlin::AssertThreadState(MemoryState* thread, std::initializer_list<ThreadState> expected) noexcept { ALWAYS_INLINE void kotlin::AssertThreadState(MemoryState* thread, std::initializer_list<ThreadState> expected) noexcept {
// Avoid redundant read in GetThreadData if runtime asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
AssertThreadState(thread->GetThreadData(), expected); AssertThreadState(thread->GetThreadData(), expected);
} }
}
ThreadState kotlin::GetThreadState(MemoryState* thread) noexcept { ThreadState kotlin::GetThreadState(MemoryState* thread) noexcept {
return thread->GetThreadData()->state(); return thread->GetThreadData()->state();
@@ -38,17 +38,28 @@ ALWAYS_INLINE inline ThreadState SwitchThreadState(mm::ThreadData* threadData, T
// Asserts that the given thread is in the given state. // Asserts that the given thread is in the given state.
ALWAYS_INLINE inline void AssertThreadState(mm::ThreadData* threadData, ThreadState expected) noexcept { ALWAYS_INLINE inline void AssertThreadState(mm::ThreadData* threadData, ThreadState expected) noexcept {
// The read of the thread state is atomic, thus the compiler cannot eliminate it
// even if its result is unused due to disabled runtime asserts.
// So we explicitly avoid the read if asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
auto actual = threadData->state(); auto actual = threadData->state();
RuntimeAssert(actual == expected, RuntimeAssert(
"Unexpected thread state. Expected: %s. Actual: %s.", actual == expected, "Unexpected thread state. Expected: %s. Actual: %s.", ThreadStateName(expected),
ThreadStateName(expected), ThreadStateName(actual)); ThreadStateName(actual));
}
} }
ALWAYS_INLINE inline void AssertThreadState(mm::ThreadData* threadData, std::initializer_list<ThreadState> expected) noexcept { ALWAYS_INLINE inline void AssertThreadState(mm::ThreadData* threadData, std::initializer_list<ThreadState> expected) noexcept {
// The read of the thread state is atomic, thus the compiler cannot eliminate it
// even if its result is unused due to disabled runtime asserts.
// So we explicitly avoid the read if asserts are disabled.
if (compiler::runtimeAssertsMode() != compiler::RuntimeAssertsMode::kIgnore) {
auto actual = threadData->state(); auto actual = threadData->state();
RuntimeAssert(std::any_of(expected.begin(), expected.end(), [actual](ThreadState expected) { return expected == actual; }), RuntimeAssert(
"Unexpected thread state. Expected one of: %s. Actual: %s", std::any_of(expected.begin(), expected.end(), [actual](ThreadState expected) { return expected == actual; }),
internal::statesToString(expected).c_str(), ThreadStateName(actual)); "Unexpected thread state. Expected one of: %s. Actual: %s", internal::statesToString(expected).c_str(),
ThreadStateName(actual));
}
} }
} // namespace kotlin } // namespace kotlin