1 | // Copyright (C) 2001-2003 |
---|
2 | // William E. Kempf |
---|
3 | // |
---|
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #include <boost/thread/detail/config.hpp> |
---|
8 | |
---|
9 | #include <boost/thread/condition.hpp> |
---|
10 | #include <boost/thread/thread.hpp> |
---|
11 | #include <boost/thread/xtime.hpp> |
---|
12 | |
---|
13 | #include <boost/test/unit_test.hpp> |
---|
14 | |
---|
15 | #include <libs/thread/test/util.inl> |
---|
16 | |
---|
17 | struct condition_test_data |
---|
18 | { |
---|
19 | condition_test_data() : notified(0), awoken(0) { } |
---|
20 | |
---|
21 | boost::mutex mutex; |
---|
22 | boost::condition condition; |
---|
23 | int notified; |
---|
24 | int awoken; |
---|
25 | }; |
---|
26 | |
---|
27 | void condition_test_thread(condition_test_data* data) |
---|
28 | { |
---|
29 | boost::mutex::scoped_lock lock(data->mutex); |
---|
30 | BOOST_CHECK(lock ? true : false); |
---|
31 | while (!(data->notified > 0)) |
---|
32 | data->condition.wait(lock); |
---|
33 | BOOST_CHECK(lock ? true : false); |
---|
34 | data->awoken++; |
---|
35 | } |
---|
36 | |
---|
37 | struct cond_predicate |
---|
38 | { |
---|
39 | cond_predicate(int& var, int val) : _var(var), _val(val) { } |
---|
40 | |
---|
41 | bool operator()() { return _var == _val; } |
---|
42 | |
---|
43 | int& _var; |
---|
44 | int _val; |
---|
45 | }; |
---|
46 | |
---|
47 | void condition_test_waits(condition_test_data* data) |
---|
48 | { |
---|
49 | boost::mutex::scoped_lock lock(data->mutex); |
---|
50 | BOOST_CHECK(lock ? true : false); |
---|
51 | |
---|
52 | // Test wait. |
---|
53 | while (data->notified != 1) |
---|
54 | data->condition.wait(lock); |
---|
55 | BOOST_CHECK(lock ? true : false); |
---|
56 | BOOST_CHECK_EQUAL(data->notified, 1); |
---|
57 | data->awoken++; |
---|
58 | data->condition.notify_one(); |
---|
59 | |
---|
60 | // Test predicate wait. |
---|
61 | data->condition.wait(lock, cond_predicate(data->notified, 2)); |
---|
62 | BOOST_CHECK(lock ? true : false); |
---|
63 | BOOST_CHECK_EQUAL(data->notified, 2); |
---|
64 | data->awoken++; |
---|
65 | data->condition.notify_one(); |
---|
66 | |
---|
67 | // Test timed_wait. |
---|
68 | boost::xtime xt = delay(10); |
---|
69 | while (data->notified != 3) |
---|
70 | data->condition.timed_wait(lock, xt); |
---|
71 | BOOST_CHECK(lock ? true : false); |
---|
72 | BOOST_CHECK_EQUAL(data->notified, 3); |
---|
73 | data->awoken++; |
---|
74 | data->condition.notify_one(); |
---|
75 | |
---|
76 | // Test predicate timed_wait. |
---|
77 | xt = delay(10); |
---|
78 | cond_predicate pred(data->notified, 4); |
---|
79 | BOOST_CHECK(data->condition.timed_wait(lock, xt, pred)); |
---|
80 | BOOST_CHECK(lock ? true : false); |
---|
81 | BOOST_CHECK(pred()); |
---|
82 | BOOST_CHECK_EQUAL(data->notified, 4); |
---|
83 | data->awoken++; |
---|
84 | data->condition.notify_one(); |
---|
85 | } |
---|
86 | |
---|
87 | void do_test_condition_notify_one() |
---|
88 | { |
---|
89 | condition_test_data data; |
---|
90 | |
---|
91 | boost::thread thread(bind(&condition_test_thread, &data)); |
---|
92 | |
---|
93 | { |
---|
94 | boost::mutex::scoped_lock lock(data.mutex); |
---|
95 | BOOST_CHECK(lock ? true : false); |
---|
96 | data.notified++; |
---|
97 | data.condition.notify_one(); |
---|
98 | } |
---|
99 | |
---|
100 | thread.join(); |
---|
101 | BOOST_CHECK_EQUAL(data.awoken, 1); |
---|
102 | } |
---|
103 | |
---|
104 | void test_condition_notify_one() |
---|
105 | { |
---|
106 | timed_test(&do_test_condition_notify_one, 2, execution_monitor::use_mutex); |
---|
107 | } |
---|
108 | |
---|
109 | void do_test_condition_notify_all() |
---|
110 | { |
---|
111 | const int NUMTHREADS = 5; |
---|
112 | boost::thread_group threads; |
---|
113 | condition_test_data data; |
---|
114 | |
---|
115 | for (int i = 0; i < NUMTHREADS; ++i) |
---|
116 | threads.create_thread(bind(&condition_test_thread, &data)); |
---|
117 | |
---|
118 | { |
---|
119 | boost::mutex::scoped_lock lock(data.mutex); |
---|
120 | BOOST_CHECK(lock ? true : false); |
---|
121 | data.notified++; |
---|
122 | data.condition.notify_all(); |
---|
123 | } |
---|
124 | |
---|
125 | threads.join_all(); |
---|
126 | BOOST_CHECK_EQUAL(data.awoken, NUMTHREADS); |
---|
127 | } |
---|
128 | |
---|
129 | void test_condition_notify_all() |
---|
130 | { |
---|
131 | // We should have already tested notify_one here, so |
---|
132 | // a timed test with the default execution_monitor::use_condition |
---|
133 | // should be OK, and gives the fastest performance |
---|
134 | timed_test(&do_test_condition_notify_all, 3); |
---|
135 | } |
---|
136 | |
---|
137 | void do_test_condition_waits() |
---|
138 | { |
---|
139 | condition_test_data data; |
---|
140 | |
---|
141 | boost::thread thread(bind(&condition_test_waits, &data)); |
---|
142 | |
---|
143 | { |
---|
144 | boost::mutex::scoped_lock lock(data.mutex); |
---|
145 | BOOST_CHECK(lock ? true : false); |
---|
146 | |
---|
147 | boost::thread::sleep(delay(1)); |
---|
148 | data.notified++; |
---|
149 | data.condition.notify_one(); |
---|
150 | while (data.awoken != 1) |
---|
151 | data.condition.wait(lock); |
---|
152 | BOOST_CHECK(lock ? true : false); |
---|
153 | BOOST_CHECK_EQUAL(data.awoken, 1); |
---|
154 | |
---|
155 | boost::thread::sleep(delay(1)); |
---|
156 | data.notified++; |
---|
157 | data.condition.notify_one(); |
---|
158 | while (data.awoken != 2) |
---|
159 | data.condition.wait(lock); |
---|
160 | BOOST_CHECK(lock ? true : false); |
---|
161 | BOOST_CHECK_EQUAL(data.awoken, 2); |
---|
162 | |
---|
163 | boost::thread::sleep(delay(1)); |
---|
164 | data.notified++; |
---|
165 | data.condition.notify_one(); |
---|
166 | while (data.awoken != 3) |
---|
167 | data.condition.wait(lock); |
---|
168 | BOOST_CHECK(lock ? true : false); |
---|
169 | BOOST_CHECK_EQUAL(data.awoken, 3); |
---|
170 | |
---|
171 | boost::thread::sleep(delay(1)); |
---|
172 | data.notified++; |
---|
173 | data.condition.notify_one(); |
---|
174 | while (data.awoken != 4) |
---|
175 | data.condition.wait(lock); |
---|
176 | BOOST_CHECK(lock ? true : false); |
---|
177 | BOOST_CHECK_EQUAL(data.awoken, 4); |
---|
178 | } |
---|
179 | |
---|
180 | thread.join(); |
---|
181 | BOOST_CHECK_EQUAL(data.awoken, 4); |
---|
182 | } |
---|
183 | |
---|
184 | void test_condition_waits() |
---|
185 | { |
---|
186 | // We should have already tested notify_one here, so |
---|
187 | // a timed test with the default execution_monitor::use_condition |
---|
188 | // should be OK, and gives the fastest performance |
---|
189 | timed_test(&do_test_condition_waits, 12); |
---|
190 | } |
---|
191 | |
---|
192 | boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[]) |
---|
193 | { |
---|
194 | boost::unit_test_framework::test_suite* test = |
---|
195 | BOOST_TEST_SUITE("Boost.Threads: condition test suite"); |
---|
196 | |
---|
197 | test->add(BOOST_TEST_CASE(&test_condition_notify_one)); |
---|
198 | test->add(BOOST_TEST_CASE(&test_condition_notify_all)); |
---|
199 | test->add(BOOST_TEST_CASE(&test_condition_waits)); |
---|
200 | |
---|
201 | return test; |
---|
202 | } |
---|