| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkConditionVariable.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:34 $ |
| 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 _itkConditionVariable_h_ |
| 18 |
DEF |
#define _itkConditionVariable_h_ |
| 19 |
|
|
| 20 |
|
#include "itkConfigure.h" |
| 21 |
|
|
| 22 |
|
// This implementation uses a routine called SignalObjectAndWait() |
| 23 |
|
// which is only defined on WinNT 4.0 or greater systems. We need to |
| 24 |
|
// define this symbol in order to get the prototype for the |
| 25 |
|
// routine. This needs to be done before we load any system headers. |
| 26 |
|
#ifdef ITK_USE_WIN32_THREADS |
| 27 |
|
#undef _WIN32_WINNT |
| 28 |
|
#define _WIN32_WINNT 0x0400 |
| 29 |
|
#endif |
| 30 |
|
|
| 31 |
|
#include "itkMutexLock.h" |
| 32 |
|
#include "itkLightObject.h" |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
namespace itk { |
| 36 |
|
|
| 37 |
|
/** \class ConditionVariable |
| 38 |
|
* \brief A thread synchronization object used to suspend execution until some |
| 39 |
|
* condition on shared data is met. |
| 40 |
|
* |
| 41 |
|
* A thread calls Wait() to suspend its execution until the condition is |
| 42 |
|
* met. Each call to Signal() from an executing thread will then cause a single |
| 43 |
|
* waiting thread to be released. A call to Signal() means, "signal |
| 44 |
|
* that the condition is true." Broadcast() releases all threads waiting on |
| 45 |
|
* the condition variable. |
| 46 |
|
* |
| 47 |
|
* The ITK ConditionVariable implementation is consistent with the standard |
| 48 |
|
* definition and use of condition variables in pthreads and other common |
| 49 |
|
* thread libraries. |
| 50 |
|
* |
| 51 |
|
* IMPORTANT: A condition variable always requires an associated SimpleMutexLock |
| 52 |
|
* object. The mutex object is used to avoid a dangerous race condition when |
| 53 |
|
* Wait() and Signal() are called simultaneously from two different |
| 54 |
|
* threads. |
| 55 |
|
* |
| 56 |
|
* On systems using pthreads, this implementation abstract the |
| 57 |
|
* standard calls to the pthread condition variable. On Win32 |
| 58 |
|
* systems, there is no system provided condition variable. This |
| 59 |
|
* class implements a condition variable using a critical section, a |
| 60 |
|
* semphore, an event and a number of counters. The implementation is |
| 61 |
|
* almost an extract translation of the implementation presented by |
| 62 |
|
* Douglas C Schmidt and Irfan Pyarali in "Strategies for Implementing |
| 63 |
|
* POSIX Condition Variables on Win32". This article can be found at |
| 64 |
|
* http://www.cs.wustl.edu/~schmidt/win32-cv-1.html |
| 65 |
|
* |
| 66 |
|
*/ |
| 67 |
|
class ITKCommon_EXPORT ConditionVariable : public LightObject |
| 68 |
|
{ |
| 69 |
|
public: |
| 70 |
|
/** Standard class typedefs. */ |
| 71 |
|
typedef ConditionVariable Self; |
| 72 |
TDA |
typedef LightObject Superclass; |
| 73 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 74 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 75 |
|
|
| 76 |
|
/** Method for creation through the object factory. */ |
| 77 |
|
itkNewMacro(Self); |
| 78 |
|
|
| 79 |
|
/** Run-time type information (and related methods). */ |
| 80 |
|
itkTypeMacro(ConditionVariable, LightObject); |
| 81 |
|
|
| 82 |
|
/** Suspend execution of this thread until the condition is signaled. The |
| 83 |
|
* argument is a SimpleMutex object that must be locked prior to calling |
| 84 |
|
* this method. */ |
| 85 |
|
void Wait(SimpleMutexLock * mutex); |
| 86 |
|
|
| 87 |
|
/** Signal that the condition is true and release one waiting thread */ |
| 88 |
|
void Signal(); |
| 89 |
|
|
| 90 |
|
/** Signal that the condition is true and release all waiting threads */ |
| 91 |
|
void Broadcast(); |
| 92 |
|
|
| 93 |
|
protected: |
| 94 |
|
ConditionVariable(); |
| 95 |
|
~ConditionVariable(); |
| 96 |
|
|
| 97 |
|
private: |
| 98 |
|
ConditionVariable(const Self & other); |
| 99 |
|
const Self & operator=( const Self & ); |
| 100 |
|
#ifdef ITK_USE_PTHREADS |
| 101 |
|
pthread_cond_t m_ConditionVariable; |
| 102 |
|
MutexType m_Mutex; |
| 103 |
IND |
#else |
| 104 |
|
int m_NumberOfWaiters; // number of waiting threads |
| 105 |
|
#ifdef WIN32 |
| 106 |
LEN |
CRITICAL_SECTION m_NumberOfWaitersLock; // Serialize access to m_NumberOfWaiters |
| 107 |
|
|
| 108 |
|
HANDLE m_Semaphore; // Semaphore to queue threads |
| 109 |
|
HANDLE m_WaitersAreDone; // Auto-reset event used by the |
| 110 |
IND |
*******************************************// broadcast/signal thread to |
| 111 |
IND |
*******************************************// wait for all the waiting |
| 112 |
IND |
*******************************************// threads to wake up and |
| 113 |
IND |
*******************************************// release the semaphore |
| 114 |
|
|
| 115 |
|
size_t m_WasBroadcast; // Keeps track of whether we |
| 116 |
IND |
*******************************************// were broadcasting or signaling |
| 117 |
|
#endif |
| 118 |
|
#endif |
| 119 |
|
}; |
| 120 |
|
|
| 121 |
|
}//end of itk namespace |
| 122 |
|
|
| 123 |
|
#endif |
| 124 |
|
|