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