home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Threads Interface / thread stresser source / main.c < prev   
Encoding:
C/C++ Source or Header  |  1994-04-03  |  4.0 KB  |  207 lines  |  [TEXT/KAHL]

  1. /***
  2.  * main
  3.  *
  4.  *  This puppy will run the thread guys in a "stressful" environment.
  5.  *
  6.  *  It will create 100 copies of the "random" thread and start them off.  The random
  7.  *  thread just runs and runs and runs.  It rolls the dice and every now and then
  8.  *  will beep, quit normally, or fail with an OS Error.
  9.  *
  10.  *  The main program will watch for a mouse down event and quit when it sees one.
  11.  *  Every minute it will check to see how many threads are currently up and running.
  12.  *  It will add till there are 100 up and running.
  13.  *
  14.  *  The mouse down will not try to quit all the events -- it will just die right off.
  15.  *        Copyright © Gordon Watts 1994 (gwatts@fnal.fnal.gov)
  16.  *
  17.  ***/
  18. #include "Exceptions.h"
  19. #include "util.h"
  20. #include "CErrorRecorder.h"
  21. #include <Threads.h>
  22. #include "CThreadManager.h"
  23. #include "CErrorRecorder.h"
  24. #include "CTestThread.h"
  25. #include "CTimeManager.h"
  26.  
  27. void    StartMoreThreads (void);
  28.  
  29. short    numActive = 0;
  30. #define MAX_ACTIVE_THREADS 70
  31. #define MODE_CHANGE_TIME    36000
  32.  
  33. typedef enum {modeReFill, modeWaitTillEmpty } testMode;
  34.  
  35. main ()
  36. {
  37.     Boolean        quit = FALSE;
  38.     Boolean     gotEvent;
  39.     EventRecord    theEvent;
  40.     short        theEventMask;
  41.     long        lastThreadCheck;
  42.     CThreadManager    *threadManager;
  43.     long        seconds;
  44.     testMode    mode;
  45.     long        nextModeChange;
  46.  
  47.  
  48.     /**
  49.      ** Get memory set up
  50.      **/
  51.  
  52.     MaxApplZone ();
  53.     MoreMasters ();
  54.     MoreMasters ();
  55.  
  56.     /**
  57.      ** Init the QD globals -- cause we need to have them around so we can access the
  58.      ** random number generator...  Also, randomize the random number seed so we
  59.      ** don't just repeat the same experiment over and over again!
  60.      **/
  61.     
  62.     InitGraf(&thePort);
  63.     GetDateTime (&seconds);
  64.     randSeed = seconds;
  65.  
  66.     /**
  67.      ** Init the menu manager...
  68.      **/
  69.  
  70.     InitFonts();
  71.     InitWindows();
  72.     InitMenus ();
  73.  
  74.     /**
  75.      ** Start testing by sitting in the wait till all threads are dead before doing
  76.      ** a refill...
  77.      **/
  78.     
  79.     mode = modeWaitTillEmpty;
  80.     nextModeChange = MODE_CHANGE_TIME;
  81.     nextModeChange += TickCount();
  82.  
  83.     /**
  84.      ** Start up the error logger
  85.      **/
  86.     
  87.     new CErrorRecorder;
  88.  
  89.     /**
  90.      ** Boot up the thread manager
  91.      **/
  92.     
  93.     threadManager = new CThreadManager;
  94.  
  95.     /**
  96.      ** Get the time manager going too
  97.      **/
  98.     
  99.     new CTimeManager;
  100.  
  101.     /**
  102.      ** Set the last Thread Check to be zero.  That way we will create a bunch
  103.      ** of new threads the first time through the loop.
  104.      **/
  105.     
  106.     lastThreadCheck = 0;
  107.  
  108.     /**
  109.      ** We only want high level events (not that we are going to do anything with
  110.      ** them...) and mouse down events
  111.      **/
  112.  
  113.     theEventMask = highLevelEventMask+mDownMask;
  114.  
  115.     /**
  116.      **  Do everything in an error type thing...
  117.      **/
  118.     
  119.     TRY {
  120.  
  121.     /**
  122.      ** The main event loop
  123.      **/
  124.  
  125.         while (!quit) {
  126.     
  127.             TestMemory ();                // Just stress things out to make sure nothing is
  128.                                         // happening
  129.  
  130.             if (TickCount() > nextModeChange) {
  131.                 switch (mode) {
  132.                 case    modeReFill:
  133.                     mode = modeWaitTillEmpty;
  134.                     break;
  135.                 case    modeWaitTillEmpty:
  136.                     mode = modeReFill;
  137.                     break;
  138.                 }
  139.                 nextModeChange = TickCount() + MODE_CHANGE_TIME;
  140.                 CErrorRecorder::gError -> OSError (mode, "\pChanging modes...");
  141.             }
  142.  
  143.             switch (mode) {
  144.  
  145.                 case modeReFill:
  146.                 
  147.                     if (TickCount() > lastThreadCheck) {
  148.                         StartMoreThreads ();
  149.                         lastThreadCheck = TickCount() + 60*60;
  150.                     }
  151.                     break;
  152.                 
  153.                 case modeWaitTillEmpty:
  154.                     if (numActive == 0)
  155.                         StartMoreThreads ();
  156.                     break;
  157.             }
  158.  
  159.             gotEvent = WaitNextEvent(theEventMask,&theEvent,30,0L);
  160.         
  161.             switch (theEvent.what) {
  162.     
  163.             case nullEvent:                            // Do our thread jobs
  164.                 YieldToAnyThread ();
  165.                 break;
  166.  
  167.             case mouseDown:
  168.                 quit = TRUE;
  169.                 break;
  170.             }
  171.         }
  172.  
  173.     } CATCH {
  174.         /**
  175.          ** Error has occured.  Write it out and retry
  176.          **/
  177.  
  178.         CErrorRecorder :: gError -> OSError (gLastError, "\pMain Loop Error");
  179.  
  180.         RETRY;
  181.     } ENDTRY;
  182.     
  183.     /**
  184.      ** Kill off the error recorder
  185.      **/
  186.     
  187.     delete CErrorRecorder :: gError;
  188.  
  189. }
  190.  
  191. /**
  192.  * StartMoreThreads
  193.  *
  194.  *  This thing will start up more threads -- till the number of active threads
  195.  *  is MAX_ACTIVE_THREADS.
  196.  *
  197.  **/
  198. void StartMoreThreads (void)
  199. {
  200.     short i;
  201.     CTestThread    *test;
  202.  
  203.     for (i = numActive; i < MAX_ACTIVE_THREADS; i++) {
  204.         test = new CTestThread;
  205.         test -> Start ();
  206.     }
  207. }