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
13 /* INCLUDES ******************************************************************/
19 /* FUNCTIONS *****************************************************************/
32 /* Try to get regular TEB slot. */
33 Index
= RtlFindClearBitsAndSet(NtCurrentPeb()->TlsBitmap
, 1, 0);
36 /* If it fails, try to find expansion TEB slot. */
37 Index
= RtlFindClearBitsAndSet(NtCurrentPeb()->TlsExpansionBitmap
, 1, 0);
40 if (NtCurrentTeb()->TlsExpansionSlots
== NULL
)
42 NtCurrentTeb()->TlsExpansionSlots
= HeapAlloc(GetProcessHeap(),
48 if (NtCurrentTeb()->TlsExpansionSlots
== NULL
)
50 RtlClearBits(NtCurrentPeb()->TlsExpansionBitmap
, Index
, 1);
52 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
56 /* Clear the value. */
57 NtCurrentTeb()->TlsExpansionSlots
[Index
] = 0;
58 Index
+= TLS_MINIMUM_AVAILABLE
;
63 SetLastError(ERROR_NO_MORE_ITEMS
);
68 /* Clear the value. */
69 NtCurrentTeb()->TlsSlots
[Index
] = 0;
86 if (Index
>= TLS_EXPANSION_SLOTS
+ TLS_MINIMUM_AVAILABLE
)
88 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
94 if (Index
>= TLS_MINIMUM_AVAILABLE
)
96 BitSet
= RtlAreBitsSet(NtCurrentPeb()->TlsExpansionBitmap
,
97 Index
- TLS_MINIMUM_AVAILABLE
,
101 RtlClearBits(NtCurrentPeb()->TlsExpansionBitmap
,
102 Index
- TLS_MINIMUM_AVAILABLE
,
107 BitSet
= RtlAreBitsSet(NtCurrentPeb()->TlsBitmap
, Index
, 1);
109 RtlClearBits(NtCurrentPeb()->TlsBitmap
, Index
, 1);
114 /* Clear the TLS cells (slots) in all threads of the current process. */
115 NtSetInformationThread(NtCurrentThread(),
122 SetLastError(ERROR_INVALID_PARAMETER
);
135 TlsGetValue(DWORD Index
)
137 if (Index
>= TLS_EXPANSION_SLOTS
+ TLS_MINIMUM_AVAILABLE
)
139 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
143 SetLastError(NO_ERROR
);
145 if (Index
>= TLS_MINIMUM_AVAILABLE
)
147 /* The expansion slots are allocated on demand, so check for it. */
148 if (NtCurrentTeb()->TlsExpansionSlots
== NULL
)
150 return NtCurrentTeb()->TlsExpansionSlots
[Index
- TLS_MINIMUM_AVAILABLE
];
154 return NtCurrentTeb()->TlsSlots
[Index
];
163 TlsSetValue(DWORD Index
,
166 if (Index
>= TLS_EXPANSION_SLOTS
+ TLS_MINIMUM_AVAILABLE
)
168 SetLastErrorByStatus(STATUS_INVALID_PARAMETER
);
172 if (Index
>= TLS_MINIMUM_AVAILABLE
)
174 if (NtCurrentTeb()->TlsExpansionSlots
== NULL
)
176 NtCurrentTeb()->TlsExpansionSlots
= HeapAlloc(GetProcessHeap(),
178 TLS_EXPANSION_SLOTS
*
181 if (NtCurrentTeb()->TlsExpansionSlots
== NULL
)
183 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
188 NtCurrentTeb()->TlsExpansionSlots
[Index
- TLS_MINIMUM_AVAILABLE
] = Value
;
192 NtCurrentTeb()->TlsSlots
[Index
] = Value
;