Skip to content

Commit 736e244

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>
1 parent 8ff8f00 commit 736e244

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

test/threads/opal_condition.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,46 @@ static volatile int thr2_count = 0;
4040

4141
static void *thr1_run(opal_object_t *obj)
4242
{
43-
int i;
4443
clock_t c1, c2;
45-
opal_mutex_lock(&mutex);
4644
c1 = clock();
47-
for (i = 0; i < TEST_COUNT; i++) {
48-
opal_condition_wait(&thr1_cond, &mutex);
49-
opal_condition_signal(&thr2_cond);
45+
opal_mutex_lock(&mutex);
46+
while (TEST_COUNT != thr1_count) {
5047
thr1_count++;
48+
opal_condition_signal(&thr2_cond);
49+
opal_condition_wait(&thr1_cond, &mutex);
5150
}
52-
c2 = clock();
51+
52+
// Whoever gets here first needs to alert the other
53+
// thread for their last iteration.
54+
opal_condition_signal(&thr2_cond);
5355
opal_mutex_unlock(&mutex);
54-
fprintf(stderr, "thr1: time per iteration: %ld usec\n", (long) ((c2 - c1) / TEST_COUNT));
56+
c2 = clock();
57+
fprintf(stderr, "thr1: time per iteration: %ld uses\n", (long)((c2 - c1) / TEST_COUNT));
5558
return NULL;
5659
}
5760

5861
static void *thr2_run(opal_object_t *obj)
5962
{
60-
int i;
6163
clock_t c1, c2;
62-
opal_mutex_lock(&mutex);
6364
c1 = clock();
64-
for (i = 0; i < TEST_COUNT; i++) {
65+
opal_mutex_lock(&mutex);
66+
while(TEST_COUNT != thr2_count) {
67+
thr2_count++;
6568
opal_condition_signal(&thr1_cond);
6669
opal_condition_wait(&thr2_cond, &mutex);
67-
thr2_count++;
6870
}
69-
c2 = clock();
71+
72+
// Whoever gets here first needs to alert the other
73+
// thread for the last iteration.
74+
opal_condition_signal(&thr1_cond);
7075
opal_mutex_unlock(&mutex);
71-
fprintf(stderr, "thr2: time per iteration: %ld usec\n", (long) ((c2 - c1) / TEST_COUNT));
76+
77+
c2 = clock();
78+
fprintf(stderr, "thr2: time per iteration: %ld usec\n", (long)((c2 - c1) / TEST_COUNT));
7279
return NULL;
7380
}
7481

82+
7583
int main(int argc, char **argv)
7684
{
7785
int rc;

0 commit comments

Comments
 (0)