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_errno.h"
19 #include "apr_general.h"
26 static apr_thread_mutex_t *thread_lock;
27 static apr_thread_once_t *control = NULL;
31 static apr_thread_t *t1;
32 static apr_thread_t *t2;
33 static apr_thread_t *t3;
34 static apr_thread_t *t4;
36 /* just some made up number to check on later */
37 static apr_status_t exit_ret_val = 123;
39 static void init_func(void)
44 static void * APR_THREAD_FUNC thread_func1(apr_thread_t *thd, void *data)
48 apr_thread_once(control, init_func);
50 for (i = 0; i < 10000; i++) {
51 apr_thread_mutex_lock(thread_lock);
53 apr_thread_mutex_unlock(thread_lock);
55 apr_thread_exit(thd, exit_ret_val);
59 static void thread_init(CuTest *tc)
63 rv = apr_thread_once_init(&control, p);
64 CuAssertIntEquals(tc, APR_SUCCESS, rv);
66 rv = apr_thread_mutex_create(&thread_lock, APR_THREAD_MUTEX_DEFAULT, p);
67 CuAssertIntEquals(tc, APR_SUCCESS, rv);
70 static void create_threads(CuTest *tc)
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);
84 static void join_threads(CuTest *tc)
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);
98 static void check_locks(CuTest *tc)
100 CuAssertIntEquals(tc, 40000, x);
103 static void check_thread_once(CuTest *tc)
105 CuAssertIntEquals(tc, 1, value);
110 static void threads_not_impl(CuTest *tc)
112 CuNotImpl(tc, "Threads not implemented on this platform");
117 CuSuite *testthread(void)
119 CuSuite *suite = CuSuiteNew("Threads");
122 SUITE_ADD_TEST(suite, threads_not_impl);
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);