OpenWalnut  1.4.0
WSharedObjectTicketRead.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 WSHAREDOBJECTTICKETREAD_H
26 #define WSHAREDOBJECTTICKETREAD_H
27 
28 #ifndef Q_MOC_RUN
29 #include <boost/shared_ptr.hpp>
30 #endif
31 
32 #include "WCondition.h"
33 #include "WSharedObjectTicket.h"
34 
35 /**
36  * 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
37  * has been destroyed. This class especially implements the shared (read) lock.
38  */
39 template < typename Data >
41 {
42 // the shared object class needs protected access to create new instances
43 friend class WSharedObject< Data >;
44 public:
45  /**
46  * Destroys the ticket and releases the lock.
47  */
49  {
50  // explicitly unlock to ensure the WSharedObjectTicket destructor can call the update callback AFTER the lock has been released
51  unlock();
52  };
53 
54  /**
55  * Returns the protected data. As long as you own the ticket, you are allowed to use it.
56  *
57  * \note making it const enforces const correctness for contained types!
58  *
59  * \return the data (const!)
60  */
61  const Data& get() const
62  {
64  };
65 
66 protected:
67  /**
68  * Create a new instance. It is protected to avoid someone to create them. It locks the mutex for read.
69  *
70  * \param data the data to protect
71  * \param mutex the mutex used to lock
72  * \param condition a condition that should be fired upon unlock. Can be NULL.
73  */
74  WSharedObjectTicketRead( Data& data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition ): // NOLINT
75  WSharedObjectTicket< Data >( data, mutex, condition ),
76  m_lock( boost::shared_lock< boost::shared_mutex >( *mutex ) )
77  {
78  };
79 
80  /**
81  * The lock.
82  */
83  boost::shared_lock< boost::shared_mutex > m_lock;
84 
85  /**
86  * Unlocks the mutex.
87  */
88  virtual void unlock()
89  {
90  m_lock.unlock();
91  }
92 
93 private:
94 };
95 
96 #endif // WSHAREDOBJECTTICKETREAD_H
97 
boost::shared_lock< boost::shared_mutex > m_lock
The lock.
WSharedObjectTicketRead(Data &data, boost::shared_ptr< boost::shared_mutex > mutex, boost::shared_ptr< WCondition > condition)
Create a new instance.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:43
virtual void unlock()
Unlocks the mutex.
virtual ~WSharedObjectTicketRead()
Destroys the ticket and releases the lock.
Class which represents granted access to a locked object.
Class which represents granted access to a locked object.