VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/nocrt-per-thread-2.cpp@ 104635

Last change on this file since 104635 was 98103, checked in by vboxsync, 22 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: 8.2 KB
Line 
1/* $Id: nocrt-per-thread-2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - No-Crt - Per Thread Data, Managment code
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41#include "internal/nocrt.h"
42#include <iprt/asm.h>
43#include <iprt/critsect.h>
44#include <iprt/errcore.h>
45#include <iprt/mem.h>
46#include <iprt/once.h>
47#include <iprt/thread.h>
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53/** Init/term once state. */
54static RTONCE g_NoCrtPerThreadOnce = RTONCE_INITIALIZER;
55
56/** List of heap allocations (PRTNOCRTTHREADDATA). */
57static RTLISTANCHOR g_NoCrtPerThreadHeapList;
58/** Critical section protecting g_NoCrtPerThreadHeapList. */
59static RTCRITSECT g_NoCrtPerThreadCritSect;
60
61/** Allocation bitmap for g_aNoCrtPerThreadStatic.
62 *
63 * In debug builds we only have one slot here, so we have a better chance of
64 * testing the heap code path. */
65#ifdef DEBUG
66static uint32_t volatile g_fNoCrtPerThreadStaticAlloc = UINT32_C(0xffffefff);
67#else
68static uint32_t volatile g_fNoCrtPerThreadStaticAlloc = 0;
69#endif
70/* Static allocations to avoid the heap and associate slowness. */
71static RTNOCRTTHREADDATA g_aNoCrtPerThreadStatic[32];
72
73
74/**
75 * @callback_method_impl{FNRTTLSDTOR}
76 */
77static DECLCALLBACK(void) rtNoCrtPerThreadDtor(void *pvValue)
78{
79 PRTNOCRTTHREADDATA pNoCrtData = (PRTNOCRTTHREADDATA)pvValue;
80 if (pNoCrtData->enmAllocType == RTNOCRTTHREADDATA::kAllocType_Heap)
81 {
82 AssertReturnVoid(RTOnceWasInitialized(&g_NoCrtPerThreadOnce));
83
84 RTCritSectEnter(&g_NoCrtPerThreadCritSect); /* timeout? */
85
86 RTListNodeRemove(&pNoCrtData->ListEntry);
87 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_End;
88
89 RTCritSectLeave(&g_NoCrtPerThreadCritSect);
90
91 RTMemFree(pNoCrtData);
92 }
93 else if (pNoCrtData->enmAllocType == RTNOCRTTHREADDATA::kAllocType_Static)
94 {
95 size_t iSlot = (size_t)(pNoCrtData - &g_aNoCrtPerThreadStatic[0]);
96 AssertReturnVoid(iSlot < RT_ELEMENTS(g_aNoCrtPerThreadStatic));
97
98 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_Invalid;
99 ASMAtomicAndU32(&g_fNoCrtPerThreadStaticAlloc, ~(uint32_t)iSlot);
100 }
101}
102
103
104/**
105 * @callback_method_impl{FNRTONCE}
106 */
107static DECLCALLBACK(int32_t) rtNoCrtPerThreadInit(void *pvUser)
108{
109 RTListInit(&g_NoCrtPerThreadHeapList);
110
111 RTTLS iTls = NIL_RTTLS;
112 int rc = RTTlsAllocEx(&iTls, rtNoCrtPerThreadDtor);
113 if (iTls != NIL_RTTLS)
114 {
115 rc = RTCritSectInit(&g_NoCrtPerThreadCritSect);
116 if (RT_SUCCESS(rc))
117 {
118 g_iTlsRtNoCrtPerThread = iTls;
119 return VINF_SUCCESS;
120 }
121 RTTlsFree(iTls);
122 }
123 RT_NOREF(pvUser);
124 return rc;
125}
126
127
128/**
129 * @callback_method_impl{FNRTONCECLEANUP}
130 */
131static DECLCALLBACK(void) rtNoCrtPerThreadCleanup(void *pvUser, bool fLazyCleanUpOk)
132{
133 RT_NOREF(pvUser);
134 if (fLazyCleanUpOk)
135 return;
136
137 /*
138 * First destroy the TLS entry.
139 */
140 RTTLS iTls = g_iTlsRtNoCrtPerThread;
141 g_iTlsRtNoCrtPerThread = NIL_RTTLS;
142 int rc = RTTlsFree(iTls);
143 AssertRC(rc);
144
145 /*
146 * Then destroy the critical section and free all entries in the list.
147 */
148 RTCritSectDelete(&g_NoCrtPerThreadCritSect);
149
150 PRTNOCRTTHREADDATA pNoCrtData;
151 while ((pNoCrtData = RTListRemoveFirst(&g_NoCrtPerThreadHeapList, RTNOCRTTHREADDATA, ListEntry)) != NULL)
152 {
153 AssertContinue(pNoCrtData->enmAllocType == RTNOCRTTHREADDATA::kAllocType_Heap);
154 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_End;
155 RTMemFree(pNoCrtData);
156 }
157}
158
159PRTNOCRTTHREADDATA rtNoCrtThreadDataGet(void)
160{
161 int rc = RTOnceEx(&g_NoCrtPerThreadOnce, rtNoCrtPerThreadInit, rtNoCrtPerThreadCleanup, NULL);
162 if (RT_SUCCESS(rc))
163 {
164 /*
165 * We typically have an entry already.
166 */
167 PRTNOCRTTHREADDATA pNoCrtData = (PRTNOCRTTHREADDATA)RTTlsGet(g_iTlsRtNoCrtPerThread);
168 if (pNoCrtData)
169 {
170 AssertReturn( pNoCrtData->enmAllocType > RTNOCRTTHREADDATA::kAllocType_Invalid
171 && pNoCrtData->enmAllocType < RTNOCRTTHREADDATA::kAllocType_End,
172 NULL);
173 return pNoCrtData;
174 }
175
176 /*
177 * Okay, allocate a new entry first using some of the statically allocated
178 * ones then falling back on heap allocations.
179 */
180 for (;;)
181 {
182 uint32_t const fAlloc = ASMAtomicUoReadU32(&g_fNoCrtPerThreadStaticAlloc);
183 uint32_t iSlot = ASMBitFirstSetU32(~fAlloc);
184 if (iSlot != 0)
185 iSlot--;
186 else
187 break;
188 if (ASMAtomicCmpXchgU32(&g_fNoCrtPerThreadStaticAlloc, fAlloc | RT_BIT_32(iSlot), fAlloc))
189 {
190 pNoCrtData = &g_aNoCrtPerThreadStatic[iSlot];
191
192 /* Init the entry in case it's being re-used: */
193 Assert(pNoCrtData->enmAllocType == RTNOCRTTHREADDATA::kAllocType_Invalid);
194 rc = RTTlsSet(g_iTlsRtNoCrtPerThread, pNoCrtData);
195 AssertRC(rc);
196 if (RT_SUCCESS(rc))
197 {
198 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_Static;
199 RTListInit(&pNoCrtData->ListEntry);
200 pNoCrtData->iErrno = 0;
201 pNoCrtData->pszStrToken = NULL;
202 return pNoCrtData;
203 }
204
205 ASMAtomicOrU32(&g_fNoCrtPerThreadStaticAlloc, RT_BIT_32(iSlot));
206 return NULL;
207 }
208 ASMNopPause();
209 }
210
211 /*
212 * Heap.
213 */
214 pNoCrtData = (PRTNOCRTTHREADDATA)RTMemAllocZ(sizeof(*pNoCrtData));
215 if (pNoCrtData)
216 {
217 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_Heap;
218
219 RTCritSectEnter(&g_NoCrtPerThreadCritSect);
220 RTListAppend(&g_NoCrtPerThreadHeapList, &pNoCrtData->ListEntry);
221 RTCritSectLeave(&g_NoCrtPerThreadCritSect);
222
223 rc = RTTlsSet(g_iTlsRtNoCrtPerThread, pNoCrtData);
224 AssertRC(rc);
225 if (RT_SUCCESS(rc))
226 return pNoCrtData;
227
228 RTCritSectEnter(&g_NoCrtPerThreadCritSect);
229 RTListNodeRemove(&pNoCrtData->ListEntry);
230 RTCritSectLeave(&g_NoCrtPerThreadCritSect);
231
232 pNoCrtData->enmAllocType = RTNOCRTTHREADDATA::kAllocType_End;
233 RTMemFree(pNoCrtData);
234 }
235 }
236 return NULL;
237}
238
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