update for HEAD-2003050101
[reactos.git] / lib / kernel32 / thread / tls.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS system libraries
5  * FILE:            lib/kernel32/thread/tls.c
6  * PURPOSE:         Thread functions
7  * PROGRAMMER:      Ariadne ( ariadne@xs4all.nl)
8  *                  Tls functions are modified from WINE
9  * UPDATE HISTORY:
10  *                  Created 01/11/98
11  */
12
13 /* INCLUDES ******************************************************************/
14
15 #include <k32.h>
16
17 #define NDEBUG
18 #include <kernel32/kernel32.h>
19
20
21 /* FUNCTIONS *****************************************************************/
22
23 DWORD STDCALL 
24 TlsAlloc(VOID)
25 {
26    ULONG Index;
27
28    RtlAcquirePebLock();
29    Index = RtlFindClearBitsAndSet (NtCurrentPeb()->TlsBitmap, 1, 0);
30    if (Index == (ULONG)-1)
31      {
32        SetLastErrorByStatus(STATUS_NO_MEMORY);
33      }
34    else
35      {
36        NtCurrentTeb()->TlsSlots[Index] = 0;
37      }
38    RtlReleasePebLock();
39    
40    return(Index);
41 }
42
43 WINBOOL STDCALL 
44 TlsFree(DWORD dwTlsIndex)
45 {
46    if (dwTlsIndex >= TLS_MINIMUM_AVAILABLE)
47      {
48         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
49         return(FALSE);
50      }
51
52    RtlAcquirePebLock();
53    if (RtlAreBitsSet(NtCurrentPeb()->TlsBitmap, dwTlsIndex, 1))
54      {
55         /*
56          * clear the tls cells (slots) in all threads
57          * of the current process
58          */
59         NtSetInformationThread(NtCurrentThread(),
60                                ThreadZeroTlsCell,
61                                &dwTlsIndex,
62                                sizeof(DWORD));
63         RtlClearBits(NtCurrentPeb()->TlsBitmap,
64                      dwTlsIndex,
65                      1);
66      }
67    RtlReleasePebLock();
68
69    return(TRUE);
70 }
71
72 LPVOID STDCALL 
73 TlsGetValue(DWORD dwTlsIndex)
74 {
75    LPVOID Value;
76
77    if (dwTlsIndex >= TLS_MINIMUM_AVAILABLE)
78      {
79         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
80         return(NULL);
81      }
82
83    Value = NtCurrentTeb()->TlsSlots[dwTlsIndex];
84    if (Value == 0)
85    {
86       SetLastError(NO_ERROR);
87    }
88    return Value;
89 }
90
91 WINBOOL STDCALL 
92 TlsSetValue(DWORD dwTlsIndex, LPVOID lpTlsValue)
93 {
94    if (dwTlsIndex >= TLS_MINIMUM_AVAILABLE)
95      {
96         SetLastErrorByStatus(STATUS_INVALID_PARAMETER);
97         return(FALSE);
98      }
99    NtCurrentTeb()->TlsSlots[dwTlsIndex] = lpTlsValue;
100    return(TRUE);
101 }
102
103 /* EOF */