00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: $RCSfile: vtkThreadedController.h,v $ 00005 Language: C++ 00006 00007 Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen 00008 All rights reserved. 00009 00010 Redistribution and use in source and binary forms, with or without 00011 modification, are permitted provided that the following conditions are met: 00012 00013 * Redistributions of source code must retain the above copyright notice, 00014 this list of conditions and the following disclaimer. 00015 00016 * Redistributions in binary form must reproduce the above copyright notice, 00017 this list of conditions and the following disclaimer in the documentation 00018 and/or other materials provided with the distribution. 00019 00020 * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names 00021 of any contributors may be used to endorse or promote products derived 00022 from this software without specific prior written permission. 00023 00024 * Modified source versions must be plainly marked as such, and must not be 00025 misrepresented as being the original software. 00026 00027 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' 00028 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR 00031 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00032 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00033 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00034 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00035 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00036 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 00038 =========================================================================*/ 00059 #ifndef __vtkThreadedController_h 00060 #define __vtkThreadedController_h 00061 00062 #include "vtkObject.h" 00063 #include "vtkMultiProcessController.h" 00064 #include "vtkMultiThreader.h" 00065 #include "vtkSharedMemoryCommunicator.h" 00066 #include "vtkCriticalSection.h" 00067 00068 class VTK_PARALLEL_EXPORT vtkThreadedController : public vtkMultiProcessController 00069 { 00070 public: 00071 static vtkThreadedController *New(); 00072 vtkTypeMacro(vtkThreadedController,vtkMultiProcessController); 00073 void PrintSelf(ostream& os, vtkIndent indent); 00074 00076 00077 virtual void Initialize(int* argc, char*** arcv); 00078 virtual void Finalize() {} 00080 00082 00087 vtkGetMacro(LocalProcessId, int); 00089 00093 virtual void SingleMethodExecute(); 00094 00098 virtual void MultipleMethodExecute(); 00099 00101 virtual void Barrier(); 00102 00105 virtual void CreateOutputWindow(); 00106 00107 00108 protected: 00109 00110 vtkThreadedController(); 00111 ~vtkThreadedController(); 00112 00113 void CreateProcessControllers(); 00114 00116 void Start(int threadIdx); 00117 00118 void ResetControllers(); 00119 00120 static VTK_THREAD_RETURN_TYPE vtkThreadedControllerStart( void *arg ); 00121 00122 // Each Process/Thread has its own controller. 00123 vtkThreadedController** Controllers; 00124 00125 //BTX 00126 00127 // Required only for static access to threadId (GetLocalController). 00128 #ifdef VTK_USE_PTHREADS 00129 typedef pthread_t ThreadIdType; 00130 #elif defined VTK_USE_SPROC 00131 typedef pid_t ThreadIdType; 00132 #elif defined VTK_USE_WIN32_THREADS 00133 typedef DWORD ThreadIdType; 00134 #else 00135 typedef int ThreadIdType; 00136 #endif 00137 00138 //ETX 00139 00140 // Used in barrier 00141 static vtkSimpleCriticalSection CounterLock; 00142 static int Counter; 00143 static int IsBarrierInProgress; 00144 static void WaitForPreviousBarrierToEnd(); 00145 static void BarrierStarted(); 00146 static void BarrierEnded(); 00147 static void SignalNextThread(); 00148 static void InitializeBarrier(); 00149 static void WaitForNextThread(); 00150 #ifdef VTK_USE_WIN32_THREADS 00151 static HANDLE BarrierEndedEvent; 00152 static HANDLE NextThread; 00153 #else 00154 static vtkSimpleCriticalSection BarrierLock; 00155 static vtkSimpleCriticalSection BarrierInProgress; 00156 #endif 00157 00158 ThreadIdType* ThreadIds; 00159 00160 int LastNumberOfProcesses; 00161 00162 vtkMultiThreader *MultiThreader; 00163 // Used internally to switch between multiple and single method execution. 00164 int MultipleMethodFlag; 00165 00166 // For static GetGlobalController. Translates controller for thread0 00167 // to controller for local thread. 00168 vtkMultiProcessController *GetLocalController(); 00169 00170 private: 00171 vtkThreadedController(const vtkThreadedController&); // Not implemented. 00172 void operator=(const vtkThreadedController&); // Not implemented. 00173 }; 00174 00175 #endif 00176 00177