Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  
igtlMultiThreader.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Open IGT Link Library
4  Module: $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlMultiThreader.h $
5  Language: C++
6  Date: $Date: 2010-06-09 16:16:36 -0400 (Wed, 09 Jun 2010) $
7  Version: $Revision: 6525 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10 
11  This software is distributed WITHOUT ANY WARRANTY; without even
12  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the above copyright notices for more information.
14 
15 =========================================================================*/
16 /*=========================================================================
17 
18  Program: Visualization Toolkit
19  Module: $RCSfile: vtkMultiThreader.h,v $
20 
21  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
22  All rights reserved.
23  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
24 
25  This software is distributed WITHOUT ANY WARRANTY; without even
26  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
27  PURPOSE. See the above copyright notice for more information.
28 
29 =========================================================================*/
30 // .NAME igtlMultiThreader - A class for performing multithreaded execution
31 // .SECTION Description
32 // igtlMultithreader is a class that provides support for multithreaded
33 // execution using sproc() on an SGI, or pthread_create on any platform
34 // supporting POSIX threads. This class can be used to execute a single
35 // method on multiple threads, or to specify a method per thread.
36 
37 #ifndef __igtlMultiThreader_h
38 #define __igtlMultiThreader_h
39 
40 #include "igtlObject.h"
41 #include "igtlObjectFactory.h"
42 #include "igtlMacro.h"
43 #include "igtlMutexLock.h"
44 
45 
46 #ifdef OpenIGTLink_USE_SPROC
47 #include <sys/types.h> // Needed for unix implementation of sproc
48 #include <unistd.h> // Needed for unix implementation of sproc
49 #endif
50 
51 #if defined(OpenIGTLink_USE_PTHREAD) || defined(OpenIGTLink_HP_PTHREAD)
52 #include <pthread.h> // Needed for PTHREAD implementation of mutex
53 #include <sys/types.h> // Needed for unix implementation of pthreads
54 #include <unistd.h> // Needed for unix implementation of pthreads
55 #endif
56 
57 namespace igtl
58 {
59 
60 // If OpenIGTLink_USE_SPROC is defined, then sproc() will be used to create
61 // multiple threads on an SGI. If OpenIGTLink_USE_PTHREAD is defined, then
62 // pthread_create() will be used to create multiple threads (on
63 // a sun, for example)
64 
65 // Defined in igtlSystemIncludes.h:
66 // IGTL_MAX_THREADS
67 
68 // If OpenIGTLink_USE_PTHREADS is defined, then the multithreaded
69 // function is of type void *, and returns NULL
70 // Otherwise the type is void which is correct for WIN32
71 // and SPROC
72 //BTX
73 
74 // The maximum number of threads allowed
75 #ifdef OpenIGTLink_USE_SPROC
76 #define IGTL_MAX_THREADS 128
77 #endif
78 
79 #ifdef OpenIGTLink_USE_PTHREADS
80 #define IGTL_MAX_THREADS 128
81 #endif
82 
83 #ifdef OpenIGTLink_USE_WIN32_THREADS
84 #define IGTL_MAX_THREADS 128
85 #endif
86 
87 // cygwin threads are unreliable
88 #ifdef __CYGWIN__
89 #undef IGTL_MAX_THREADS
90 #define IGTL_MAX_THREADS 128
91 #endif
92 
93 // mingw threads cause crashes so limit to 1
94 #if defined(__MINGW32__)
95 #undef IGTL_MAX_THREADS
96 #define IGTL_MAX_THREADS 1
97 #endif
98 
99 // On some sgi machines, threads and stl don't mix so limit to 1
100 #if defined(__sgi) && defined(_COMPILER_VERSION) && _COMPILER_VERSION <= 730
101 #undef IGTL_MAX_THREADS
102 #define IGTL_MAX_THREADS 1
103 #endif
104 
105 #ifndef IGTL_MAX_THREADS
106 #define IGTL_MAX_THREADS 1
107 #endif
108 
109 #ifdef OpenIGTLink_USE_SPROC
110 typedef int ThreadProcessIDType;
111 typedef int MultiThreaderIDType;
112 #endif
113 
114 #ifdef OpenIGTLink_USE_PTHREADS
115 typedef void *(*ThreadFunctionType)(void *);
116 typedef pthread_t ThreadProcessIDType;
117 typedef pthread_t MultiThreaderIDType;
118 #endif
119 
120 #ifdef OpenIGTLink_USE_WIN32_THREADS
121 typedef igtlWindowsLPTHREAD_START_ROUTINE ThreadFunctionType;
122 typedef igtlWindowsHANDLE ThreadProcessIDType;
123 typedef igtlWindowsDWORD MultiThreaderIDType;
124 #endif
125 
126 #if !defined(OpenIGTLink_USE_PTHREADS) && !defined(OpenIGTLink_USE_WIN32_THREADS)
127 typedef void (*ThreadFunctionType)(void *);
130 #endif
131 //ETX
132 
133 
135 {
136 public:
142 
143  igtlNewMacro(Self);
145 
146  // Description:
147  // This is the structure that is passed to the thread that is
148  // created from the SingleMethodExecute, MultipleMethodExecute or
149  // the SpawnThread method. It is passed in as a void *, and it is
150  // up to the method to cast correctly and extract the information.
151  // The ThreadID is a number between 0 and NumberOfThreads-1 that indicates
152  // the id of this thread. The NumberOfThreads is this->NumberOfThreads for
153  // threads created from SingleMethodExecute or MultipleMethodExecute,
154  // and it is 1 for threads created from SpawnThread.
155  // The UserData is the (void *)arg passed into the SetSingleMethod,
156  // SetMultipleMethod, or SpawnThread method.
157 
158  //BTX
159 #define ThreadInfoStruct MultiThreader::ThreadInfo
161  {
162  public:
163  int ThreadID;
167  void *UserData;
168  };
169  //ETX
170 
171  // Description:
172  // Get/Set the number of threads to create. It will be clamped to the range
173  // 1 - IGTL_MAX_THREADS, so the caller of this method should check that the
174  // requested number of threads was accepted.
175  igtlSetClampMacro( NumberOfThreads, int, 1, IGTL_MAX_THREADS );
176  virtual int GetNumberOfThreads();
177 
178  // Description:
179  // Set/Get the maximum number of threads to use when multithreading.
180  // This limits and overrides any other settings for multithreading.
181  // A value of zero indicates no limit.
182  static void SetGlobalMaximumNumberOfThreads(int val);
183  static int GetGlobalMaximumNumberOfThreads();
184 
185  // Description:
186  // Set/Get the value which is used to initialize the NumberOfThreads
187  // in the constructor. Initially this default is set to the number of
188  // processors or IGTL_MAX_THREADS (which ever is less).
189  static void SetGlobalDefaultNumberOfThreads(int val);
190  static int GetGlobalDefaultNumberOfThreads();
191 
192  // These methods are excluded from Tcl wrapping 1) because the
193  // wrapper gives up on them and 2) because they really shouldn't be
194  // called from a script anyway.
195  //BTX
196 
197  // Description:
198  // Execute the SingleMethod (as define by SetSingleMethod) using
199  // this->NumberOfThreads threads.
200  void SingleMethodExecute();
201 
202  // Description:
203  // Execute the MultipleMethods (as define by calling SetMultipleMethod
204  // for each of the required this->NumberOfThreads methods) using
205  // this->NumberOfThreads threads.
206  void MultipleMethodExecute();
207 
208  // Description:
209  // Set the SingleMethod to f() and the UserData field of the
210  // ThreadInfo that is passed to it will be data.
211  // This method (and all the methods passed to SetMultipleMethod)
212  // must be of type ThreadFunctionType and must take a single argument of
213  // type void *.
214  void SetSingleMethod(ThreadFunctionType, void *data );
215 
216  // Description:
217  // Set the MultipleMethod at the given index to f() and the UserData
218  // field of the ThreadInfo that is passed to it will be data.
219  void SetMultipleMethod( int index, ThreadFunctionType, void *data );
220 
221  // Description:
222  // Create a new thread for the given function. Return a thread id
223  // which is a number between 0 and IGTL_MAX_THREADS - 1. This id should
224  // be used to kill the thread at a later time.
225  int SpawnThread( ThreadFunctionType, void *data );
226 
227  // Description:
228  // Terminate the thread that was created with a SpawnThreadExecute()
229  void TerminateThread( int thread_id );
230 
231  // Description:
232  // Get the thread identifier of the calling thread.
233  static MultiThreaderIDType GetCurrentThreadID();
234 
235  // Description:
236  // Check whether two thread identifiers refer to the same thread.
237  static int ThreadsEqual(MultiThreaderIDType t1,
239 
240 protected:
241  MultiThreader();
242  ~MultiThreader();
243 
244  void PrintSelf(std::ostream& os) const;
245 
246  // The number of threads to use
248 
249  // An array of thread info containing a thread id
250  // (0, 1, 2, .. IGTL_MAX_THREADS-1), the thread count, and a pointer
251  // to void so that user data can be passed to each thread
252  ThreadInfo m_ThreadInfoArray[IGTL_MAX_THREADS];
253 
254  // The methods
257 
258  // Storage of MutexFunctions and ints used to control spawned
259  // threads and the spawned thread ids
260  int m_SpawnedThreadActiveFlag[IGTL_MAX_THREADS];
261  MutexLock::Pointer m_SpawnedThreadActiveFlagLock[IGTL_MAX_THREADS];
262  ThreadProcessIDType m_SpawnedThreadProcessID[IGTL_MAX_THREADS];
263  ThreadInfo m_SpawnedThreadInfoArray[IGTL_MAX_THREADS];
264 
265 //ETX
266 
267  // Internal storage of the data
269  void *m_MultipleData[IGTL_MAX_THREADS];
270 
271 private:
272  MultiThreader(const MultiThreader&); // Not implemented.
273  void operator=(const MultiThreader&); // Not implemented.
274 };
275 
276 } // namespace igtl
277 #endif
278 
279 
280 
281 
282 
283 

Generated at Thu Mar 20 2014 16:32:06 for OpenIGTLink by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2000