Skip to content

Commit 75e3bf9

Browse files
authored
Merge pull request #8833 from hjelmn/fix_make_check_macos
tests: fix make check on macOS
2 parents 8e00f1d + e4a86be commit 75e3bf9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

test/threads/opal_atomic_thread_bench.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,65 @@
2121
#define ITERATIONS 1000000
2222
#define ITEM_COUNT 100
2323

24+
#ifdef __APPLE__
25+
/* pthread barrier implementation copied from stack overflow.
26+
* Source:
27+
* https://stackoverflow.com/questions/3640853/performance-test-sem-t-v-s-dispatch-semaphore-t-and-pthread-once-t-v-s-dispatch
28+
*/
29+
typedef int pthread_barrierattr_t;
30+
struct pthread_barrier_t {
31+
pthread_mutex_t mutex;
32+
pthread_cond_t cond;
33+
int count;
34+
int tripCount;
35+
};
36+
37+
typedef struct pthread_barrier_t pthread_barrier_t;
38+
39+
int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr,
40+
unsigned int count)
41+
{
42+
if (count == 0) {
43+
errno = EINVAL;
44+
return -1;
45+
}
46+
if (pthread_mutex_init(&barrier->mutex, 0) < 0) {
47+
return -1;
48+
}
49+
if (pthread_cond_init(&barrier->cond, 0) < 0) {
50+
pthread_mutex_destroy(&barrier->mutex);
51+
return -1;
52+
}
53+
barrier->tripCount = count;
54+
barrier->count = 0;
55+
56+
return 0;
57+
}
58+
59+
int pthread_barrier_destroy(pthread_barrier_t *barrier)
60+
{
61+
pthread_cond_destroy(&barrier->cond);
62+
pthread_mutex_destroy(&barrier->mutex);
63+
return 0;
64+
}
65+
66+
int pthread_barrier_wait(pthread_barrier_t *barrier)
67+
{
68+
pthread_mutex_lock(&barrier->mutex);
69+
++(barrier->count);
70+
if (barrier->count >= barrier->tripCount) {
71+
barrier->count = 0;
72+
pthread_cond_broadcast(&barrier->cond);
73+
pthread_mutex_unlock(&barrier->mutex);
74+
return 1;
75+
}
76+
77+
pthread_cond_wait(&barrier->cond, &(barrier->mutex));
78+
pthread_mutex_unlock(&barrier->mutex);
79+
return 0;
80+
}
81+
#endif
82+
2483
static opal_atomic_int64_t var_64 = 0;
2584
static opal_atomic_int32_t var_32 = 0;
2685
static pthread_barrier_t barrier;

0 commit comments

Comments
 (0)