1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_thread_proc.h"
18 #include "apr_file_io.h"
19 #include "apr_thread_mutex.h"
20 #include "apr_thread_rwlock.h"
21 #include "apr_thread_cond.h"
22 #include "apr_errno.h"
23 #include "apr_general.h"
24 #include "apr_getopt.h"
29 #define MAX_ITER 40000
30 #define MAX_COUNTER 100000
33 static void *APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data);
34 static void *APR_THREAD_FUNC thread_mutex_function(apr_thread_t *thd, void *data);
35 static void *APR_THREAD_FUNC thread_cond_producer(apr_thread_t *thd, void *data);
36 static void *APR_THREAD_FUNC thread_cond_consumer(apr_thread_t *thd, void *data);
38 static apr_thread_mutex_t *thread_mutex;
39 static apr_thread_rwlock_t *rwlock;
40 static int i = 0, x = 0;
42 static int buff[MAX_COUNTER];
45 apr_thread_mutex_t *mutex;
51 apr_thread_mutex_t *mutex;
52 apr_thread_cond_t *cond;
56 static apr_thread_mutex_t *timeout_mutex;
57 static apr_thread_cond_t *timeout_cond;
59 static void *APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data)
65 apr_thread_rwlock_rdlock(rwlock);
68 apr_thread_rwlock_unlock(rwlock);
73 apr_thread_rwlock_wrlock(rwlock);
79 apr_thread_rwlock_unlock(rwlock);
84 static void *APR_THREAD_FUNC thread_mutex_function(apr_thread_t *thd, void *data)
88 /* slight delay to allow things to settle */
93 apr_thread_mutex_lock(thread_mutex);
101 apr_thread_mutex_unlock(thread_mutex);
109 static void *APR_THREAD_FUNC thread_cond_producer(apr_thread_t *thd, void *data)
112 apr_thread_mutex_lock(put.mutex);
113 if (put.nput >= MAX_COUNTER) {
114 apr_thread_mutex_unlock(put.mutex);
117 buff[put.nput] = put.nval;
120 apr_thread_mutex_unlock(put.mutex);
122 apr_thread_mutex_lock(nready.mutex);
123 if (nready.nready == 0)
124 apr_thread_cond_signal(nready.cond);
126 apr_thread_mutex_unlock(nready.mutex);
128 *((int *) data) += 1;
134 static void *APR_THREAD_FUNC thread_cond_consumer(apr_thread_t *thd, void *data)
138 for (i = 0; i < MAX_COUNTER; i++) {
139 apr_thread_mutex_lock(nready.mutex);
140 while (nready.nready == 0)
141 apr_thread_cond_wait(nready.cond, nready.mutex);
143 apr_thread_mutex_unlock(nready.mutex);
146 printf("buff[%d] = %d\n", i, buff[i]);
152 static void test_thread_mutex(CuTest *tc)
154 apr_thread_t *t1, *t2, *t3, *t4;
155 apr_status_t s1, s2, s3, s4;
157 s1 = apr_thread_mutex_create(&thread_mutex, APR_THREAD_MUTEX_DEFAULT, p);
158 CuAssertIntEquals(tc, APR_SUCCESS, s1);
159 CuAssertPtrNotNull(tc, thread_mutex);
164 s1 = apr_thread_create(&t1, NULL, thread_mutex_function, NULL, p);
165 CuAssertIntEquals(tc, APR_SUCCESS, s1);
166 s2 = apr_thread_create(&t2, NULL, thread_mutex_function, NULL, p);
167 CuAssertIntEquals(tc, APR_SUCCESS, s2);
168 s3 = apr_thread_create(&t3, NULL, thread_mutex_function, NULL, p);
169 CuAssertIntEquals(tc, APR_SUCCESS, s3);
170 s4 = apr_thread_create(&t4, NULL, thread_mutex_function, NULL, p);
171 CuAssertIntEquals(tc, APR_SUCCESS, s4);
173 apr_thread_join(&s1, t1);
174 apr_thread_join(&s2, t2);
175 apr_thread_join(&s3, t3);
176 apr_thread_join(&s4, t4);
178 CuAssertIntEquals(tc, MAX_ITER, x);
181 static void test_thread_rwlock(CuTest *tc)
183 apr_thread_t *t1, *t2, *t3, *t4;
184 apr_status_t s1, s2, s3, s4;
186 s1 = apr_thread_rwlock_create(&rwlock, p);
187 apr_assert_success(tc, "rwlock_create", s1);
188 CuAssertPtrNotNull(tc, rwlock);
193 s1 = apr_thread_create(&t1, NULL, thread_rwlock_func, NULL, p);
194 apr_assert_success(tc, "create thread 1", s1);
195 s2 = apr_thread_create(&t2, NULL, thread_rwlock_func, NULL, p);
196 apr_assert_success(tc, "create thread 2", s2);
197 s3 = apr_thread_create(&t3, NULL, thread_rwlock_func, NULL, p);
198 apr_assert_success(tc, "create thread 3", s3);
199 s4 = apr_thread_create(&t4, NULL, thread_rwlock_func, NULL, p);
200 apr_assert_success(tc, "create thread 4", s4);
202 apr_thread_join(&s1, t1);
203 apr_thread_join(&s2, t2);
204 apr_thread_join(&s3, t3);
205 apr_thread_join(&s4, t4);
207 CuAssertIntEquals(tc, MAX_ITER, x);
209 apr_thread_rwlock_destroy(rwlock);
212 static void test_cond(CuTest *tc)
214 apr_thread_t *p1, *p2, *p3, *p4, *c1;
215 apr_status_t s0, s1, s2, s3, s4;
216 int count1, count2, count3, count4;
219 s1 = apr_thread_mutex_create(&put.mutex, APR_THREAD_MUTEX_DEFAULT, p);
220 CuAssertIntEquals(tc, APR_SUCCESS, s1);
221 CuAssertPtrNotNull(tc, put.mutex);
223 s1 = apr_thread_mutex_create(&nready.mutex, APR_THREAD_MUTEX_DEFAULT, p);
224 CuAssertIntEquals(tc, APR_SUCCESS, s1);
225 CuAssertPtrNotNull(tc, nready.mutex);
227 s1 = apr_thread_cond_create(&nready.cond, p);
228 CuAssertIntEquals(tc, APR_SUCCESS, s1);
229 CuAssertPtrNotNull(tc, nready.cond);
231 count1 = count2 = count3 = count4 = 0;
232 put.nput = put.nval = 0;
237 s0 = apr_thread_create(&p1, NULL, thread_cond_producer, &count1, p);
238 CuAssertIntEquals(tc, APR_SUCCESS, s0);
239 s1 = apr_thread_create(&p2, NULL, thread_cond_producer, &count2, p);
240 CuAssertIntEquals(tc, APR_SUCCESS, s1);
241 s2 = apr_thread_create(&p3, NULL, thread_cond_producer, &count3, p);
242 CuAssertIntEquals(tc, APR_SUCCESS, s2);
243 s3 = apr_thread_create(&p4, NULL, thread_cond_producer, &count4, p);
244 CuAssertIntEquals(tc, APR_SUCCESS, s3);
245 s4 = apr_thread_create(&c1, NULL, thread_cond_consumer, NULL, p);
246 CuAssertIntEquals(tc, APR_SUCCESS, s4);
248 apr_thread_join(&s0, p1);
249 apr_thread_join(&s1, p2);
250 apr_thread_join(&s2, p3);
251 apr_thread_join(&s3, p4);
252 apr_thread_join(&s4, c1);
254 sum = count1 + count2 + count3 + count4;
256 printf("count1 = %d count2 = %d count3 = %d count4 = %d\n",
257 count1, count2, count3, count4);
259 CuAssertIntEquals(tc, MAX_COUNTER, sum);
262 static void test_timeoutcond(CuTest *tc)
265 apr_interval_time_t timeout;
266 apr_time_t begin, end;
269 s = apr_thread_mutex_create(&timeout_mutex, APR_THREAD_MUTEX_DEFAULT, p);
270 CuAssertIntEquals(tc, APR_SUCCESS, s);
271 CuAssertPtrNotNull(tc, timeout_mutex);
273 s = apr_thread_cond_create(&timeout_cond, p);
274 CuAssertIntEquals(tc, APR_SUCCESS, s);
275 CuAssertPtrNotNull(tc, timeout_cond);
277 timeout = apr_time_from_sec(5);
279 for (i = 0; i < MAX_RETRY; i++) {
280 apr_thread_mutex_lock(timeout_mutex);
282 begin = apr_time_now();
283 s = apr_thread_cond_timedwait(timeout_cond, timeout_mutex, timeout);
284 end = apr_time_now();
285 apr_thread_mutex_unlock(timeout_mutex);
287 if (s != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(s)) {
290 CuAssertIntEquals(tc, 1, APR_STATUS_IS_TIMEUP(s));
291 CuAssert(tc, "Timer returned too late", end - begin - timeout < 100000);
294 CuAssert(tc, "Too many retries", i < MAX_RETRY);
297 #endif /* !APR_HAS_THREADS */
300 static void threads_not_impl(CuTest *tc)
302 CuNotImpl(tc, "Threads not implemented on this platform");
307 CuSuite *testlock(void)
309 CuSuite *suite = CuSuiteNew("Thread Locks");
312 SUITE_ADD_TEST(suite, threads_not_impl);
314 SUITE_ADD_TEST(suite, test_thread_mutex);
315 SUITE_ADD_TEST(suite, test_thread_rwlock);
316 SUITE_ADD_TEST(suite, test_cond);
317 SUITE_ADD_TEST(suite, test_timeoutcond);