Article View: comp.os.msdos.programmer
Article #1372Re: Threads/Coroutines for DOS
From: Tan See Mong
Date: Wed, 09 Sep 1992 02:03
Date: Wed, 09 Sep 1992 02:03
45 lines
2377 bytes
2377 bytes
In article <715992030snx@cotswold.demon.co.uk> Nigel Roles, nigel@cotswold.demon.co.uk writes:
>BC++ 3.1 reference manual on longjmp states it is sufficient for implementing
>coroutines.
Well, I tried that, but it didn't quite work -- can someone tell me
what dumb thing I did wrong?
To create a thread, I malloc space for the new stack. Then I build
a return frame on the new stack by switching the sp and ss to the address
of the new stack, and doing a setjmp (I owe the structure of this code
to the Awesime package by Dirk Grunwald). Later on, I can switch to
running the new process with a longjmp. Here is the code:
static PROCESS process; /* set by calling function */
static jmp_buf originalContext; /* static so not ref'd off the stack */
/*
* buildContext() creates a return context on the process' new stack.
* The return frame is stored in the jmp_buf process->context.
* To switch to the new proces, a longjmp(process->context) is done.
*/
void buildContext()
{
if (setjmp(originalContext) == 0) { /* save where we came from */
_SS = FP_SEG(process->stackBase); /* switch to new */
_SP = FG_OFF(process->stackBase); /* stack and then */
if (setjmp(process->context) == 0) { /* save context */
longjmp(originalContext); /* jump back */
} else {
/* switched to new process for 1st time */
/* so call the process' entry point */
process = activeProcess();
(process->entryPoint)(process->arg);
killProcess(process);
/* not reached */
}
}
/* return frame built successfully */
}
After switching to the new stack and doing a setjmp there, I can't
seem to get back to the original context with the longjmp.
If anyone can see what's wrong, please let me know!
stan
Technical Details
Message-ID:
<1992Sep9.020315.9558@iti.gov.sg>Path:
arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!nuscc!iti.gov.sg!stanReferences:
<715992030snx@cotswold.demon.co.uk> <1992Sep8.050225.1013@iti.gov.sg>Show Full Headers
Complete Article Headers
Newsgroups: comp.os.msdos.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!nuscc!iti.gov.sg!stan From: stan@iti.gov.sg (Tan See Mong) Subject: Re: Threads/Coroutines for DOS Message-ID: <1992Sep9.020315.9558@iti.gov.sg> Followup-To: comp.os.msdos.programmer Summary: How to use longjmp/setjmp for coroutines? Keywords: setjmp, longjmp, coroutines, threads Organization: Information Technology Institute, National Computer Board, Singapore. References: <715992030snx@cotswold.demon.co.uk> <1992Sep8.050225.1013@iti.gov.sg> Date: Wed, 9 Sep 1992 02:03:15 GMT Lines: 45