diff options
Diffstat (limited to 'kernel/task.c')
| -rw-r--r-- | kernel/task.c | 142 |
1 files changed, 44 insertions, 98 deletions
diff --git a/kernel/task.c b/kernel/task.c index 08301cf..620977e 100644 --- a/kernel/task.c +++ b/kernel/task.c @@ -14,75 +14,27 @@ #include "fs/ext2.h" #define FOOLOS_MODULE_NAME "task" -int started; -static volatile int c1,c2,c3; -void task_test1() -{ - // simple built-in shell - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"spawning build-in shell"); - - while(1) - { - - char c; - if(ringbuffer_get(&c)) - { - console_put_char_white(c); - } - } - - /* - c1++; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 1 progress: %d", c1); - sleep(2); - */ - - - -} +#define MAX_TASKS 10 -void task_test2() -{ - while(1) - { - c2++; - //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 2 progress: %d", c2); -// sleep(2); - - } +static volatile int current_task=-2; -} - -void task_test3() +static volatile struct task_list_struct { - while(1) - { - c3++; -// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"task 3 progress: %d", c3); -// sleep(2); - } - -} - -////////////// -// -typedef struct{ //Simple structure for a thread - unsigned int esp0; //Stack for kernel - unsigned int esp3; //Stack for process -} Thread; - - -Thread Threads[3]; //Space for our simple threads. -volatile int CurrentTask; //The thread currenlty running (-1 == none) + bool active; + uint32_t esp; // stack pointer of the task; + uint32_t vmem; // number of virtual memory table to switch to + +}task_list[MAX_TASKS]; void task_create(int pid,void(*thread)()) { unsigned int *stack; - Threads[pid].esp0 = pmmngr_alloc_block(); - stack = (unsigned int*)Threads[pid].esp0+4095; //This makes a pointer to the stack for us + task_list[pid].esp = pmmngr_alloc_block(); + stack = (unsigned int*)task_list[pid].esp+4095; //This makes a pointer to the stack for us + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"new task: stack: 0x%08X thread: 0x%08X", stack, thread); //First, this stuff is pushed by the processor @@ -108,60 +60,54 @@ void task_create(int pid,void(*thread)()) *--stack = 0x10; //GS */ - Threads[pid].esp0 = (uint32_t)stack; //Update the stack pointer - + task_list[pid].esp = (uint32_t)stack; //Update the stack pointer + task_list[pid].active=true; }; +// this gets called by our clock interrupt regularly! uint32_t task_switch_next(uint32_t oldesp) { + timer_tick(); - //console_put_char('.'); - timer_tick(); - if(started!=0xabcde) return oldesp; - if(CurrentTask!=-1)Threads[CurrentTask].esp0=oldesp; - - CurrentTask++; - if(CurrentTask>2)CurrentTask=0; - - /* - log( - FOOLOS_MODULE_NAME, - FOOLOS_LOG_INFO, - "oldesp: 0x%08X saved / next task: %d (esp: 0x%08X) ", - oldesp, - CurrentTask, - Threads[CurrentTask].esp0); - -// return oldesp; -// */ - - return Threads[CurrentTask].esp0; //Return new stack pointer to ASM -} + if(current_task==-2)return oldesp; + if(current_task!=-1)task_list[current_task].esp=oldesp; -void stack_trace(uint32_t *stack,int size) -{ - for(int i=size;i>=0;i--) + for(int i=0;i<MAX_TASKS;i++) { - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"stack: 0x%08X -> 0x%08X", stack, *stack); - stack++; + int pid=(current_task+1+i)%MAX_TASKS; // schedule round robin style + if(task_list[pid].active) + { + log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"switch from %d to pid: %d (0x%08X)", current_task,pid,task_list[pid].esp); + current_task=pid; + return task_list[pid].esp; + } } -} - -void task_init() -{ + // if no task found retunr the old esp + return oldesp; +} - CurrentTask=-1; +volatile int c1,c2; - log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"init multitasking."); - task_create(0,task_test1); - task_create(1,task_test2); - task_create(2,task_test3); - started=0xabcde; +volatile void test1() +{ + c1++; + // syscall_write(1,">",1); } +volatile void test2() +{ + c2++; + // syscall_write(1,"<",1); +} +void task_init() +{ + task_create(1,test1); + task_create(2,test2); + current_task=-1; +} |
