[1] | 1 | /* $Id: tls-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Thread Local Storage (TLS), Win32.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
[62592] | 42 | #include <iprt/win/windows.h>
|
---|
[1] | 43 |
|
---|
| 44 | #include <iprt/thread.h>
|
---|
| 45 | #include <iprt/assert.h>
|
---|
[83101] | 46 | #include <iprt/critsect.h>
|
---|
[76452] | 47 | #include <iprt/errcore.h>
|
---|
[83101] | 48 | #include <iprt/log.h>
|
---|
| 49 | #include <iprt/mem.h>
|
---|
| 50 | #include <iprt/once.h>
|
---|
[1] | 51 | #include "internal/thread.h"
|
---|
| 52 |
|
---|
| 53 |
|
---|
[83101] | 54 | /*********************************************************************************************************************************
|
---|
| 55 | * Structures and Typedefs *
|
---|
| 56 | *********************************************************************************************************************************/
|
---|
| 57 | typedef struct RTTLSWINDTOR
|
---|
| 58 | {
|
---|
| 59 | RTLISTNODE ListEntry;
|
---|
| 60 | DWORD iTls;
|
---|
| 61 | PFNRTTLSDTOR pfnDestructor;
|
---|
| 62 | } RTTLSWINDTOR;
|
---|
| 63 | typedef RTTLSWINDTOR *PRTTLSWINDTOR;
|
---|
[1] | 64 |
|
---|
| 65 |
|
---|
[83101] | 66 | /*********************************************************************************************************************************
|
---|
| 67 | * Global Variables *
|
---|
| 68 | *********************************************************************************************************************************/
|
---|
| 69 | /** Init once for the list and critical section. */
|
---|
| 70 | static RTONCE g_Once = RTONCE_INITIALIZER;
|
---|
| 71 | /** Critical section protecting the TLS destructor list. */
|
---|
| 72 | static RTCRITSECTRW g_CritSect;
|
---|
| 73 | /** List of TLS destrictors (RTTLSWINDTOR). */
|
---|
| 74 | static RTLISTANCHOR g_TlsDtorHead;
|
---|
| 75 | /** Number of desturctors in the list (helps putting of initialization). */
|
---|
| 76 | static uint32_t volatile g_cTlsDtors = 0;
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @callback_method_impl{FNRTONCE}
|
---|
| 81 | */
|
---|
| 82 | static DECLCALLBACK(int32_t) rtTlsWinInitLock(void *pvUser)
|
---|
| 83 | {
|
---|
| 84 | RT_NOREF(pvUser);
|
---|
| 85 | RTListInit(&g_TlsDtorHead);
|
---|
| 86 | return RTCritSectRwInit(&g_CritSect);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[6957] | 90 | RTR3DECL(RTTLS) RTTlsAlloc(void)
|
---|
[1] | 91 | {
|
---|
[83101] | 92 | AssertCompile(sizeof(RTTLS) >= sizeof(DWORD));
|
---|
[6957] | 93 | DWORD iTls = TlsAlloc();
|
---|
| 94 | return iTls != TLS_OUT_OF_INDEXES ? (RTTLS)iTls : NIL_RTTLS;
|
---|
[1] | 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
[6957] | 98 | RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor)
|
---|
[1] | 99 | {
|
---|
[83101] | 100 | int rc;
|
---|
| 101 | if (!pfnDestructor)
|
---|
[1] | 102 | {
|
---|
[83101] | 103 | DWORD iTls = TlsAlloc();
|
---|
| 104 | if (iTls != TLS_OUT_OF_INDEXES)
|
---|
| 105 | {
|
---|
| 106 | Assert((RTTLS)iTls != NIL_RTTLS);
|
---|
| 107 | *piTls = (RTTLS)iTls;
|
---|
| 108 | Assert((DWORD)*piTls == iTls);
|
---|
| 109 | rc = VINF_SUCCESS;
|
---|
| 110 | }
|
---|
| 111 | else
|
---|
| 112 | rc = VERR_NO_MEMORY;
|
---|
[1] | 113 | }
|
---|
[83101] | 114 | else
|
---|
| 115 | {
|
---|
| 116 | rc = RTOnce(&g_Once, rtTlsWinInitLock, NULL);
|
---|
| 117 | if (RT_SUCCESS(rc))
|
---|
| 118 | {
|
---|
| 119 | PRTTLSWINDTOR pDtor = (PRTTLSWINDTOR)RTMemAlloc(sizeof(*pDtor));
|
---|
| 120 | if (pDtor)
|
---|
| 121 | {
|
---|
| 122 | DWORD iTls = TlsAlloc();
|
---|
| 123 | if (iTls != TLS_OUT_OF_INDEXES)
|
---|
| 124 | {
|
---|
| 125 | Assert((RTTLS)iTls != NIL_RTTLS);
|
---|
| 126 | *piTls = (RTTLS)iTls;
|
---|
| 127 | Assert((DWORD)*piTls == iTls);
|
---|
[1] | 128 |
|
---|
[83101] | 129 | /*
|
---|
| 130 | * Add the destructor to the list. We keep it sorted.
|
---|
| 131 | */
|
---|
| 132 | pDtor->iTls = iTls;
|
---|
| 133 | pDtor->pfnDestructor = pfnDestructor;
|
---|
| 134 | RTCritSectRwEnterExcl(&g_CritSect);
|
---|
| 135 | RTListAppend(&g_TlsDtorHead, &pDtor->ListEntry);
|
---|
| 136 | ASMAtomicIncU32(&g_cTlsDtors);
|
---|
| 137 | RTCritSectRwLeaveExcl(&g_CritSect);
|
---|
| 138 |
|
---|
| 139 | rc = VINF_SUCCESS;
|
---|
| 140 | }
|
---|
| 141 | else
|
---|
| 142 | rc = VERR_NO_MEMORY;
|
---|
| 143 | }
|
---|
| 144 | else
|
---|
| 145 | rc = VERR_NO_MEMORY;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | return rc;
|
---|
[1] | 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
[6957] | 152 | RTR3DECL(int) RTTlsFree(RTTLS iTls)
|
---|
[1] | 153 | {
|
---|
[6957] | 154 | if (iTls == NIL_RTTLS)
|
---|
[1] | 155 | return VINF_SUCCESS;
|
---|
[34507] | 156 | if (TlsFree((DWORD)iTls))
|
---|
[83101] | 157 | {
|
---|
| 158 | if (ASMAtomicReadU32(&g_cTlsDtors) > 0)
|
---|
| 159 | {
|
---|
| 160 | RTCritSectRwEnterExcl(&g_CritSect);
|
---|
| 161 | PRTTLSWINDTOR pDtor;
|
---|
| 162 | RTListForEach(&g_TlsDtorHead, pDtor, RTTLSWINDTOR, ListEntry)
|
---|
| 163 | {
|
---|
| 164 | if (pDtor->iTls == (DWORD)iTls)
|
---|
| 165 | {
|
---|
| 166 | RTListNodeRemove(&pDtor->ListEntry);
|
---|
| 167 | ASMAtomicDecU32(&g_cTlsDtors);
|
---|
| 168 | RTMemFree(pDtor);
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | RTCritSectRwLeaveExcl(&g_CritSect);
|
---|
| 173 | }
|
---|
[6957] | 174 | return VINF_SUCCESS;
|
---|
[83101] | 175 | }
|
---|
[6957] | 176 | return RTErrConvertFromWin32(GetLastError());
|
---|
[1] | 177 | }
|
---|
| 178 |
|
---|
| 179 |
|
---|
[6957] | 180 | RTR3DECL(void *) RTTlsGet(RTTLS iTls)
|
---|
[1] | 181 | {
|
---|
[34507] | 182 | return TlsGetValue((DWORD)iTls);
|
---|
[1] | 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
[6957] | 186 | RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue)
|
---|
[1] | 187 | {
|
---|
[34507] | 188 | void *pv = TlsGetValue((DWORD)iTls);
|
---|
[6957] | 189 | if (pv)
|
---|
[1] | 190 | {
|
---|
[6957] | 191 | *ppvValue = pv;
|
---|
| 192 | return VINF_SUCCESS;
|
---|
[1] | 193 | }
|
---|
[6957] | 194 |
|
---|
| 195 | /* TlsGetValue always updates last error */
|
---|
| 196 | *ppvValue = NULL;
|
---|
| 197 | return RTErrConvertFromWin32(GetLastError());
|
---|
[1] | 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
[6957] | 201 | RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue)
|
---|
[1] | 202 | {
|
---|
[34507] | 203 | if (TlsSetValue((DWORD)iTls, pvValue))
|
---|
[1] | 204 | return VINF_SUCCESS;
|
---|
[6957] | 205 | return RTErrConvertFromWin32(GetLastError());
|
---|
[1] | 206 | }
|
---|
| 207 |
|
---|
[83101] | 208 |
|
---|
| 209 | /**
|
---|
| 210 | * Called by dllmain-win.cpp when a thread detaches.
|
---|
| 211 | */
|
---|
[83124] | 212 | DECLHIDDEN(void) rtThreadWinTlsDestruction(void)
|
---|
[83101] | 213 | {
|
---|
| 214 | if (ASMAtomicReadU32(&g_cTlsDtors) > 0)
|
---|
| 215 | {
|
---|
| 216 | RTCritSectRwEnterShared(&g_CritSect);
|
---|
| 217 | PRTTLSWINDTOR pDtor;
|
---|
| 218 | RTListForEach(&g_TlsDtorHead, pDtor, RTTLSWINDTOR, ListEntry)
|
---|
| 219 | {
|
---|
| 220 | void *pvValue = TlsGetValue(pDtor->iTls);
|
---|
| 221 | if (pvValue != NULL)
|
---|
| 222 | {
|
---|
| 223 | pDtor->pfnDestructor(pvValue);
|
---|
| 224 | TlsSetValue(pDtor->iTls, NULL);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | RTCritSectRwLeaveShared(&g_CritSect);
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|