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 =========================================================================*/ 00043 #ifndef __vtkThreadedController_h 00044 #define __vtkThreadedController_h 00045 00046 #include "vtkMultiProcessController.h" 00047 #include "vtkCriticalSection.h" // For vtkSimpleCriticalSection 00048 00049 class vtkMultiThreader; 00050 00051 class VTK_PARALLEL_EXPORT vtkThreadedController : public vtkMultiProcessController 00052 { 00053 public: 00054 static vtkThreadedController *New(); 00055 vtkTypeRevisionMacro(vtkThreadedController,vtkMultiProcessController); 00056 void PrintSelf(ostream& os, vtkIndent indent); 00057 00059 00060 virtual void Initialize(int* argc, char*** argv, int) 00061 { this->Initialize(argc, argv); } 00062 virtual void Initialize(int* argc, char*** argv); 00063 virtual void Finalize(); 00064 virtual void Finalize(int) {this->Finalize();} 00066 00068 00073 vtkGetMacro(LocalProcessId, int); 00075 00079 virtual void SingleMethodExecute(); 00080 00084 virtual void MultipleMethodExecute(); 00085 00087 virtual void Barrier(); 00088 00091 virtual void CreateOutputWindow(); 00092 00093 protected: 00094 vtkThreadedController(); 00095 ~vtkThreadedController(); 00096 00097 void CreateProcessControllers(); 00098 00100 void Start(int threadIdx); 00101 00102 void ResetControllers(); 00103 00104 static VTK_THREAD_RETURN_TYPE vtkThreadedControllerStart( void *arg ); 00105 00106 // Each Process/Thread has its own controller. 00107 vtkThreadedController** Controllers; 00108 00109 //BTX 00110 00111 // Required only for static access to threadId (GetLocalController). 00112 #ifdef VTK_USE_PTHREADS 00113 typedef pthread_t ThreadIdType; 00114 #elif defined VTK_USE_SPROC 00115 typedef pid_t ThreadIdType; 00116 #elif defined VTK_USE_WIN32_THREADS 00117 typedef DWORD ThreadIdType; 00118 #else 00119 typedef int ThreadIdType; 00120 #endif 00121 00122 //ETX 00123 00124 // Used in barrier 00125 static vtkSimpleCriticalSection CounterLock; 00126 static int Counter; 00127 static int IsBarrierInProgress; 00128 static void WaitForPreviousBarrierToEnd(); 00129 static void BarrierStarted(); 00130 static void BarrierEnded(); 00131 static void SignalNextThread(); 00132 static void InitializeBarrier(); 00133 static void WaitForNextThread(); 00134 #ifdef VTK_USE_WIN32_THREADS 00135 static HANDLE BarrierEndedEvent; 00136 static HANDLE NextThread; 00137 #else 00138 static vtkSimpleCriticalSection* BarrierLock; 00139 static vtkSimpleCriticalSection* BarrierInProgress; 00140 #endif 00141 00142 ThreadIdType ThreadId; 00143 00144 int LastNumberOfProcesses; 00145 00146 vtkMultiThreader *MultiThreader; 00147 // Used internally to switch between multiple and single method execution. 00148 int MultipleMethodFlag; 00149 00150 // For static GetGlobalController. Translates controller for thread0 00151 // to controller for local thread. 00152 vtkMultiProcessController *GetLocalController(); 00153 00154 private: 00155 vtkThreadedController(const vtkThreadedController&); // Not implemented. 00156 void operator=(const vtkThreadedController&); // Not implemented. 00157 }; 00158 00159 #endif 00160 00161