home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Threads Support / ThreadSupport.cp < prev    next >
Encoding:
Text File  |  1998-06-02  |  2.6 KB  |  141 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ThreadSupport.cp
  3.  
  4.     Contains:    stuff
  5.  
  6. */
  7.  
  8. #include "ThreadSupport.h"
  9. #include "Exceptions.h"
  10. #include "Futures.h"
  11. #include <Gestalt.h>
  12.  
  13.  
  14. LinkedListOf<TThread> TThread::fgThreadList;    // Global thread list
  15.  
  16.  
  17. TThread::TThread(ThreadID id) : fThreadID(id)
  18. {
  19. #if !GENERATINGCFM
  20.     fSavedA5        = SetCurrentA5();
  21. #endif
  22.  
  23. #if CW7_EXCEPTIONS
  24.     // set up interface to C++ exception runtime support
  25.     __new_exception_state(&fExceptionState, fCatchBuffer, sizeof(fCatchBuffer));
  26. #endif
  27.  
  28.     FailOSErr(SetThreadSwitcher(  id, SwitchOutProc,    this, true));
  29.     FailOSErr(SetThreadSwitcher(  id, SwitchInProc,        this, false));
  30.     FailOSErr(SetThreadTerminator(id, TerminationProc,    this));
  31. }
  32.  
  33.  
  34. TThread::~TThread()
  35. {
  36. }
  37.  
  38.  
  39. void TThread::SwitchIn(void)
  40. {
  41. #if CW7_EXCEPTIONS
  42.     // restore exception-handling context
  43.     ExceptionState dummy;
  44.     __switch_exception_state(&fExceptionState, &dummy);
  45. #endif
  46.  
  47.     IteratorForLinkedListOf<ThreadContext> iter(fContextList);
  48.     
  49.     for (ThreadContext* context = iter.First(); context; context = iter.Next())
  50.     {
  51.         context->SwitchIn();
  52.     }
  53. }
  54.  
  55.  
  56. void TThread::SwitchOut(void)
  57. {
  58.     {
  59.         IteratorForLinkedListOf<ThreadContext> iter(fContextList);
  60.  
  61.         for (ThreadContext* context = iter.Last(); context; context = iter.Prev())
  62.         {
  63.             context->SwitchOut();
  64.         }
  65.     }
  66.  
  67. #if CW7_EXCEPTIONS
  68.     // save exception-handling context
  69.     __switch_exception_state(&fExceptionState, &fExceptionState);
  70. #endif
  71. }
  72.  
  73.  
  74. void TThread::Terminate(void)
  75. {
  76.     IteratorForLinkedListOf<ThreadContext> iter(fContextList);
  77.     
  78.     for (ThreadContext* context = iter.Last(); context; context = iter.Prev())
  79.     {
  80.         context->Terminate();
  81.     }
  82. }
  83.  
  84.  
  85. pascal void TThread::SwitchInProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  86. {
  87.     TThread* thread = (TThread*) switchProcParam;
  88.     
  89. #if !GENERATINGCFM
  90.     long savedA5 = SetA5(thread->fSavedA5);
  91. #endif
  92.  
  93. #if THREAD_PROFILE
  94.     ::ProfilerSwitchToThread(thread->mProfilerRef);
  95. #endif
  96.     
  97.     thread->SwitchIn();
  98.     
  99. #if !GENERATINGCFM
  100.     SetA5(savedA5);
  101. #endif
  102. }
  103.  
  104.  
  105. pascal void TThread::SwitchOutProc(ThreadID /*threadBeingSwitched*/, void *switchProcParam)
  106. {
  107.     TThread* thread = (TThread*) switchProcParam;
  108.     
  109. #if !GENERATINGCFM
  110.     long savedA5 = SetA5(thread->fSavedA5);
  111. #endif
  112.  
  113.      thread->SwitchOut();
  114.  
  115. #if !GENERATINGCFM
  116.     SetA5(savedA5);
  117. #endif
  118. }
  119.  
  120.  
  121. pascal void TThread::TerminationProc(ThreadID /*threadBeingSwitched*/, void *terminationProcParam)
  122. {
  123.     TThread* thread = (TThread*) terminationProcParam;
  124.     
  125. #if !GENERATINGCFM
  126.     long savedA5 = SetA5(thread->fSavedA5);
  127. #endif
  128.  
  129.     thread->Terminate();
  130.  
  131. #if !GENERATINGCFM
  132.     SetA5(savedA5);
  133. #endif
  134. }
  135.  
  136. //--------------------------------------------------------------------------------
  137. void* TThread::GetObjectPtr()
  138. {
  139.     return this;
  140. }
  141.