1 /*
2 * Copyright (c) 2012 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #include <stdio.h>
9 #include <rand.h>
10 #include <lk/err.h>
11 #include <app/tests.h>
12 #include <kernel/thread.h>
13 #include <kernel/mutex.h>
14 #include <kernel/semaphore.h>
15 #include <kernel/event.h>
16 #include <platform.h>
17
fibo_thread(void * argv)18 static int fibo_thread(void *argv) {
19 long fibo = (intptr_t)argv;
20
21 thread_t *t[2];
22
23 if (fibo == 0)
24 return 0;
25 if (fibo == 1)
26 return 1;
27
28 char name[32];
29 snprintf(name, sizeof(name), "fibo %lu", fibo - 1);
30 t[0] = thread_create(name, &fibo_thread, (void *)(fibo - 1), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
31 if (!t[0]) {
32 printf("error creating thread for fibo %ld\n", fibo-1);
33 return 0;
34 }
35 snprintf(name, sizeof(name), "fibo %lu", fibo - 2);
36 t[1] = thread_create(name, &fibo_thread, (void *)(fibo - 2), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
37 if (!t[1]) {
38 printf("error creating thread for fibo %ld\n", fibo-2);
39 thread_resume(t[0]);
40 thread_join(t[0], NULL, INFINITE_TIME);
41 return 0;
42 }
43
44 thread_resume(t[0]);
45 thread_resume(t[1]);
46
47 int retcode0, retcode1;
48
49 thread_join(t[0], &retcode0, INFINITE_TIME);
50 thread_join(t[1], &retcode1, INFINITE_TIME);
51
52 return retcode0 + retcode1;
53 }
54
fibo(int argc,const console_cmd_args * argv)55 int fibo(int argc, const console_cmd_args *argv) {
56
57 if (argc < 2) {
58 printf("not enough args\n");
59 return -1;
60 }
61
62 lk_time_t tim = current_time();
63
64 thread_t *t = thread_create("fibo", &fibo_thread, (void *)(uintptr_t)argv[1].u, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
65 thread_resume(t);
66
67 int retcode;
68 thread_join(t, &retcode, INFINITE_TIME);
69
70 tim = current_time() - tim;
71
72 printf("fibo %d\n", retcode);
73 printf("took %u msecs to calculate\n", tim);
74
75 return NO_ERROR;
76 }
77