delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/16/21:14:27

From: "XtReMe" <xtreme AT x-project DOT demon DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: a VERY difficult problem
Date: Fri, 17 Sep 1999 00:08:20 +0200
Message-ID: <937519527.589.0.pluto.d4ee0fa5@news.demon.nl>
NNTP-Posting-Host: x-project.demon.nl
X-NNTP-Posting-Host: x-project.demon.nl:212.238.15.165
X-Trace: news.demon.nl 937519527 pluto:589 NO-IDENT x-project.demon.nl:212.238.15.165
X-Complaints-To: abuse AT demon DOT net
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Lines: 84
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hi!

Is there anyone who can help me with my problem?
I'm trying to do multithreading in DJGPP

I will try to explain what I did...

First, I declared a structure "Thread", which holds all the registers
and a boolean value to indicate if it's activated (and some more
things, but they aren't relevant)

Then I declared a global thread, with the idea to transfer the control
from the currently executing function to the global thread.

All threads are linked together with pointers, so at first, globalThread
points to itself.

Then I hook the timer interrupt (irq 0, int 08h). I've written a function
that saves all registers to the current thread, then it reads all the
registers from the next thread and returns to where the next thread left
off. In psuedo code:

    saveregisters (currentThread);
    currentThread = currentThread->nextThread;
    while (currentThread->Activated == false)
        currentThread = currentThread->nextThread;
    loadregisters (currentThread);
    jump currentThread->cs:eip;

Ok, now the timer interrupt is hooked and all code executes normally
where it left off since the first interrupt. Actually, the whole program
just works fine, till it reaches the last statement, and then it exits
normally. Because globalThread->next points to itself, it just
saves all registers to the thread and then it loads all the registers
just saved and it returns. Like it should. So, at this time, there's no
error in my interrupt handler

Then, I included a new thread, but without activating it.
A linked list should look like this:
globalThread  ->  newThread  ->  globalThread  ->  newThread  -> .....
The above code handles the new thread, but Activated isn't true,
so it just skips it. I placed a "WaitForKey" loop in the global thread and
a simple counter loop in the new thread.
Again, all goes well. The counter isn't incemented, because the
new thread wasn't acitivated and the program exits on keypress

But then, when I activate the new thread, my program crashes.

So I gave it a new approach.
I unhooked the timer interrupt, and called my interrupt handler
myself at the end of the waitforkey loop in globalThread and at the end
of the counter loop in newThread

I had something like:
void newThread_run ()
{
    while (true) {
        counter++;
        timer_handler ();        /* this is my interrupt handler */
    }
}

and in globalThread :

    while (!kbhit ()) {
        timer_handler ();
    }

The program starts, and switches from thread on every timer_handler ()
like it should. So, again, nothing is wrong with my handler.

Then why doesn't it work properly when I let the CPU call the interrupt?
Why does it crash?
Does anyone have a sollution for this problem or just a reason why I
have this problem?
(If anyone actually understood what I tried to explain anyway. Sorry
but this was the best I can do...)

Thanx!

Sylvester Hesp
oisyn AT x-project DOT demon DOT nl


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019