home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_01 / yuen / cthread.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  2.2 KB  |  92 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "cthread.hpp"
  4.  
  5. /* This module is compiler-specific. It relies on the 
  6.    structure of jmp_buf and the C function prolog and 
  7.    epilog to work properly */
  8.   
  9. /* The following definitions are for the Zortech V3.0 
  10.    C++ Windows and OS/2 compilers */
  11.  
  12. #define WORDSTOREMOVE    3    //used for stack adjustment
  13.  
  14.  
  15. typedef struct            //jmp_buf map
  16.     {
  17.     long ip;         //CS:IP
  18.     short filler1; 
  19.     long sp;         //SS:SP
  20.     } *mapptr;    
  21.  
  22.  
  23. /* Used to transfer to the specified thread. The context
  24.    of the running thread is saved in the thread object's
  25.    private data area */
  26. void Cthread::Transfer(Cthread &thread)
  27. {
  28.     /* don't transfer control if the new thread's 
  29.        stack is corrupt */
  30.     if ((thread.threadbody == NULL) || 
  31.         (thread.threadbody->Overflowed)) return;
  32.  
  33.     /* save old thread's context and transfer control */ 
  34.     if (!setjmp(this->threadbody->Context))
  35.         longjmp(thread.threadbody->Context, 1);
  36.  
  37. }
  38.  
  39.  
  40. /* Used to create a thread object and copy a number of
  41.    parameters to the thread's stack */
  42. Cthread::Cthread(THDFN func, int stacksize, 
  43.         int *frame, int framesize)
  44. {
  45.     int i;
  46.     mapptr ptr;
  47.  
  48.     /* allocate stack */
  49.     stacksize /= sizeof(int);    //convert to out allocation unit
  50.     stacksize = (stacksize > THD_MAX_STACK) ? THD_MAX_STACK: stacksize;
  51.  
  52.     threadbody = (THDPTR) new char[i = (sizeof(THD) - sizeof(int) * 
  53.             (THD_MAX_STACK - stacksize))]; 
  54.     threadbody->Overflowed = 0;
  55.     threadbody->TotalLen = i;
  56.     if (threadbody  == NULL)
  57.         {
  58.         printf("Thread creation failed...\n");
  59.         }
  60.  
  61.  
  62.  
  63.     setjmp(threadbody->Context);    //initialize jmp_buf structure
  64.     ptr = (mapptr) &threadbody->Context;
  65.     /* initialize stack with parameters if any */
  66.     if (stacksize)
  67.         {
  68.         if ((frame != NULL) && (stacksize > framesize))
  69.             {
  70.             for (i = 0; i < framesize; i++)
  71.                 threadbody->Stack[stacksize - framesize + i] = 
  72.                     *frame++;
  73.             stacksize -= framesize;
  74.             } 
  75.         //set stack pointer
  76.         ptr->sp = (long) &(threadbody->Stack[stacksize - 
  77.             WORDSTOREMOVE]);
  78.         }
  79.  
  80.     /* set up the start of the thread body */
  81.     ptr->ip = (long) func;
  82.  
  83. }
  84.  
  85.  
  86. /* Destructor */
  87. Cthread::~Cthread(void)
  88. {
  89.     delete [threadbody->TotalLen] (char *) threadbody;
  90.     threadbody = NULL;
  91. }
  92.