TLS calls (currently unused)
[reactos.git] / posix / lib / psxdll / misc / tls.c
1 /* $Id: tls.c,v 1.1 2002/05/17 02:07:14 hyperion Exp $
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS POSIX+ Subsystem
6 * FILE: subsys/psx/lib/psxdll/misc/tls.c
7 * PURPOSE: Thread local storage
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 30/04/2002: Created
11 */
12
13 #include <ddk/ntddk.h>
14 #include <ntdll/rtl.h>
15 #include <psx/tls.h>
16 #include <psx/errno.h>
17
18 __tls_index_t __tls_alloc()
19 {
20 ULONG nIndex;
21
22 RtlAcquirePebLock();
23
24 nIndex = RtlFindClearBitsAndSet(NtCurrentPeb()->TlsBitmap, 1, 0);
25
26 if (nIndex == (ULONG)-1)
27 errno = ENOMEM;
28 else
29 NtCurrentTeb()->TlsSlots[nIndex] = 0;
30
31 RtlReleasePebLock();
32
33 return(nIndex);
34
35 }
36
37 int __tls_free(__tls_index_t index)
38 {
39 if (index >= TLS_MINIMUM_AVAILABLE)
40 {
41 errno = ERANGE;
42 return (-1);
43 }
44
45 RtlAcquirePebLock();
46
47 if(RtlAreBitsSet(NtCurrentPeb()->TlsBitmap, index, 1))
48 {
49 NtSetInformationThread
50 (
51 NtCurrentThread(),
52 ThreadZeroTlsCell,
53 &index,
54 sizeof(DWORD)
55 );
56
57 RtlClearBits(NtCurrentPeb()->TlsBitmap, index, 1);
58 }
59
60 RtlReleasePebLock();
61
62 return (0);
63 }
64
65 void * __tls_get_val(__tls_index_t index)
66 {
67 if(index >= TLS_MINIMUM_AVAILABLE)
68 {
69 errno = ERANGE;
70 return (0);
71 }
72
73 return (NtCurrentTeb()->TlsSlots[index]);
74 }
75
76 int __tls_put_val(__tls_index_t index, void * data)
77 {
78 if(index >= TLS_MINIMUM_AVAILABLE)
79 {
80 errno = ERANGE;
81 return (-1);
82 }
83
84 NtCurrentTeb()->TlsSlots[index] = data;
85 return (0);
86 }
87
88 /* EOF */
89