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