Skip to content

Commit ab935d1

Browse files
authored
Merge pull request #9628 from awlauria/make_check_picks
v5.0.x: tests: Backporting some fixes from master
2 parents cbe6e5c + bbb5479 commit ab935d1

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

test/threads/Makefile.am

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ check_PROGRAMS = \
2828
opal_condition \
2929
opal_atomic_thread_bench
3030

31-
# JMS possibly to be re-added when #1232 is fixed
32-
#TESTS = $(check_PROGRAMS)
33-
TESTS =
31+
TESTS = $(check_PROGRAMS)
3432

3533
opal_thread_SOURCES = opal_thread.c
3634
opal_thread_LDADD = \

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;

test/threads/opal_condition.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,40 +42,47 @@ static volatile int thr2_count = 0;
4242

4343
static void* thr1_run(opal_object_t* obj)
4444
{
45-
int i;
4645
clock_t c1, c2;
47-
opal_mutex_lock(&mutex);
4846
c1 = clock();
49-
for(i=0; i<TEST_COUNT; i++) {
50-
opal_condition_wait(&thr1_cond, &mutex);
51-
opal_condition_signal(&thr2_cond);
47+
opal_mutex_lock(&mutex);
48+
while (TEST_COUNT != thr1_count) {
5249
thr1_count++;
50+
opal_condition_signal(&thr2_cond);
51+
opal_condition_wait(&thr1_cond, &mutex);
5352
}
54-
c2 = clock();
53+
54+
// Whoever gets here first needs to alert the other
55+
// thread for their last iteration.
56+
opal_condition_signal(&thr2_cond);
5557
opal_mutex_unlock(&mutex);
56-
fprintf(stderr, "thr1: time per iteration: %ld usec\n", (long)((c2 - c1) / TEST_COUNT));
58+
c2 = clock();
59+
fprintf(stderr, "thr1: time per iteration: %ld uses\n", (long)((c2 - c1) / TEST_COUNT));
5760
return NULL;
5861
}
5962

6063
static void* thr2_run(opal_object_t* obj)
6164
{
62-
int i;
6365
clock_t c1, c2;
64-
opal_mutex_lock(&mutex);
6566
c1 = clock();
66-
for(i=0; i<TEST_COUNT; i++) {
67+
opal_mutex_lock(&mutex);
68+
while(TEST_COUNT != thr2_count) {
69+
thr2_count++;
6770
opal_condition_signal(&thr1_cond);
6871
opal_condition_wait(&thr2_cond, &mutex);
69-
thr2_count++;
7072
}
71-
c2 = clock();
73+
74+
// Whoever gets here first needs to alert the other
75+
// thread for the last iteration.
76+
opal_condition_signal(&thr1_cond);
7277
opal_mutex_unlock(&mutex);
78+
79+
c2 = clock();
7380
fprintf(stderr, "thr2: time per iteration: %ld usec\n", (long)((c2 - c1) / TEST_COUNT));
7481
return NULL;
7582
}
7683

7784

78-
int main(int argc, char** argv)
85+
int main(int argc, char **argv)
7986
{
8087
int rc;
8188
opal_thread_t* thr1;

0 commit comments

Comments
 (0)