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