1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
25
#ifndef WSHAREDOBJECT_H
26
#define WSHAREDOBJECT_H
28
#include <boost/thread.hpp>
30
#include "WCondition.h"
31
#include "WSharedObjectTicket.h"
32
#include "WSharedObjectTicketRead.h"
33
#include "WSharedObjectTicketWrite.h"
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.
40
template < typename T >
46
* Default constructor.
53
virtual ~WSharedObject();
56
* Type for read tickets.
58
typedef boost::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket;
61
* Type for write tickets.
63
typedef boost::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket;
66
* Returns a ticket to get read access to the contained data. After the ticket is freed, the read lock vanishes.
68
* \return the read ticket
70
ReadTicket getReadTicket() const;
73
* Returns a ticket to get write access to the contained data. After the ticket is freed, the write lock vanishes.
75
* \param suppressNotify true if no notification should be send after unlocking.
79
WriteTicket getWriteTicket( bool suppressNotify = false ) const;
82
* This condition fires whenever the encapsulated object changed. This is fired automatically by endWrite().
84
* \return the condition
86
boost::shared_ptr< WCondition > getChangeCondition() const;
91
* The object wrapped by this class. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
92
* non-const reference to m_object.
97
* The lock to ensure thread safe access. This member is mutable as the \ref getReadTicket and \ref getWriteTicket functions are const but need a
98
* non-const reference to m_lock.
100
mutable boost::shared_ptr< boost::shared_mutex > m_lock;
103
* This condition set fires whenever the contained object changes. This corresponds to the Observable pattern.
105
boost::shared_ptr< WCondition > m_changeCondition;
110
template < typename T >
111
WSharedObject< T >::WSharedObject():
112
m_lock( new boost::shared_mutex ),
113
m_changeCondition( new WCondition() )
118
template < typename T >
119
WSharedObject< T >::~WSharedObject()
124
template < typename T >
125
boost::shared_ptr< WCondition > WSharedObject< T >::getChangeCondition() const
127
return m_changeCondition;
130
template < typename T >
131
typename WSharedObject< T >::ReadTicket WSharedObject< T >::getReadTicket() const
133
return boost::shared_ptr< WSharedObjectTicketRead< T > >(
134
new WSharedObjectTicketRead< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
138
template < typename T >
139
typename WSharedObject< T >::WriteTicket WSharedObject< T >::getWriteTicket( bool suppressNotify ) const
143
return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
144
new WSharedObjectTicketWrite< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
149
return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
150
new WSharedObjectTicketWrite< T >( m_object, m_lock, m_changeCondition )
155
#endif // WSHAREDOBJECT_H