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