~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/WSharedObject.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 >
 
41
class WSharedObject
 
42
{
 
43
public:
 
44
 
 
45
    /**
 
46
     * Default constructor.
 
47
     */
 
48
    WSharedObject();
 
49
 
 
50
    /**
 
51
     * Destructor.
 
52
     */
 
53
    virtual ~WSharedObject();
 
54
 
 
55
    /**
 
56
     * Type for read tickets.
 
57
     */
 
58
    typedef boost::shared_ptr< WSharedObjectTicketRead< T > > ReadTicket;
 
59
 
 
60
    /**
 
61
     * Type for write tickets.
 
62
     */
 
63
    typedef boost::shared_ptr< WSharedObjectTicketWrite< T > > WriteTicket;
 
64
 
 
65
    /**
 
66
     * Returns a ticket to get read access to the contained data. After the ticket is freed, the read lock vanishes.
 
67
     *
 
68
     * \return the read ticket
 
69
     */
 
70
    ReadTicket getReadTicket() const;
 
71
 
 
72
    /**
 
73
     * Returns a ticket to get write access to the contained data. After the ticket is freed, the write lock vanishes.
 
74
     *
 
75
     * \param suppressNotify true if no notification should be send after unlocking.
 
76
     *
 
77
     * \return the ticket
 
78
     */
 
79
    WriteTicket getWriteTicket( bool suppressNotify = false ) const;
 
80
 
 
81
    /**
 
82
     * This condition fires whenever the encapsulated object changed. This is fired automatically by endWrite().
 
83
     *
 
84
     * \return the condition
 
85
     */
 
86
    boost::shared_ptr< WCondition > getChangeCondition() const;
 
87
 
 
88
protected:
 
89
 
 
90
    /**
 
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.
 
93
     */
 
94
    mutable T m_object;
 
95
 
 
96
    /**
 
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.
 
99
     */
 
100
    mutable boost::shared_ptr< boost::shared_mutex > m_lock;
 
101
 
 
102
    /**
 
103
     * This condition set fires whenever the contained object changes. This corresponds to the Observable pattern.
 
104
     */
 
105
    boost::shared_ptr< WCondition > m_changeCondition;
 
106
 
 
107
private:
 
108
};
 
109
 
 
110
template < typename T >
 
111
WSharedObject< T >::WSharedObject():
 
112
    m_lock( new boost::shared_mutex ),
 
113
    m_changeCondition( new WCondition() )
 
114
{
 
115
    // init members
 
116
}
 
117
 
 
118
template < typename T >
 
119
WSharedObject< T >::~WSharedObject()
 
120
{
 
121
    // clean up
 
122
}
 
123
 
 
124
template < typename T >
 
125
boost::shared_ptr< WCondition > WSharedObject< T >::getChangeCondition() const
 
126
{
 
127
    return m_changeCondition;
 
128
}
 
129
 
 
130
template < typename T >
 
131
typename WSharedObject< T >::ReadTicket WSharedObject< T >::getReadTicket() const
 
132
{
 
133
    return boost::shared_ptr< WSharedObjectTicketRead< T > >(
 
134
            new WSharedObjectTicketRead< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
 
135
    );
 
136
}
 
137
 
 
138
template < typename T >
 
139
typename WSharedObject< T >::WriteTicket WSharedObject< T >::getWriteTicket( bool suppressNotify ) const
 
140
{
 
141
    if( suppressNotify )
 
142
    {
 
143
        return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
 
144
                new WSharedObjectTicketWrite< T >( m_object, m_lock, boost::shared_ptr< WCondition >() )
 
145
        );
 
146
    }
 
147
    else
 
148
    {
 
149
        return boost::shared_ptr< WSharedObjectTicketWrite< T > >(
 
150
                new WSharedObjectTicketWrite< T >( m_object, m_lock, m_changeCondition )
 
151
        );
 
152
    }
 
153
}
 
154
 
 
155
#endif  // WSHAREDOBJECT_H
 
156