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