diff --git a/src/atomics-ops.h b/src/atomics-ops.h index 7dae2f34..01eb22d1 100644 --- a/src/atomics-ops.h +++ b/src/atomics-ops.h @@ -171,10 +171,8 @@ MDBX_MAYBE_UNUSED static __always_inline void atomic_yield(void) { #elif defined(__mips) || defined(__mips__) || defined(__mips64) || defined(__mips64__) || defined(_M_MRX000) || \ defined(_MIPS_) || defined(__MWERKS__) || defined(__sgi) __asm__ __volatile__(".word 0x00000140"); -#elif defined(__linux__) || defined(__gnu_linux__) || defined(_UNIX03_SOURCE) - sched_yield(); -#elif (defined(_GNU_SOURCE) && __GLIBC_PREREQ(2, 1)) || defined(_OPEN_THREADS) - pthread_yield(); +#else + osal_yield(); #endif } diff --git a/src/coherency.c b/src/coherency.c index af1cc6fd..894b4847 100644 --- a/src/coherency.c +++ b/src/coherency.c @@ -103,15 +103,7 @@ __cold int coherency_timeout(uint64_t *timestamp, intptr_t pgno, const MDBX_env } osal_memory_fence(mo_AcquireRelease, true); -#if defined(_WIN32) || defined(_WIN64) - SwitchToThread(); -#elif defined(__linux__) || defined(__gnu_linux__) || defined(_UNIX03_SOURCE) - sched_yield(); -#elif (defined(_GNU_SOURCE) && __GLIBC_PREREQ(2, 1)) || defined(_OPEN_THREADS) - pthread_yield(); -#else - usleep(42); -#endif + osal_yield(); return MDBX_RESULT_TRUE; } diff --git a/src/dbi.c b/src/dbi.c index f5390e8d..70f08831 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -166,13 +166,8 @@ int dbi_defer_release(MDBX_env *const env, defer_free_item_t *const chain) { #endif /* MDBX_ENABLE_DBI_LOCKFREE */ ENSURE(env, osal_fastmutex_release(&env->dbi_lock) == MDBX_SUCCESS); - if (length > 42) { -#if defined(_WIN32) || defined(_WIN64) - SwitchToThread(); -#else - sched_yield(); -#endif /* Windows */ - } + if (length > 42) + osal_yield(); while (obsolete_chain) { defer_free_item_t *item = obsolete_chain; obsolete_chain = obsolete_chain->next; diff --git a/src/osal.c b/src/osal.c index 6acbe603..687a05fc 100644 --- a/src/osal.c +++ b/src/osal.c @@ -2624,7 +2624,7 @@ __cold void osal_jitter(bool tiny) { break; #if defined(_WIN32) || defined(_WIN64) if (coin < 43 * 2 / 3) - SwitchToThread(); + osal_yield(); else { static HANDLE timer; if (!timer) @@ -2639,7 +2639,7 @@ __cold void osal_jitter(bool tiny) { break; } #else - sched_yield(); + osal_yield(); if (coin > 43 * 2 / 3) usleep(coin); #endif diff --git a/src/osal.h b/src/osal.h index 6202ff47..0e33a698 100644 --- a/src/osal.h +++ b/src/osal.h @@ -171,6 +171,14 @@ typedef char pathchar_t; #define MDBX_PRIsPATH "s" #endif +static inline bool osal_yield(void) { +#if defined(_WIN32) || defined(_WIN64) + return SleepEx(0, true) == WAIT_IO_COMPLETION; +#else + return sched_yield() != 0; +#endif +} + typedef struct osal_mmap { union { void *base; diff --git a/test/osal-unix.c++ b/test/osal-unix.c++ index c1bedd24..743dc2b1 100644 --- a/test/osal-unix.c++ +++ b/test/osal-unix.c++ @@ -593,11 +593,6 @@ int osal_actor_poll(mdbx_pid_t &pid, unsigned timeout) { return sigbreak ? EINTR : 0 /* timeout */; } -void osal_yield(void) { - if (sched_yield()) - failure_perror("sched_yield()", errno); -} - void osal_udelay(size_t us) { chrono::time until, now = chrono::now_monotonic(); until.fixedpoint = now.fixedpoint + chrono::from_us(us).fixedpoint; diff --git a/test/osal-windows.c++ b/test/osal-windows.c++ index b03d73ea..4a3f9e52 100644 --- a/test/osal-windows.c++ +++ b/test/osal-windows.c++ @@ -401,8 +401,6 @@ int osal_actor_poll(mdbx_pid_t &pid, unsigned timeout) { } } -void osal_yield(void) { SwitchToThread(); } - void osal_udelay(size_t us) { chrono::time until, now = chrono::now_monotonic(); until.fixedpoint = now.fixedpoint + chrono::from_us(us).fixedpoint; diff --git a/test/osal.h++ b/test/osal.h++ index 85c5f0f3..c442289e 100644 --- a/test/osal.h++ +++ b/test/osal.h++ @@ -20,7 +20,6 @@ bool osal_multiactor_mode(void); int osal_delay(unsigned seconds); void osal_udelay(size_t us); -void osal_yield(void); bool osal_istty(int fd); std::string osal_tempdir(void);