diff options
author | David Young <dyoung@hdfgroup.org> | 2020-02-27 02:38:57 (GMT) |
---|---|---|
committer | David Young <dyoung@hdfgroup.org> | 2020-05-20 14:31:55 (GMT) |
commit | 8c5e0a1e53cf7b1e2b7c32cb0d7051c28dae7a16 (patch) | |
tree | 4828ce62efe7db4403b86fe3e255d7809674b6e5 /test/thread_id.c | |
parent | eeb8a5a754f1a226d7ab609343b8c57238afee2b (diff) | |
download | hdf5-8c5e0a1e53cf7b1e2b7c32cb0d7051c28dae7a16.zip hdf5-8c5e0a1e53cf7b1e2b7c32cb0d7051c28dae7a16.tar.gz hdf5-8c5e0a1e53cf7b1e2b7c32cb0d7051c28dae7a16.tar.bz2 |
Implement pthread_barrier(3) for Darwin using a counter, condition variable,
and mutex. Untested.
Diffstat (limited to 'test/thread_id.c')
-rw-r--r-- | test/thread_id.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/test/thread_id.c b/test/thread_id.c index af36625..4637d9c 100644 --- a/test/thread_id.c +++ b/test/thread_id.c @@ -42,6 +42,129 @@ my_errx(int code, const char *fmt, ...) #if defined(H5_HAVE_THREADSAFE) && !defined(H5_HAVE_WIN_THREADS) +#if defined(H5_HAVE_DARWIN) + +typedef struct _pthread_barrierattr { + uint8_t unused; +} pthread_barrierattr_t; + +typedef struct _pthread_barrier { + uint32_t magic; + unsigned int count; + uint64_t nentered; + pthread_cond_t cv; + pthread_mutex_t mtx; +} pthread_barrier_t; + +int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, + unsigned int); +int pthread_barrier_wait(pthread_barrier_t *); +int pthread_barrier_destroy(pthread_barrier_t *); + +static const uint32_t barrier_magic = 0xf00dd00f; + +int +pthread_barrier_init(pthread_barrier_t *barrier, + const pthread_barrierattr_t *attr, unsigned int count) +{ + int rc; + + if (count == 0) + return EINVAL; + + if (attr != NULL) + return EINVAL; + + memset(barrier, 0, sizeof(*barrier)); + + barrier->count = count; + + if ((rc = pthread_cond_init(&barrier->cv, NULL)) != 0) + return rc; + + if ((rc = pthread_mutex_init(&barrier->mtx, NULL)) != 0) { + (void)pthread_cond_destroy(&barrier->cv); + return rc; + } + + barrier->magic = barrier_magic; + + return 0; +} + +static void +barrier_lock(pthread_barrier_t *barrier) +{ + int rc; + + if ((rc = pthread_mutex_lock(&barrier->mtx)) != 0) { + my_errx(exit_failure, "%s: pthread_mutex_lock: %s", __func__, + strerror(rc)); + } +} + +static void +barrier_unlock(pthread_barrier_t *barrier) +{ + int rc; + + if ((rc = pthread_mutex_unlock(&barrier->mtx)) != 0) { + my_errx(exit_failure, "%s: pthread_mutex_unlock: %s", __func__, + strerror(rc)); + } +} + +int +pthread_barrier_destroy(pthread_barrier_t *barrier) +{ + int rc; + + barrier_lock(barrier); + if (barrier->magic != barrier_magic) + rc = EINVAL; + else if (barrier->count != 0) + rc = EBUSY; + else { + rc = 0; + barrier->magic = ~barrier->magic; + } + barrier_unlock(barrier); + + if (rc != 0) + return rc; + + (void)pthread_cond_destroy(&barrier->cv); + (void)pthread_mutex_destroy(&barrier->mtx); + + return 0; +} + +int +pthread_barrier_wait(pthread_barrier_t *barrier) +{ + int rc; + + if (barrier == NULL) + return EINVAL; + + barrier_lock(barrier); + if (barrier->magic != barrier_magic) { + rc = EINVAL; + goto out; + } + barrier->nentered++; + while (barrier->nentered % barrier->count != 0) { + if ((rc = pthread_cond_wait(&barrier->cv, &barrier->mtx)) != 0) + goto out; + } + rc = pthread_cond_broadcast(&barrier->cv); +out: + barrier_unlock(barrier); + return rc; +} + +#endif /* H5_HAVE_DARWIN */ + static void my_err(int, const char *, ...) H5_ATTR_FORMAT(printf, 2, 3); static void |