|
22 | 22 | #define ITERATIONS 1000000
|
23 | 23 | #define ITEM_COUNT 100
|
24 | 24 |
|
| 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 | + |
25 | 84 | static opal_atomic_int64_t var_64 = 0;
|
26 | 85 | static opal_atomic_int32_t var_32 = 0;
|
27 | 86 | static pthread_barrier_t barrier;
|
|
0 commit comments