OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #include "WExportCommon.h"
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  */
40 class OWCOMMON_EXPORT WConditionOneShot: public WCondition
41 {
42  friend class WConditionOneShot_test;
43 public:
44 
45  /**
46  * Default constructor.
47  */
49 
50  /**
51  * Destructor.
52  */
53  virtual ~WConditionOneShot();
54 
55  /**
56  * Wait for the condition. Sets the calling thread asleep.
57  */
58  virtual void wait() const;
59 
60  /**
61  * Notifies all waiting threads.
62  */
63  virtual void notify();
64 
65 protected:
66 
67  /**
68  * Locked as long the condition was not fired.
69  */
70  boost::unique_lock<boost::shared_mutex> m_lock;
71 
72 private:
73 };
74 
75 #endif // WCONDITIONONESHOT_H
76