OpenWalnut  1.5.0dev
WConditionSet.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCONDITIONSET_H
26 #define WCONDITIONSET_H
27 
28 #include <map>
29 #include <memory>
30 #include <shared_mutex>
31 #include <utility>
32 
33 #include <boost/thread.hpp>
34 
35 #include "WCondition.h"
36 
37 
38 /**
39  * Class allowing multiple conditions to be used for one waiting cycle. Since wait() can not be used for waiting on multiple
40  * conditions, this class can encapsulate multiple conditions and offer a wait() command to wait for one of them to change its
41  * state. Please not that this class can also be used as condition.
42  */
44 {
45 friend class WConditionSetTest; //!< Access for test class.
46 public:
47  /**
48  * Shared pointer to instance of this class.
49  */
50  typedef std::shared_ptr< WConditionSet > SPtr;
51 
52  /**
53  * Shared pointer to const instance of this class.
54  */
55  typedef std::shared_ptr< const WConditionSet > ConstSPtr;
56 
57  /**
58  * Default constructor.
59  */
60  WConditionSet();
61 
62  /**
63  * Destructor.
64  */
65  virtual ~WConditionSet();
66 
67  /**
68  * Adds another condition to the set of conditions to wait for. Note that, whenever someone is waiting for this WConditionSet,
69  * the newly added one is also directly included into the wait() call.
70  *
71  * \param condition the condition to add.
72  */
73  virtual void add( std::shared_ptr< WCondition > condition );
74 
75  /**
76  * Removes the specified condition. As add() this immediately takes effect on running wait() calls.
77  *
78  * \param condition the condition to remove
79  */
80  virtual void remove( std::shared_ptr< WCondition > condition );
81 
82  /**
83  * Wait for the condition. Sets the calling thread asleep. If the condition set is resetable, this will return immediately
84  * when a condition in the set fired in the past and there has been no reset() call until now.
85  */
86  virtual void wait() const;
87 
88  /**
89  * Resets the internal fire state. This does nothing if !isResetable().
90  */
91  virtual void reset() const;
92 
93  /**
94  * Sets the resetable flag. This causes the condition set to act like a WConditionOneShot. There are several implications to
95  * this you should consider when using the condition set as a resetable. If one condition in the condition set fires, a
96  * subsequent call to wait() will immediately return until a reset() call has been done. If you share one condition set among
97  * several threads, you should consider, that one thread can reset the condition set before the other thread had a chance to
98  * call wait() which causes the other thread to wait until the next condition in the set fires.
99  *
100  * \param resetable true if the fire state should be delayed and can be reseted.
101  * \param autoReset true if the state should be reset whenever a wait call is called and continues.This is especially useful if a
102  * condition set is used only by one thread, so there is no need to call reset() explicitly.
103  */
104  void setResetable( bool resetable = true, bool autoReset = true );
105 
106  /**
107  * Returns whether the condition set acts like a one shot condition.
108  *
109  * \return true if the fire state is delayed and can be reseted.
110  */
111  bool isResetable();
112 
113  /**
114  * Notifies all waiting threads.
115  */
116  virtual void notify();
117 
118 protected:
119  /**
120  * Flag denoting whether the condition set should act like a one shot condition.
121  */
123 
124  /**
125  * Flag which shows whether the wait() call should reset the state m_fired when it returns.
126  */
128 
129  /**
130  * We need to keep track of the connections a condition has made since boost::function objects do not provide a == operator and can therefore
131  * not easily be removed from a signals by signal.desconnect( functor ).
132  */
133  typedef std::map< std::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionMap;
134 
135  /**
136  * Set of conditions to be waited for.
137  */
139 
140  /**
141  * Each condition has a connection.
142  */
143  typedef std::pair< std::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair;
144 
145  /**
146  * Lock used for thread-safe writing to the condition set.
147  */
148  std::shared_mutex m_conditionSetLock;
149 
150  /**
151  * Notifier function getting notified whenever a condition got fired.
152  */
153  virtual void conditionFired();
154 
155  /**
156  * Flag denoting whether one condition fired in the past. Just useful when m_resetable is true.
157  */
158  mutable bool m_fired;
159 
160  /**
161  * The notifier which gets called by all conditions if they fire
162  */
164 
165 private:
166 };
167 
168 #endif // WCONDITIONSET_H
169 
Test WConditionSet.
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:44
virtual void wait() const
Wait for the condition.
std::map< std::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionMap
We need to keep track of the connections a condition has made since boost::function objects do not pr...
std::shared_ptr< WConditionSet > SPtr
Shared pointer to instance of this class.
Definition: WConditionSet.h:50
ConditionConnectionMap m_conditionSet
Set of conditions to be waited for.
bool m_fired
Flag denoting whether one condition fired in the past.
virtual ~WConditionSet()
Destructor.
virtual void reset() const
Resets the internal fire state.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
virtual void remove(std::shared_ptr< WCondition > condition)
Removes the specified condition.
bool m_resetable
Flag denoting whether the condition set should act like a one shot condition.
virtual void add(std::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
std::shared_ptr< const WConditionSet > ConstSPtr
Shared pointer to const instance of this class.
Definition: WConditionSet.h:55
bool isResetable()
Returns whether the condition set acts like a one shot condition.
WCondition::t_ConditionNotifierType m_notifier
The notifier which gets called by all conditions if they fire.
bool m_autoReset
Flag which shows whether the wait() call should reset the state m_fired when it returns.
virtual void conditionFired()
Notifier function getting notified whenever a condition got fired.
std::shared_mutex m_conditionSetLock
Lock used for thread-safe writing to the condition set.
virtual void notify()
Notifies all waiting threads.
WConditionSet()
Default constructor.
std::pair< std::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair
Each condition has a connection.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
boost::function0< void > t_ConditionNotifierType
Type used for signalling condition changes.
Definition: WCondition.h:78