more x86_64 work. started to put the mmu bits together in the (former) stage2 loader.
[newos.git] / apps / testapp / threadtests.cpp
blob09220501462e835333fc40c605257c0bbe8bd050
1 /*
2 ** Copyright 2003, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <string.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <sys/syscalls.h>
10 int sleep_test(int arg)
12 printf("should display 'booyah!' 10 times, one second apart\n");
13 for(int i = 0; i < 10; i++) {
14 usleep(1000000);
15 write(0, "booyah!", strlen("booyah!"));
18 return 0;
21 static int test_thread(void *args)
23 int i = (int)args;
24 char buf[128];
26 for(;;) {
27 sprintf(buf, "%c", 'a' + i);
28 write(0, buf, 1);
30 return 0;
33 static int test_thread_self_terminate(void *args)
35 int i = (int)args;
36 char buf[128];
37 bigtime_t start_time = _kern_system_time();
39 for(;;) {
40 if(_kern_system_time() - start_time >= 10000000) {
41 sprintf(buf, "thread %c terminating...\n", 'a' + i);
42 write(0, buf, strlen(buf));
43 break;
45 sprintf(buf, "%c", 'a' + i);
46 write(0, buf, 1);
48 return 0;
51 #if 0
52 static int dummy_thread(void *args)
54 return 1;
57 static int cpu_eater_thread(void *args)
59 for(;;)
63 static int fpu_cruncher_thread(void *args)
65 double y = *(double *)args;
66 double z;
68 for(;;) {
69 z = y * 1.47;
70 y = z / 1.47;
71 if(y != *(double *)args)
72 printf("error: y %f\n", y);
75 #endif
77 int thread_spawn_test(int arg)
79 int i;
80 int (*thread_func)(void *) = NULL;
81 int num_threads = 10;
83 if(arg == 0) {
84 printf("creating 10 threads, runs forever...\n");
85 thread_func = &test_thread;
86 } else if(arg == 1) {
87 printf("creating 10 threads, then killing them after 10 seconds\n");
88 thread_func = &test_thread;
89 } else if(arg == 2) {
90 printf("creating 10 threads, self terminating after 10 seconds\n");
91 thread_func = &test_thread_self_terminate;
92 } else
93 return -1;
95 thread_id tids[num_threads];
96 for(i=0; i<num_threads; i++) {
97 tids[i] = _kern_thread_create_thread("foo", thread_func, (void *)i);
98 _kern_thread_resume_thread(tids[i]);
101 if(arg == 0) {
102 for(;;)
103 usleep(1000000);
104 } else if(arg == 1) {
105 usleep(10000000);
106 for(i=0; i<num_threads; i++) {
107 printf("killing thread %d...\n", tids[i]);
108 _kern_thread_kill_thread(tids[i]);
110 } else if(arg == 2) {
111 _kern_thread_wait_on_thread(tids[0], NULL);
112 usleep(1000000);
115 printf("done\n");
116 return 0;