update for HEAD-2003050101
[reactos.git] / lib / ntdll / rtl / thread.c
1 /*
2  * COPYRIGHT:         See COPYING in the top level directory
3  * PROJECT:           ReactOS kernel
4  * PURPOSE:           Rtl user thread functions
5  * FILE:              lib/ntdll/rtl/thread.c
6  * PROGRAMER:         Eric Kohl
7  * REVISION HISTORY:
8  *                    09/07/99: Created
9  *                    09/10/99: Cleanup and full stack support.
10  *                    25/04/03: Near rewrite. Made code more readable, replaced
11  *                              INITIAL_TEB with USER_STACK, added support for
12  *                              fixed-size stacks
13  *                    28/04/03: Moved all code to a new statically linked
14  *                              library (ROSRTL) so it can be shared with
15  *                              kernel32.dll without exporting non-standard
16  *                              functions from ntdll.dll
17  */
18
19 /* INCLUDES *****************************************************************/
20
21 #include <ddk/ntddk.h>
22 #include <napi/teb.h>
23 #include <ntdll/rtl.h>
24 #include <rosrtl/thread.h>
25
26 #define NDEBUG
27 #include <ntdll/ntdll.h>
28
29 /* FUNCTIONS ***************************************************************/
30
31 NTSTATUS STDCALL RtlCreateUserThread
32 (
33  HANDLE ProcessHandle,
34  PSECURITY_DESCRIPTOR SecurityDescriptor,
35  BOOLEAN CreateSuspended,
36  LONG StackZeroBits,
37  PULONG StackReserve,
38  PULONG StackCommit,
39  PTHREAD_START_ROUTINE StartAddress,
40  PVOID Parameter,
41  PHANDLE ThreadHandle,
42  PCLIENT_ID ClientId
43 )
44 {
45  OBJECT_ATTRIBUTES oaThreadAttribs;
46  
47  InitializeObjectAttributes
48  (
49   &oaThreadAttribs,
50   NULL,
51   0,
52   NULL,
53   SecurityDescriptor
54  );
55  
56  return RtlRosCreateUserThreadEx
57  (
58   ProcessHandle,
59   &oaThreadAttribs,
60   CreateSuspended,
61   StackZeroBits,
62   StackReserve,
63   StackCommit,
64   StartAddress,
65   ThreadHandle,
66   ClientId,
67   1,
68   (ULONG_PTR *)&Parameter
69  );
70 }
71
72 NTSTATUS STDCALL RtlInitializeContext
73 (
74  HANDLE ProcessHandle,
75  PCONTEXT Context,
76  PVOID Parameter,
77  PTHREAD_START_ROUTINE StartAddress,
78  PUSER_STACK UserStack
79 )
80 {
81  return RtlRosInitializeContextEx
82  (
83   ProcessHandle,
84   Context,
85   StartAddress,
86   UserStack,
87   1,
88   (ULONG_PTR *)&Parameter
89  );
90 }
91
92 NTSTATUS STDCALL RtlFreeUserThreadStack
93 (
94  HANDLE ProcessHandle,
95  HANDLE ThreadHandle
96 )
97 {
98  THREAD_BASIC_INFORMATION tbiInfo;
99  NTSTATUS nErrCode;
100  ULONG nDummy;
101  ULONG nSize = 0;
102  PVOID pStackBase;
103  PTEB pTeb;
104
105  /* query basic information about the thread */
106  nErrCode = NtQueryInformationThread
107  (
108   ThreadHandle,
109   ThreadBasicInformation,
110   &tbiInfo,
111   sizeof(tbiInfo),
112   NULL
113  );
114
115  /* failure */
116  if(!NT_SUCCESS(nErrCode)) return nErrCode;
117  if(tbiInfo.TebBaseAddress == NULL) return STATUS_ACCESS_VIOLATION;
118
119  pTeb = (PTEB)tbiInfo.TebBaseAddress;
120
121  /* read the base address of the stack to be deallocated */
122  nErrCode = NtReadVirtualMemory
123  (
124   ProcessHandle,
125   &pTeb->DeallocationStack,
126   &pStackBase,
127   sizeof(pStackBase),
128   &nDummy
129  );
130
131  /* failure */
132  if(!NT_SUCCESS(nErrCode)) return nErrCode;
133  if(pStackBase == NULL) return STATUS_ACCESS_VIOLATION;
134
135  /* deallocate the stack */
136  nErrCode = NtFreeVirtualMemory(ProcessHandle, pStackBase, &nSize, MEM_RELEASE);
137
138  return nErrCode;
139 }
140
141 NTSTATUS STDCALL RtlExitUserThread(NTSTATUS nErrCode)
142 {
143  return NtTerminateThread(NtCurrentThread(), nErrCode);
144 }
145
146 /* EOF */