summaryrefslogtreecommitdiff
path: root/src/user/test/main.c
blob: f8fec484d5f3511402cc94659e3e62371394035d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <tce/syscall.h>
#include <stdlib.h>
#include <tce/Folder.h>
#include <stdio.h>

int threads = 0;

void thread_cascade(void* d) {
	int n = (int)d;

	threads++;

	if (d == 0) {
		//printk("{#} 0 cascade element started => end\n");
		printk("*");
	} else {
		if (n < 0) {
			//printk("{#} - cascade element started\n");
			printk("-");
			n = 0 - n;
		} else {
			//printk("{#} + cascade element started\n");
			printk("+");
		}
		//printk("{#} FORK + ...\n");
		printk(">");
		thread_new(thread_cascade, (void*)(n - 1));
		//printk("{#} FORK - ...\n");
		printk("<");
		thread_new(thread_cascade, (void*)(1 - n));
		//printk("{#} Thread cascade element finished.\n");
		printk(".");
	}

	threads--;
}

void read_dir(Object o, int l) {
	int i = 0, j;
	char name[FILENAME_MAX_LEN];
	while (1) {
		for (j = 0; j < l; j++) printk("   ");
		j = Folder_GetChildNameAt(o, i, name);
		if (j <= 0) {
			printk(" - reached end : ");
			printk_int(j);
			printk("\n");
			break;
		}
		printk(" - '");
		printk(name);

		if (strcmp(name, ".") == 0) {
			printk("', skipping -_-'\n");
		} else {
			Object oo = open_relative(name, o);
			if (oo <= 0) {
				printk("', failed to open, error ");
				printk_int(oo);
				printk("...\n");
			} else {
				printk("', opened as ");
				printk_int(oo);
				printk(", content:\n");
				read_dir(oo, l+1);
			}
		}
		i++;
	}
}


int main() {
	printk("Test starts. ");

	printk("Create thread cascade (2**5 = 32 threads). ");
	thread_new(thread_cascade, (void*)5);

	printk("Main thread waits.\n");
	while (1) {
		thread_sleep(100);
		if (threads == 0) break;
	}
	printk("\nOk, that was fine. Now trying something else...\n");
	Object o = open("/");
	if (o <= 0) {
		printk("Cannot open '/', error ");
		printk_int(o);
		printk("...\n");
	} else {
		printk("Reading '/' (");
		printk_int(o);
		printk(") :\n");
		read_dir(o, 0);
	}

	printk("Ok. Now sleeping, forever and ever.");
	while(1) {
		thread_sleep(1000);
	}
	return 0;
}