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