summaryrefslogtreecommitdiff
path: root/src/pal/src/thread/tls.cpp
blob: ef6d02f00ad30c4e5402d488dadb4a9b9fdce64c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*++



Module Name:

    tls.cpp

Abstract:

    Implementation of Thread local storage functions.



--*/

#include "pal/thread.hpp"
#include "procprivate.hpp"

#include <pthread.h>

#include "pal/dbgmsg.h"
#include "pal/misc.h"
#include "pal/virtual.h"
#include "pal/process.h"
#include "pal/init.h"
#include "pal/malloc.hpp"
#include "pal_endian.h"

#include <stddef.h>
using namespace CorUnix;

SET_DEFAULT_DEBUG_CHANNEL(THREAD);

// In safemath.h, Template SafeInt uses macro _ASSERTE, which need to use variable
// defdbgchan defined by SET_DEFAULT_DEBUG_CHANNEL. Therefore, the include statement
// should be placed after the SET_DEFAULT_DEBUG_CHANNEL(THREAD)
#include <safemath.h>

/* This tracks the slots that are used for TlsAlloc. Its size in bits
   must be the same as TLS_SLOT_SIZE in pal/thread.h. Since this is
   static, it is initialized to 0, which is what we want. */
static unsigned __int64 sTlsSlotFields;

/*++
Function:
  TlsAlloc

See MSDN doc.
--*/
DWORD
PALAPI
TlsAlloc(
    VOID)
{
    DWORD dwIndex;
    unsigned int i;

    PERF_ENTRY(TlsAlloc);
    ENTRY("TlsAlloc()\n");

    /* Yes, this could be ever so slightly improved. It's not
       likely to be called enough to matter, though, so we won't
       optimize here until or unless we need to. */
       
    PROCProcessLock();
    
    for(i = 0; i < sizeof(sTlsSlotFields) * 8; i++)
    {
        if ((sTlsSlotFields & ((unsigned __int64) 1 << i)) == 0)
        {
            sTlsSlotFields |= ((unsigned __int64) 1 << i);
            break;
        }
    }
    if (i == sizeof(sTlsSlotFields) * 8)
    {
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        dwIndex = TLS_OUT_OF_INDEXES;
    }
    else
    {
        dwIndex = i;
    }
    
    PROCProcessUnlock();

    LOGEXIT("TlsAlloc returns DWORD %u\n", dwIndex);
    PERF_EXIT(TlsAlloc);
    return dwIndex;
}


/*++
Function:
  TlsGetValue

See MSDN doc.
--*/
LPVOID
PALAPI
TlsGetValue(
        IN DWORD dwTlsIndex)
{
    CPalThread *pThread;
    
    PERF_ENTRY(TlsGetValue);
    ENTRY("TlsGetValue()\n");
    
    if (dwTlsIndex == (DWORD) -1 || dwTlsIndex >= TLS_SLOT_SIZE)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }

    pThread = InternalGetCurrentThread();

    /* From MSDN : "The TlsGetValue function calls SetLastError to clear a
       thread's last error when it succeeds." */
    pThread->SetLastError(NO_ERROR);
    
    LOGEXIT("TlsGetValue \n" );
    PERF_EXIT(TlsGetValue);
    
    return pThread->tlsInfo.tlsSlots[dwTlsIndex];
}


/*++
Function:
  TlsSetValue

See MSDN doc.
--*/
BOOL
PALAPI
TlsSetValue(
        IN DWORD dwTlsIndex,
        IN LPVOID lpTlsValue)
{
    CPalThread *pThread;
    BOOL bRet = FALSE;
    PERF_ENTRY(TlsSetValue);
    ENTRY("TlsSetValue(dwTlsIndex=%u, lpTlsValue=%p)\n", dwTlsIndex, lpTlsValue);

    if (dwTlsIndex == (DWORD) -1 || dwTlsIndex >= TLS_SLOT_SIZE)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        goto EXIT;
    }

    pThread = InternalGetCurrentThread();
    pThread->tlsInfo.tlsSlots[dwTlsIndex] = lpTlsValue;
    bRet = TRUE;
    
EXIT:
    LOGEXIT("TlsSetValue returns BOOL %d\n", bRet);
    PERF_EXIT(TlsSetValue);
    return bRet;
}


/*++
Function:
  TlsFree

See MSDN doc.
--*/
BOOL
PALAPI
TlsFree(
    IN DWORD dwTlsIndex)
{
    CPalThread *pThread;

    PERF_ENTRY(TlsFree);
    ENTRY("TlsFree(dwTlsIndex=%u)\n", dwTlsIndex);

    
    if (dwTlsIndex == (DWORD) -1 || dwTlsIndex >= TLS_SLOT_SIZE)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        LOGEXIT("TlsFree returns BOOL FALSE\n");
        PERF_EXIT(TlsFree);
        return FALSE;
    }

    PROCProcessLock();

    /* Reset all threads' values to zero for this index. */
    for(pThread = pGThreadList; 
        pThread != NULL; pThread = pThread->GetNext())
    {
        pThread->tlsInfo.tlsSlots[dwTlsIndex] = 0;
    }
    sTlsSlotFields &= ~((unsigned __int64) 1 << dwTlsIndex);
    
    PROCProcessUnlock();

    LOGEXIT("TlsFree returns BOOL TRUE\n");
    PERF_EXIT(TlsFree);
    return TRUE;
}

PAL_ERROR
CThreadTLSInfo::InitializePostCreate(
    CPalThread *pThread,
    SIZE_T threadId,
    DWORD dwLwpId
    )
{
    PAL_ERROR palError = NO_ERROR;
    
    if (pthread_setspecific(thObjKey, reinterpret_cast<void*>(pThread)))
    {
        ASSERT("Unable to set the thread object key's value\n");
        palError = ERROR_INTERNAL_ERROR;
    }

    return palError;
}