SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConditionVariable.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 
30 
31 ///
32 ///@file ConditionVariable
33 ///@brief Condition variable primitive
34 ///
35 ///@author
36 /// Steve Parker
37 /// Department of Computer Science
38 /// University of Utah
39 ///@date June 1997
40 ///
41 
42 #ifndef Core_Thread_ConditionVariable_h
43 #define Core_Thread_ConditionVariable_h
44 
48 
50 
51 struct timespec;
52 
53 namespace SCIRun {
54 
55 class ConditionVariable_private;
56 struct CrowdMonitor_private;
57 
58 /**************************************
59 
60  @class
61  ConditionVariable
62 
63  KEYWORDS
64  Thread
65 
66  @details
67  Condition variable primitive. When a thread calls the
68  <i>wait</i> method,which will block until another thread calls
69  the <i>conditionSignal</i> or <i>conditionBroadcast</i> methods. When
70  there are multiple threads waiting, <i>conditionBroadcast</i> will unblock
71  all of them, while <i>conditionSignal</i> will unblock only one (an
72  arbitrary one) of them. This primitive is used to allow a thread
73  to wait for some condition to exist, such as an available resource.
74  The thread waits for that condition, until it is unblocked by another
75  thread that caused the condition to exist (<i>i.e.</i> freed the
76  resource).
77 
78 ****************************************/
79 
81 public:
82  //////////
83  /// Create a condition variable. <i>name</i> should be a static
84  /// string which describes the primitive for debugging purposes.
85  ConditionVariable(const char* name);
86 
87  //////////
88  /// Destroy the condition variable
90 
91  //////////
92  /// Wait for a condition. This method atomically unlocks
93  /// <b>mutex</b>, and blocks. The <b>mutex</b> is typically
94  /// used to guard access to the resource that the thread is
95  /// waiting for.
96  void wait(Mutex& m);
97  void wait(RecursiveMutex& m);
98 
99  //////////
100  /// Wait for a condition. This method atomically unlocks
101  /// <b>mutex</b>, and blocks. The <b>mutex</b> is typically
102  /// used to guard access to the resource that the thread is
103  /// waiting for. If the time abstime is reached before
104  /// the ConditionVariable is signaled, this will return
105  /// false. Otherewise it will return true.
106  bool timedWait(Mutex& m, const struct ::timespec* abstime);
107  bool timedWait(RecursiveMutex& m, const struct ::timespec* abstime);
108 
109  //////////
110  /// Signal a condition. This will unblock one of the waiting
111  /// threads. No guarantee is made as to which thread will be
112  /// unblocked, but thread implementations typically give
113  /// preference to the thread that has waited the longest.
114  void conditionSignal();
115 
116  //////////
117  /// Signal a condition. This will unblock all of the waiting
118  /// threads. Note that only the number of waiting threads will
119  /// be unblocked. No guarantee is made that these are the same
120  /// N threads that were blocked at the time of the broadcast.
121  void conditionBroadcast();
122 
123 private:
124  const char* name_;
126 
127  // Cannot copy them
129  ConditionVariable& operator=(const ConditionVariable&);
130 };
131 } // End namespace SCIRun
132 
133 #endif
134 
Definition: RecursiveMutex.h:68
#define SCISHARE
Definition: share.h:39
Definition: ConditionVariable.h:80
Definition: Mutex.h:65
const char * name[]
Definition: BoostGraphExampleTests.cc:87
Definition: Thread_none.cc:471