Files
codezero/conts/baremetal/threads_demo/thread.c
Bahadir Balban 6fa4884a5a Changes since April
Clean up of build directories.
Simplifications to capability model.
2010-06-01 15:08:13 +03:00

109 lines
1.7 KiB
C

/*
* Test l4_mutex_control system call.
*
* Copyright (C) 2010 B Labs Ltd.
*
* Author: Bahadir Balban
*/
#include <l4lib/macros.h>
#include L4LIB_INC_ARCH(syslib.h)
#include L4LIB_INC_ARCH(syscalls.h)
#include <l4lib/lib/thread.h>
#define NTHREADS 6
#define dbg_printf printf
int thread_test_func1(void *arg)
{
l4id_t tid = self_tid();
printf("tid = %d is called.\n", tid);
/* Wait for a while before exiting */
int j = 0x400000;
while (j--)
;
return tid;
}
int thread_test_func2(void *arg)
{
l4id_t tid = self_tid();
printf("tid = %d is called.\n", tid);
/* Wait for a while before exiting */
int j = 0x400000;
while (j--)
;
thread_exit(0);
return 0;
}
int thread_demo()
{
struct l4_thread *thread[NTHREADS];
int err;
/* Create threads */
for (int i = 0; i < NTHREADS; i++) {
if (i % 2 ) {
err = thread_create(thread_test_func1, 0,
TC_SHARE_SPACE, &thread[i]);
if (err < 0) {
dbg_printf("Thread create failed. "
"err=%d i= %d\n", err, i);
return err;
}
} else {
err = thread_create(thread_test_func2, 0,
TC_SHARE_SPACE, &thread[i]);
if (err < 0) {
dbg_printf("Thread create failed. "
"err=%d i= %d\n", err, i);
return err;
}
}
}
/*
* Wait for all threads to exit successfully
*/
for (int i = 0; i < NTHREADS; i++) {
if ((err = thread_wait(thread[i])) < 0) {
dbg_printf("THREAD_WAIT failed. "
"err=%d\n", err);
return err;
}
}
dbg_printf("Thread test successful.\n");
return 0;
}
int main(void)
{
int err;
__l4_threadlib_init();
if ((err = thread_demo()) < 0)
goto out_err;
printf("THREAD DEMO: -- PASSED --\n");
return 0;
out_err:
printf("THREAD DEMO: -- FAILED --\n");
return err;
}