2020-02-04 01:59:14 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
|
* Copyright by The HDF Group. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
|
|
|
* the COPYING file, which can be found at the root of the source code *
|
2021-02-17 22:52:04 +08:00
|
|
|
* distribution tree, or in https://www.hdfgroup.org/licenses. *
|
2020-02-04 01:59:14 +08:00
|
|
|
* If you do not have access to either file, you may request a copy from *
|
|
|
|
* help@hdfgroup.org. *
|
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
|
|
|
|
|
|
/* Check that a thread ID returned by H5TS_thread_id() possesses the
|
2020-04-21 07:12:00 +08:00
|
|
|
* following properties:
|
2020-02-04 01:59:14 +08:00
|
|
|
*
|
|
|
|
* 1 ID >= 1.
|
|
|
|
* 2 The ID is constant over the thread's lifetime.
|
|
|
|
* 3 No two threads share an ID during their lifetimes.
|
|
|
|
* 4 A thread's ID is available for reuse as soon as it is joined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Include required headers. This file tests internal library functions,
|
|
|
|
* so we include the private headers here.
|
|
|
|
*/
|
|
|
|
#include "testhdf5.h"
|
|
|
|
|
2020-08-14 01:13:18 +08:00
|
|
|
#if defined(H5_HAVE_THREADSAFE) && !defined(H5_HAVE_WIN_THREADS)
|
|
|
|
|
2020-02-08 04:45:08 +08:00
|
|
|
static void my_errx(int, const char *, ...) H5_ATTR_FORMAT(printf, 2, 3);
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_errx(int code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2020-02-13 04:07:47 +08:00
|
|
|
(void)HDfprintf(stderr, "thread_id: ");
|
|
|
|
HDva_start(ap, fmt);
|
|
|
|
(void)HDvfprintf(stderr, fmt, ap);
|
|
|
|
HDva_end(ap);
|
|
|
|
(void)HDfputc('\n', stderr);
|
|
|
|
HDexit(code);
|
2020-02-08 04:45:08 +08:00
|
|
|
}
|
|
|
|
|
2020-02-27 10:38:57 +08:00
|
|
|
#if defined(H5_HAVE_DARWIN)
|
|
|
|
|
|
|
|
typedef struct _pthread_barrierattr {
|
|
|
|
uint8_t unused;
|
|
|
|
} pthread_barrierattr_t;
|
|
|
|
|
|
|
|
typedef struct _pthread_barrier {
|
2020-09-30 22:27:10 +08:00
|
|
|
uint32_t magic;
|
|
|
|
unsigned int count;
|
|
|
|
uint64_t nentered;
|
|
|
|
pthread_cond_t cv;
|
2020-02-27 10:38:57 +08:00
|
|
|
pthread_mutex_t mtx;
|
|
|
|
} pthread_barrier_t;
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
int pthread_barrier_init(pthread_barrier_t *, const pthread_barrierattr_t *, unsigned int);
|
2020-02-27 10:38:57 +08:00
|
|
|
int pthread_barrier_wait(pthread_barrier_t *);
|
|
|
|
int pthread_barrier_destroy(pthread_barrier_t *);
|
|
|
|
|
|
|
|
static const uint32_t barrier_magic = 0xf00dd00f;
|
|
|
|
|
|
|
|
int
|
2020-09-30 22:27:10 +08:00
|
|
|
pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
|
2020-02-27 10:38:57 +08:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (attr != NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
2020-10-24 08:13:05 +08:00
|
|
|
HDmemset(barrier, 0, sizeof(*barrier));
|
2020-02-27 10:38:57 +08:00
|
|
|
|
|
|
|
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) {
|
2020-10-24 08:13:05 +08:00
|
|
|
my_errx(EXIT_FAILURE, "%s: pthread_mutex_lock: %s", __func__, HDstrerror(rc));
|
2020-02-27 10:38:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
barrier_unlock(pthread_barrier_t *barrier)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ((rc = pthread_mutex_unlock(&barrier->mtx)) != 0) {
|
2020-10-24 08:13:05 +08:00
|
|
|
my_errx(EXIT_FAILURE, "%s: pthread_mutex_unlock: %s", __func__, HDstrerror(rc));
|
2020-02-27 10:38:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
pthread_barrier_destroy(pthread_barrier_t *barrier)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
barrier_lock(barrier);
|
|
|
|
if (barrier->magic != barrier_magic)
|
|
|
|
rc = EINVAL;
|
2020-02-28 00:17:24 +08:00
|
|
|
else if (barrier->nentered % barrier->count != 0)
|
2020-02-27 10:38:57 +08:00
|
|
|
rc = EBUSY;
|
|
|
|
else {
|
2020-09-30 22:27:10 +08:00
|
|
|
rc = 0;
|
2020-02-27 10:38:57 +08:00
|
|
|
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)
|
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
int rc;
|
2020-02-28 01:27:45 +08:00
|
|
|
uint64_t threshold;
|
2020-02-27 10:38:57 +08:00
|
|
|
|
|
|
|
if (barrier == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
barrier_lock(barrier);
|
|
|
|
if (barrier->magic != barrier_magic) {
|
|
|
|
rc = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-02-28 01:27:45 +08:00
|
|
|
/* Compute the release `threshold`. All threads entering with count = 5
|
|
|
|
* and `nentered` in [0, 4] should be released once `nentered` reaches 5:
|
|
|
|
* call 5 the release `threshold`. All threads entering with count = 5
|
|
|
|
* and `nentered` in [5, 9] should be released once `nentered` reaches 10.
|
|
|
|
*/
|
|
|
|
threshold = (barrier->nentered / barrier->count + 1) * barrier->count;
|
2020-02-27 10:38:57 +08:00
|
|
|
barrier->nentered++;
|
2020-02-28 01:27:45 +08:00
|
|
|
while (barrier->nentered < threshold) {
|
2020-02-27 10:38:57 +08:00
|
|
|
if ((rc = pthread_cond_wait(&barrier->cv, &barrier->mtx)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
rc = pthread_cond_broadcast(&barrier->cv);
|
|
|
|
out:
|
|
|
|
barrier_unlock(barrier);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
#endif /* H5_HAVE_DARWIN */
|
2020-02-27 10:38:57 +08:00
|
|
|
|
2020-02-08 04:45:08 +08:00
|
|
|
static void my_err(int, const char *, ...) H5_ATTR_FORMAT(printf, 2, 3);
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_err(int code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2020-09-30 22:27:10 +08:00
|
|
|
int errno_copy = errno;
|
2020-02-08 04:45:08 +08:00
|
|
|
|
2020-02-13 04:07:47 +08:00
|
|
|
(void)HDfprintf(stderr, "thread_id: ");
|
|
|
|
HDva_start(ap, fmt);
|
|
|
|
(void)HDvfprintf(stderr, fmt, ap);
|
|
|
|
HDva_end(ap);
|
|
|
|
(void)HDfprintf(stderr, ": %s\n", HDstrerror(errno_copy));
|
|
|
|
HDexit(code);
|
2020-02-08 04:45:08 +08:00
|
|
|
}
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
#define threads_failure(_call, _result) \
|
|
|
|
do { \
|
|
|
|
my_errx(EXIT_FAILURE, "%s.%d: " #_call ": %s", __func__, __LINE__, HDstrerror(_result)); \
|
|
|
|
} while (false)
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
#define NTHREADS 5
|
|
|
|
|
2020-09-30 22:27:10 +08:00
|
|
|
static volatile bool failed = false;
|
2020-02-04 01:59:14 +08:00
|
|
|
static pthread_barrier_t barrier;
|
2020-09-30 22:27:10 +08:00
|
|
|
static bool used[NTHREADS];
|
|
|
|
static pthread_mutex_t used_lock;
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
atomic_printf(const char *fmt, ...)
|
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
char buf[80];
|
2020-02-04 01:59:14 +08:00
|
|
|
va_list ap;
|
|
|
|
ssize_t nprinted, nwritten;
|
|
|
|
|
2020-10-24 08:13:05 +08:00
|
|
|
HDva_start(ap, fmt);
|
|
|
|
nprinted = HDvsnprintf(buf, sizeof(buf), fmt, ap);
|
|
|
|
HDva_end(ap);
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
if (nprinted == -1)
|
2020-02-08 04:45:08 +08:00
|
|
|
my_err(EXIT_FAILURE, "%s.%d: vsnprintf", __func__, __LINE__);
|
2020-02-04 01:59:14 +08:00
|
|
|
else if (nprinted >= (ssize_t)sizeof(buf))
|
2020-02-08 04:45:08 +08:00
|
|
|
my_errx(EXIT_FAILURE, "%s.%d: vsnprintf overflowed", __func__, __LINE__);
|
2020-02-04 01:59:14 +08:00
|
|
|
|
2020-10-24 08:13:05 +08:00
|
|
|
nwritten = HDwrite(STDOUT_FILENO, buf, (size_t)nprinted);
|
2020-02-04 01:59:14 +08:00
|
|
|
if (nwritten < nprinted) {
|
2020-09-30 22:27:10 +08:00
|
|
|
my_errx(EXIT_FAILURE, "%s.%d: write error or short write", __func__, __LINE__);
|
2020-02-04 01:59:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Each thread runs this routine. The routine fetches the current
|
|
|
|
* thread's ID, makes sure that it is in the expected range, makes
|
|
|
|
* sure that in this round of testing, no two threads shared the
|
2020-02-28 06:14:44 +08:00
|
|
|
* same ID, and checks that each thread's ID is constant over its lifetime.
|
|
|
|
*
|
|
|
|
* main() checks that every ID in [1, NTHREADS] is used in each round
|
|
|
|
* of testing. All NTHREADS threads synchronize on a barrier after each
|
|
|
|
* has fetched its ID. The barrier guarantees that all threads' lifetimes
|
|
|
|
* overlap at least momentarily, so the IDs will be unique, and there
|
|
|
|
* will be NTHREADS of them. Further, since thread IDs are assigned
|
|
|
|
* starting with 1, and the number of threads with IDs alive never exceeds
|
|
|
|
* NTHREADS, the least ID has to be 1 and the greatest, NTHREADS.
|
2020-02-04 01:59:14 +08:00
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
thread_main(void H5_ATTR_UNUSED *arg)
|
|
|
|
{
|
2020-02-04 06:23:06 +08:00
|
|
|
uint64_t ntid, tid;
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
tid = H5TS_thread_id();
|
|
|
|
|
|
|
|
if (tid < 1 || NTHREADS < tid) {
|
2020-02-04 06:23:06 +08:00
|
|
|
atomic_printf("unexpected tid %" PRIu64 " FAIL\n", tid);
|
2020-02-04 01:59:14 +08:00
|
|
|
goto pre_barrier_error;
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&used_lock);
|
|
|
|
if (used[tid - 1]) {
|
2020-02-04 06:23:06 +08:00
|
|
|
atomic_printf("reused tid %" PRIu64 " FAIL\n", tid);
|
2020-02-04 01:59:14 +08:00
|
|
|
pthread_mutex_unlock(&used_lock);
|
|
|
|
goto pre_barrier_error;
|
|
|
|
}
|
|
|
|
used[tid - 1] = true;
|
|
|
|
pthread_mutex_unlock(&used_lock);
|
|
|
|
|
2020-02-04 06:23:06 +08:00
|
|
|
atomic_printf("tid %" PRIu64 " in [1, %d] PASS\n", tid, NTHREADS);
|
2020-02-04 01:59:14 +08:00
|
|
|
pthread_barrier_wait(&barrier);
|
|
|
|
|
|
|
|
ntid = H5TS_thread_id();
|
|
|
|
if (ntid != tid) {
|
2020-09-30 22:27:10 +08:00
|
|
|
atomic_printf("tid changed from %" PRIu64 " to %" PRIu64 " FAIL\n", tid, ntid);
|
2020-02-04 01:59:14 +08:00
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
pre_barrier_error:
|
|
|
|
pthread_barrier_wait(&barrier);
|
|
|
|
failed = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2020-09-30 22:27:10 +08:00
|
|
|
int i, rc, times;
|
2020-02-04 01:59:14 +08:00
|
|
|
pthread_t threads[NTHREADS];
|
|
|
|
|
|
|
|
/* Run H5open() to initialize the library's thread-ID freelist,
|
|
|
|
* mutex, etc.
|
|
|
|
*/
|
|
|
|
if (H5open() != SUCCEED)
|
2020-02-08 04:45:08 +08:00
|
|
|
my_errx(EXIT_FAILURE, "%s.%d: H5open failed", __func__, __LINE__);
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
if ((rc = pthread_mutex_init(&used_lock, NULL)) == -1)
|
|
|
|
threads_failure(pthread_mutex_init, rc);
|
|
|
|
|
|
|
|
if ((rc = pthread_barrier_init(&barrier, NULL, NTHREADS)) != 0)
|
|
|
|
threads_failure(pthread_barrier_init, rc);
|
|
|
|
|
|
|
|
/* Start the test threads and join them twice to make sure that
|
|
|
|
* the thread IDs are recycled in the second round.
|
|
|
|
*/
|
|
|
|
for (times = 0; times < 2; times++) {
|
|
|
|
|
|
|
|
for (i = 0; i < NTHREADS; i++)
|
2020-09-30 22:27:10 +08:00
|
|
|
used[i] = false; // access synchronized by thread create/join
|
2020-02-04 01:59:14 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
|
|
|
rc = pthread_create(&threads[i], NULL, thread_main, NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
threads_failure(pthread_create, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
|
|
|
rc = pthread_join(threads[i], NULL);
|
|
|
|
if (rc != 0)
|
|
|
|
threads_failure(pthread_join, rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
|
|
|
if (!used[i]) // access synchronized by thread create/join
|
2020-02-08 04:45:08 +08:00
|
|
|
my_errx(EXIT_FAILURE, "thread ID %d did not run.", i + 1);
|
2020-02-04 01:59:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((rc = pthread_barrier_destroy(&barrier)) != 0)
|
|
|
|
threads_failure(pthread_barrier_destroy, rc);
|
|
|
|
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:55:18 +08:00
|
|
|
#else /*H5_HAVE_THREADSAFE && !H5_HAVE_WIN_THREADS*/
|
2020-02-04 01:59:14 +08:00
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
2020-02-07 00:43:37 +08:00
|
|
|
HDfprintf(stderr, "not implemented in this configuration.\n");
|
|
|
|
return EXIT_SUCCESS;
|
2020-02-04 01:59:14 +08:00
|
|
|
}
|
|
|
|
#endif /*H5_HAVE_THREADSAFE && !H5_HAVE_WIN_THREADS*/
|