1 | /* $Id: tls-generic.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Thread Local Storage (TSL), Generic Implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_THREAD
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include "internal/iprt.h"
|
---|
34 |
|
---|
35 | #include <iprt/errcore.h>
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/log.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include "internal/thread.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Global Variables *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /** Allocation bitmap. Set bits indicates allocated entries. */
|
---|
46 | static uint32_t volatile g_au32AllocatedBitmap[(RTTHREAD_TLS_ENTRIES + 31) / 32];
|
---|
47 | /** Destructors for each of the TLS entries. */
|
---|
48 | static PFNRTTLSDTOR g_apfnDestructors[RTTHREAD_TLS_ENTRIES];
|
---|
49 |
|
---|
50 |
|
---|
51 | RTR3DECL(RTTLS) RTTlsAlloc(void)
|
---|
52 | {
|
---|
53 | RTTLS iTls;
|
---|
54 | int rc = RTTlsAllocEx(&iTls, NULL);
|
---|
55 | return RT_SUCCESS(rc) ? iTls : NIL_RTTLS;
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor)
|
---|
60 | {
|
---|
61 | for (unsigned i = 0; i < 128; i++)
|
---|
62 | {
|
---|
63 | int iTls = ASMBitFirstClear(&g_au32AllocatedBitmap[0], RTTHREAD_TLS_ENTRIES);
|
---|
64 | if (iTls < 0)
|
---|
65 | {
|
---|
66 | *piTls = NIL_RTTLS;
|
---|
67 | return VERR_NO_MEMORY;
|
---|
68 | }
|
---|
69 | if (!ASMAtomicBitTestAndSet(&g_au32AllocatedBitmap[0], iTls))
|
---|
70 | {
|
---|
71 | g_apfnDestructors[iTls] = pfnDestructor;
|
---|
72 | *piTls = iTls;
|
---|
73 | return VINF_SUCCESS;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | AssertFailed();
|
---|
78 | return VERR_NO_MEMORY;
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | RTR3DECL(int) RTTlsFree(RTTLS iTls)
|
---|
83 | {
|
---|
84 | if (iTls == NIL_RTTLS)
|
---|
85 | return VINF_SUCCESS;
|
---|
86 | if ( iTls < 0
|
---|
87 | || iTls >= RTTHREAD_TLS_ENTRIES
|
---|
88 | || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls))
|
---|
89 | return VERR_INVALID_PARAMETER;
|
---|
90 |
|
---|
91 | ASMAtomicWriteNullPtr(&g_apfnDestructors[iTls]);
|
---|
92 | rtThreadClearTlsEntry(iTls);
|
---|
93 | ASMAtomicBitClear(&g_au32AllocatedBitmap[0], iTls);
|
---|
94 | return VINF_SUCCESS;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | RTR3DECL(void *) RTTlsGet(RTTLS iTls)
|
---|
99 | {
|
---|
100 | void *pv;
|
---|
101 | int rc = RTTlsGetEx(iTls, &pv);
|
---|
102 | return RT_SUCCESS(rc) ? pv : NULL;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue)
|
---|
107 | {
|
---|
108 | if (RT_UNLIKELY( iTls < 0
|
---|
109 | || iTls >= RTTHREAD_TLS_ENTRIES
|
---|
110 | || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls)))
|
---|
111 | return VERR_INVALID_PARAMETER;
|
---|
112 |
|
---|
113 | PRTTHREADINT pThread = rtThreadGet(RTThreadSelf());
|
---|
114 | AssertReturn(pThread, VERR_NOT_SUPPORTED);
|
---|
115 | void *pv = pThread->apvTlsEntries[iTls];
|
---|
116 | rtThreadRelease(pThread);
|
---|
117 | *ppvValue = pv;
|
---|
118 | return VINF_SUCCESS;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue)
|
---|
123 | {
|
---|
124 | if (RT_UNLIKELY( iTls < 0
|
---|
125 | || iTls >= RTTHREAD_TLS_ENTRIES
|
---|
126 | || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls)))
|
---|
127 | return VERR_INVALID_PARAMETER;
|
---|
128 |
|
---|
129 | PRTTHREADINT pThread = rtThreadGet(RTThreadSelf());
|
---|
130 | AssertReturn(pThread, VERR_NOT_SUPPORTED);
|
---|
131 | pThread->apvTlsEntries[iTls] = pvValue;
|
---|
132 | rtThreadRelease(pThread);
|
---|
133 | return VINF_SUCCESS;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Called at thread termination to invoke TLS destructors.
|
---|
140 | *
|
---|
141 | * @param pThread The current thread.
|
---|
142 | */
|
---|
143 | DECLHIDDEN(void) rtThreadTlsDestruction(PRTTHREADINT pThread)
|
---|
144 | {
|
---|
145 | for (RTTLS iTls = 0; iTls < RTTHREAD_TLS_ENTRIES; iTls++)
|
---|
146 | {
|
---|
147 | void *pv = pThread->apvTlsEntries[iTls];
|
---|
148 | if (pv)
|
---|
149 | {
|
---|
150 | PFNRTTLSDTOR pfnDestructor = (PFNRTTLSDTOR)(uintptr_t)ASMAtomicUoReadPtr((void * volatile *)(uintptr_t)&g_apfnDestructors[iTls]);
|
---|
151 | if (pfnDestructor)
|
---|
152 | {
|
---|
153 | pThread->apvTlsEntries[iTls] = NULL;
|
---|
154 | pfnDestructor(pv);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|