kthread implement yield

This commit is contained in:
Mathieu Maret 2020-04-24 23:34:34 +02:00
parent b2d3cd62ca
commit dafaba7407
3 changed files with 60 additions and 11 deletions

View File

@ -1,19 +1,25 @@
#include "assert.h"
#include "kthread.h"
#include "list.h"
#include "alloc.h"
#include "assert.h"
#include "irq.h"
#include "klibc.h"
#include "list.h"
#include "vga.h"
static struct kthread *currentThread;
void kthreadExit(){
void kthreadExit()
{
uint32_t flags;
disable_IRQs(flags);
struct kthread *current = currentThread;
struct kthread *next = kthreadSelectNext();
struct kthread *next = kthreadSelectNext();
if (next == current)
assert("cannot exit thread");
currentThread = next;
cpu_context_exit_to(next->cpuState, (cpu_kstate_function_arg1_t *)kthreadDelete, (uint32_t)current);
cpu_context_exit_to(next->cpuState, (cpu_kstate_function_arg1_t *)kthreadDelete,
(uint32_t)current);
restore_IRQs(flags);
return;
}
@ -71,10 +77,26 @@ struct kthread *kthreadSelectNext(){
return nextThread;
}
struct cpu_state *switchKthread(struct cpu_state *prevCpu){
currentThread->cpuState = prevCpu;
struct cpu_state *switchKthread(struct cpu_state *prevCpu)
{
uint32_t flags;
disable_IRQs(flags);
currentThread->cpuState = prevCpu;
struct kthread *nextThread = kthreadSelectNext();
printStringDetails(nextThread->name, RED, BLACK, 40, VGA_HEIGHT - 1);
currentThread = nextThread;
restore_IRQs(flags);
return nextThread->cpuState;
}
int kthreadYield()
{
uint32_t flags;
disable_IRQs(flags);
struct kthread *next = kthreadSelectNext();
struct kthread *current = currentThread;
currentThread = next;
cpu_context_switch(&current->cpuState, next->cpuState);
restore_IRQs(flags);
return 0;
}

View File

@ -15,14 +15,13 @@ struct kthread {
struct kthread *prev;
};
int kthreadSetup(vaddr_t mainStack, size_t mainStackSize);
void kthreadExit();
struct kthread *kthreadCreate(const char *name, cpu_kstate_function_arg1_t func,
void *args);
struct kthread *kthreadCreate(const char *name, cpu_kstate_function_arg1_t func, void *args);
void kthreadDelete(struct kthread *thread);
struct kthread *kthreadSelectNext();
struct cpu_state *switchKthread(struct cpu_state *prevCpu);
int kthreadYield();

View File

@ -2,6 +2,7 @@
#include "assert.h"
#include "cpu_context.h"
#include "klibc.h"
#include "kthread.h"
#include "list.h"
#include "mem.h"
#include "paging.h"
@ -217,6 +218,32 @@ void testCoroutine()
printf("Back in main !\n");
}
static void kthread1(void *strIn)
{
char *str = (char *)strIn;
printf("\nkth1: %s\n", (char *)strIn);
for (; *str != '\n'; str++) {
printf("kth1_: %c\n", *str);
kthreadYield();
}
}
static void kthread2(void *strIn)
{
char *str = (char *)strIn;
printf("\nkth2: %s\n", (char *)strIn);
for (; *str != '\n'; str++) {
printf("kth2_: %c\n", *str);
kthreadYield();
}
}
void testKthread()
{
kthreadCreate("Test1", (cpu_kstate_function_arg1_t *)kthread1, (void *)"Hlowrd\n");
kthreadCreate("Test2", (cpu_kstate_function_arg1_t *)kthread2, (void *)"el ol\n");
}
void run_test(void)
{
testPaging();
@ -230,4 +257,5 @@ void run_test(void)
printf("Testing backtrace\n");
test_backtrace();
testCoroutine();
testKthread();
}