minor cleanup

This commit is contained in:
nmzik
2026-10-12 15:06:38 +02:00
parent 5b0ef07742
commit df5b9b92be
+15 -19
View File
@@ -8,10 +8,6 @@
#include <condition_variable> // IWYU pragma: keep #include <condition_variable> // IWYU pragma: keep
#include <mutex> #include <mutex>
#if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS && KYTY_COMPILER == KYTY_COMPILER_CLANG
#define KYTY_WIN_CS
#endif
// macOS has no clock_nanosleep. // macOS has no clock_nanosleep.
#if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS && !defined(__APPLE__) #if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS && !defined(__APPLE__)
#define KYTY_POSIX_HIGH_RES_SLEEP #define KYTY_POSIX_HIGH_RES_SLEEP
@@ -22,7 +18,7 @@
#include <string> #include <string>
#include <thread> #include <thread>
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
#include <windows.h> // IWYU pragma: keep #include <windows.h> // IWYU pragma: keep
// IWYU pragma: no_include <winbase.h> // IWYU pragma: no_include <winbase.h>
constexpr DWORD KYTY_CS_SPIN_COUNT = 4000; constexpr DWORD KYTY_CS_SPIN_COUNT = 4000;
@@ -133,7 +129,7 @@ namespace Common {
using thread_id_t = std::thread::id; using thread_id_t = std::thread::id;
struct MutexPrivate { struct MutexPrivate {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
MutexPrivate() { InitializeCriticalSectionAndSpinCount(&m_cs, KYTY_CS_SPIN_COUNT); } MutexPrivate() { InitializeCriticalSectionAndSpinCount(&m_cs, KYTY_CS_SPIN_COUNT); }
~MutexPrivate() { DeleteCriticalSection(&m_cs); } ~MutexPrivate() { DeleteCriticalSection(&m_cs); }
KYTY_CLASS_NO_COPY(MutexPrivate); KYTY_CLASS_NO_COPY(MutexPrivate);
@@ -144,7 +140,7 @@ struct MutexPrivate {
}; };
struct CondVarPrivate { struct CondVarPrivate {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
CondVarPrivate() { CondVarPrivate() {
static auto func = ResolveInitializeConditionVariable(); static auto func = ResolveInitializeConditionVariable();
EXIT_NOT_IMPLEMENTED(func == nullptr); EXIT_NOT_IMPLEMENTED(func == nullptr);
@@ -216,7 +212,7 @@ void Thread::Detach() {
} }
void Thread::SleepMicro(uint32_t micros) { void Thread::SleepMicro(uint32_t micros) {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
SleepHighResolution100ns(static_cast<uint64_t>(micros) * 10); SleepHighResolution100ns(static_cast<uint64_t>(micros) * 10);
#elif defined(KYTY_POSIX_HIGH_RES_SLEEP) #elif defined(KYTY_POSIX_HIGH_RES_SLEEP)
SleepHighResolutionNanos(static_cast<uint64_t>(micros) * 1000); SleepHighResolutionNanos(static_cast<uint64_t>(micros) * 1000);
@@ -226,7 +222,7 @@ void Thread::SleepMicro(uint32_t micros) {
} }
void Thread::SleepNano(uint64_t nanos) { void Thread::SleepNano(uint64_t nanos) {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
SleepHighResolution100ns((nanos + 99) / 100); SleepHighResolution100ns((nanos + 99) / 100);
#elif defined(KYTY_POSIX_HIGH_RES_SLEEP) #elif defined(KYTY_POSIX_HIGH_RES_SLEEP)
SleepHighResolutionNanos(nanos); SleepHighResolutionNanos(nanos);
@@ -262,7 +258,7 @@ Mutex::~Mutex() {
} }
void Mutex::Lock() { void Mutex::Lock() {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
EnterCriticalSection(&m_mutex->m_cs); EnterCriticalSection(&m_mutex->m_cs);
#else #else
m_mutex->m_mutex.lock(); m_mutex->m_mutex.lock();
@@ -270,7 +266,7 @@ void Mutex::Lock() {
} }
void Mutex::Unlock() { void Mutex::Unlock() {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
LeaveCriticalSection(&m_mutex->m_cs); LeaveCriticalSection(&m_mutex->m_cs);
#else #else
m_mutex->m_mutex.unlock(); m_mutex->m_mutex.unlock();
@@ -278,7 +274,7 @@ void Mutex::Unlock() {
} }
bool Mutex::TryLock() { bool Mutex::TryLock() {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
return (TryEnterCriticalSection(&m_mutex->m_cs) != 0); return (TryEnterCriticalSection(&m_mutex->m_cs) != 0);
#else #else
return m_mutex->m_mutex.try_lock(); return m_mutex->m_mutex.try_lock();
@@ -292,7 +288,7 @@ CondVar::~CondVar() {
} }
void CondVar::Wait(Mutex* mutex) { void CondVar::Wait(Mutex* mutex) {
#ifndef KYTY_WIN_CS #if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS
std::unique_lock<std::recursive_mutex> cpp_lock(mutex->m_mutex->m_mutex, std::adopt_lock_t()); std::unique_lock<std::recursive_mutex> cpp_lock(mutex->m_mutex->m_mutex, std::adopt_lock_t());
#endif #endif
auto poll_callback = [&] { auto poll_callback = [&] {
@@ -300,7 +296,7 @@ void CondVar::Wait(Mutex* mutex) {
if (callback == nullptr) { if (callback == nullptr) {
return; return;
} }
#if defined(KYTY_WIN_CS) #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
LeaveCriticalSection(&mutex->m_mutex->m_cs); LeaveCriticalSection(&mutex->m_mutex->m_cs);
callback(); callback();
EnterCriticalSection(&mutex->m_mutex->m_cs); EnterCriticalSection(&mutex->m_mutex->m_cs);
@@ -310,7 +306,7 @@ void CondVar::Wait(Mutex* mutex) {
cpp_lock.lock(); cpp_lock.lock();
#endif #endif
}; };
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
static auto func = ResolveSleepConditionVariableCS(); static auto func = ResolveSleepConditionVariableCS();
EXIT_NOT_IMPLEMENTED(func == nullptr); EXIT_NOT_IMPLEMENTED(func == nullptr);
if (g_cond_wait_poll_callback == nullptr) { if (g_cond_wait_poll_callback == nullptr) {
@@ -340,10 +336,10 @@ void CondVar::SetWaitPollCallback(wait_poll_func_t callback) {
bool CondVar::WaitFor(Mutex* mutex, uint32_t micros) { bool CondVar::WaitFor(Mutex* mutex, uint32_t micros) {
bool ok = false; bool ok = false;
#ifndef KYTY_WIN_CS #if KYTY_PLATFORM != KYTY_PLATFORM_WINDOWS
std::unique_lock<std::recursive_mutex> cpp_lock(mutex->m_mutex->m_mutex, std::adopt_lock_t()); std::unique_lock<std::recursive_mutex> cpp_lock(mutex->m_mutex->m_mutex, std::adopt_lock_t());
#endif #endif
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
static auto func = ResolveSleepConditionVariableCS(); static auto func = ResolveSleepConditionVariableCS();
EXIT_NOT_IMPLEMENTED(func == nullptr); EXIT_NOT_IMPLEMENTED(func == nullptr);
ok = !(func(&m_cond_var->m_cv, &mutex->m_mutex->m_cs, (micros < 1000 ? 1 : micros / 1000)) == ok = !(func(&m_cond_var->m_cv, &mutex->m_mutex->m_cs, (micros < 1000 ? 1 : micros / 1000)) ==
@@ -358,7 +354,7 @@ bool CondVar::WaitFor(Mutex* mutex, uint32_t micros) {
} }
void CondVar::Signal() { void CondVar::Signal() {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
static auto func = ResolveWakeConditionVariable(); static auto func = ResolveWakeConditionVariable();
EXIT_NOT_IMPLEMENTED(func == nullptr); EXIT_NOT_IMPLEMENTED(func == nullptr);
func(&m_cond_var->m_cv); func(&m_cond_var->m_cv);
@@ -368,7 +364,7 @@ void CondVar::Signal() {
} }
void CondVar::SignalAll() { void CondVar::SignalAll() {
#ifdef KYTY_WIN_CS #if KYTY_PLATFORM == KYTY_PLATFORM_WINDOWS
static auto func = ResolveWakeAllConditionVariable(); static auto func = ResolveWakeAllConditionVariable();
EXIT_NOT_IMPLEMENTED(func == nullptr); EXIT_NOT_IMPLEMENTED(func == nullptr);
func(&m_cond_var->m_cv); func(&m_cond_var->m_cv);