blob: 2c7c41f79fb089c54f84c3d748518cb5f09d7564 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20
21/**
22 * \page page_pjlib_thread_test Test: Thread Test
23 *
24 * This file contains \a thread_test() definition.
25 *
26 * \section thread_test_scope_sec Scope of Test
27 * This tests:
28 * - whether PJ_THREAD_SUSPENDED flag works.
29 * - whether multithreading works.
30 * - whether thread timeslicing works, and threads have equal
31 * time-slice proportion.
32 *
33 * APIs tested:
34 * - pj_thread_create()
35 * - pj_thread_register()
36 * - pj_thread_this()
37 * - pj_thread_get_name()
38 * - pj_thread_destroy()
39 * - pj_thread_resume()
40 * - pj_thread_sleep()
41 * - pj_thread_join()
42 * - pj_thread_destroy()
43 *
44 *
45 * This file is <b>pjlib-test/thread.c</b>
46 *
47 * \include pjlib-test/thread.c
48 */
49#if INCLUDE_THREAD_TEST
50
51#include <pjlib.h>
52
53#define THIS_FILE "thread_test"
54
55static int quit_flag=0;
56
57/*
58 * The thread's entry point.
59 *
60 * Each of the thread mainly will just execute the loop which
61 * increments a variable.
62 */
63static void* thread_proc(pj_uint32_t *pcounter)
64{
65 /* Test that pj_thread_register() works. */
66 pj_thread_desc desc;
67 pj_thread_t *this_thread;
68 pj_status_t rc;
69
70 rc = pj_thread_register("thread", desc, &this_thread);
71 if (rc != PJ_SUCCESS) {
72 app_perror("...error in pj_thread_register", rc);
73 return NULL;
74 }
75
76 /* Test that pj_thread_this() works */
77 this_thread = pj_thread_this();
78 if (this_thread == NULL) {
79 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_this() returns NULL!"));
80 return NULL;
81 }
82
83 /* Test that pj_thread_get_name() works */
84 if (pj_thread_get_name(this_thread) == NULL) {
85 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_get_name() returns NULL!"));
86 return NULL;
87 }
88
89 /* Main loop */
90 for (;!quit_flag;) {
91 (*pcounter)++;
92 //Must sleep if platform doesn't do time-slicing.
93 pj_thread_sleep(0);
94 }
95
96 return NULL;
97}
98
99/*
100 * simple_thread()
101 */
102static int simple_thread(const char *title, unsigned flags)
103{
104 pj_pool_t *pool;
105 pj_thread_t *thread;
106 pj_status_t rc;
107 pj_uint32_t counter = 0;
108
109 PJ_LOG(3,(THIS_FILE, "..%s", title));
110
111 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
112 if (!pool)
113 return -1000;
114
115 quit_flag = 0;
116
117 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
118 &counter,
119 PJ_THREAD_DEFAULT_STACK_SIZE,
120 flags,
121 &thread);
122
123 if (rc != PJ_SUCCESS) {
124 app_perror("...error: unable to create thread", rc);
125 return -1010;
126 }
127
128 if (flags & PJ_THREAD_SUSPENDED) {
129 rc = pj_thread_resume(thread);
130 if (rc != PJ_SUCCESS) {
131 app_perror("...error: resume thread error", rc);
132 return -1020;
133 }
134 }
135
136 PJ_LOG(3,(THIS_FILE, "..waiting for thread to quit.."));
137
138 quit_flag = 1;
139 pj_thread_join(thread);
140
141 pj_pool_release(pool);
142
143 PJ_LOG(3,(THIS_FILE, "...%s success", title));
144 return PJ_SUCCESS;
145}
146
147
148/*
149 * timeslice_test()
150 */
151static int timeslice_test(void)
152{
153 enum { NUM_THREADS = 4 };
154 pj_pool_t *pool;
155 pj_uint32_t counter[NUM_THREADS], lowest, highest, diff;
156 pj_thread_t *thread[NUM_THREADS];
157 int i;
158 pj_status_t rc;
159
160 quit_flag = 0;
161
162 pool = pj_pool_create(mem, NULL, 4096, 0, NULL);
163 if (!pool)
164 return -10;
165
166 PJ_LOG(3,(THIS_FILE, "..timeslice testing with %d threads", NUM_THREADS));
167
168 /* Create all threads in suspended mode. */
169 for (i=0; i<NUM_THREADS; ++i) {
170 counter[i] = 0;
171 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
172 &counter[i],
173 PJ_THREAD_DEFAULT_STACK_SIZE,
174 PJ_THREAD_SUSPENDED,
175 &thread[i]);
176 if (rc!=PJ_SUCCESS) {
177 app_perror("...ERROR in pj_thread_create()", rc);
178 return -20;
179 }
180 }
181
182 /* Sleep for 1 second.
183 * The purpose of this is to test whether all threads are suspended.
184 */
185 pj_thread_sleep(1000);
186
187 /* Check that all counters are still zero. */
188 for (i=0; i<NUM_THREADS; ++i) {
189 if (counter[i] != 0) {
190 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread %d-th is not suspended!",
191 i));
192 return -30;
193 }
194 }
195
196 /* Now resume all threads. */
197 for (i=0; i<NUM_THREADS; ++i) {
198 rc = pj_thread_resume(thread[i]);
199 if (rc != PJ_SUCCESS) {
200 app_perror("...ERROR in pj_thread_resume()", rc);
201 return -40;
202 }
203 }
204
205 /* Main thread sleeps for some time to allow threads to run.
206 * The longer we sleep, the more accurate the calculation will be,
207 * but it'll make user waits for longer for the test to finish.
208 */
209 pj_thread_sleep(5000);
210
211 /* Signal all threads to quit. */
212 quit_flag = 1;
213
214 /* Wait until all threads quit, then destroy. */
215 for (i=0; i<NUM_THREADS; ++i) {
216 rc = pj_thread_join(thread[i]);
217 if (rc != PJ_SUCCESS) {
218 app_perror("...ERROR in pj_thread_join()", rc);
219 return -50;
220 }
221 rc = pj_thread_destroy(thread[i]);
222 if (rc != PJ_SUCCESS) {
223 app_perror("...ERROR in pj_thread_destroy()", rc);
224 return -60;
225 }
226 }
227
228 /* Now examine the value of the counters.
229 * Check that all threads had equal proportion of processing.
230 */
231 lowest = 0xFFFFFFFF;
232 highest = 0;
233 for (i=0; i<NUM_THREADS; ++i) {
234 if (counter[i] < lowest)
235 lowest = counter[i];
236 if (counter[i] > highest)
237 highest = counter[i];
238 }
239
240 /* Check that all threads are running. */
241 if (lowest < 2) {
242 PJ_LOG(3,(THIS_FILE, "...ERROR: not all threads were running!"));
243 return -70;
244 }
245
246 /* The difference between lowest and higest should be lower than 50%.
247 */
248 diff = (highest-lowest)*100 / ((highest+lowest)/2);
249 if ( diff >= 50) {
250 PJ_LOG(3,(THIS_FILE, "...ERROR: thread didn't have equal timeslice!"));
251 PJ_LOG(3,(THIS_FILE, ".....lowest counter=%u, highest counter=%u, diff=%u%%",
252 lowest, highest, diff));
253 return -80;
254 } else {
255 PJ_LOG(3,(THIS_FILE,
256 "...info: timeslice diff between lowest & highest=%u%%",
257 diff));
258 }
259
260 return 0;
261}
262
263int thread_test(void)
264{
265 int rc;
266
267 rc = simple_thread("simple thread test", 0);
268 if (rc != PJ_SUCCESS)
269 return rc;
270
271 rc = simple_thread("suspended thread test", PJ_THREAD_SUSPENDED);
272 if (rc != PJ_SUCCESS)
273 return rc;
274
275 rc = timeslice_test();
276 if (rc != PJ_SUCCESS)
277 return rc;
278
279 return rc;
280}
281
282#else
283/* To prevent warning about "translation unit is empty"
284 * when this test is disabled.
285 */
286int dummy_thread_test;
287#endif /* INCLUDE_THREAD_TEST */
288
289