blob: 5634776cf506617704528b92c0e4577c7d05f9ff [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "test.h"
21
22/**
23 * \page page_pjlib_thread_test Test: Thread Test
24 *
25 * This file contains \a thread_test() definition.
26 *
27 * \section thread_test_scope_sec Scope of Test
28 * This tests:
29 * - whether PJ_THREAD_SUSPENDED flag works.
30 * - whether multithreading works.
31 * - whether thread timeslicing works, and threads have equal
32 * time-slice proportion.
33 *
34 * APIs tested:
35 * - pj_thread_create()
36 * - pj_thread_register()
37 * - pj_thread_this()
38 * - pj_thread_get_name()
39 * - pj_thread_destroy()
40 * - pj_thread_resume()
41 * - pj_thread_sleep()
42 * - pj_thread_join()
43 * - pj_thread_destroy()
44 *
45 *
46 * This file is <b>pjlib-test/thread.c</b>
47 *
48 * \include pjlib-test/thread.c
49 */
50#if INCLUDE_THREAD_TEST
51
52#include <pjlib.h>
53
54#define THIS_FILE "thread_test"
55
Benny Prijono42c5b9e2006-05-10 19:24:40 +000056static volatile int quit_flag=0;
57
58#if 0
59# define TRACE__(args) PJ_LOG(3,args)
60#else
61# define TRACE__(args)
62#endif
63
Benny Prijono5dcb38d2005-11-21 01:55:47 +000064
65/*
66 * The thread's entry point.
67 *
68 * Each of the thread mainly will just execute the loop which
69 * increments a variable.
70 */
71static void* thread_proc(pj_uint32_t *pcounter)
72{
73 /* Test that pj_thread_register() works. */
74 pj_thread_desc desc;
75 pj_thread_t *this_thread;
Benny Prijonod8410532006-06-15 11:04:33 +000076 unsigned id;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000077 pj_status_t rc;
78
Benny Prijonod8410532006-06-15 11:04:33 +000079 id = *pcounter;
Benny Prijono42c5b9e2006-05-10 19:24:40 +000080 TRACE__((THIS_FILE, " thread %d running..", id));
81
Benny Prijonocf8ed792006-11-21 13:40:23 +000082 pj_bzero(desc, sizeof(desc));
83
Benny Prijono5dcb38d2005-11-21 01:55:47 +000084 rc = pj_thread_register("thread", desc, &this_thread);
85 if (rc != PJ_SUCCESS) {
86 app_perror("...error in pj_thread_register", rc);
87 return NULL;
88 }
89
90 /* Test that pj_thread_this() works */
91 this_thread = pj_thread_this();
92 if (this_thread == NULL) {
93 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_this() returns NULL!"));
94 return NULL;
95 }
96
97 /* Test that pj_thread_get_name() works */
98 if (pj_thread_get_name(this_thread) == NULL) {
99 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_get_name() returns NULL!"));
100 return NULL;
101 }
102
103 /* Main loop */
104 for (;!quit_flag;) {
105 (*pcounter)++;
106 //Must sleep if platform doesn't do time-slicing.
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000107 //pj_thread_sleep(0);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000108 }
109
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000110 TRACE__((THIS_FILE, " thread %d quitting..", id));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000111 return NULL;
112}
113
114/*
115 * simple_thread()
116 */
117static int simple_thread(const char *title, unsigned flags)
118{
119 pj_pool_t *pool;
120 pj_thread_t *thread;
121 pj_status_t rc;
122 pj_uint32_t counter = 0;
123
124 PJ_LOG(3,(THIS_FILE, "..%s", title));
125
126 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
127 if (!pool)
128 return -1000;
129
130 quit_flag = 0;
131
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000132 TRACE__((THIS_FILE, " Creating thread 0.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000133 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
134 &counter,
135 PJ_THREAD_DEFAULT_STACK_SIZE,
136 flags,
137 &thread);
138
139 if (rc != PJ_SUCCESS) {
140 app_perror("...error: unable to create thread", rc);
141 return -1010;
142 }
143
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000144 TRACE__((THIS_FILE, " Main thread waiting.."));
145 pj_thread_sleep(1500);
146 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono2cab3a52006-03-22 19:08:19 +0000147
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000148 if (flags & PJ_THREAD_SUSPENDED) {
Benny Prijono2cab3a52006-03-22 19:08:19 +0000149
150 /* Check that counter is still zero */
151 if (counter != 0) {
152 PJ_LOG(3,(THIS_FILE, "...error: thread is not suspended"));
153 return -1015;
154 }
155
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000156 rc = pj_thread_resume(thread);
157 if (rc != PJ_SUCCESS) {
158 app_perror("...error: resume thread error", rc);
159 return -1020;
160 }
161 }
162
163 PJ_LOG(3,(THIS_FILE, "..waiting for thread to quit.."));
164
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000165 pj_thread_sleep(1500);
Benny Prijono2cab3a52006-03-22 19:08:19 +0000166
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000167 quit_flag = 1;
168 pj_thread_join(thread);
169
170 pj_pool_release(pool);
171
Benny Prijono2cab3a52006-03-22 19:08:19 +0000172 if (counter == 0) {
173 PJ_LOG(3,(THIS_FILE, "...error: thread is not running"));
174 return -1025;
175 }
176
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000177 PJ_LOG(3,(THIS_FILE, "...%s success", title));
178 return PJ_SUCCESS;
179}
180
181
182/*
183 * timeslice_test()
184 */
185static int timeslice_test(void)
186{
187 enum { NUM_THREADS = 4 };
188 pj_pool_t *pool;
189 pj_uint32_t counter[NUM_THREADS], lowest, highest, diff;
190 pj_thread_t *thread[NUM_THREADS];
Benny Prijono5fb99e52007-01-23 04:17:56 +0000191 unsigned i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000192 pj_status_t rc;
193
194 quit_flag = 0;
195
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000196 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000197 if (!pool)
198 return -10;
199
200 PJ_LOG(3,(THIS_FILE, "..timeslice testing with %d threads", NUM_THREADS));
201
202 /* Create all threads in suspended mode. */
203 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000204 counter[i] = i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000205 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
206 &counter[i],
207 PJ_THREAD_DEFAULT_STACK_SIZE,
208 PJ_THREAD_SUSPENDED,
209 &thread[i]);
210 if (rc!=PJ_SUCCESS) {
211 app_perror("...ERROR in pj_thread_create()", rc);
212 return -20;
213 }
214 }
215
216 /* Sleep for 1 second.
217 * The purpose of this is to test whether all threads are suspended.
218 */
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000219 TRACE__((THIS_FILE, " Main thread waiting.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000220 pj_thread_sleep(1000);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000221 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000222
223 /* Check that all counters are still zero. */
224 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000225 if (counter[i] > i) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000226 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread %d-th is not suspended!",
227 i));
228 return -30;
229 }
230 }
231
232 /* Now resume all threads. */
233 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000234 TRACE__((THIS_FILE, " Resuming thread %d [%p]..", i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000235 rc = pj_thread_resume(thread[i]);
236 if (rc != PJ_SUCCESS) {
237 app_perror("...ERROR in pj_thread_resume()", rc);
238 return -40;
239 }
240 }
241
242 /* Main thread sleeps for some time to allow threads to run.
243 * The longer we sleep, the more accurate the calculation will be,
244 * but it'll make user waits for longer for the test to finish.
245 */
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000246 TRACE__((THIS_FILE, " Main thread waiting (5s).."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000247 pj_thread_sleep(5000);
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000248 TRACE__((THIS_FILE, " Main thread resuming.."));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000249
250 /* Signal all threads to quit. */
251 quit_flag = 1;
252
253 /* Wait until all threads quit, then destroy. */
254 for (i=0; i<NUM_THREADS; ++i) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000255 TRACE__((THIS_FILE, " Main thread joining thread %d [%p]..",
256 i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000257 rc = pj_thread_join(thread[i]);
258 if (rc != PJ_SUCCESS) {
259 app_perror("...ERROR in pj_thread_join()", rc);
260 return -50;
261 }
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000262 TRACE__((THIS_FILE, " Destroying thread %d [%p]..", i, thread[i]));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000263 rc = pj_thread_destroy(thread[i]);
264 if (rc != PJ_SUCCESS) {
265 app_perror("...ERROR in pj_thread_destroy()", rc);
266 return -60;
267 }
268 }
269
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000270 TRACE__((THIS_FILE, " Main thread calculating time slices.."));
271
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000272 /* Now examine the value of the counters.
273 * Check that all threads had equal proportion of processing.
274 */
275 lowest = 0xFFFFFFFF;
276 highest = 0;
277 for (i=0; i<NUM_THREADS; ++i) {
278 if (counter[i] < lowest)
279 lowest = counter[i];
280 if (counter[i] > highest)
281 highest = counter[i];
282 }
283
284 /* Check that all threads are running. */
285 if (lowest < 2) {
286 PJ_LOG(3,(THIS_FILE, "...ERROR: not all threads were running!"));
287 return -70;
288 }
289
290 /* The difference between lowest and higest should be lower than 50%.
291 */
292 diff = (highest-lowest)*100 / ((highest+lowest)/2);
293 if ( diff >= 50) {
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000294 PJ_LOG(3,(THIS_FILE,
295 "...ERROR: thread didn't have equal timeslice!"));
296 PJ_LOG(3,(THIS_FILE,
297 ".....lowest counter=%u, highest counter=%u, diff=%u%%",
298 lowest, highest, diff));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000299 return -80;
300 } else {
301 PJ_LOG(3,(THIS_FILE,
302 "...info: timeslice diff between lowest & highest=%u%%",
303 diff));
304 }
305
Benny Prijono42c5b9e2006-05-10 19:24:40 +0000306 pj_pool_release(pool);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000307 return 0;
308}
309
310int thread_test(void)
311{
312 int rc;
313
314 rc = simple_thread("simple thread test", 0);
315 if (rc != PJ_SUCCESS)
316 return rc;
317
318 rc = simple_thread("suspended thread test", PJ_THREAD_SUSPENDED);
319 if (rc != PJ_SUCCESS)
320 return rc;
321
322 rc = timeslice_test();
323 if (rc != PJ_SUCCESS)
324 return rc;
325
326 return rc;
327}
328
329#else
330/* To prevent warning about "translation unit is empty"
331 * when this test is disabled.
332 */
333int dummy_thread_test;
334#endif /* INCLUDE_THREAD_TEST */
335
336