OpenWalnut  1.4.0
WSharedObjectTicket.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 WSHAREDOBJECTTICKET_H
26 #define WSHAREDOBJECTTICKET_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/shared_ptr.hpp>
30 #endif
31 
32 #include "WCondition.h"
33 
34 // The shared object class
35 template < typename T >
36 class WSharedObject;
37 
38 /**
39  * Class which represents granted access to a locked object. It contains a reference to the object and a lock. The lock is freed after the ticket
40  * has been destroyed.
41  *
42  * \note This class does not provide any member to actually get the contained value/instance. This is done in read and write tickets.
43  */
44 template < typename Data >
46 {
47 // the shared object class needs protected access to create new instances
48 friend class WSharedObject< Data >;
49 public:
50  /**
51  * Destroys the ticket and releases the lock.
52  */
54  {
55  // NOTE: the derived destructor already unlocks.
56  if( m_condition )
57  {
58  m_condition->notify();
59  }
60  };
61 
62  /**
63  * If called, the unlock will NOT fire the condition. This is useful in some situations if you find out "hey there actually was nothing
64  * changed".
65  */
67  {
68  m_condition = boost::shared_ptr< WCondition >();
69  }
70 
71 protected:
72  /**
73  * Create a new instance. It is protected to avoid someone to create them. It locks the mutex.
74  *
75  * \param data the data to protect
76  * \param mutex the mutex used to lock
77  * \param condition a condition that should be fired upon unlock. Can be NULL.
78  */
79  WSharedObjectTicket( Data& data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition ): // NOLINT
80  m_data( data ),
81  m_mutex( mutex ),
82  m_condition( condition )
83  {
84  };
85 
86  /**
87  * The data to which access is allowed by the ticket
88  */
89  Data& m_data;
90 
91  /**
92  * The mutex used for locking.
93  */
94  boost::shared_ptr< boost::shared_mutex > m_mutex;
95 
96  /**
97  * A condition which gets notified after unlocking. Especially useful to notify waiting threads about a change in the object.
98  */
99  boost::shared_ptr< WCondition > m_condition;
100 
101  /**
102  * Unlocks the mutex.
103  */
104  virtual void unlock() = 0;
105 
106 private:
107 };
108 
109 #endif // WSHAREDOBJECTTICKET_H
110 
void suppressUnlockCondition()
If called, the unlock will NOT fire the condition.
boost::shared_ptr< WCondition > m_condition
A condition which gets notified after unlocking.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:43
virtual void unlock()=0
Unlocks the mutex.
virtual ~WSharedObjectTicket()
Destroys the ticket and releases the lock.
WSharedObjectTicket(Data &data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition)
Create a new instance.
Class which represents granted access to a locked object.
Data & m_data
The data to which access is allowed by the ticket.
boost::shared_ptr< boost::shared_mutex > m_mutex
The mutex used for locking.