OpenWalnut  1.4.0
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 <utility>
30 
31 #ifndef Q_MOC_RUN
32 #include <boost/shared_ptr.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/thread.hpp>
36 #endif
37 
38 #include "WCondition.h"
39 
40 
41 /**
42  * Class allowing multiple conditions to be used for one waiting cycle. Since wait() can not be used for waiting on multiple
43  * conditions, this class can encapsulate multiple conditions and offer a wait() command to wait for one of them to change its
44  * state. Please not that this class can also be used as condition.
45  */
47 {
48 friend class WConditionSetTest;
49 public:
50  /**
51  * Shared pointer to instance of this class.
52  */
53  typedef boost::shared_ptr< WConditionSet > SPtr;
54 
55  /**
56  * Shared pointer to const instance of this class.
57  */
58  typedef boost::shared_ptr< const WConditionSet > ConstSPtr;
59 
60  /**
61  * Default constructor.
62  */
63  WConditionSet();
64 
65  /**
66  * Destructor.
67  */
68  virtual ~WConditionSet();
69 
70  /**
71  * Adds another condition to the set of conditions to wait for. Note that, whenever someone is waiting for this WConditionSet,
72  * the newly added one is also directly included into the wait() call.
73  *
74  * \param condition the condition to add.
75  */
76  virtual void add( boost::shared_ptr< WCondition > condition );
77 
78  /**
79  * Removes the specified condition. As add() this immediately takes effect on running wait() calls.
80  *
81  * \param condition the condition to remove
82  */
83  virtual void remove( boost::shared_ptr< WCondition > condition );
84 
85  /**
86  * Wait for the condition. Sets the calling thread asleep. If the condition set is resetable, this will return immediately
87  * when a condition in the set fired in the past and there has been no reset() call until now.
88  */
89  virtual void wait() const;
90 
91  /**
92  * Resets the internal fire state. This does nothing if !isResetable().
93  */
94  virtual void reset() const;
95 
96  /**
97  * Sets the resetable flag. This causes the condition set to act like a WConditionOneShot. There are several implications to
98  * this you should consider when using the condition set as a resetable. If one condition in the condition set fires, a
99  * subsequent call to wait() will immediately return until a reset() call has been done. If you share one condition set among
100  * several threads, you should consider, that one thread can reset the condition set before the other thread had a chance to
101  * call wait() which causes the other thread to wait until the next condition in the set fires.
102  *
103  * \param resetable true if the fire state should be delayed and can be reseted.
104  * \param autoReset true if the state should be reset whenever a wait call is called and continues.This is especially useful if a
105  * condition set is used only by one thread, so there is no need to call reset() explicitly.
106  */
107  void setResetable( bool resetable = true, bool autoReset = true );
108 
109  /**
110  * Returns whether the condition set acts like a one shot condition.
111  *
112  * \return true if the fire state is delayed and can be reseted.
113  */
114  bool isResetable();
115 
116 protected:
117  /**
118  * Flag denoting whether the condition set should act like a one shot condition.
119  */
121 
122  /**
123  * Flag which shows whether the wait() call should reset the state m_fired when it returns.
124  */
126 
127  /**
128  * We need to keep track of the connections a condition has made since boost::function objects do not provide a == operator and can therefore
129  * not easily be removed from a signals by signal.desconnect( functor ).
130  */
131  typedef std::map< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionMap;
132 
133  /**
134  * Set of conditions to be waited for.
135  */
136  ConditionConnectionMap m_conditionSet;
137 
138  /**
139  * Each condition has a connection.
140  */
141  typedef std::pair< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair;
142 
143  /**
144  * Lock used for thread-safe writing to the condition set.
145  */
146  boost::shared_mutex m_conditionSetLock;
147 
148  /**
149  * Notifier function getting notified whenever a condition got fired.
150  */
151  virtual void conditionFired();
152 
153  /**
154  * Flag denoting whether one condition fired in the past. Just useful when m_resetable is true.
155  */
156  mutable bool m_fired;
157 
158  /**
159  * The notifier which gets called by all conditions if they fire
160  */
162 
163 private:
164 };
165 
166 #endif // WCONDITIONSET_H
167 
WConditionSet()
Default constructor.
std::map< boost::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...
virtual void wait() const
Wait for the condition.
boost::shared_ptr< const WConditionSet > ConstSPtr
Shared pointer to const instance of this class.
Definition: WConditionSet.h:58
bool m_autoReset
Flag which shows whether the wait() call should reset the state m_fired when it returns.
boost::function0< void > t_ConditionNotifierType
Type used for signalling condition changes.
Definition: WCondition.h:84
virtual void add(boost::shared_ptr< WCondition > condition)
Adds another condition to the set of conditions to wait for.
virtual ~WConditionSet()
Destructor.
std::pair< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair
Each condition has a connection.
virtual void reset() const
Resets the internal fire state.
boost::shared_mutex m_conditionSetLock
Lock used for thread-safe writing to the condition set.
Class allowing multiple conditions to be used for one waiting cycle.
Definition: WConditionSet.h:46
virtual void conditionFired()
Notifier function getting notified whenever a condition got fired.
void setResetable(bool resetable=true, bool autoReset=true)
Sets the resetable flag.
bool m_fired
Flag denoting whether one condition fired in the past.
bool isResetable()
Returns whether the condition set acts like a one shot condition.
ConditionConnectionMap m_conditionSet
Set of conditions to be waited for.
Test WConditionSet.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:47
bool m_resetable
Flag denoting whether the condition set should act like a one shot condition.
boost::shared_ptr< WConditionSet > SPtr
Shared pointer to instance of this class.
Definition: WConditionSet.h:53
WCondition::t_ConditionNotifierType m_notifier
The notifier which gets called by all conditions if they fire.