2012-12-22 15:50:36 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 Travis Geiselbrecht
|
|
|
|
|
*
|
2019-07-05 17:22:23 -07:00
|
|
|
* Use of this source code is governed by a MIT-style
|
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
|
* https://opensource.org/licenses/MIT
|
2012-12-22 15:50:36 -08:00
|
|
|
*/
|
2013-06-13 01:50:40 -07:00
|
|
|
#include <stdio.h>
|
2012-12-22 15:50:36 -08:00
|
|
|
#include <rand.h>
|
2019-06-17 18:28:51 -07:00
|
|
|
#include <lk/err.h>
|
2012-12-22 15:50:36 -08:00
|
|
|
#include <app/tests.h>
|
|
|
|
|
#include <kernel/thread.h>
|
|
|
|
|
#include <kernel/mutex.h>
|
|
|
|
|
#include <kernel/semaphore.h>
|
|
|
|
|
#include <kernel/event.h>
|
|
|
|
|
#include <platform.h>
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
static int fibo_thread(void *argv) {
|
2016-02-14 12:24:01 -08:00
|
|
|
long fibo = (intptr_t)argv;
|
|
|
|
|
|
|
|
|
|
thread_t *t[2];
|
|
|
|
|
|
|
|
|
|
if (fibo == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if (fibo == 1)
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
char name[32];
|
|
|
|
|
snprintf(name, sizeof(name), "fibo %lu", fibo - 1);
|
|
|
|
|
t[0] = thread_create(name, &fibo_thread, (void *)(fibo - 1), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
|
|
|
|
|
if (!t[0]) {
|
2020-05-16 17:55:50 -07:00
|
|
|
printf("error creating thread for fibo %ld\n", fibo-1);
|
2016-02-14 12:24:01 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
snprintf(name, sizeof(name), "fibo %lu", fibo - 2);
|
|
|
|
|
t[1] = thread_create(name, &fibo_thread, (void *)(fibo - 2), DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
|
|
|
|
|
if (!t[1]) {
|
2020-05-16 17:55:50 -07:00
|
|
|
printf("error creating thread for fibo %ld\n", fibo-2);
|
2016-02-14 12:24:01 -08:00
|
|
|
thread_resume(t[0]);
|
|
|
|
|
thread_join(t[0], NULL, INFINITE_TIME);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread_resume(t[0]);
|
|
|
|
|
thread_resume(t[1]);
|
|
|
|
|
|
|
|
|
|
int retcode0, retcode1;
|
|
|
|
|
|
|
|
|
|
thread_join(t[0], &retcode0, INFINITE_TIME);
|
|
|
|
|
thread_join(t[1], &retcode1, INFINITE_TIME);
|
|
|
|
|
|
|
|
|
|
return retcode0 + retcode1;
|
2012-12-22 15:50:36 -08:00
|
|
|
}
|
|
|
|
|
|
2020-07-25 15:59:58 -07:00
|
|
|
int fibo(int argc, const console_cmd_args *argv) {
|
2012-12-22 15:50:36 -08:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
if (argc < 2) {
|
|
|
|
|
printf("not enough args\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-12-22 15:50:36 -08:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
lk_time_t tim = current_time();
|
2014-05-01 19:05:08 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
thread_t *t = thread_create("fibo", &fibo_thread, (void *)(uintptr_t)argv[1].u, DEFAULT_PRIORITY, DEFAULT_STACK_SIZE);
|
|
|
|
|
thread_resume(t);
|
2012-12-22 15:50:36 -08:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
int retcode;
|
|
|
|
|
thread_join(t, &retcode, INFINITE_TIME);
|
2012-12-22 15:50:36 -08:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
tim = current_time() - tim;
|
2014-05-01 19:05:08 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
printf("fibo %d\n", retcode);
|
|
|
|
|
printf("took %u msecs to calculate\n", tim);
|
2012-12-22 15:50:36 -08:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
return NO_ERROR;
|
2012-12-22 15:50:36 -08:00
|
|
|
}
|