home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Threads Interface / CThread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-03  |  2.8 KB  |  161 lines  |  [TEXT/KAHL]

  1. /***
  2.  * CThread.c
  3.  *
  4.  *  Simple, abstract, interface to the threads package.
  5.  *        Copyright © Gordon Watts 1994 (gwatts@fnal.fnal.gov)
  6.  *
  7.  ***/
  8. #include "CThread.h"
  9. #include "CErrorRecorder.h"
  10. #include <Exceptions.h>
  11. #include "util.h"
  12.  
  13. #define errStartBUG 400                    /* Hey mon, internal programming error! */
  14.  
  15. static short gThreadCount = 0;
  16. extern FailInfo *gTopHandler;
  17.  
  18. /**
  19.  * CThread
  20.  *
  21.  *  Zero our variables
  22.  *
  23.  **/
  24. CThread :: CThread (void)
  25. {
  26.     theThread = 0;
  27.     theOptions = kFPUNotNeeded;
  28.     recycleThread = false;
  29.     theStackSize = 0;                    /* Use default size. */
  30.     deleteWhenDone = false;
  31. }
  32.  
  33. /**
  34.  * ~CThread
  35.  *
  36.  * Kill off this thread if we need to
  37.  *
  38.  **/
  39. CThread :: ~CThread (void)
  40. {
  41.     if (isRunning()) {
  42.         Kill ();
  43.     }
  44.     AssertStr (theThread == 0, "\pBad bug -- thread not zero after dele of thread obj!");
  45. }
  46.  
  47. /**
  48.  * Start
  49.  *
  50.  *  Start up the thread.  This is a subclass responsibility in a really big way.
  51.  *
  52.  **/
  53. void CThread :: Start (void)
  54. {
  55.     Failure (errStartBUG, 0L);
  56. }
  57.  
  58. /**
  59.  * ThreadRoutine
  60.  *
  61.  *  This is the method that does the work for this thread.  It will be called in
  62.  *  the thread's environment
  63.  *
  64.  **/
  65. void CThread :: ThreadRoutine (void)
  66. {
  67. }
  68.  
  69. /**
  70.  * ThreadExtProc
  71.  *
  72.  *  The class, external procedure that calls the thread routine
  73.  *
  74.  **/
  75. static pascal void *CThread :: ThreadExtProc (void *param)
  76. {
  77.     CThread *theThreadObj = (CThread *) param;
  78.     short    myCount = gThreadCount;
  79.     FailInfo        *myHandler;
  80.  
  81.     gThreadCount++;
  82.  
  83.     TRY {
  84.  
  85.         myHandler = gTopHandler;
  86.         theThreadObj -> ThreadRoutine ();
  87.         if (myHandler != gTopHandler)
  88.             CErrorRecorder :: gError -> Message ("\pHandler does not match!");
  89.  
  90.     } CATCH {
  91.  
  92.         if (gLastError != kSilentErr)
  93.             CErrorRecorder :: gError -> OSError (gLastError, "\pA thread died abnormally");
  94.  
  95.         NO_PROPAGATE;
  96.  
  97.     } ENDTRY;
  98.  
  99.     /**
  100.      ** Clean up now
  101.      **/
  102.  
  103.     theThreadObj->theThread = 0;
  104.  
  105.     /**
  106.      ** If we are going to delete this thread, had better do it now...
  107.      **/
  108.  
  109.     if (theThreadObj->deleteWhenDone)
  110.         delete theThreadObj;
  111.  
  112.     return noErr;
  113. }
  114.  
  115. /**
  116.  * Kill
  117.  *
  118.  *  Kill this thread.  Right off the bat.  Boom.
  119.  *
  120.  **/
  121. void CThread :: Kill (void)
  122. {
  123.     ThreadID    tempID;
  124.  
  125.     AssertStr (theThread != kNoThreadID, "\pCan not kill a non-existant thread!");
  126.  
  127.     FailOSErr (ThreadBeginCritical());
  128.  
  129.     tempID = theThread;
  130.     theThread = kNoThreadID;
  131.  
  132.     FailOSErr (DisposeThread (tempID, 0L, recycleThread));
  133.  
  134.     FailOSErr (ThreadEndCritical());
  135. }
  136.  
  137. /**
  138.  * Sleep
  139.  *
  140.  *  Put this thread to sleep
  141.  *
  142.  **/
  143. void CThread :: Sleep (void)
  144. {
  145.     AssertStr (theThread != kNoThreadID, "\pBad thread to put to sleep");
  146.  
  147.     FailOSErr (SetThreadState (theThread, kStoppedThreadState, kNoThreadID));
  148. }
  149.  
  150. /**
  151.  * DeleteOnFinish
  152.  *
  153.  *  Let outside folks decide if they should release our memory when our thread
  154.  *  executes.  In other words -- it is a one time only type of thing.
  155.  *
  156.  **/
  157. void CThread :: DeleteOnFinish (Boolean doIt)
  158. {
  159.     deleteWhenDone = doIt;
  160. }
  161.