summaryrefslogtreecommitdiffstats
path: root/test/thread_id.c
blob: ed1e0a8a611a14ae9efc1c1b80826d1121c45e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * 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
 * following properties:
 *
 * 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"

#if defined(H5_HAVE_THREADSAFE) && !defined(H5_HAVE_WIN_THREADS)

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;

    (void)fprintf(stderr, "thread_id: ");
    va_start(ap, fmt);
    (void)vfprintf(stderr, fmt, ap);
    va_end(ap);
    (void)fputc('\n', stderr);
    exit(code);
}

#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->nentered % 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;
    uint64_t threshold;

    if (barrier == NULL)
        return EINVAL;

    barrier_lock(barrier);
    if (barrier->magic != barrier_magic) {
        rc = EINVAL;
        goto out;
    }
    /* 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;
    barrier->nentered++;
    while (barrier->nentered < threshold) {
        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
my_err(int code, const char *fmt, ...)
{
    va_list ap;
    int     errno_copy = errno;

    (void)fprintf(stderr, "thread_id: ");
    va_start(ap, fmt);
    (void)vfprintf(stderr, fmt, ap);
    va_end(ap);
    (void)fprintf(stderr, ": %s\n", strerror(errno_copy));
    exit(code);
}

#define threads_failure(_call, _result)                                                                      \
    do {                                                                                                     \
        my_errx(EXIT_FAILURE, "%s.%d: " #_call ": %s", __func__, __LINE__, strerror(_result));               \
    } while (false)

#define NTHREADS 5

static volatile bool     failed = false;
static pthread_barrier_t barrier;
static bool              used[NTHREADS];
static pthread_mutex_t   used_lock;

static void
atomic_printf(const char *fmt, ...)
{
    char    buf[80];
    va_list ap;
    ssize_t nprinted, nwritten;

    va_start(ap, fmt);
    nprinted = vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);

    if (nprinted == -1)
        my_err(EXIT_FAILURE, "%s.%d: vsnprintf", __func__, __LINE__);
    else if (nprinted >= (ssize_t)sizeof(buf))
        my_errx(EXIT_FAILURE, "%s.%d: vsnprintf overflowed", __func__, __LINE__);

    nwritten = HDwrite(STDOUT_FILENO, buf, (size_t)nprinted);
    if (nwritten < nprinted) {
        my_errx(EXIT_FAILURE, "%s.%d: write error or short write", __func__, __LINE__);
    }
}

/* 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
 * 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.
 */
static void *
thread_main(void H5_ATTR_UNUSED *arg)
{
    uint64_t ntid, tid;

    tid = H5TS_thread_id();

    if (tid < 1 || NTHREADS < tid) {
        atomic_printf("unexpected tid %" PRIu64 " FAIL\n", tid);
        goto pre_barrier_error;
    }
    pthread_mutex_lock(&used_lock);
    if (used[tid - 1]) {
        atomic_printf("reused tid %" PRIu64 " FAIL\n", tid);
        pthread_mutex_unlock(&used_lock);
        goto pre_barrier_error;
    }
    used[tid - 1] = true;
    pthread_mutex_unlock(&used_lock);

    atomic_printf("tid %" PRIu64 " in [1, %d] PASS\n", tid, NTHREADS);
    pthread_barrier_wait(&barrier);

    ntid = H5TS_thread_id();
    if (ntid != tid) {
        atomic_printf("tid changed from %" PRIu64 " to %" PRIu64 " FAIL\n", tid, ntid);
        failed = true;
    }
    return NULL;
pre_barrier_error:
    pthread_barrier_wait(&barrier);
    failed = true;
    return NULL;
}

int
main(void)
{
    int       i, rc, times;
    pthread_t threads[NTHREADS];

    /* Run H5open() to initialize the library's thread-ID freelist,
     * mutex, etc.
     */
    if (H5open() != SUCCEED)
        my_errx(EXIT_FAILURE, "%s.%d: H5open failed", __func__, __LINE__);

    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++)
            used[i] = false; // access synchronized by thread create/join

        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
                my_errx(EXIT_FAILURE, "thread ID %d did not run.", i + 1);
        }
    }
    if ((rc = pthread_barrier_destroy(&barrier)) != 0)
        threads_failure(pthread_barrier_destroy, rc);
    return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}

#else  /*H5_HAVE_THREADSAFE && !H5_HAVE_WIN_THREADS*/
int
main(void)
{
    fprintf(stderr, "not implemented in this configuration.\n");
    return EXIT_SUCCESS;
}
#endif /*H5_HAVE_THREADSAFE && !H5_HAVE_WIN_THREADS*/