mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-02 00:14:14 +08:00
mdbx-test: support for systems lack of _POSIX_THREAD_PROCESS_SHARED.
Change-Id: I75437b83b430eaa10551a74b786faaba407d7026
This commit is contained in:
parent
3e7944f732
commit
885d5b2121
@ -17,7 +17,6 @@
|
|||||||
void configure_actor(unsigned &last_space_id, const actor_testcase testcase,
|
void configure_actor(unsigned &last_space_id, const actor_testcase testcase,
|
||||||
const char *space_id_cstr, const actor_params ¶ms) {
|
const char *space_id_cstr, const actor_params ¶ms) {
|
||||||
unsigned wait4id = 0;
|
unsigned wait4id = 0;
|
||||||
|
|
||||||
if (params.waitfor_nops) {
|
if (params.waitfor_nops) {
|
||||||
for (auto i = global::actors.rbegin(); i != global::actors.rend(); ++i) {
|
for (auto i = global::actors.rbegin(); i != global::actors.rend(); ++i) {
|
||||||
if (i->is_waitable(params.waitfor_nops)) {
|
if (i->is_waitable(params.waitfor_nops)) {
|
||||||
|
@ -21,22 +21,67 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#if !defined(_POSIX_THREAD_PROCESS_SHARED) || _POSIX_THREAD_PROCESS_SHARED < 1
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif defined(__APPLE__)
|
||||||
#include "darwin/pthread_barrier.c"
|
#include "darwin/pthread_barrier.c"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if __cplusplus >= 201103L
|
||||||
|
#include <atomic>
|
||||||
|
static __inline __maybe_unused int atomic_decrement(std::atomic_int *p) {
|
||||||
|
return std::atomic_fetch_sub(p, 1) - 1;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static __inline __maybe_unused int atomic_decrement(volatile int *p) {
|
||||||
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
|
return __sync_sub_and_fetch(p, 1);
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
STATIC_ASSERT(sizeof(volatile long) == sizeof(volatile int));
|
||||||
|
return _InterlockedDecrement((volatile long *)p);
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
return OSAtomicDecrement32Barrier((volatile int *)p);
|
||||||
|
#else
|
||||||
|
#error FIXME: Unsupported compiler
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif /* C++11 */
|
||||||
|
|
||||||
struct shared_t {
|
struct shared_t {
|
||||||
|
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0
|
||||||
pthread_barrier_t barrier;
|
pthread_barrier_t barrier;
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
size_t conds_size;
|
size_t count;
|
||||||
pthread_cond_t conds[1];
|
pthread_cond_t events[1];
|
||||||
|
#else
|
||||||
|
struct {
|
||||||
|
#if __cplusplus >= 201103L
|
||||||
|
std::atomic_int countdown;
|
||||||
|
#else
|
||||||
|
volatile int countdown;
|
||||||
|
#endif /* C++11 */
|
||||||
|
sem_t sema;
|
||||||
|
} barrier;
|
||||||
|
size_t count;
|
||||||
|
sem_t events[1];
|
||||||
|
#endif /* _POSIX_THREAD_PROCESS_SHARED */
|
||||||
};
|
};
|
||||||
|
|
||||||
static shared_t *shared;
|
static shared_t *shared;
|
||||||
|
|
||||||
void osal_wait4barrier(void) {
|
void osal_wait4barrier(void) {
|
||||||
assert(shared != nullptr && shared != MAP_FAILED);
|
assert(shared != nullptr && shared != MAP_FAILED);
|
||||||
|
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0
|
||||||
int rc = pthread_barrier_wait(&shared->barrier);
|
int rc = pthread_barrier_wait(&shared->barrier);
|
||||||
|
#else
|
||||||
|
int rc = (atomic_decrement(&shared->barrier.countdown) > 0 &&
|
||||||
|
sem_wait(&shared->barrier.sema))
|
||||||
|
? errno
|
||||||
|
: 0;
|
||||||
|
if (rc == 0)
|
||||||
|
rc = sem_post(&shared->barrier.sema) ? errno : 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) {
|
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) {
|
||||||
failure_perror("pthread_barrier_wait(shared)", rc);
|
failure_perror("pthread_barrier_wait(shared)", rc);
|
||||||
}
|
}
|
||||||
@ -45,22 +90,44 @@ void osal_wait4barrier(void) {
|
|||||||
void osal_setup(const std::vector<actor_config> &actors) {
|
void osal_setup(const std::vector<actor_config> &actors) {
|
||||||
assert(shared == nullptr);
|
assert(shared == nullptr);
|
||||||
|
|
||||||
pthread_mutexattr_t mutexattr;
|
shared = (shared_t *)mmap(
|
||||||
int rc = pthread_mutexattr_init(&mutexattr);
|
nullptr, sizeof(shared_t) + actors.size() * sizeof(shared->events[0]),
|
||||||
if (rc)
|
PROT_READ | PROT_WRITE,
|
||||||
failure_perror("pthread_mutexattr_init()", rc);
|
MAP_SHARED | MAP_ANONYMOUS
|
||||||
rc = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
|
#ifdef MAP_HASSEMAPHORE
|
||||||
if (rc)
|
| MAP_HASSEMAPHORE
|
||||||
failure_perror("pthread_mutexattr_setpshared()", rc);
|
#endif
|
||||||
|
,
|
||||||
|
-1, 0);
|
||||||
|
if (MAP_FAILED == (void *)shared)
|
||||||
|
failure_perror("mmap(shared_conds)", errno);
|
||||||
|
|
||||||
|
shared->count = actors.size() + 1;
|
||||||
|
|
||||||
|
#if defined(__APPLE__) || (defined(_POSIX_THREAD_PROCESS_SHARED) && \
|
||||||
|
_POSIX_THREAD_PROCESS_SHARED > 0)
|
||||||
|
|
||||||
pthread_barrierattr_t barrierattr;
|
pthread_barrierattr_t barrierattr;
|
||||||
rc = pthread_barrierattr_init(&barrierattr);
|
int rc = pthread_barrierattr_init(&barrierattr);
|
||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("pthread_barrierattr_init()", rc);
|
failure_perror("pthread_barrierattr_init()", rc);
|
||||||
rc = pthread_barrierattr_setpshared(&barrierattr, PTHREAD_PROCESS_SHARED);
|
rc = pthread_barrierattr_setpshared(&barrierattr, PTHREAD_PROCESS_SHARED);
|
||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("pthread_barrierattr_setpshared()", rc);
|
failure_perror("pthread_barrierattr_setpshared()", rc);
|
||||||
|
|
||||||
|
rc = pthread_barrier_init(&shared->barrier, &barrierattr, shared->count);
|
||||||
|
if (rc)
|
||||||
|
failure_perror("pthread_barrier_init(shared)", rc);
|
||||||
|
pthread_barrierattr_destroy(&barrierattr);
|
||||||
|
|
||||||
|
pthread_mutexattr_t mutexattr;
|
||||||
|
rc = pthread_mutexattr_init(&mutexattr);
|
||||||
|
if (rc)
|
||||||
|
failure_perror("pthread_mutexattr_init()", rc);
|
||||||
|
rc = pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
|
||||||
|
if (rc)
|
||||||
|
failure_perror("pthread_mutexattr_setpshared()", rc);
|
||||||
|
|
||||||
pthread_condattr_t condattr;
|
pthread_condattr_t condattr;
|
||||||
rc = pthread_condattr_init(&condattr);
|
rc = pthread_condattr_init(&condattr);
|
||||||
if (rc)
|
if (rc)
|
||||||
@ -69,64 +136,76 @@ void osal_setup(const std::vector<actor_config> &actors) {
|
|||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("pthread_condattr_setpshared()", rc);
|
failure_perror("pthread_condattr_setpshared()", rc);
|
||||||
|
|
||||||
shared = (shared_t *)mmap(
|
|
||||||
nullptr, sizeof(shared_t) + actors.size() * sizeof(pthread_cond_t),
|
|
||||||
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
|
|
||||||
if (MAP_FAILED == (void *)shared)
|
|
||||||
failure_perror("mmap(shared_conds)", errno);
|
|
||||||
|
|
||||||
rc = pthread_mutex_init(&shared->mutex, &mutexattr);
|
rc = pthread_mutex_init(&shared->mutex, &mutexattr);
|
||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("pthread_mutex_init(shared)", rc);
|
failure_perror("pthread_mutex_init(shared)", rc);
|
||||||
|
|
||||||
rc = pthread_barrier_init(&shared->barrier, &barrierattr, actors.size() + 1);
|
for (size_t i = 0; i < shared->count; ++i) {
|
||||||
if (rc)
|
pthread_cond_t *event = &shared->events[i];
|
||||||
failure_perror("pthread_barrier_init(shared)", rc);
|
|
||||||
|
|
||||||
const size_t n = actors.size() + 1;
|
|
||||||
for (size_t i = 0; i < n; ++i) {
|
|
||||||
pthread_cond_t *event = &shared->conds[i];
|
|
||||||
rc = pthread_cond_init(event, &condattr);
|
rc = pthread_cond_init(event, &condattr);
|
||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("pthread_cond_init(shared)", rc);
|
failure_perror("pthread_cond_init(shared)", rc);
|
||||||
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));
|
||||||
}
|
}
|
||||||
shared->conds_size = actors.size() + 1;
|
|
||||||
|
|
||||||
pthread_barrierattr_destroy(&barrierattr);
|
|
||||||
pthread_condattr_destroy(&condattr);
|
pthread_condattr_destroy(&condattr);
|
||||||
pthread_mutexattr_destroy(&mutexattr);
|
pthread_mutexattr_destroy(&mutexattr);
|
||||||
|
#else
|
||||||
|
shared->barrier.countdown = shared->count;
|
||||||
|
int rc = sem_init(&shared->barrier.sema, true, 1) ? errno : 0;
|
||||||
|
if (rc)
|
||||||
|
failure_perror("sem_init(shared.barrier)", rc);
|
||||||
|
for (size_t i = 0; i < shared->count; ++i) {
|
||||||
|
sem_t *event = &shared->events[i];
|
||||||
|
rc = sem_init(event, true, 0) ? errno : 0;
|
||||||
|
if (rc)
|
||||||
|
failure_perror("sem_init(shared.event)", rc);
|
||||||
|
log_trace("osal_setup: event(shared sem_init) %" PRIuPTR " -> %p", i,
|
||||||
|
__Wpedantic_format_voidptr(event));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void osal_broadcast(unsigned id) {
|
void osal_broadcast(unsigned id) {
|
||||||
assert(shared != nullptr && shared != MAP_FAILED);
|
assert(shared != nullptr && shared != MAP_FAILED);
|
||||||
log_trace("osal_broadcast: event %u", id);
|
log_trace("osal_broadcast: event %u", id);
|
||||||
if (id >= shared->conds_size)
|
if (id >= shared->count)
|
||||||
failure("osal_broadcast: id > limit");
|
failure("osal_broadcast: id > limit");
|
||||||
int rc = pthread_cond_broadcast(shared->conds + id);
|
#if defined(_POSIX_THREAD_PROCESS_SHARED) && _POSIX_THREAD_PROCESS_SHARED > 0
|
||||||
|
int rc = pthread_cond_broadcast(shared->events + id);
|
||||||
|
if (rc)
|
||||||
|
failure_perror("pthread_cond_broadcast(shared)", rc);
|
||||||
|
#else
|
||||||
|
int rc = sem_post(shared->events + id) ? errno : 0;
|
||||||
if (rc)
|
if (rc)
|
||||||
failure_perror("sem_post(shared)", rc);
|
failure_perror("sem_post(shared)", rc);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int osal_waitfor(unsigned id) {
|
int osal_waitfor(unsigned id) {
|
||||||
assert(shared != nullptr && shared != MAP_FAILED);
|
assert(shared != nullptr && shared != MAP_FAILED);
|
||||||
|
|
||||||
log_trace("osal_waitfor: event %u", id);
|
log_trace("osal_waitfor: event %u", id);
|
||||||
if (id >= shared->conds_size)
|
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
|
||||||
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);
|
||||||
|
|
||||||
rc = pthread_cond_wait(shared->conds + id, &shared->mutex);
|
rc = pthread_cond_wait(shared->events + id, &shared->mutex);
|
||||||
if (rc && rc != EINTR)
|
if (rc && rc != EINTR)
|
||||||
failure_perror("pthread_cond_wait(shared)", rc);
|
failure_perror("pthread_cond_wait(shared)", rc);
|
||||||
|
|
||||||
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
|
||||||
|
int rc = sem_wait(shared->events + id) ? errno : 0;
|
||||||
|
if (rc == 0)
|
||||||
|
rc = sem_post(shared->events + id) ? errno : 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
return (rc == 0) ? true : false;
|
return (rc == 0) ? true : false;
|
||||||
}
|
}
|
||||||
@ -309,9 +388,16 @@ void osal_udelay(unsigned us) {
|
|||||||
|
|
||||||
static unsigned threshold_us;
|
static unsigned threshold_us;
|
||||||
if (threshold_us == 0) {
|
if (threshold_us == 0) {
|
||||||
|
#ifdef CLOCK_PROCESS_CPUTIME_ID
|
||||||
if (clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)) {
|
if (clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts)) {
|
||||||
int rc = errno;
|
int rc = errno;
|
||||||
failure_perror("clock_getres(CLOCK_PROCESS_CPUTIME_ID)", rc);
|
log_warning("clock_getres(CLOCK_PROCESS_CPUTIME_ID), failed errno %d",
|
||||||
|
rc);
|
||||||
|
}
|
||||||
|
#endif /* CLOCK_PROCESS_CPUTIME_ID */
|
||||||
|
if (threshold_us == 0 && clock_getres(CLOCK_MONOTONIC, &ts)) {
|
||||||
|
int rc = errno;
|
||||||
|
failure_perror("clock_getres(CLOCK_MONOTONIC)", rc);
|
||||||
}
|
}
|
||||||
chrono::time threshold = chrono::from_timespec(ts);
|
chrono::time threshold = chrono::from_timespec(ts);
|
||||||
assert(threshold.seconds() == 0);
|
assert(threshold.seconds() == 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user