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