mdbx: add mdbx_osal_jitter() and mdbx_jitter4testing().

This commit is contained in:
Leo Yuriev 2017-04-27 15:18:33 +03:00
parent 40dee6f05f
commit 7204c46421
4 changed files with 36 additions and 13 deletions

View File

@ -799,6 +799,14 @@ void mdbx_panic(const char *fmt, ...)
/*----------------------------------------------------------------------------*/
static __inline void mdbx_jitter4testing(bool tiny) {
#ifndef NDEBUG
mdbx_osal_jitter(tiny);
#else
(void)tiny;
#endif
}
int mdbx_reader_check0(MDB_env *env, int rlocked, int *dead);
#define METAPAGE_1(env) (&((MDB_metabuf *)(env)->me_map)->mb_metabuf.mm_meta)

View File

@ -24,19 +24,6 @@
* LY
*/
static __inline void jitter4testing(void) {
#ifndef NDEBUG
for (;;) {
unsigned coin = ((unsigned)__rdtsc() * 277u) % 43u;
if (coin < 43 / 3)
break;
SwitchToThread();
if (coin > 43 * 2 / 3)
Sleep(1);
}
#endif
}
/*----------------------------------------------------------------------------*/
/* rthc */

View File

@ -626,3 +626,29 @@ int mdbx_mlock(const void *address, size_t length) {
return (mlock(address, length) == 0) ? MDB_SUCCESS : errno;
#endif
}
/*----------------------------------------------------------------------------*/
__cold void mdbx_osal_jitter(bool tiny) {
for (;;) {
#if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
defined(__x86_64__)
const unsigned salt = 277u * (unsigned)__rdtsc();
#else
const unsigned salt = rand();
#endif
const unsigned coin = salt % (tiny ? 29u : 43u);
if (coin < 43 / 3)
break;
#if defined(_WIN32) || defined(_WIN64)
SwitchToThread();
if (coin > 43 * 2 / 3)
Sleep(1);
#else
sched_yield();
if (coin > 43 * 2 / 3)
usleep(coin);
#endif
}
}

View File

@ -383,6 +383,8 @@ static __inline mdbx_pid_t mdbx_getpid(void) {
#endif
}
void mdbx_osal_jitter(bool tiny);
/*----------------------------------------------------------------------------*/
#ifndef mdbx_assert_fail