| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkBarrier.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:33 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
DEF |
#ifndef __itkBarrier_h_ |
| 18 |
DEF |
#define __itkBarrier_h_ |
| 19 |
|
|
| 20 |
|
#include "itkLightObject.h" |
| 21 |
|
#include "itkConditionVariable.h" |
| 22 |
|
#include "itkMutexLock.h" |
| 23 |
|
|
| 24 |
|
#ifdef ITK_USE_FETCHOP_BARRIERS |
| 25 |
|
extern "C" { |
| 26 |
|
#include <sys/pmo.h> |
| 27 |
|
#include <fetchop.h> |
| 28 |
IND |
} |
| 29 |
|
#endif |
| 30 |
|
|
| 31 |
|
#ifdef ITK_USE_SPROC |
| 32 |
|
extern "C" { |
| 33 |
|
#include <ulocks.h> |
| 34 |
IND |
} |
| 35 |
|
#endif |
| 36 |
|
|
| 37 |
|
namespace itk { |
| 38 |
|
|
| 39 |
|
/** |
| 40 |
|
* \class Barrier |
| 41 |
|
* \brief Standard barrier class implementation for synchronizing the execution |
| 42 |
|
* of threads. |
| 43 |
|
* |
| 44 |
|
* A barrier class is used to synchronize threaded execution by allowing |
| 45 |
|
* threads to block until each has reached a desired state. As each thread |
| 46 |
|
* enters the barrier it blocks. When all threads have entered the barrier, |
| 47 |
WCM |
* all all released and continue to execute. |
| 48 |
|
* |
| 49 |
|
* A thread enters the barrier by calling Wait() on the barrier class. |
| 50 |
|
* To set up a barrier class, call Initialize(n) where n is the number of |
| 51 |
|
* waiting threads that will trigger a release of the barrier. |
| 52 |
|
* |
| 53 |
|
* \par NOTE FOR SGI USERS. You may optionally enable a fetchop library |
| 54 |
|
* implementation of barriers that will give significantly faster performance |
| 55 |
|
* over the sproc barrier class. With the fetchop implementation, you are |
| 56 |
|
* limited to Barrier::m_MaxBarriers separate barrier instantiations, although |
| 57 |
|
* this limit can be safely raised if necessary. To enable the fetchop |
| 58 |
|
* implementation, set ITK_USE_FETCHOP_BARRIERS and link applications against |
| 59 |
|
* -lfetchop. |
| 60 |
|
* |
| 61 |
IND |
*/ |
| 62 |
|
class ITKCommon_EXPORT Barrier : public LightObject |
| 63 |
|
{ |
| 64 |
|
public: |
| 65 |
|
/** Standard class typedefs. */ |
| 66 |
|
typedef Barrier Self; |
| 67 |
|
typedef LightObject Superclass; |
| 68 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 69 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 70 |
|
|
| 71 |
|
/** Method for creation through the object factory. */ |
| 72 |
|
itkNewMacro(Self); |
| 73 |
|
|
| 74 |
|
/** Run-time type information (and related methods). */ |
| 75 |
|
itkTypeMacro(Barrier, Object); |
| 76 |
|
|
| 77 |
|
/** Creates a new system variable used to implement the barrier. The |
| 78 |
IND |
******argument to this method is the number of threads that must Wait() on the |
| 79 |
IND |
******barrier before it is cleared. */ |
| 80 |
|
void Initialize(unsigned int); |
| 81 |
|
|
| 82 |
|
/** A thread calling this method waits until m_NumberOfThreads have called |
| 83 |
|
* Wait() on the barrier. When the final expected thread calls Wait(), all |
| 84 |
|
* threads are released. */ |
| 85 |
|
void Wait(); |
| 86 |
|
|
| 87 |
|
private: |
| 88 |
|
Barrier(); |
| 89 |
|
~Barrier(); |
| 90 |
|
|
| 91 |
|
#if defined ITK_USE_FETCHOP_BARRIERS |
| 92 |
|
static bool m_ReservoirInitialized; |
| 93 |
|
static atomic_reservoir_t m_Reservoir; |
| 94 |
|
static int m_MaxBarriers; |
| 95 |
|
atomic_var_t *m_Pvar; |
| 96 |
|
|
| 97 |
|
char pad1[128]; // Attempt to put |
| 98 |
|
volatile int m_FetchopFlag; // m_Fetchop on its |
| 99 |
|
char pad2[128]; // own cache line. |
| 100 |
|
|
| 101 |
|
#elif defined ITK_USE_SPROC |
| 102 |
|
barrier_t *m_Barrier; |
| 103 |
|
|
| 104 |
IND |
#else |
| 105 |
|
ConditionVariable::Pointer m_ConditionVariable; |
| 106 |
|
unsigned int m_NumberArrived; |
| 107 |
|
SimpleMutexLock m_Mutex; |
| 108 |
|
|
| 109 |
|
#endif |
| 110 |
|
|
| 111 |
|
unsigned int m_NumberExpected; |
| 112 |
|
}; |
| 113 |
|
|
| 114 |
|
} // end namespace itk |
| 115 |
|
|
| 116 |
|
#endif |
| 117 |
|
|