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