summaryrefslogtreecommitdiff
path: root/src/user/app/test/main.c
blob: d9b6332f5376516a6869b1c27fd87ee2687ef9e6 (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
#include <tce/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>

int threads = 1;

mutex_t d_mutex = MUTEX_UNLOCKED;
int d_pos = 0;
int vgatxt = -1;

void display_it(char k) {
	mutex_lock(&d_mutex);

	// just display some stuff on /.dev/vgatxt
	if (vgatxt == -1) {
		vgatxt = open("/.dev/vgatxt", FM_READ | FM_WRITE);
	}
	uint16_t c = k | (4 << 8);
	write(vgatxt, 2 * d_pos, 2, (char*)&c);

	d_pos++;

	mutex_unlock(&d_mutex);
}

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

	char *v = malloc(2048);
	if (!v) display_it('!');

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

	free(v);

	threads--;
}

void useless_thread(void* d) {
	while(1) {
		display_it('~');
		schedule();
	}
}

int main(int argc, char** args) {
	printk("(test) Creating thread cascade (total 2**7 = 128 threads)\n");
	thread_new(useless_thread, 0);
	thread_new(thread_cascade, (void*)7);

	while (1) {
		schedule();
		if (threads == 0) break;
	}
	printk("\n(test) Test process exiting. Press the super key to go to the home terminal.\n");

	printf("(test) End.\n");

	return 0;
}