OpenWalnut  1.5.0dev
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 <memory>
29 #include <shared_mutex>
30 
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 /**
48  * The shared object class needs protected access to create new instances.
49  */
50 friend class WSharedObject< Data >;
51 public:
52  /**
53  * Destroys the ticket and releases the lock.
54  */
56  {
57  // NOTE: the derived destructor already unlocks.
58  if( m_condition )
59  {
60  m_condition->notify();
61  }
62  };
63 
64  /**
65  * If called, the unlock will NOT fire the condition. This is useful in some situations if you find out "hey there actually was nothing
66  * changed".
67  */
69  {
70  m_condition = std::shared_ptr< WCondition >();
71  }
72 
73 protected:
74  /**
75  * Create a new instance. It is protected to avoid someone to create them. It locks the mutex.
76  *
77  * \param data the data to protect
78  * \param mutex the mutex used to lock
79  * \param condition a condition that should be fired upon unlock. Can be NULL.
80  */
81  WSharedObjectTicket( Data& data, std::shared_ptr< std::shared_mutex > mutex, std::shared_ptr< WCondition > condition ): // NOLINT
82  m_data( data ),
83  m_mutex( mutex ),
84  m_condition( condition )
85  {
86  };
87 
88  /**
89  * The data to which access is allowed by the ticket
90  */
91  Data& m_data;
92 
93  /**
94  * The mutex used for locking.
95  */
96  std::shared_ptr< std::shared_mutex > m_mutex;
97 
98  /**
99  * A condition which gets notified after unlocking. Especially useful to notify waiting threads about a change in the object.
100  */
101  std::shared_ptr< WCondition > m_condition;
102 
103  /**
104  * Unlocks the mutex.
105  */
106  virtual void unlock() = 0;
107 
108 private:
109 };
110 
111 #endif // WSHAREDOBJECTTICKET_H
Class which represents granted access to a locked object.
void suppressUnlockCondition()
If called, the unlock will NOT fire the condition.
std::shared_ptr< WCondition > m_condition
A condition which gets notified after unlocking.
WSharedObjectTicket(Data &data, std::shared_ptr< std::shared_mutex > mutex, std::shared_ptr< WCondition > condition)
Create a new instance.
Data & m_data
The data to which access is allowed by the ticket.
std::shared_ptr< std::shared_mutex > m_mutex
The mutex used for locking.
virtual ~WSharedObjectTicket()
Destroys the ticket and releases the lock.
virtual void unlock()=0
Unlocks the mutex.
Wrapper around an object/type for thread safe sharing of objects among multiple threads.
Definition: WSharedObject.h:45