00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkThreadedController.h,v $ 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00032 #ifndef __vtkThreadedController_h 00033 #define __vtkThreadedController_h 00034 00035 #include "vtkMultiProcessController.h" 00036 #include "vtkCriticalSection.h" // For vtkSimpleCriticalSection 00037 00038 class vtkMultiThreader; 00039 00040 class VTK_PARALLEL_EXPORT vtkThreadedController : public vtkMultiProcessController 00041 { 00042 public: 00043 static vtkThreadedController *New(); 00044 vtkTypeRevisionMacro(vtkThreadedController,vtkMultiProcessController); 00045 void PrintSelf(ostream& os, vtkIndent indent); 00046 00048 00049 virtual void Initialize(int* argc, char*** argv, int) 00050 { this->Initialize(argc, argv); } 00051 virtual void Initialize(int* argc, char*** argv); 00052 virtual void Finalize(); 00053 virtual void Finalize(int) {this->Finalize();} 00055 00057 00062 vtkGetMacro(LocalProcessId, int); 00064 00068 virtual void SingleMethodExecute(); 00069 00073 virtual void MultipleMethodExecute(); 00074 00076 virtual void Barrier(); 00077 00080 virtual void CreateOutputWindow(); 00081 00082 protected: 00083 vtkThreadedController(); 00084 ~vtkThreadedController(); 00085 00086 void CreateProcessControllers(); 00087 00089 void Start(int threadIdx); 00090 00091 void ResetControllers(); 00092 00093 static VTK_THREAD_RETURN_TYPE vtkThreadedControllerStart( void *arg ); 00094 00095 // Each Process/Thread has its own controller. 00096 vtkThreadedController** Controllers; 00097 00098 //BTX 00099 00100 // Required only for static access to threadId (GetLocalController). 00101 #ifdef VTK_USE_PTHREADS 00102 typedef pthread_t ThreadIdType; 00103 #elif defined VTK_USE_SPROC 00104 typedef pid_t ThreadIdType; 00105 #elif defined VTK_USE_WIN32_THREADS 00106 typedef DWORD ThreadIdType; 00107 #else 00108 typedef int ThreadIdType; 00109 #endif 00110 00111 //ETX 00112 00113 // Used in barrier 00114 static vtkSimpleCriticalSection CounterLock; 00115 static int Counter; 00116 static int IsBarrierInProgress; 00117 static void WaitForPreviousBarrierToEnd(); 00118 static void BarrierStarted(); 00119 static void BarrierEnded(); 00120 static void SignalNextThread(); 00121 static void InitializeBarrier(); 00122 static void WaitForNextThread(); 00123 #ifdef VTK_USE_WIN32_THREADS 00124 static HANDLE BarrierEndedEvent; 00125 static HANDLE NextThread; 00126 #else 00127 static vtkSimpleCriticalSection* BarrierLock; 00128 static vtkSimpleCriticalSection* BarrierInProgress; 00129 #endif 00130 00131 ThreadIdType ThreadId; 00132 00133 int LastNumberOfProcesses; 00134 00135 vtkMultiThreader *MultiThreader; 00136 // Used internally to switch between multiple and single method execution. 00137 int MultipleMethodFlag; 00138 00139 // For static GetGlobalController. Translates controller for thread0 00140 // to controller for local thread. 00141 vtkMultiProcessController *GetLocalController(); 00142 00143 private: 00144 vtkThreadedController(const vtkThreadedController&); // Not implemented. 00145 void operator=(const vtkThreadedController&); // Not implemented. 00146 }; 00147 00148 #endif 00149 00150