#include "test.h"
#if INCLUDE_THREAD_TEST
#include <pjlib.h>
#define THIS_FILE "thread_test"
static volatile int quit_flag=0;
#if 0
# define TRACE__(args) PJ_LOG(3,args)
#else
# define TRACE__(args)
#endif
{
unsigned id;
id = *pcounter;
TRACE__((THIS_FILE, " thread %d running..", id));
app_perror("...error in pj_thread_register", rc);
return NULL;
}
if (this_thread == NULL) {
PJ_LOG(3,(THIS_FILE,
"...error: pj_thread_this() returns NULL!"));
return NULL;
}
PJ_LOG(3,(THIS_FILE,
"...error: pj_thread_get_name() returns NULL!"));
return NULL;
}
for (;!quit_flag;) {
(*pcounter)++;
}
TRACE__((THIS_FILE, " thread %d quitting..", id));
return NULL;
}
static int simple_thread(const char *title, unsigned flags)
{
PJ_LOG(3,(THIS_FILE,
"..%s", title));
if (!pool)
return -1000;
quit_flag = 0;
TRACE__((THIS_FILE, " Creating thread 0.."));
&counter,
flags,
&thread);
app_perror("...error: unable to create thread", rc);
return -1010;
}
TRACE__((THIS_FILE, " Main thread waiting.."));
TRACE__((THIS_FILE, " Main thread resuming.."));
if (flags & PJ_THREAD_SUSPENDED) {
if (counter != 0) {
PJ_LOG(3,(THIS_FILE,
"...error: thread is not suspended"));
return -1015;
}
app_perror("...error: resume thread error", rc);
return -1020;
}
}
PJ_LOG(3,(THIS_FILE,
"..waiting for thread to quit.."));
quit_flag = 1;
if (counter == 0) {
PJ_LOG(3,(THIS_FILE,
"...error: thread is not running"));
return -1025;
}
PJ_LOG(3,(THIS_FILE,
"...%s success", title));
}
static int timeslice_test(void)
{
enum { NUM_THREADS = 4 };
pj_uint32_t counter[NUM_THREADS], lowest, highest, diff;
unsigned i;
quit_flag = 0;
if (!pool)
return -10;
PJ_LOG(3,(THIS_FILE,
"..timeslice testing with %d threads", NUM_THREADS));
for (i=0; i<NUM_THREADS; ++i) {
counter[i] = i;
&counter[i],
PJ_THREAD_SUSPENDED,
&thread[i]);
app_perror("...ERROR in pj_thread_create()", rc);
return -20;
}
}
TRACE__((THIS_FILE, " Main thread waiting.."));
TRACE__((THIS_FILE, " Main thread resuming.."));
for (i=0; i<NUM_THREADS; ++i) {
if (counter[i] > i) {
PJ_LOG(3,(THIS_FILE,
"....ERROR! Thread %d-th is not suspended!",
i));
return -30;
}
}
for (i=0; i<NUM_THREADS; ++i) {
TRACE__((THIS_FILE, " Resuming thread %d [%p]..", i, thread[i]));
app_perror("...ERROR in pj_thread_resume()", rc);
return -40;
}
}
TRACE__((THIS_FILE, " Main thread waiting (5s).."));
TRACE__((THIS_FILE, " Main thread resuming.."));
quit_flag = 1;
for (i=0; i<NUM_THREADS; ++i) {
TRACE__((THIS_FILE, " Main thread joining thread %d [%p]..",
i, thread[i]));
app_perror("...ERROR in pj_thread_join()", rc);
return -50;
}
TRACE__((THIS_FILE, " Destroying thread %d [%p]..", i, thread[i]));
app_perror("...ERROR in pj_thread_destroy()", rc);
return -60;
}
}
TRACE__((THIS_FILE, " Main thread calculating time slices.."));
lowest = 0xFFFFFFFF;
highest = 0;
for (i=0; i<NUM_THREADS; ++i) {
if (counter[i] < lowest)
lowest = counter[i];
if (counter[i] > highest)
highest = counter[i];
}
if (lowest < 2) {
PJ_LOG(3,(THIS_FILE,
"...ERROR: not all threads were running!"));
return -70;
}
diff = (highest-lowest)*100 / ((highest+lowest)/2);
if ( diff >= 50) {
"...ERROR: thread didn't have equal timeslice!"));
".....lowest counter=%u, highest counter=%u, diff=%u%%",
lowest, highest, diff));
return -80;
} else {
"...info: timeslice diff between lowest & highest=%u%%",
diff));
}
return 0;
}
int thread_test(void)
{
int rc;
rc = simple_thread("simple thread test", 0);
return rc;
rc = simple_thread("suspended thread test", PJ_THREAD_SUSPENDED);
return rc;
rc = timeslice_test();
return rc;
return rc;
}
#else
int dummy_thread_test;
#endif
int pj_status_t
Definition: types.h:68
struct pj_thread_t pj_thread_t
Definition: types.h:236
unsigned int pj_uint32_t
Definition: types.h:43
@ PJ_SUCCESS
Definition: types.h:93
#define PJ_LOG(level, arg)
Definition: log.h:106
pj_pool_t * pj_pool_create(pj_pool_factory *factory, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback)
void pj_pool_release(pj_pool_t *pool)
void pj_bzero(void *dst, pj_size_t size)
Definition: string.h:762
long pj_thread_desc[(64)]
Definition: os.h:170
pj_status_t pj_thread_resume(pj_thread_t *thread)
int() pj_thread_proc(void *)
Definition: os.h:157
pj_status_t pj_thread_destroy(pj_thread_t *thread)
pj_status_t pj_thread_register(const char *thread_name, pj_thread_desc desc, pj_thread_t **thread)
pj_status_t pj_thread_join(pj_thread_t *thread)
pj_thread_t * pj_thread_this(void)
const char * pj_thread_get_name(pj_thread_t *thread)
pj_status_t pj_thread_create(pj_pool_t *pool, const char *thread_name, pj_thread_proc *proc, void *arg, pj_size_t stack_size, unsigned flags, pj_thread_t **thread)
pj_status_t pj_thread_sleep(unsigned msec)
#define PJ_UNUSED_ARG(arg)
Definition: config.h:1343
#define PJ_THREAD_DEFAULT_STACK_SIZE
Definition: config.h:607