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