Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/thread.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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
55 typedef unsigned long counter_t;
56 #define counter_fmt "%lu"
57 static volatile int quit_flag=0;
58 static volatile int unregister_test_flag=0;
59
60 #if 0
61 # define TRACE__(args) PJ_LOG(3,args)
62 #else
63 # define TRACE__(args)
64 #endif
65
66
67 /*
68 * The thread's entry point.
69 *
70 * Each of the thread mainly will just execute the loop which
71 * increments a variable.
72 */
73 static int thread_proc(void *data)
74 {
75 /* Test that pj_thread_register() works. */
76 pj_thread_desc desc;
77 pj_thread_t *this_thread, *initial_thread_desc;
78 unsigned id;
79 pj_status_t rc;
80 counter_t *pcounter = (counter_t *)data;
81
82 id = *pcounter;
83 PJ_UNUSED_ARG(id); /* Warning about unused var if TRACE__ is disabled */
84 TRACE__((THIS_FILE, " thread %d running..", id));
85
86 if (!pj_thread_is_registered()) {
87 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_is_registered() returns FALSE!"));
88 return -1;
89 }
90
91 initial_thread_desc = pj_thread_this();
92 if (initial_thread_desc == NULL) {
93 PJ_LOG(3,(THIS_FILE, "...error: initial_thread_desc = pj_thread_this() returns NULL!"));
94 return -1;
95 }
96
97 pj_bzero(desc, sizeof(desc));
98
99 //error in test logic: re-registering an already registered thread
100 rc = pj_thread_register("thread", desc, &this_thread);
101 if (rc != PJ_SUCCESS) {
102 app_perror("...error in pj_thread_register", rc);
103 return rc;
104 }
105
106 /* Test that pj_thread_this() works */
107 this_thread = pj_thread_this();
108 if (this_thread == NULL) {
109 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_this() returns NULL!"));
110 return -1;
111 }
112
113 /* Test that pj_thread_get_name() works */
114 if (pj_thread_get_name(this_thread) == NULL) {
115 PJ_LOG(3,(THIS_FILE, "...error: pj_thread_get_name() returns NULL!"));
116 return -1;
117 }
118
119 /* reregister on the initial thread descriptor to check
120 * reregistered descriptor is joinable
121 */
122 for (; unregister_test_flag; --unregister_test_flag) {
123 rc = pj_thread_unregister();
124 if (rc != PJ_SUCCESS) {
125 app_perror("...error in pj_thread_unregister", rc);
126 unregister_test_flag = 0;
127 return rc;
128 }
129
130 rc = pj_thread_attach("thread",
131 (void*)initial_thread_desc,
132 &this_thread);
133 if (rc != PJ_SUCCESS) {
134 app_perror("...error in pj_thread_attach", rc);
135 unregister_test_flag = 0;
136 return rc;
137 }
138 }
139
140 /* Main loop */
141 for (;!quit_flag;) {
142 (*pcounter)++;
143 //Must sleep if platform doesn't do time-slicing.
144 //2024-12-18: always sleep since otherwise test may occasionaly throw
145 // error on Linux (bennylp)
146 pj_thread_sleep(0);
147 }
148
149 TRACE__((THIS_FILE, " thread %d quitting..", id));
150 return PJ_SUCCESS;
151 }
152
153 /*
154 * simple_thread()
155 */
156 static int simple_thread(const char *title, unsigned flags)
157 {
158 pj_pool_t *pool;
159 pj_thread_t *thread;
160 pj_status_t rc;
161 counter_t counter = 0;
162 enum { MAX_REREGISTER = 10 };
163
164 PJ_LOG(3,(THIS_FILE, "..%s", title));
165
166 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
167 if (!pool)
168 return -1000;
169
170 quit_flag = 0;
171
172 unregister_test_flag = flags ? 0 : MAX_REREGISTER;
173
174 TRACE__((THIS_FILE, " Creating thread 0.."));
175 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
176 &counter,
177 PJ_THREAD_DEFAULT_STACK_SIZE,
178 flags,
179 &thread);
180
181 if (rc != PJ_SUCCESS) {
182 app_perror("...error: unable to create thread", rc);
183 return -1010;
184 }
185
186 TRACE__((THIS_FILE, " Main thread waiting.."));
187 pj_thread_sleep(1500);
188 TRACE__((THIS_FILE, " Main thread resuming.."));
189
190 if (flags & PJ_THREAD_SUSPENDED) {
191
192 /* Check that counter is still zero */
193 if (counter != 0) {
194 PJ_LOG(3,(THIS_FILE, "...error: thread is not suspended"));
195 return -1015;
196 }
197
198 rc = pj_thread_resume(thread);
199 if (rc != PJ_SUCCESS) {
200 app_perror("...error: resume thread error", rc);
201 return -1020;
202 }
203 }
204
205 PJ_LOG(3,(THIS_FILE, "..waiting for thread to quit.."));
206
207 pj_thread_sleep(1500);
208
209 quit_flag = 1;
210 while (unregister_test_flag) {
211 pj_thread_sleep(0);
212 }
213
214 rc = pj_thread_join(thread);
215 if (rc != PJ_SUCCESS) {
216 app_perror("...error: unable to join thread", rc);
217 return -1030;
218 }
219
220 pj_pool_release(pool);
221
222 if (counter == 0) {
223 PJ_LOG(3,(THIS_FILE, "...error: thread is not running"));
224 return -1025;
225 }
226
227 PJ_LOG(3,(THIS_FILE, "...%s success", title));
228 return PJ_SUCCESS;
229 }
230
231
232 /*
233 * timeslice_test()
234 */
235 static int timeslice_test(void)
236 {
237 enum { NUM_THREADS = 4 };
238 pj_pool_t *pool;
239 counter_t counter[NUM_THREADS], lowest, highest, diff;
240 pj_thread_t *thread[NUM_THREADS];
241 unsigned i;
242 pj_status_t rc;
243
244 quit_flag = 0;
245 unregister_test_flag = 0;
246
247 pool = pj_pool_create(mem, NULL, 4000, 4000, NULL);
248 if (!pool)
249 return -10;
250
251 PJ_LOG(3,(THIS_FILE, "..timeslice testing with %d threads", NUM_THREADS));
252
253 /* Create all threads in suspended mode. */
254 for (i=0; i<NUM_THREADS; ++i) {
255 counter[i] = i;
256 rc = pj_thread_create(pool, "thread", (pj_thread_proc*)&thread_proc,
257 &counter[i],
258 PJ_THREAD_DEFAULT_STACK_SIZE,
259 PJ_THREAD_SUSPENDED,
260 &thread[i]);
261 if (rc!=PJ_SUCCESS) {
262 app_perror("...ERROR in pj_thread_create()", rc);
263 return -20;
264 }
265 }
266
267 /* Sleep for 1 second.
268 * The purpose of this is to test whether all threads are suspended.
269 */
270 TRACE__((THIS_FILE, " Main thread waiting.."));
271 pj_thread_sleep(1000);
272 TRACE__((THIS_FILE, " Main thread resuming.."));
273
274 /* Check that all counters are still zero. */
275 for (i=0; i<NUM_THREADS; ++i) {
276 if (counter[i] > i) {
277 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread %d-th is not suspended!",
278 i));
279 return -30;
280 }
281 }
282
283 /* Now resume all threads. */
284 for (i=0; i<NUM_THREADS; ++i) {
285 TRACE__((THIS_FILE, " Resuming thread %d [%p]..", i, thread[i]));
286 rc = pj_thread_resume(thread[i]);
287 if (rc != PJ_SUCCESS) {
288 app_perror("...ERROR in pj_thread_resume()", rc);
289 return -40;
290 }
291 }
292
293 /* Main thread sleeps for some time to allow threads to run.
294 * The longer we sleep, the more accurate the calculation will be,
295 * but it'll make user waits for longer for the test to finish.
296 */
297 TRACE__((THIS_FILE, " Main thread waiting (5s).."));
298 pj_thread_sleep(5000);
299 TRACE__((THIS_FILE, " Main thread resuming.."));
300
301 /* Signal all threads to quit. */
302 quit_flag = 1;
303
304 /* Wait until all threads quit, then destroy. */
305 for (i=0; i<NUM_THREADS; ++i) {
306 TRACE__((THIS_FILE, " Main thread joining thread %d [%p]..",
307 i, thread[i]));
308 rc = pj_thread_join(thread[i]);
309 if (rc != PJ_SUCCESS) {
310 app_perror("...ERROR in pj_thread_join()", rc);
311 return -50;
312 }
313 TRACE__((THIS_FILE, " Destroying thread %d [%p]..", i, thread[i]));
314 rc = pj_thread_destroy(thread[i]);
315 if (rc != PJ_SUCCESS) {
316 app_perror("...ERROR in pj_thread_destroy()", rc);
317 return -60;
318 }
319 }
320
321 TRACE__((THIS_FILE, " Main thread calculating time slices.."));
322
323 /* Now examine the value of the counters.
324 * Check that all threads had equal proportion of processing.
325 */
326 lowest = highest = counter[0];
327 /* Check that all threads are running. */
328 if (counter[0] == 0) {
329 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread 0 was not running!"));
330 return -70;
331 }
332 for (i=1; i<NUM_THREADS; ++i) {
333
334 /* Check that all threads are running. */
335 if (counter[i] == i) {
336 PJ_LOG(3,(THIS_FILE, "....ERROR! Thread %d-th was not running!",
337 i));
338 return -75;
339 }
340
341 if (counter[i] < lowest)
342 lowest = counter[i];
343 if (counter[i] > highest)
344 highest = counter[i];
345 }
346
347 /* The difference between lowest and higest should be lower than 50%.
348 */
349 diff = (highest-lowest)*100 / ((highest+lowest)/2);
350 if ( diff >= 50) {
351 PJ_LOG(3,(THIS_FILE,
352 "...ERROR: thread didn't have equal timeslice!"));
353 PJ_LOG(3,(THIS_FILE,
354 ".....lowest counter=" counter_fmt
355 ", highest counter=" counter_fmt
356 ", diff=" counter_fmt "%%",
357 lowest, highest, diff));
358 return -80;
359 } else {
360 PJ_LOG(3,(THIS_FILE,
361 "...info: timeslice diff between lowest & highest=" counter_fmt "%%",
362 diff));
363 }
364
365 pj_pool_release(pool);
366 return 0;
367 }
368
369 int thread_test(void)
370 {
371 int rc;
372
373 rc = simple_thread("simple thread test", 0);
374 if (rc != PJ_SUCCESS)
375 return rc;
376
377 rc = simple_thread("suspended thread test", PJ_THREAD_SUSPENDED);
378 if (rc != PJ_SUCCESS)
379 return rc;
380
381 rc = timeslice_test();
382 if (rc != PJ_SUCCESS)
383 return rc;
384
385 return rc;
386 }
387
388 #else
389 /* To prevent warning about "translation unit is empty"
390 * when this test is disabled.
391 */
392 int dummy_thread_test;
393 #endif /* INCLUDE_THREAD_TEST */
394
395