VirtualBox

source: vbox/trunk/include/iprt/cpp/mtlist.h@ 106061

Last change on this file since 106061 was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/** @file
2 * IPRT - Generic thread-safe list Class.
3 */
4
5/*
6 * Copyright (C) 2011-2024 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef IPRT_INCLUDED_cpp_mtlist_h
37#define IPRT_INCLUDED_cpp_mtlist_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/cpp/list.h>
43#include <iprt/semaphore.h>
44
45/** @addtogroup grp_rt_cpp_list
46 * @{
47 */
48
49/**
50 * A guard class for thread-safe read/write access.
51 */
52template <>
53class RTCListGuard<true>
54{
55public:
56 RTCListGuard() : m_hRWSem(NIL_RTSEMRW)
57 {
58#if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3)
59 RTLOCKVALCLASS hClass;
60 int rc = RTLockValidatorClassCreate(&hClass, true /*fAutodidact*/, RT_SRC_POS, "RTCListGuard");
61 AssertStmt(RT_SUCCESS(rc), hClass = NIL_RTLOCKVALCLASS);
62 rc = RTSemRWCreateEx(&m_hRWSem, 0 /*fFlags*/, hClass, RTLOCKVAL_SUB_CLASS_NONE, NULL /*pszNameFmt*/);
63 AssertRC(rc);
64#else
65 int rc = RTSemRWCreateEx(&m_hRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, 0, NULL);
66 AssertRC(rc);
67#endif
68 }
69
70 ~RTCListGuard()
71 {
72 RTSemRWDestroy(m_hRWSem);
73 m_hRWSem = NIL_RTSEMRW;
74 }
75
76 inline void enterRead() const { int rc = RTSemRWRequestRead(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
77 inline void leaveRead() const { int rc = RTSemRWReleaseRead(m_hRWSem); AssertRC(rc); }
78 inline void enterWrite() { int rc = RTSemRWRequestWrite(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
79 inline void leaveWrite() { int rc = RTSemRWReleaseWrite(m_hRWSem); AssertRC(rc); }
80
81 /* Define our own new and delete. */
82 RTMEMEF_NEW_AND_DELETE_OPERATORS();
83
84private:
85 mutable RTSEMRW m_hRWSem;
86};
87
88/**
89 * @brief Generic thread-safe list class.
90 *
91 * RTCMTList is a thread-safe implementation of the list class. It uses a
92 * read/write semaphore to serialize the access to the items. Several readers
93 * can simultaneous access different or the same item. If one thread is writing
94 * to an item, the other accessors are blocked until the write has finished.
95 *
96 * Although the access is guarded, the user has to make sure the list content
97 * is consistent when iterating over the list or doing any other kind of access
98 * which makes assumptions about the list content. For a finer control of access
99 * restrictions, use your own locking mechanism and the standard list
100 * implementation.
101 *
102 * @see RTCListBase
103 */
104template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
105class RTCMTList : public RTCListBase<T, ITYPE, true>
106{
107 /* Traits */
108 typedef RTCListBase<T, ITYPE, true> BASE;
109
110public:
111 /**
112 * Creates a new list.
113 *
114 * This preallocates @a cCapacity elements within the list.
115 *
116 * @param cCapacity The initial capacity the list has.
117 * @throws std::bad_alloc
118 */
119 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
120 : BASE(cCapacity) {}
121
122 /* Define our own new and delete. */
123 RTMEMEF_NEW_AND_DELETE_OPERATORS();
124};
125
126/**
127 * Specialized thread-safe list class for using the native type list for
128 * unsigned 64-bit values even on a 32-bit host.
129 *
130 * @see RTCListBase
131 */
132template <>
133class RTCMTList<uint64_t>: public RTCListBase<uint64_t, uint64_t, true>
134{
135 /* Traits */
136 typedef RTCListBase<uint64_t, uint64_t, true> BASE;
137
138public:
139 /**
140 * Creates a new list.
141 *
142 * This preallocates @a cCapacity elements within the list.
143 *
144 * @param cCapacity The initial capacity the list has.
145 * @throws std::bad_alloc
146 */
147 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
148 : BASE(cCapacity) {}
149
150 /* Define our own new and delete. */
151 RTMEMEF_NEW_AND_DELETE_OPERATORS();
152};
153
154/**
155 * Specialized thread-safe list class for using the native type list for
156 * signed 64-bit values even on a 32-bit host.
157 *
158 * @see RTCListBase
159 */
160template <>
161class RTCMTList<int64_t>: public RTCListBase<int64_t, int64_t, true>
162{
163 /* Traits */
164 typedef RTCListBase<int64_t, int64_t, true> BASE;
165
166public:
167 /**
168 * Creates a new list.
169 *
170 * This preallocates @a cCapacity elements within the list.
171 *
172 * @param cCapacity The initial capacity the list has.
173 * @throws std::bad_alloc
174 */
175 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
176 : BASE(cCapacity) {}
177
178 /* Define our own new and delete. */
179 RTMEMEF_NEW_AND_DELETE_OPERATORS();
180};
181
182/** @} */
183
184#endif /* !IPRT_INCLUDED_cpp_mtlist_h */
185
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette