upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testlock.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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"
25 #include "test_apr.h"
26
27 #if APR_HAS_THREADS
28
29 #define MAX_ITER 40000
30 #define MAX_COUNTER 100000
31 #define MAX_RETRY 5
32
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);
37
38 static apr_thread_mutex_t *thread_mutex;
39 static apr_thread_rwlock_t *rwlock;
40 static int i = 0, x = 0;
41
42 static int buff[MAX_COUNTER];
43
44 struct {
45     apr_thread_mutex_t *mutex;
46     int                nput;
47     int                nval;
48 } put;
49
50 struct {
51     apr_thread_mutex_t *mutex;
52     apr_thread_cond_t  *cond;
53     int                nready;
54 } nready;
55
56 static apr_thread_mutex_t *timeout_mutex;
57 static apr_thread_cond_t *timeout_cond;
58
59 static void *APR_THREAD_FUNC thread_rwlock_func(apr_thread_t *thd, void *data)
60 {
61     int exitLoop = 1;
62
63     while (1)
64     {
65         apr_thread_rwlock_rdlock(rwlock);
66         if (i == MAX_ITER)
67             exitLoop = 0;
68         apr_thread_rwlock_unlock(rwlock);
69
70         if (!exitLoop)
71             break;
72
73         apr_thread_rwlock_wrlock(rwlock);
74         if (i != MAX_ITER)
75         {
76             i++;
77             x++;
78         }
79         apr_thread_rwlock_unlock(rwlock);
80     }
81     return NULL;
82
83
84 static void *APR_THREAD_FUNC thread_mutex_function(apr_thread_t *thd, void *data)
85 {
86     int exitLoop = 1;
87
88     /* slight delay to allow things to settle */
89     apr_sleep (1);
90     
91     while (1)
92     {
93         apr_thread_mutex_lock(thread_mutex);
94         if (i == MAX_ITER)
95             exitLoop = 0;
96         else 
97         {
98             i++;
99             x++;
100         }
101         apr_thread_mutex_unlock(thread_mutex);
102
103         if (!exitLoop)
104             break;
105     }
106     return NULL;
107
108
109 static void *APR_THREAD_FUNC thread_cond_producer(apr_thread_t *thd, void *data)
110 {
111     for (;;) {
112         apr_thread_mutex_lock(put.mutex);
113         if (put.nput >= MAX_COUNTER) {
114             apr_thread_mutex_unlock(put.mutex);
115             return NULL;
116         }
117         buff[put.nput] = put.nval;
118         put.nput++;
119         put.nval++;
120         apr_thread_mutex_unlock(put.mutex);
121
122         apr_thread_mutex_lock(nready.mutex);
123         if (nready.nready == 0)
124             apr_thread_cond_signal(nready.cond);
125         nready.nready++;
126         apr_thread_mutex_unlock(nready.mutex);
127
128         *((int *) data) += 1;
129     }
130
131     return NULL;
132 }
133
134 static void *APR_THREAD_FUNC thread_cond_consumer(apr_thread_t *thd, void *data)
135 {
136     int i;
137
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);
142         nready.nready--;
143         apr_thread_mutex_unlock(nready.mutex);
144
145         if (buff[i] != i)
146             printf("buff[%d] = %d\n", i, buff[i]);
147     }
148
149     return NULL;
150 }
151
152 static void test_thread_mutex(CuTest *tc)
153 {
154     apr_thread_t *t1, *t2, *t3, *t4;
155     apr_status_t s1, s2, s3, s4;
156
157     s1 = apr_thread_mutex_create(&thread_mutex, APR_THREAD_MUTEX_DEFAULT, p);
158     CuAssertIntEquals(tc, APR_SUCCESS, s1);
159     CuAssertPtrNotNull(tc, thread_mutex);
160
161     i = 0;
162     x = 0;
163
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);
172
173     apr_thread_join(&s1, t1);
174     apr_thread_join(&s2, t2);
175     apr_thread_join(&s3, t3);
176     apr_thread_join(&s4, t4);
177
178     CuAssertIntEquals(tc, MAX_ITER, x);
179 }
180
181 static void test_thread_rwlock(CuTest *tc)
182 {
183     apr_thread_t *t1, *t2, *t3, *t4;
184     apr_status_t s1, s2, s3, s4;
185
186     s1 = apr_thread_rwlock_create(&rwlock, p);
187     apr_assert_success(tc, "rwlock_create", s1);
188     CuAssertPtrNotNull(tc, rwlock);
189
190     i = 0;
191     x = 0;
192
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);
201
202     apr_thread_join(&s1, t1);
203     apr_thread_join(&s2, t2);
204     apr_thread_join(&s3, t3);
205     apr_thread_join(&s4, t4);
206
207     CuAssertIntEquals(tc, MAX_ITER, x);
208
209     apr_thread_rwlock_destroy(rwlock);
210 }
211
212 static void test_cond(CuTest *tc)
213 {
214     apr_thread_t *p1, *p2, *p3, *p4, *c1;
215     apr_status_t s0, s1, s2, s3, s4;
216     int count1, count2, count3, count4;
217     int sum;
218
219     s1 = apr_thread_mutex_create(&put.mutex, APR_THREAD_MUTEX_DEFAULT, p);
220     CuAssertIntEquals(tc, APR_SUCCESS, s1);
221     CuAssertPtrNotNull(tc, put.mutex);
222
223     s1 = apr_thread_mutex_create(&nready.mutex, APR_THREAD_MUTEX_DEFAULT, p);
224     CuAssertIntEquals(tc, APR_SUCCESS, s1);
225     CuAssertPtrNotNull(tc, nready.mutex);
226
227     s1 = apr_thread_cond_create(&nready.cond, p);
228     CuAssertIntEquals(tc, APR_SUCCESS, s1);
229     CuAssertPtrNotNull(tc, nready.cond);
230
231     count1 = count2 = count3 = count4 = 0;
232     put.nput = put.nval = 0;
233     nready.nready = 0;
234     i = 0;
235     x = 0;
236
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);
247
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);
253
254     sum = count1 + count2 + count3 + count4;
255     /*
256     printf("count1 = %d count2 = %d count3 = %d count4 = %d\n",
257             count1, count2, count3, count4);
258     */
259     CuAssertIntEquals(tc, MAX_COUNTER, sum);
260 }
261
262 static void test_timeoutcond(CuTest *tc)
263 {
264     apr_status_t s;
265     apr_interval_time_t timeout;
266     apr_time_t begin, end;
267     int i;
268
269     s = apr_thread_mutex_create(&timeout_mutex, APR_THREAD_MUTEX_DEFAULT, p);
270     CuAssertIntEquals(tc, APR_SUCCESS, s);
271     CuAssertPtrNotNull(tc, timeout_mutex);
272
273     s = apr_thread_cond_create(&timeout_cond, p);
274     CuAssertIntEquals(tc, APR_SUCCESS, s);
275     CuAssertPtrNotNull(tc, timeout_cond);
276
277     timeout = apr_time_from_sec(5);
278
279     for (i = 0; i < MAX_RETRY; i++) {
280         apr_thread_mutex_lock(timeout_mutex);
281
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);
286         
287         if (s != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(s)) {
288             continue;
289         }
290         CuAssertIntEquals(tc, 1, APR_STATUS_IS_TIMEUP(s));
291         CuAssert(tc, "Timer returned too late", end - begin - timeout < 100000);
292         break;
293     }
294     CuAssert(tc, "Too many retries", i < MAX_RETRY);
295 }
296
297 #endif /* !APR_HAS_THREADS */
298
299 #if !APR_HAS_THREADS
300 static void threads_not_impl(CuTest *tc)
301 {
302     CuNotImpl(tc, "Threads not implemented on this platform");
303 }
304 #endif
305
306
307 CuSuite *testlock(void)
308 {
309     CuSuite *suite = CuSuiteNew("Thread Locks");
310
311 #if !APR_HAS_THREADS
312     SUITE_ADD_TEST(suite, threads_not_impl);
313 #else
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);
318 #endif
319
320     return suite;
321 }
322