mdbx-test: refine locking options.

Change-Id: I6cb8798fd71b4b1ce2a76238ba955e7f6d539e45
This commit is contained in:
Leonid Yuriev 2019-11-12 13:28:24 +03:00
parent ac75fe2604
commit c882f77f54

View File

@ -21,12 +21,23 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#if !defined(_POSIX_THREAD_PROCESS_SHARED) || _POSIX_THREAD_PROCESS_SHARED < 1 #ifndef MDBX_LOCKING
#include <semaphore.h> #error "Opps, MDBX_LOCKING is undefined!"
#elif defined(__APPLE__)
#include "darwin/pthread_barrier.c"
#endif #endif
#if defined(__APPLE__) && (MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
MDBX_LOCKING == MDBX_LOCKING_POSIX2008)
#include "darwin/pthread_barrier.c"
#endif /* __APPLE__ && MDBX_LOCKING >= MDBX_LOCKING_POSIX2001 */
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
#include <sys/ipc.h>
#include <sys/sem.h>
#endif /* MDBX_LOCKING == MDBX_LOCKING_SYSV */
#if MDBX_LOCKING == MDBX_LOCKING_POSIX1988
#include <semaphore.h>
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
#include <atomic> #include <atomic>
static __inline __maybe_unused int atomic_decrement(std::atomic_int *p) { static __inline __maybe_unused int atomic_decrement(std::atomic_int *p) {
@ -46,14 +57,17 @@ static __inline __maybe_unused int atomic_decrement(volatile int *p) {
#endif #endif
} }
#endif /* C++11 */ #endif /* C++11 */
#endif /* MDBX_LOCKING == MDBX_LOCKING_POSIX1988 */
#if MDBX_LOCKING != MDBX_LOCKING_SYSV
struct shared_t { struct shared_t {
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0 #if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
pthread_barrier_t barrier; pthread_barrier_t barrier;
pthread_mutex_t mutex; pthread_mutex_t mutex;
size_t count; size_t count;
pthread_cond_t events[1]; pthread_cond_t events[1];
#else #elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
struct { struct {
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
std::atomic_int countdown; std::atomic_int countdown;
@ -64,32 +78,42 @@ struct shared_t {
} barrier; } barrier;
size_t count; size_t count;
sem_t events[1]; sem_t events[1];
#endif /* _POSIX_THREAD_PROCESS_SHARED */ #else
#error "FIXME"
#endif /* MDBX_LOCKING */
}; };
static shared_t *shared; static shared_t *shared;
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
void osal_wait4barrier(void) { void osal_wait4barrier(void) {
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
#warning "TODO"
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
assert(shared != nullptr && shared != MAP_FAILED); assert(shared != nullptr && shared != MAP_FAILED);
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0 int err = pthread_barrier_wait(&shared->barrier);
int rc = pthread_barrier_wait(&shared->barrier); if (err != 0 && err != PTHREAD_BARRIER_SERIAL_THREAD)
failure_perror("pthread_barrier_wait(shared)", err);
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
assert(shared != nullptr && shared != MAP_FAILED);
int err = (atomic_decrement(&shared->barrier.countdown) > 0 &&
sem_wait(&shared->barrier.sema))
? errno
: 0;
if (err != 0)
failure_perror("sem_wait(shared)", err);
if (sem_post(&shared->barrier.sema))
failure_perror("sem_post(shared)", errno);
#else #else
int rc = (atomic_decrement(&shared->barrier.countdown) > 0 && #error "FIXME"
sem_wait(&shared->barrier.sema)) #endif /* MDBX_LOCKING */
? errno
: 0;
if (rc == 0)
rc = sem_post(&shared->barrier.sema) ? errno : 0;
#endif
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) {
failure_perror("pthread_barrier_wait(shared)", rc);
}
} }
void osal_setup(const std::vector<actor_config> &actors) { void osal_setup(const std::vector<actor_config> &actors) {
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
#warning "TODO"
#else
assert(shared == nullptr); assert(shared == nullptr);
shared = (shared_t *)mmap( shared = (shared_t *)mmap(
nullptr, sizeof(shared_t) + actors.size() * sizeof(shared->events[0]), nullptr, sizeof(shared_t) + actors.size() * sizeof(shared->events[0]),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
@ -104,92 +128,101 @@ void osal_setup(const std::vector<actor_config> &actors) {
shared->count = actors.size() + 1; shared->count = actors.size() + 1;
#if defined(__APPLE__) || (defined(_POSIX_THREAD_PROCESS_SHARED) && \ #if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
_POSIX_THREAD_PROCESS_SHARED > 0) MDBX_LOCKING == MDBX_LOCKING_POSIX2008
pthread_barrierattr_t barrierattr; pthread_barrierattr_t barrierattr;
int rc = pthread_barrierattr_init(&barrierattr); int err = pthread_barrierattr_init(&barrierattr);
if (rc) if (err)
failure_perror("pthread_barrierattr_init()", rc); failure_perror("pthread_barrierattr_init()", err);
rc = pthread_barrierattr_setpshared(&barrierattr, PTHREAD_PROCESS_SHARED); err = pthread_barrierattr_setpshared(&barrierattr, PTHREAD_PROCESS_SHARED);
if (rc) if (err)
failure_perror("pthread_barrierattr_setpshared()", rc); failure_perror("pthread_barrierattr_setpshared()", err);
rc = pthread_barrier_init(&shared->barrier, &barrierattr, shared->count); err = pthread_barrier_init(&shared->barrier, &barrierattr, shared->count);
if (rc) if (err)
failure_perror("pthread_barrier_init(shared)", rc); failure_perror("pthread_barrier_init(shared)", err);
pthread_barrierattr_destroy(&barrierattr); pthread_barrierattr_destroy(&barrierattr);
pthread_mutexattr_t mutexattr; pthread_mutexattr_t mutexattr;
rc = pthread_mutexattr_init(&mutexattr); err = pthread_mutexattr_init(&mutexattr);
if (rc) if (err)
failure_perror("pthread_mutexattr_init()", rc); failure_perror("pthread_mutexattr_init()", err);
rc = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED); err = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
if (rc) if (err)
failure_perror("pthread_mutexattr_setpshared()", rc); failure_perror("pthread_mutexattr_setpshared()", err);
pthread_condattr_t condattr; pthread_condattr_t condattr;
rc = pthread_condattr_init(&condattr); err = pthread_condattr_init(&condattr);
if (rc) if (err)
failure_perror("pthread_condattr_init()", rc); failure_perror("pthread_condattr_init()", err);
rc = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED); err = pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED);
if (rc) if (err)
failure_perror("pthread_condattr_setpshared()", rc); failure_perror("pthread_condattr_setpshared()", err);
rc = pthread_mutex_init(&shared->mutex, &mutexattr); err = pthread_mutex_init(&shared->mutex, &mutexattr);
if (rc) if (err)
failure_perror("pthread_mutex_init(shared)", rc); failure_perror("pthread_mutex_init(shared)", err);
for (size_t i = 0; i < shared->count; ++i) { for (size_t i = 0; i < shared->count; ++i) {
pthread_cond_t *event = &shared->events[i]; pthread_cond_t *event = &shared->events[i];
rc = pthread_cond_init(event, &condattr); err = pthread_cond_init(event, &condattr);
if (rc) if (err)
failure_perror("pthread_cond_init(shared)", rc); failure_perror("pthread_cond_init(shared)", err);
log_trace("osal_setup: event(shared pthread_cond) %" PRIuPTR " -> %p", i, log_trace("osal_setup: event(shared pthread_cond) %" PRIuPTR " -> %p", i,
__Wpedantic_format_voidptr(event)); __Wpedantic_format_voidptr(event));
} }
pthread_condattr_destroy(&condattr); pthread_condattr_destroy(&condattr);
pthread_mutexattr_destroy(&mutexattr); pthread_mutexattr_destroy(&mutexattr);
#else #elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
shared->barrier.countdown = shared->count; shared->barrier.countdown = shared->count;
int rc = sem_init(&shared->barrier.sema, true, 1) ? errno : 0; if (sem_init(&shared->barrier.sema, true, 1))
if (rc) failure_perror("sem_init(shared.barrier)", errno);
failure_perror("sem_init(shared.barrier)", rc);
for (size_t i = 0; i < shared->count; ++i) { for (size_t i = 0; i < shared->count; ++i) {
sem_t *event = &shared->events[i]; sem_t *event = &shared->events[i];
rc = sem_init(event, true, 0) ? errno : 0; if (sem_init(event, true, 0))
if (rc) failure_perror("sem_init(shared.event)", errno);
failure_perror("sem_init(shared.event)", rc);
log_trace("osal_setup: event(shared sem_init) %" PRIuPTR " -> %p", i, log_trace("osal_setup: event(shared sem_init) %" PRIuPTR " -> %p", i,
__Wpedantic_format_voidptr(event)); __Wpedantic_format_voidptr(event));
} }
#endif #else
#error "FIXME"
#endif /* MDBX_LOCKING */
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
} }
void osal_broadcast(unsigned id) { void osal_broadcast(unsigned id) {
assert(shared != nullptr && shared != MAP_FAILED);
log_trace("osal_broadcast: event %u", id); log_trace("osal_broadcast: event %u", id);
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
#warning "TODO"
#else
assert(shared != nullptr && shared != MAP_FAILED);
if (id >= shared->count) if (id >= shared->count)
failure("osal_broadcast: id > limit"); failure("osal_broadcast: id > limit");
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0 #if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
int rc = pthread_cond_broadcast(shared->events + id); MDBX_LOCKING == MDBX_LOCKING_POSIX2008
if (rc) int err = pthread_cond_broadcast(shared->events + id);
failure_perror("pthread_cond_broadcast(shared)", rc); if (err)
failure_perror("pthread_cond_broadcast(shared)", err);
#elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
if (sem_post(shared->events + id))
failure_perror("sem_post(shared)", errno);
#else #else
int rc = sem_post(shared->events + id) ? errno : 0; #error "FIXME"
if (rc) #endif /* MDBX_LOCKING */
failure_perror("sem_post(shared)", rc); #endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
#endif
} }
int osal_waitfor(unsigned id) { int osal_waitfor(unsigned id) {
assert(shared != nullptr && shared != MAP_FAILED);
log_trace("osal_waitfor: event %u", id); log_trace("osal_waitfor: event %u", id);
#if MDBX_LOCKING == MDBX_LOCKING_SYSV
#warning "TODO"
#else
assert(shared != nullptr && shared != MAP_FAILED);
if (id >= shared->count) if (id >= shared->count)
failure("osal_waitfor: id > limit"); failure("osal_waitfor: id > limit");
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0 #if MDBX_LOCKING == MDBX_LOCKING_POSIX2001 || \
MDBX_LOCKING == MDBX_LOCKING_POSIX2008
int rc = pthread_mutex_lock(&shared->mutex); int rc = pthread_mutex_lock(&shared->mutex);
if (rc != 0) if (rc != 0)
failure_perror("pthread_mutex_lock(shared)", rc); failure_perror("pthread_mutex_lock(shared)", rc);
@ -201,11 +234,14 @@ int osal_waitfor(unsigned id) {
rc = pthread_mutex_unlock(&shared->mutex); rc = pthread_mutex_unlock(&shared->mutex);
if (rc != 0) if (rc != 0)
failure_perror("pthread_mutex_unlock(shared)", rc); failure_perror("pthread_mutex_unlock(shared)", rc);
#else #elif MDBX_LOCKING == MDBX_LOCKING_POSIX1988
int rc = sem_wait(shared->events + id) ? errno : 0; int rc = sem_wait(shared->events + id) ? errno : 0;
if (rc == 0) if (rc == 0 && sem_post(shared->events + id))
rc = sem_post(shared->events + id) ? errno : 0; failure_perror("sem_post(shared)", errno);
#endif #else
#error "FIXME"
#endif /* MDBX_LOCKING */
#endif /* MDBX_LOCKING != MDBX_LOCKING_SYSV */
return (rc == 0) ? true : false; return (rc == 0) ? true : false;
} }