OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  /**
50  * Destroys the ticket and releases the lock.
51  */
53  {
54  // NOTE: the derived destructor already unlocks.
55  if( m_condition )
56  {
57  m_condition->notify();
58  }
59  };
60 
61  /**
62  * If called, the unlock will NOT fire the condition. This is useful in some situations if you find out "hey there actually was nothing
63  * changed".
64  */
66  {
67  m_condition = boost::shared_ptr< WCondition >();
68  }
69 
70 protected:
71 
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