home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / os / msdos / programm / 9175 < prev    next >
Encoding:
Text File  |  1992-09-08  |  2.3 KB  |  59 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!jvnc.net!nuscc!iti.gov.sg!stan
  3. From: stan@iti.gov.sg (Tan See Mong)
  4. Subject: Re: Threads/Coroutines for DOS
  5. Message-ID: <1992Sep9.020315.9558@iti.gov.sg>
  6. Followup-To: comp.os.msdos.programmer
  7. Summary: How to use longjmp/setjmp for coroutines?
  8. Keywords: setjmp, longjmp, coroutines, threads
  9. Organization: Information Technology Institute, National Computer Board, Singapore.
  10. References: <715992030snx@cotswold.demon.co.uk> <1992Sep8.050225.1013@iti.gov.sg>
  11. Date: Wed, 9 Sep 1992 02:03:15 GMT
  12. Lines: 45
  13.  
  14. In article <715992030snx@cotswold.demon.co.uk> Nigel Roles, nigel@cotswold.demon.co.uk writes:
  15. >BC++ 3.1 reference manual on longjmp states it is sufficient for implementing
  16. >coroutines.
  17.  
  18. Well, I tried that, but it didn't quite work -- can someone tell me
  19. what dumb thing I did wrong?
  20.  
  21. To create a thread, I malloc space for the new stack.  Then I build
  22. a return frame on the new stack by switching the sp and ss to the address
  23. of the new stack, and doing a setjmp (I owe the structure of this code
  24. to the Awesime package by Dirk Grunwald).  Later on, I can switch to
  25. running the new process with a longjmp.  Here is the code:
  26.  
  27. static PROCESS process;        /* set by calling function */
  28. static jmp_buf originalContext; /* static so not ref'd off the stack */
  29.  
  30. /*
  31.  * buildContext() creates a return context on the process' new stack.
  32.  * The return frame is stored in the jmp_buf process->context.
  33.  * To switch to the new proces, a longjmp(process->context) is done.
  34.  */
  35. void buildContext()
  36. {
  37.     if (setjmp(originalContext) == 0) {    /* save where we came from */
  38.         _SS = FP_SEG(process->stackBase);    /* switch to new  */
  39.         _SP = FG_OFF(process->stackBase);    /* stack and then */
  40.         if (setjmp(process->context) == 0) {    /* save context */
  41.             longjmp(originalContext);    /* jump back */
  42.         } else {
  43.             /* switched to new process for 1st time */
  44.             /* so call the process' entry point */
  45.             process = activeProcess();
  46.             (process->entryPoint)(process->arg);
  47.             killProcess(process);
  48.             /* not reached */
  49.         }
  50.     }    
  51.     /* return frame built successfully */
  52. }
  53.  
  54. After switching to the new stack and doing a setjmp there, I can't
  55. seem to get back to the original context with the longjmp. 
  56. If anyone can see what's wrong, please let me know!
  57.  
  58. stan
  59.