summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/include/objectslist.h
blob: fad5a20bdf32416152cf525d3dccf81f609c1d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* $Id: objectslist.h $ */
/** @file
 *
 * List of COM objects
 */

/*
 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#ifndef MAIN_INCLUDED_objectslist_h
#define MAIN_INCLUDED_objectslist_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif

#include <list>
#include <VBox/com/ptr.h>

/**
 * Implements a "flat" objects list with a lock. Since each such list
 * has its own lock it is not a good idea to implement trees with this.
 *
 * ObjectList<T> is designed to behave as if it were a std::list of
 * COM pointers of class T; in other words,
 * ObjectList<Medium> behaves like std::list< ComObjPtr<Medium> > but
 * it's less typing. Iterators, front(), size(), begin() and end()
 * are implemented.
 *
 * In addition it automatically includes an RWLockHandle which can be
 * accessed with getLockHandle().
 *
 * If you need the raw std::list for some reason you can access it with
 * getList().
 *
 * The destructor automatically calls uninit() on every contained
 * COM object. If this is not desired, clear the member list before
 * deleting the list object.
 */
template<typename T>
class ObjectsList
{
public:
    typedef ComObjPtr<T> MyType;
    typedef std::list<MyType> MyList;

    typedef typename MyList::iterator iterator;
    typedef typename MyList::const_iterator const_iterator;
        // typename is necessary to disambiguate "::iterator" in templates; see
        // the "this might hurt your head" part in
        // http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

    ObjectsList(RWLockHandle &lockHandle)
        : m_lock(lockHandle)
    { }

    ~ObjectsList()
    {
        uninitAll();
    }

private:
    // prohibit copying and assignment
    ObjectsList(const ObjectsList &d);
    ObjectsList& operator=(const ObjectsList &d);

public:

    /**
     * Returns the lock handle which protects this list, for use with
     * AutoReadLock or AutoWriteLock.
     */
    RWLockHandle& getLockHandle()
    {
        return m_lock;
    }

    /**
     * Calls m_ll.push_back(p) with locking.
     * @param p
     */
    void addChild(MyType p)
    {
        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
        m_ll.push_back(p);
    }

    /**
     * Calls m_ll.remove(p) with locking. Does NOT call uninit()
     * on the contained object.
     * @param p
     */
    void removeChild(MyType p)
    {
        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
        m_ll.remove(p);
    }

    /**
     * Appends all objects from another list to the member list.
     * Locks the other list for reading but does not lock "this"
     * (because it might be on the caller's stack and needs no
     * locking).
     * @param ll
     */
    void appendOtherList(ObjectsList<T> &ll)
    {
        AutoReadLock alr(ll.getLockHandle() COMMA_LOCKVAL_SRC_POS);
        for (const_iterator it = ll.begin();
             it != ll.end();
             ++it)
        {
            m_ll.push_back(*it);
        }
    }

    /**
     * Calls uninit() on every COM object on the list and then
     * clears the list, with locking.
     */
    void uninitAll()
    {
        /* The implementation differs from the high level description, because
         * it isn't safe to hold any locks when invoking uninit() methods. It
         * leads to incorrect lock order (first lock, then the Caller related
         * event semaphore) and thus deadlocks. Dropping the lock is vital,
         * and means we can't rely on iterators while not holding the lock. */
        AutoWriteLock al(m_lock COMMA_LOCKVAL_SRC_POS);
        while (!m_ll.empty())
        {
            /* Need a copy of the element, have to delete the entry before
             * dropping the lock, otherwise someone else might mess with the
             * list in the mean time, leading to erratic behavior. */
            MyType q = m_ll.front();
            m_ll.pop_front();
            al.release();
            q->uninit();
            al.acquire();
        }
    }

    /**
     * Returns the no. of objects on the list (std::list compatibility)
     * with locking.
     */
    size_t size()
    {
        AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
        return m_ll.size();
    }

    /**
     * Returns a raw pointer to the member list of objects.
     * Does not lock!
     * @return
     */
    MyList& getList()
    {
        return m_ll;
    }

    /**
     * Returns the first object on the list (std::list compatibility)
     * with locking.
     */
    MyType front()
    {
        AutoReadLock al(m_lock COMMA_LOCKVAL_SRC_POS);
        return m_ll.front();
    }

    /**
     * Returns the begin iterator from the list (std::list compatibility).
     * Does not lock!
     * @return
     */
    iterator begin()
    {
        return m_ll.begin();
    }

    /**
     * Returns the end iterator from the list (std::list compatibility).
     * Does not lock!
     */
    iterator end()
    {
        return m_ll.end();
    }

    void insert(iterator it, MyType &p)
    {
        m_ll.insert(it, p);
    }

    void erase(iterator it)
    {
        m_ll.erase(it);
    }

private:
    MyList          m_ll;
    RWLockHandle    &m_lock;
};

#endif /* !MAIN_INCLUDED_objectslist_h */