Fixed multitasking bugs

This commit is contained in:
Morten Delenk 2016-04-22 15:07:52 +02:00
parent 9a3a11d78c
commit 2cc45e996f
4 changed files with 24 additions and 31 deletions

View file

@ -11,8 +11,6 @@ namespace MTGosHAL {
uint32_t tss[32]; uint32_t tss[32];
protected: protected:
private: private:
static auto task_a() -> void;
static auto task_b() -> void;
Task* first_task; Task* first_task;
Task* curr_task; Task* curr_task;
}; };
@ -26,6 +24,7 @@ namespace MTGosHAL {
auto unpause() -> struct cpu_state*; auto unpause() -> struct cpu_state*;
auto pause(struct cpu_state*) -> Task *; auto pause(struct cpu_state*) -> Task *;
auto addTask(Task*) -> void; auto addTask(Task*) -> void;
auto hasNext() -> bool;
}; };
} // namespace MTGosHAL } // namespace MTGosHAL

View file

@ -14,8 +14,6 @@ namespace MTGosHAL {
uint32_t tss[32]; uint32_t tss[32];
protected: protected:
private: private:
static auto task_a() -> void;
static auto task_b() -> void;
Task* first_task; Task* first_task;
Task* curr_task; Task* curr_task;
}; };
@ -29,6 +27,7 @@ namespace MTGosHAL {
auto unpause() -> struct cpu_state*; auto unpause() -> struct cpu_state*;
auto pause(struct cpu_state*) -> Task *; auto pause(struct cpu_state*) -> Task *;
auto addTask(Task*) -> void; auto addTask(Task*) -> void;
auto hasNext() -> bool;
}; };
} // namespace MTGosHAL } // namespace MTGosHAL

View file

@ -55,40 +55,24 @@ auto Multitasking::initTask(void(* entry)()) -> struct cpu_state*
else { else {
first_task=task; first_task=task;
} }
curr_task=first_task;
return state; return state;
} }
auto Multitasking::task_a() -> void
{
while(true) {
char arr[513];
arr[512]=0;
disk.readSector(disk.getDriveNumByName("ATA0m"),0,(uint8_t*)arr);
out << arr;
}
}
auto Multitasking::task_b() -> void
{
while(true) {
char arr[513];
arr[512]=0;
disk.readSector(disk.getDriveNumByName("ATA0m"),1,(uint8_t*)arr);
out << arr;
}
}
auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state* auto Multitasking::schedule(struct cpu_state* cpu) -> struct cpu_state*
{ {
Task* next=curr_task->pause(cpu); Task* next=nullptr;
if(next==nullptr) if(curr_task) {
next=curr_task->pause(cpu);
}
if (!next) {
next=first_task; next=first_task;
}
curr_task=next; curr_task=next;
return next->unpause(); return next->unpause();
} }
Task::Task(struct cpu_state* cpu): cpu_state(cpu), next(nullptr) {}; Task::Task(struct cpu_state* cpu): cpu_state(cpu), next(nullptr) {};
//This is run every time this task is chosen by the scheduler //This is run every time this task is chosen by the scheduler
auto Task::unpause() -> struct cpu_state* { auto Task::unpause() -> struct cpu_state* {
MTGosHAL::tasks.tss[1] = (uint32_t) (cpu_state + 1);
return cpu_state; return cpu_state;
} }
//This is run every time the timer ticks and a task is running //This is run every time the timer ticks and a task is running
@ -101,4 +85,7 @@ auto Task::addTask(Task* task) -> void {
return next->addTask(task); return next->addTask(task);
next=task; next=task;
} }
auto Task::hasNext() -> bool {
return next!=nullptr;
}
} // namespace MTGosHAL } // namespace MTGosHAL

View file

@ -6,20 +6,28 @@
#include <Multitasking.h> #include <Multitasking.h>
#include <blockdev.hpp> #include <blockdev.hpp>
using namespace MTGosHAL; using namespace MTGosHAL;
void pid_null() {
for(;;);
}
void task_a() { void task_a() {
MTGosHAL::out << "a"; while(true)
MTGosHAL::out << "a";
} }
void task_b() { void task_b() {
MTGosHAL::out << "b"; while(true)
MTGosHAL::out << "b";
} }
void task_c() { void task_c() {
MTGosHAL::out << "c"; while(true)
MTGosHAL::out << "c";
} }
void task_d() { void task_d() {
MTGosHAL::out << "d"; while(true)
MTGosHAL::out << "d";
} }
void main() { void main() {
MTGosHAL::out << "Initializing Kernel!\n"; MTGosHAL::out << "Initializing Kernel!\n";
MTGosHAL::tasks.initTask(&pid_null);
MTGosHAL::tasks.initTask(&task_a); MTGosHAL::tasks.initTask(&task_a);
MTGosHAL::tasks.initTask(&task_b); MTGosHAL::tasks.initTask(&task_b);
MTGosHAL::tasks.initTask(&task_c); MTGosHAL::tasks.initTask(&task_c);