Skip to content

Commit aead414

Browse files
committed
test/threads: Fix hang opal_condition.c
Simplify the loop to get rid of the i, add a signal at the end to ensure the other thread can perform the last increment, and not hang waiting. Signed-off-by: Austen Lauria <awlauria@us.ibm.com> (cherry picked from commit 736e244)
1 parent f1c03be commit aead414

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

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)