OpenWalnut  1.5.0dev
WSharedObject.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 WSHAREDOBJECT_H
26 #define WSHAREDOBJECT_H
27 
28 #include <memory>
29 #include <shared_mutex>
30 
31 #include <boost/thread.hpp>
32 
33 #include "WCondition.h"
34 #include "WSharedObjectTicket.h"
35 #include "WSharedObjectTicketRead.h"
36 #include "WSharedObjectTicketWrite.h"
37 
38 /**
39  * Wrapper around an object/type for thread safe sharing of objects among multiple threads. The advantage of this class over WFlag
40  * is, that WFlag just protects simple get/set operations, while this class can protect a whole bunch of operations on the
41  * encapsulated object.
42  */
43 template < typename T >
45 {
46 public:
47  /**
48  * Default constructor.
49  */
51 
52  /**
53  * Destructor.
54  */
55  virtual ~WSharedObject();
56 
57  /**
58  * The type protected by this shared object class
59  */
60  typedef T ValueT;
61 
62  /**
63  * Type for read tickets.
64  */
65  typedef std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket;
66 
67  /**
68  * Type for write tickets.
69  */
70  typedef std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket;
71 
72  /**
73  * Shared pointer abbreviation.
74  */
75  typedef std::shared_ptr< WSharedObject< T > > SPtr;
76 
77  /**
78  * Const shared ptr abbreviation.
79  */
80  typedef std::shared_ptr< WSharedObject< T > > ConstSPtr;
81 
82  /**
83  * Returns a ticket to get read access to the contained data. After the ticket is freed, the read lock vanishes.
84  *
85  * \return the read ticket
86  */
88 
89  /**
90  * Returns a ticket to get write access to the contained data. After the ticket is freed, the write lock vanishes.
91  *
92  * \param suppressNotify true if no notification should be send after unlocking.
93  *
94  * \return the ticket
95  */
96  WriteTicket getWriteTicket( bool suppressNotify = false ) const;
97 
98  /**
99  * This condition fires whenever the encapsulated object changed. This is fired automatically by endWrite().
100  *
101  * \return the condition
102  */
103  std::shared_ptr< WCondition > getChangeCondition() const;
104 
105 protected:
106  /**
107  * The object wrapped by this class. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
108  * non-const reference to m_object.
109  */
110  mutable T m_object;
111 
112  /**
113  * The lock to ensure thread safe access. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
114  * non-const reference to m_lock.
115  */
116  mutable std::shared_ptr< std::shared_mutex > m_lock;
117 
118  /**
119  * This condition set fires whenever the contained object changes. This corresponds to the Observable pattern.
120  */
121  std::shared_ptr< WCondition > m_changeCondition;
122 
123 private:
124 };
125 
126 template < typename T >
128  m_lock( new std::shared_mutex ),
129  m_changeCondition( new WCondition() )
130 {
131  // init members
132 }
133 
134 template < typename T >
136 {
137  // clean up
138 }
139 
140 template < typename T >
141 std::shared_ptr< WCondition > WSharedObject< T >::getChangeCondition() const
142 {
143  return m_changeCondition;
144 }
145 
146 template < typename T >
148 {
149  return std::shared_ptr< WSharedObjectTicketRead< T > >(
150  new WSharedObjectTicketRead< T >( m_object, m_lock, std::shared_ptr< WCondition >() )
151  );
152 }
153 
154 template < typename T >
156 {
157  if( suppressNotify )
158  {
159  return std::shared_ptr< WSharedObjectTicketWrite< T > >(
160  new WSharedObjectTicketWrite< T >( m_object, m_lock, std::shared_ptr< WCondition >() )
161  );
162  }
163  else
164  {
165  return std::shared_ptr< WSharedObjectTicketWrite< T > >(
166  new WSharedObjectTicketWrite< T >( m_object, m_lock, m_changeCondition )
167  );
168  }
169 }
170 
171 #endif // WSHAREDOBJECT_H
172 
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
Class which represents granted access to a locked object.
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:45
T m_object
The object wrapped by this class.
std::shared_ptr< std::shared_mutex > m_lock
The lock to ensure thread safe access.
std::shared_ptr< WSharedObject< T > > SPtr
Shared pointer abbreviation.
Definition: WSharedObject.h:75
std::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket
Type for read tickets.
Definition: WSharedObject.h:65
std::shared_ptr< WCondition > m_changeCondition
This condition set fires whenever the contained object changes.
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
std::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket
Type for write tickets.
Definition: WSharedObject.h:70
virtual ~WSharedObject()
Destructor.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.
std::shared_ptr< WCondition > getChangeCondition() const
This condition fires whenever the encapsulated object changed.
T ValueT
The type protected by this shared object class.
Definition: WSharedObject.h:60
std::shared_ptr< WSharedObject< T > > ConstSPtr
Const shared ptr abbreviation.
Definition: WSharedObject.h:80
WSharedObject()
Default constructor.