VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/tls-generic.cpp@ 7348

Last change on this file since 7348 was 6961, checked in by vboxsync, 17 years ago

Generic implementation (in case it's needed).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1/* $Id: tls-generic.cpp 6961 2008-02-14 17:39:02Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Thread Local Storage (TSL), Generic Implementation.
4 */
5
6/*
7 * Copyright (C) 2008 innotek GmbH
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
33#include <iprt/thread.h>
34#include <iprt/err.h>
35#include <iprt/asm.h>
36#include <iprt/log.h>
37#include <iprt/assert.h>
38#include "internal/thread.h"
39
40
41/*******************************************************************************
42* Global Variables *
43*******************************************************************************/
44/** Allocation bitmap. Set bits indicates allocated entries. */
45static uint32_t volatile g_au32AllocatedBitmap[(RTTHREAD_TLS_ENTRIES + 31) / 32];
46/** Destructors for each of the TLS entries. */
47static PFNRTTLSDTOR g_apfnDestructors[RTTHREAD_TLS_ENTRIES];
48
49
50RTR3DECL(RTTLS) RTTlsAlloc(void)
51{
52 RTTLS iTls;
53 int rc = RTTlsAllocEx(&iTls, NULL);
54 return RT_SUCCESS(rc) ? iTls : NIL_RTTLS;
55}
56
57
58RTR3DECL(int) RTTlsAllocEx(PRTTLS piTls, PFNRTTLSDTOR pfnDestructor)
59{
60 for (unsigned i = 0; i < 128; i++)
61 {
62 int iTls = ASMBitFirstClear(&g_au32AllocatedBitmap[0], RTTHREAD_TLS_ENTRIES);
63 if (iTls < 0)
64 {
65 *piTls = NIL_RTTLS;
66 return VERR_NO_MEMORY;
67 }
68 if (!ASMAtomicBitTestAndSet(&g_au32AllocatedBitmap[0], iTls))
69 {
70 g_apfnDestructors[iTls] = pfnDestructor;
71 *piTls = iTls;
72 return VINF_SUCCESS;
73 }
74 }
75
76 AssertFailed();
77 return VERR_NO_MEMORY;
78}
79
80
81RTR3DECL(int) RTTlsFree(RTTLS iTls)
82{
83 if (iTls == NIL_RTTLS)
84 return VINF_SUCCESS;
85 if ( iTls < 0
86 || iTls >= RTTHREAD_TLS_ENTRIES
87 || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls))
88 return VERR_INVALID_PARAMETER;
89
90 ASMAtomicWritePtr((void * volatile *)&g_apfnDestructors[iTls], NULL);
91 rtThreadClearTlsEntry(iTls);
92 ASMAtomicBitClear(&g_au32AllocatedBitmap[0], iTls);
93 return VINF_SUCCESS;
94}
95
96
97RTR3DECL(void *) RTTlsGet(RTTLS iTls)
98{
99 void *pv;
100 int rc = RTTlsGetEx(iTls, &pv);
101 return RT_SUCCESS(rc) ? pv : NULL;
102}
103
104
105RTR3DECL(int) RTTlsGetEx(RTTLS iTls, void **ppvValue)
106{
107 if (RT_UNLIKELY( iTls < 0
108 || iTls >= RTTHREAD_TLS_ENTRIES
109 || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls)))
110 return VERR_INVALID_PARAMETER;
111
112 PRTTHREADINT pThread = rtThreadGet(RTThreadSelf());
113 AssertReturn(pThread, VERR_NOT_SUPPORTED);
114 void *pv = pThread->apvTlsEntries[iTls];
115 rtThreadRelease(pThread);
116 *ppvValue = pv;
117 return VINF_SUCCESS;
118}
119
120
121RTR3DECL(int) RTTlsSet(RTTLS iTls, void *pvValue)
122{
123 if (RT_UNLIKELY( iTls < 0
124 || iTls >= RTTHREAD_TLS_ENTRIES
125 || !ASMBitTest(&g_au32AllocatedBitmap[0], iTls)))
126 return VERR_INVALID_PARAMETER;
127
128 PRTTHREADINT pThread = rtThreadGet(RTThreadSelf());
129 AssertReturn(pThread, VERR_NOT_SUPPORTED);
130 pThread->apvTlsEntries[iTls] = pvValue;
131 rtThreadRelease(pThread);
132 return VINF_SUCCESS;
133}
134
135
136
137/**
138 * Called at thread termination to invoke TLS destructors.
139 *
140 * @param pThread The current thread.
141 */
142void rtThreadTlsDestruction(PRTTHREADINT pThread)
143{
144 for (RTTLS iTls = 0; iTls < RTTHREAD_TLS_ENTRIES; iTls++)
145 {
146 void *pv = pThread->apvTlsEntries[iTls];
147 if (pv)
148 {
149 PFNRTTLSDTOR pfnDestructor = (PFNRTTLSDTOR)(uintptr_t)ASMAtomicUoReadPtr((void * volatile *)(uintptr_t)&g_apfnDestructors[iTls]);
150 if (pfnDestructor)
151 {
152 pThread->apvTlsEntries[iTls] = NULL;
153 pfnDestructor(pv);
154 }
155 }
156 }
157}
158
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