I have some problems with my (basic) implementation of a semaphore.
The kernel is written in C++ and based on OOStuBS. It was started in school and I continued to develop it after class.
Currently it doesn't do a lot: interrupts and user-level-threads. Scheduling is time based and uses self implemented versions of get-/setcontext and make-/swapcontext.
It doesn't have any memory management or processes yet.
Before, every thread had to disable and enable interrupts when printing onto screen. To make that unnecessary, I have implemented a semaphore which blocks threads that request access to the screen when it is already in use.
The problem I have is that after the first thread has been switched out, the Scheduler::resume() doesn't get called based on timer interrupts anymore but rather every "cycle" of the thread. In addition, sometimes an exception gets thrown (either 1, 6 or 13).
The problem seems to come with the resume() call when a thread tries to get access to the screen but gets blocked because it is in use already. When it is commented out, the scheduler gets called as intended and no exceptions get thrown (but the screen is messed up of course ), but I just can't find the mistake >.<
Scheduler::resume()
Code: Select all
void Scheduler::resume()
{
cpu.disable_int();
Thread *temp = activeThread;
do
{
threads.enqueue(temp);
temp = threads.dequeue();
}
while(!temp->isReady());
dispatch(*temp);
}
Code: Select all
void Semaphore::wait(Thread* thread)
{
cpu.disable_int();
if(count > 0)
{
count = 0;
cpu.enable_int();
kout << endl;
}
else
{
queue.enqueue(thread);
cpu.enable_int();
thread->block();
}
}
void Semaphore::signal()
{
cpu.disable_int();
if(queue.isEmpty())
count = 1;
else
queue.dequeue()->ready();
cpu.enable_int();
}
Thanks in advance