VirtualBox

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

Last change on this file since 70092 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/** @file
2 * IPRT - Generic thread-safe list Class.
3 */
4
5/*
6 * Copyright (C) 2011-2017 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_cpp_mtlist_h
27#define ___iprt_cpp_mtlist_h
28
29#include <iprt/cpp/list.h>
30#include <iprt/semaphore.h>
31
32/** @addtogroup grp_rt_cpp_list
33 * @{
34 */
35
36/**
37 * A guard class for thread-safe read/write access.
38 */
39template <>
40class RTCListGuard<true>
41{
42public:
43 RTCListGuard() : m_hRWSem(NIL_RTSEMRW)
44 {
45#if defined(RT_LOCK_STRICT_ORDER) && defined(IN_RING3)
46 RTLOCKVALCLASS hClass;
47 int rc = RTLockValidatorClassCreate(&hClass, true /*fAutodidact*/, RT_SRC_POS, "RTCListGuard");
48 AssertStmt(RT_SUCCESS(rc), hClass = NIL_RTLOCKVALCLASS);
49 rc = RTSemRWCreateEx(&m_hRWSem, 0 /*fFlags*/, hClass, RTLOCKVAL_SUB_CLASS_NONE, NULL /*pszNameFmt*/);
50 AssertRC(rc);
51#else
52 int rc = RTSemRWCreateEx(&m_hRWSem, 0 /*fFlags*/, NIL_RTLOCKVALCLASS, 0, NULL);
53 AssertRC(rc);
54#endif
55 }
56
57 ~RTCListGuard()
58 {
59 RTSemRWDestroy(m_hRWSem);
60 m_hRWSem = NIL_RTSEMRW;
61 }
62
63 inline void enterRead() const { int rc = RTSemRWRequestRead(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
64 inline void leaveRead() const { int rc = RTSemRWReleaseRead(m_hRWSem); AssertRC(rc); }
65 inline void enterWrite() { int rc = RTSemRWRequestWrite(m_hRWSem, RT_INDEFINITE_WAIT); AssertRC(rc); }
66 inline void leaveWrite() { int rc = RTSemRWReleaseWrite(m_hRWSem); AssertRC(rc); }
67
68 /* Define our own new and delete. */
69 RTMEMEF_NEW_AND_DELETE_OPERATORS();
70
71private:
72 mutable RTSEMRW m_hRWSem;
73};
74
75/**
76 * @brief Generic thread-safe list class.
77 *
78 * RTCMTList is a thread-safe implementation of the list class. It uses a
79 * read/write semaphore to serialize the access to the items. Several readers
80 * can simultaneous access different or the same item. If one thread is writing
81 * to an item, the other accessors are blocked until the write has finished.
82 *
83 * Although the access is guarded, the user has to make sure the list content
84 * is consistent when iterating over the list or doing any other kind of access
85 * which makes assumptions about the list content. For a finer control of access
86 * restrictions, use your own locking mechanism and the standard list
87 * implementation.
88 *
89 * @see RTCListBase
90 */
91template <class T, typename ITYPE = typename RTCIf<(sizeof(T) > sizeof(void*)), T*, T>::result>
92class RTCMTList : public RTCListBase<T, ITYPE, true>
93{
94 /* Traits */
95 typedef RTCListBase<T, ITYPE, true> BASE;
96
97public:
98 /**
99 * Creates a new list.
100 *
101 * This preallocates @a cCapacity elements within the list.
102 *
103 * @param cCapacity The initial capacity the list has.
104 * @throws std::bad_alloc
105 */
106 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
107 : BASE(cCapacity) {}
108
109 /* Define our own new and delete. */
110 RTMEMEF_NEW_AND_DELETE_OPERATORS();
111};
112
113/**
114 * Specialized thread-safe list class for using the native type list for
115 * unsigned 64-bit values even on a 32-bit host.
116 *
117 * @see RTCListBase
118 */
119template <>
120class RTCMTList<uint64_t>: public RTCListBase<uint64_t, uint64_t, true>
121{
122 /* Traits */
123 typedef RTCListBase<uint64_t, uint64_t, true> BASE;
124
125public:
126 /**
127 * Creates a new list.
128 *
129 * This preallocates @a cCapacity elements within the list.
130 *
131 * @param cCapacity The initial capacity the list has.
132 * @throws std::bad_alloc
133 */
134 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
135 : BASE(cCapacity) {}
136
137 /* Define our own new and delete. */
138 RTMEMEF_NEW_AND_DELETE_OPERATORS();
139};
140
141/**
142 * Specialized thread-safe list class for using the native type list for
143 * signed 64-bit values even on a 32-bit host.
144 *
145 * @see RTCListBase
146 */
147template <>
148class RTCMTList<int64_t>: public RTCListBase<int64_t, int64_t, true>
149{
150 /* Traits */
151 typedef RTCListBase<int64_t, int64_t, true> BASE;
152
153public:
154 /**
155 * Creates a new list.
156 *
157 * This preallocates @a cCapacity elements within the list.
158 *
159 * @param cCapacity The initial capacity the list has.
160 * @throws std::bad_alloc
161 */
162 RTCMTList(size_t cCapacity = BASE::kDefaultCapacity)
163 : BASE(cCapacity) {}
164
165 /* Define our own new and delete. */
166 RTMEMEF_NEW_AND_DELETE_OPERATORS();
167};
168
169/** @} */
170
171#endif /* !___iprt_cpp_mtlist_h */
172
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