bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testthread.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_errno.h"
19 #include "apr_general.h"
20 #include "errno.h"
21 #include "apr_time.h"
22 #include "test_apr.h"
23
24 #if APR_HAS_THREADS
25
26 static apr_thread_mutex_t *thread_lock;
27 static apr_thread_once_t *control = NULL;
28 static int x = 0;
29 static int value = 0;
30
31 static apr_thread_t *t1;
32 static apr_thread_t *t2;
33 static apr_thread_t *t3;
34 static apr_thread_t *t4;
35
36 /* just some made up number to check on later */
37 static apr_status_t exit_ret_val = 123;
38
39 static void init_func(void)
40 {
41     value++;
42 }
43
44 static void * APR_THREAD_FUNC thread_func1(apr_thread_t *thd, void *data)
45 {
46     int i;
47
48     apr_thread_once(control, init_func);
49
50     for (i = 0; i < 10000; i++) {
51         apr_thread_mutex_lock(thread_lock);
52         x++;
53         apr_thread_mutex_unlock(thread_lock);
54     }
55     apr_thread_exit(thd, exit_ret_val);
56     return NULL;
57
58
59 static void thread_init(CuTest *tc)
60 {
61     apr_status_t rv;
62
63     rv = apr_thread_once_init(&control, p);
64     CuAssertIntEquals(tc, APR_SUCCESS, rv);
65
66     rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p); 
67     CuAssertIntEquals(tc, APR_SUCCESS, rv);
68 }
69
70 static void create_threads(CuTest *tc)
71 {
72     apr_status_t rv;
73
74     rv = apr_thread_create(&t1, NULL, thread_func1, NULL, p);
75     CuAssertIntEquals(tc, APR_SUCCESS, rv);
76     rv = apr_thread_create(&t2, NULL, thread_func1, NULL, p);
77     CuAssertIntEquals(tc, APR_SUCCESS, rv);
78     rv = apr_thread_create(&t3, NULL, thread_func1, NULL, p);
79     CuAssertIntEquals(tc, APR_SUCCESS, rv);
80     rv = apr_thread_create(&t4, NULL, thread_func1, NULL, p);
81     CuAssertIntEquals(tc, APR_SUCCESS, rv);
82 }
83
84 static void join_threads(CuTest *tc)
85 {
86     apr_status_t s;
87
88     apr_thread_join(&s, t1);
89     CuAssertIntEquals(tc, exit_ret_val, s);
90     apr_thread_join(&s, t2);
91     CuAssertIntEquals(tc, exit_ret_val, s);
92     apr_thread_join(&s, t3);
93     CuAssertIntEquals(tc, exit_ret_val, s);
94     apr_thread_join(&s, t4);
95     CuAssertIntEquals(tc, exit_ret_val, s);
96 }
97
98 static void check_locks(CuTest *tc)
99 {
100     CuAssertIntEquals(tc, 40000, x);
101 }
102
103 static void check_thread_once(CuTest *tc)
104 {
105     CuAssertIntEquals(tc, 1, value);
106 }
107
108 #else
109
110 static void threads_not_impl(CuTest *tc)
111 {
112     CuNotImpl(tc, "Threads not implemented on this platform");
113 }
114
115 #endif
116
117 CuSuite *testthread(void)
118 {
119     CuSuite *suite = CuSuiteNew("Threads");
120
121 #if !APR_HAS_THREADS
122     SUITE_ADD_TEST(suite, threads_not_impl);
123 #else
124     SUITE_ADD_TEST(suite, thread_init);
125     SUITE_ADD_TEST(suite, create_threads);
126     SUITE_ADD_TEST(suite, join_threads);
127     SUITE_ADD_TEST(suite, check_locks);
128     SUITE_ADD_TEST(suite, check_thread_once);
129 #endif
130
131     return suite;
132 }
133