OpenWalnut  1.4.0
WConditionOneShot.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 WCONDITIONONESHOT_H
26 #define WCONDITIONONESHOT_H
27 
28 #include <boost/thread.hpp>
29 
30 #include "WCondition.h"
31 
32 
33 /**
34  * Implements a WCondition, but can be fired only ONCE. This is useful if you want to have a thread waiting for a condition but
35  * you can not assure that the thread already waits when you set the condition. This would cause the thread to wait endlessly
36  * because he does not know that you already fired it. Implementation is simple. The constructor uses a unique lock (write lock)
37  * on a mutex. All waiting threads try to get a read lock which is not possible as long it is write-locked. The notify method
38  * releases the write lock and all waiting threads can continue.
39  */
41 {
42  friend class WConditionOneShot_test;
43 public:
44  /**
45  * Default constructor.
46  */
48 
49  /**
50  * Destructor.
51  */
52  virtual ~WConditionOneShot();
53 
54  /**
55  * Wait for the condition. Sets the calling thread asleep.
56  */
57  virtual void wait() const;
58 
59  /**
60  * Notifies all waiting threads.
61  */
62  virtual void notify();
63 
64 protected:
65  /**
66  * Locked as long the condition was not fired.
67  */
68  boost::unique_lock<boost::shared_mutex> m_lock;
69 
70 private:
71 };
72 
73 #endif // WCONDITIONONESHOT_H
74 
Implements a WCondition, but can be fired only ONCE.
virtual void notify()
Notifies all waiting threads.
boost::unique_lock< boost::shared_mutex > m_lock
Locked as long the condition was not fired.
WConditionOneShot()
Default constructor.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:39
virtual ~WConditionOneShot()
Destructor.
virtual void wait() const
Wait for the condition.