blob: e901df7d5df04bbddfd36a2dbb906497f07d16b5 [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
Benny Prijono42c5b9e2006-05-10 19:24:40 +000055static volatile int quit_flag=0;
56
57#if 0
58# define TRACE__(args) PJ_LOG(3,args)
59#else
60# define TRACE__(args)
61#endif
62
Benny Prijono5dcb38d2005-11-21 01:55:47 +000063
64/*
65 * The thread's entry point.
66 *
67 * Each of the thread mainly will just execute the loop which
68 * increments a variable.
69 */
70static void* thread_proc(pj_uint32_t *pcounter)
71{
72 /* Test that pj_thread_register() works. */
73 pj_thread_desc desc;
74 pj_thread_t *this_thread;
Benny Prijonod8410532006-06-15 11:04:33 +000075 unsigned id;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000076 pj_status_t rc;
77
Benny Prijonod8410532006-06-15 11:04:33 +000078 id = *pcounter;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000079 TRACE__((THIS_FILE, " thread %d running..", id));
80
Benny Prijonocf8ed792006-11-21 13:40:23 +000081 pj_bzero(desc, sizeof(desc));
82
Benny Prijono5dcb38d2005-11-21 01:55:47 +000083 rc = pj_thread_register("thread", desc, &this_thread);
84 if (rc != PJ_SUCCESS) {
85 app_perror("...error in pj_thread_register", rc);
86 return NULL;
87 }
88
89 /* Test that pj_thread_this() works */
90 this_thread = pj_thread_this();
91 if (this_thread == NULL) {
92 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_this() returns NULL!"));
93 return NULL;
94 }
95
96 /* Test that pj_thread_get_name() works */
97 if (pj_thread_get_name(this_thread) == NULL) {
98 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_get_name() returns NULL!"));
99 return NULL;
100 }
101
102 /* Main loop */
103 for (;!quit_flag;) {
104 (*pcounter)++;
105 //Must sleep if platform doesn't do time-slicing.
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000106 //pj_thread_sleep(0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000107 }
108
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000109 TRACE__((THIS_FILE, " thread %d quitting..", id));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000110 return NULL;
111}
112
113/*
114 * simple_thread()
115 */
116static int simple_thread(const char *title, unsigned flags)
117{
118 pj_pool_t *pool;
119 pj_thread_t *thread;
120 pj_status_t rc;
121 pj_uint32_t counter = 0;
122
123 PJ_LOG(3,(THIS_FILE, "..%s", title));
124
125 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
126 if (!pool)
127 return -1000;
128
129 quit_flag = 0;
130
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000131 TRACE__((THIS_FILE, " Creating thread 0.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000132 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
133 &counter,
134 PJ_THREAD_DEFAULT_STACK_SIZE,
135 flags,
136 &thread);
137
138 if (rc != PJ_SUCCESS) {
139 app_perror("...error: unable to create thread", rc);
140 return -1010;
141 }
142
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000143 TRACE__((THIS_FILE, " Main thread waiting.."));
144 pj_thread_sleep(1500);
145 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono2cab3a52006-03-22 19:08:19 +0000146
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000147 if (flags & PJ_THREAD_SUSPENDED) {
Benny Prijono2cab3a52006-03-22 19:08:19 +0000148
149 /* Check that counter is still zero */
150 if (counter != 0) {
151 PJ_LOG(3,(THIS_FILE, "...error: thread is not suspended"));
152 return -1015;
153 }
154
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000155 rc = pj_thread_resume(thread);
156 if (rc != PJ_SUCCESS) {
157 app_perror("...error: resume thread error", rc);
158 return -1020;
159 }
160 }
161
162 PJ_LOG(3,(THIS_FILE, "..waiting for thread to quit.."));
163
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000164 pj_thread_sleep(1500);
Benny Prijono2cab3a52006-03-22 19:08:19 +0000165
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000166 quit_flag = 1;
167 pj_thread_join(thread);
168
169 pj_pool_release(pool);
170
Benny Prijono2cab3a52006-03-22 19:08:19 +0000171 if (counter == 0) {
172 PJ_LOG(3,(THIS_FILE, "...error: thread is not running"));
173 return -1025;
174 }
175
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000176 PJ_LOG(3,(THIS_FILE, "...%s success", title));
177 return PJ_SUCCESS;
178}
179
180
181/*
182 * timeslice_test()
183 */
184static int timeslice_test(void)
185{
186 enum { NUM_THREADS = 4 };
187 pj_pool_t *pool;
188 pj_uint32_t counter[NUM_THREADS], lowest, highest, diff;
189 pj_thread_t *thread[NUM_THREADS];
190 int i;
191 pj_status_t rc;
192
193 quit_flag = 0;
194
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000195 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000196 if (!pool)
197 return -10;
198
199 PJ_LOG(3,(THIS_FILE, "..timeslice testing with %d threads", NUM_THREADS));
200
201 /* Create all threads in suspended mode. */
202 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000203 counter[i] = i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000204 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
205 &counter[i],
206 PJ_THREAD_DEFAULT_STACK_SIZE,
207 PJ_THREAD_SUSPENDED,
208 &thread[i]);
209 if (rc!=PJ_SUCCESS) {
210 app_perror("...ERROR in pj_thread_create()", rc);
211 return -20;
212 }
213 }
214
215 /* Sleep for 1 second.
216 * The purpose of this is to test whether all threads are suspended.
217 */
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000218 TRACE__((THIS_FILE, " Main thread waiting.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000219 pj_thread_sleep(1000);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000220 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000221
222 /* Check that all counters are still zero. */
223 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000224 if (counter[i] > i) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000225 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread %d-th is not suspended!",
226 i));
227 return -30;
228 }
229 }
230
231 /* Now resume all threads. */
232 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000233 TRACE__((THIS_FILE, " Resuming thread %d [%p]..", i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000234 rc = pj_thread_resume(thread[i]);
235 if (rc != PJ_SUCCESS) {
236 app_perror("...ERROR in pj_thread_resume()", rc);
237 return -40;
238 }
239 }
240
241 /* Main thread sleeps for some time to allow threads to run.
242 * The longer we sleep, the more accurate the calculation will be,
243 * but it'll make user waits for longer for the test to finish.
244 */
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000245 TRACE__((THIS_FILE, " Main thread waiting (5s).."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000246 pj_thread_sleep(5000);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000247 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000248
249 /* Signal all threads to quit. */
250 quit_flag = 1;
251
252 /* Wait until all threads quit, then destroy. */
253 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000254 TRACE__((THIS_FILE, " Main thread joining thread %d [%p]..",
255 i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000256 rc = pj_thread_join(thread[i]);
257 if (rc != PJ_SUCCESS) {
258 app_perror("...ERROR in pj_thread_join()", rc);
259 return -50;
260 }
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000261 TRACE__((THIS_FILE, " Destroying thread %d [%p]..", i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000262 rc = pj_thread_destroy(thread[i]);
263 if (rc != PJ_SUCCESS) {
264 app_perror("...ERROR in pj_thread_destroy()", rc);
265 return -60;
266 }
267 }
268
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000269 TRACE__((THIS_FILE, " Main thread calculating time slices.."));
270
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000271 /* Now examine the value of the counters.
272 * Check that all threads had equal proportion of processing.
273 */
274 lowest = 0xFFFFFFFF;
275 highest = 0;
276 for (i=0; i<NUM_THREADS; ++i) {
277 if (counter[i] < lowest)
278 lowest = counter[i];
279 if (counter[i] > highest)
280 highest = counter[i];
281 }
282
283 /* Check that all threads are running. */
284 if (lowest < 2) {
285 PJ_LOG(3,(THIS_FILE, "...ERROR: not all threads were running!"));
286 return -70;
287 }
288
289 /* The difference between lowest and higest should be lower than 50%.
290 */
291 diff = (highest-lowest)*100 / ((highest+lowest)/2);
292 if ( diff >= 50) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000293 PJ_LOG(3,(THIS_FILE,
294 "...ERROR: thread didn't have equal timeslice!"));
295 PJ_LOG(3,(THIS_FILE,
296 ".....lowest counter=%u, highest counter=%u, diff=%u%%",
297 lowest, highest, diff));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000298 return -80;
299 } else {
300 PJ_LOG(3,(THIS_FILE,
301 "...info: timeslice diff between lowest & highest=%u%%",
302 diff));
303 }
304
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000305 pj_pool_release(pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000306 return 0;
307}
308
309int thread_test(void)
310{
311 int rc;
312
313 rc = simple_thread("simple thread test", 0);
314 if (rc != PJ_SUCCESS)
315 return rc;
316
317 rc = simple_thread("suspended thread test", PJ_THREAD_SUSPENDED);
318 if (rc != PJ_SUCCESS)
319 return rc;
320
321 rc = timeslice_test();
322 if (rc != PJ_SUCCESS)
323 return rc;
324
325 return rc;
326}
327
328#else
329/* To prevent warning about "translation unit is empty"
330 * when this test is disabled.
331 */
332int dummy_thread_test;
333#endif /* INCLUDE_THREAD_TEST */
334
335