Skip to content

Commit f1c03be

Browse files
hjelmnawlauria
authored andcommitted
tests: fix make check on macOS
macOS does not implement the pthread barrier interface. This commit adds an implementation to the broken test to ensure it works on macOS. Signed-off-by: Nathan Hjelm <hjelmn@mac.com> (cherry picked from commit e4a86be)
1 parent cbe6e5c commit f1c03be

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
@@ -22,6 +22,65 @@
2222
#define ITERATIONS 1000000
2323
#define ITEM_COUNT 100
2424

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

0 commit comments

Comments
 (0)