Multiple thread switching issue
Posted: Wed Mar 29, 2017 2:44 pm
Hi all,
I have been implementing a thread system and am implementing a VERY basic time slicing algorithm to start. I have been having issues with the instruction pointer being corrupted after a few cycles.
It switches between two separate threads (which are essentially just an address to a function located in the kernel) but after a few switches, my eip register gets corrupt and manages to take one thread out. The other one seems to be stable, so it's very confusing as to why only a single thread has this issue.
Here is the basic outline of my code, a few lines removed for clarity:
This function is called in my timer interrupt every 10 ticks (which is where the "register * r" parameter comes from)
Is this the direction that I need to take for thread switching?
-----
Here are my thread functions, purely for clarity's sake:
so just saving "eip" should be enough for how simple the functions are
I have been implementing a thread system and am implementing a VERY basic time slicing algorithm to start. I have been having issues with the instruction pointer being corrupted after a few cycles.
It switches between two separate threads (which are essentially just an address to a function located in the kernel) but after a few switches, my eip register gets corrupt and manages to take one thread out. The other one seems to be stable, so it's very confusing as to why only a single thread has this issue.
Here is the basic outline of my code, a few lines removed for clarity:
Code: Select all
static int cTask = 0;
std::vector<Thread *> thread_list;
void schedule_next(registers * r) {
if(cTask == 0) {
if(thread_list[thread_list.size()-1]->ran)
thread_list[thread_list.size()-1]->eip = r->eip;
} else {
thread_list[cTask-1]->eip = r->eip;
}
thread_list[cTask]->ran = true;
r->eip = thread_list[cTask]->eip;
cTask++;
if(cTask >= thread_list.size()) {
cTask = 0;
}
}
Is this the direction that I need to take for thread switching?
-----
Here are my thread functions, purely for clarity's sake:
Code: Select all
static void test_thread() {
for(;;) {
BochsConsolePrintChar('a');
}
}
static void test_thread2() {
for(;;) {
BochsConsolePrintChar('b');
}
}