| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static int thread_proc(void *data) |
| 74 |
{ |
| 75 |
|
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 120 |
|
| 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 |
|
| 141 |
for (;!quit_flag;) { |
| 142 |
(*pcounter)++; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
pj_thread_sleep(0); |
| 147 |
} |
| 148 |
|
| 149 |
TRACE__((THIS_FILE, " thread %d quitting..", id)); |
| 150 |
return PJ_SUCCESS; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
TRACE__((THIS_FILE, " Main thread waiting..")); |
| 271 |
pj_thread_sleep(1000); |
| 272 |
TRACE__((THIS_FILE, " Main thread resuming..")); |
| 273 |
|
| 274 |
|
| 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 |
|
| 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 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
TRACE__((THIS_FILE, " Main thread waiting (5s)..")); |
| 298 |
pj_thread_sleep(5000); |
| 299 |
TRACE__((THIS_FILE, " Main thread resuming..")); |
| 300 |
|
| 301 |
|
| 302 |
quit_flag = 1; |
| 303 |
|
| 304 |
|
| 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 |
|
| 324 |
|
| 325 |
|
| 326 |
lowest = highest = counter[0]; |
| 327 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 390 |
|
| 391 |
|
| 392 |
int dummy_thread_test; |
| 393 |
#endif |
| 394 |
|
| 395 |
|