VTK
vtkConditionVariable.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkConditionVariable.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
33#ifndef vtkConditionVariable_h
34#define vtkConditionVariable_h
35
36#include "vtkCommonCoreModule.h" // For export macro
37#include "vtkObject.h"
38
39#include "vtkMutexLock.h" // Need for friend access to vtkSimpleMutexLock
40
41#if defined(VTK_USE_PTHREADS) || defined(VTK_HP_PTHREADS)
42# include <pthread.h> // Need POSIX thread implementation of mutex (even win32 provides mutexes)
43typedef pthread_cond_t vtkConditionType;
44#endif
45
46
47// Typically a top level windows application sets _WIN32_WINNT. If it is not set we set it to
48// 0x0501 (Windows XP)
49#ifdef VTK_USE_WIN32_THREADS
50# ifndef _WIN32_WINNT
51# define _WIN32_WINNT 0x0501 // 0x0501 means target Windows XP or later
52# endif
53# include "vtkWindows.h" // Needed for win32 CRITICAL_SECTION, HANDLE, etc.
54#endif
55
56#ifdef VTK_USE_WIN32_THREADS
57#if 1
58typedef struct
59{
60 // Number of threads waiting on condition.
61 int WaitingThreadCount;
62
63 // Lock for WaitingThreadCount
64 CRITICAL_SECTION WaitingThreadCountCritSec;
65
66 // Semaphore to block threads waiting for the condition to change.
67 vtkWindowsHANDLE Semaphore;
68
69 // An event used to wake up thread(s) waiting on the semaphore
70 // when pthread_cond_signal or pthread_cond_broadcast is called.
71 vtkWindowsHANDLE DoneWaiting;
72
73 // Was pthread_cond_broadcast called?
74 size_t WasBroadcast;
75} pthread_cond_t;
76
77typedef pthread_cond_t vtkConditionType;
78# else // 0
79typedef struct
80{
81 // Number of threads waiting on condition.
82 int WaitingThreadCount;
83
84 // Lock for WaitingThreadCount
85 CRITICAL_SECTION WaitingThreadCountCritSec;
86
87 // Number of threads to release when pthread_cond_broadcast()
88 // or pthread_cond_signal() is called.
89 int ReleaseCount;
90
91 // Used to prevent one thread from decrementing ReleaseCount all
92 // by itself instead of letting others respond.
93 int NotifyCount;
94
95 // A manual-reset event that's used to block and release waiting threads.
96 vtkWindowsHANDLE Event;
97} pthread_cond_t;
98
99typedef pthread_cond_t vtkConditionType;
100# endif // 0
101#endif // VTK_USE_WIN32_THREADS
102
103#ifndef VTK_USE_PTHREADS
104#ifndef VTK_HP_PTHREADS
105#ifndef VTK_USE_WIN32_THREADS
107#endif
108#endif
109#endif
110
111// Condition variable that is not a vtkObject.
112class VTKCOMMONCORE_EXPORT vtkSimpleConditionVariable
113{
114public:
117
119
120 void Delete() { delete this; }
121
125 void Signal();
126
130 void Broadcast();
131
142
143protected:
145
146private:
147 vtkSimpleConditionVariable(const vtkSimpleConditionVariable& other) VTK_DELETE_FUNCTION;
148 vtkSimpleConditionVariable& operator=(const vtkSimpleConditionVariable& rhs) VTK_DELETE_FUNCTION;
149};
150
151class VTKCOMMONCORE_EXPORT vtkConditionVariable : public vtkObject
152{
153public:
156 void PrintSelf( ostream& os, vtkIndent indent ) VTK_OVERRIDE;
157
161 void Signal();
162
166 void Broadcast();
167
177 int Wait( vtkMutexLock* mutex );
178
179protected:
181
183
184private:
185 vtkConditionVariable( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
186 void operator = ( const vtkConditionVariable& ) VTK_DELETE_FUNCTION;
187};
188
190{
192}
193
195{
197}
198
200{
201 return this->SimpleConditionVariable.Wait( lock->SimpleMutexLock );
202}
203
204#endif // vtkConditionVariable_h
mutual exclusion locking class
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
int Wait(vtkMutexLock *mutex)
Wait for the condition to change.
void Broadcast()
Wake all threads waiting for the condition to change.
vtkSimpleConditionVariable SimpleConditionVariable
void Signal()
Wake one thread waiting for the condition to change.
static vtkConditionVariable * New()
a simple class to control print indentation
Definition: vtkIndent.h:40
mutual exclusion locking class
Definition: vtkMutexLock.h:88
vtkSimpleMutexLock SimpleMutexLock
Definition: vtkMutexLock.h:109
abstract base class for most VTK objects
Definition: vtkObject.h:60
void Signal()
Wake one thread waiting for the condition to change.
static vtkSimpleConditionVariable * New()
void Broadcast()
Wake all threads waiting for the condition to change.
int Wait(vtkSimpleMutexLock &mutex)
Wait for the condition to change.
int vtkConditionType