This source file includes following definitions.
- thr1_run
- thr2_run
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include "opal_config.h"
21
22 #include <stdio.h>
23 #include <time.h>
24
25 #include "support.h"
26 #include "opal/runtime/opal.h"
27 #include "opal/constants.h"
28 #include "opal/threads/threads.h"
29 #include "opal/threads/condition.h"
30 #include "opal/sys/atomic.h"
31
32 static opal_mutex_t mutex;
33 static opal_condition_t thr1_cond;
34 static opal_condition_t thr2_cond;
35
36 static volatile int thr1_count = 0;
37 static volatile int thr2_count = 0;
38
39
40 #define TEST_COUNT 100000
41
42
43 static void* thr1_run(opal_object_t* obj)
44 {
45 int i;
46 clock_t c1, c2;
47 opal_mutex_lock(&mutex);
48 c1 = clock();
49 for(i=0; i<TEST_COUNT; i++) {
50 opal_condition_wait(&thr1_cond, &mutex);
51 opal_condition_signal(&thr2_cond);
52 thr1_count++;
53 }
54 c2 = clock();
55 opal_mutex_unlock(&mutex);
56 fprintf(stderr, "thr1: time per iteration: %ld usec\n", (long)((c2 - c1) / TEST_COUNT));
57 return NULL;
58 }
59
60 static void* thr2_run(opal_object_t* obj)
61 {
62 int i;
63 clock_t c1, c2;
64 opal_mutex_lock(&mutex);
65 c1 = clock();
66 for(i=0; i<TEST_COUNT; i++) {
67 opal_condition_signal(&thr1_cond);
68 opal_condition_wait(&thr2_cond, &mutex);
69 thr2_count++;
70 }
71 c2 = clock();
72 opal_mutex_unlock(&mutex);
73 fprintf(stderr, "thr2: time per iteration: %ld usec\n", (long)((c2 - c1) / TEST_COUNT));
74 return NULL;
75 }
76
77
78 int main(int argc, char** argv)
79 {
80 int rc;
81 opal_thread_t* thr1;
82 opal_thread_t* thr2;
83
84 test_init("opal_condition_t");
85
86 rc = opal_init(&argc, &argv);
87 test_verify_int(OPAL_SUCCESS, rc);
88 if (OPAL_SUCCESS != rc) {
89 test_finalize();
90 exit(1);
91 }
92 opal_set_using_threads(true);
93
94 OBJ_CONSTRUCT(&mutex, opal_mutex_t);
95 OBJ_CONSTRUCT(&thr1_cond, opal_condition_t);
96 OBJ_CONSTRUCT(&thr2_cond, opal_condition_t);
97
98 thr1 = OBJ_NEW(opal_thread_t);
99 thr2 = OBJ_NEW(opal_thread_t);
100 thr1->t_run = thr1_run;
101 thr2->t_run = thr2_run;
102
103 rc = opal_thread_start(thr1);
104 test_verify_int(OPAL_SUCCESS, rc);
105
106 rc = opal_thread_start(thr2);
107 test_verify_int(OPAL_SUCCESS, rc);
108
109 rc = opal_thread_join(thr1, NULL);
110 test_verify_int(OPAL_SUCCESS, rc);
111 test_verify_int(TEST_COUNT, thr1_count);
112
113 rc = opal_thread_join(thr2, NULL);
114 test_verify_int(OPAL_SUCCESS, rc);
115 test_verify_int(TEST_COUNT, thr2_count);
116
117 opal_finalize();
118
119 return test_finalize();
120 }