OpenWalnut  1.4.0
WCondition.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 WCONDITION_H
26 #define WCONDITION_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/shared_ptr.hpp>
30 #endif
31 #ifndef Q_MOC_RUN
32 #include <boost/function.hpp>
33 #endif
34 #ifndef Q_MOC_RUN
35 #include <boost/signals2/signal.hpp>
36 #endif
37 #ifndef Q_MOC_RUN
38 #include <boost/thread.hpp>
39 #endif
40 
41 
42 
43 /**
44  * Class to encapsulate boost::condition_variable_any. You may use it to efficiently wait for events (a condition comes true). It
45  * is a very simple implementation. It might be extended easily. Timed wait functions and so on.
46  */
47 class WCondition // NOLINT
48 {
49  friend class WCondition_test;
50 public:
51  /**
52  * Shared pointer type for WCondition.
53  */
54  typedef boost::shared_ptr< WCondition > SPtr;
55 
56  /**
57  * Const shared pointer type for WCondition.
58  */
59  typedef boost::shared_ptr< const WCondition > ConstSPtr;
60 
61  /**
62  * Default constructor.
63  */
64  WCondition();
65 
66  /**
67  * Destructor.
68  */
69  virtual ~WCondition();
70 
71  /**
72  * Wait for the condition. Sets the calling thread asleep.
73  */
74  virtual void wait() const;
75 
76  /**
77  * Notifies all waiting threads.
78  */
79  virtual void notify();
80 
81  /**
82  * Type used for signalling condition changes.
83  */
84  typedef boost::function0< void > t_ConditionNotifierType;
85 
86  /**
87  * Subscribes a specified function to be notified on condition change.
88  *
89  * \param notifier the notifier function
90  *
91  * \return the connection.
92  */
93  boost::signals2::connection subscribeSignal( t_ConditionNotifierType notifier ) const;
94 
95 protected:
96  /**
97  * Type used for condition notification.
98  */
99  typedef boost::signals2::signal<void ( void )> t_ConditionSignalType;
100 
101  /**
102  * Signal getting fired whenever the condition fires.
103  */
104  mutable t_ConditionSignalType signal_ConditionFired;
105 
106  /**
107  * The condition.
108  */
109  mutable boost::condition_variable_any m_condition;
110 
111  /**
112  * The mutex used for the condition.
113  */
114  mutable boost::shared_mutex m_mutex;
115 
116 private:
117 };
118 
119 #endif // WCONDITION_H
120 
boost::signals2::connection subscribeSignal(t_ConditionNotifierType notifier) const
Subscribes a specified function to be notified on condition change.
Definition: WCondition.cpp:50
boost::signals2::signal< void(void)> t_ConditionSignalType
Type used for condition notification.
Definition: WCondition.h:99
boost::function0< void > t_ConditionNotifierType
Type used for signalling condition changes.
Definition: WCondition.h:84
t_ConditionSignalType signal_ConditionFired
Signal getting fired whenever the condition fires.
Definition: WCondition.h:104
boost::shared_mutex m_mutex
The mutex used for the condition.
Definition: WCondition.h:114
boost::shared_ptr< const WCondition > ConstSPtr
Const shared pointer type for WCondition.
Definition: WCondition.h:59
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:47
virtual void notify()
Notifies all waiting threads.
Definition: WCondition.cpp:44
boost::condition_variable_any m_condition
The condition.
Definition: WCondition.h:109
WCondition()
Default constructor.
Definition: WCondition.cpp:27
virtual ~WCondition()
Destructor.
Definition: WCondition.cpp:32
boost::shared_ptr< WCondition > SPtr
Shared pointer type for WCondition.
Definition: WCondition.h:54
virtual void wait() const
Wait for the condition.
Definition: WCondition.cpp:37