4 * Copyright 1996 Alexandre Julliard
11 #include "selectors.h"
14 THDB *pCurrentThread = NULL;
17 /***********************************************************************
20 THDB *THREAD_Create( PDB32 *pdb, DWORD stack_size )
22 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
23 if (!thdb) return NULL;
24 thdb->header.type = K32OBJ_THREAD;
25 thdb->header.refcount = 1;
27 thdb->teb.except = (void *)-1;
28 thdb->teb.htask16 = 0; /* FIXME */
29 thdb->teb.stack_sel = 0; /* FIXME */
30 thdb->teb.self = &thdb->teb;
31 thdb->teb.tls_ptr = thdb->tls_array;
33 thdb->exit_code = 0x103; /* STILL_ACTIVE */
35 /* Allocate the stack */
37 if (!stack_size) stack_size = 1024 * 1024; /* default size = 1Mb */
38 thdb->stack_base = VirtualAlloc( NULL, stack_size, MEM_COMMIT,
39 PAGE_EXECUTE_READWRITE );
40 if (!thdb->stack_base) goto error;
41 /* Un-commit the first page (FIXME: should use PAGE_GUARD instead) */
42 VirtualFree( thdb->stack_base, 1, MEM_DECOMMIT );
43 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
44 thdb->teb.stack_low = thdb->teb.stack_top;
45 thdb->exit_stack = thdb->teb.stack_top;
47 /* Allocate the TEB selector (%fs register) */
49 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
51 if (!thdb->teb_sel) goto error;
55 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
56 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
57 HeapFree( SystemHeap, 0, thdb );
62 /***********************************************************************
65 void THREAD_Destroy( K32OBJ *ptr )
67 THDB *thdb = (THDB *)ptr;
68 assert( ptr->type == K32OBJ_THREAD );
69 ptr->type = K32OBJ_UNKNOWN;
70 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
71 HeapFree( SystemHeap, 0, thdb );
75 /***********************************************************************
76 * GetCurrentThread (KERNEL32.200)
78 HANDLE32 GetCurrentThread(void)
84 /***********************************************************************
85 * GetCurrentThreadId (KERNEL32.201)
86 * Returns crypted (xor'ed) pointer to THDB in Win95.
88 DWORD GetCurrentThreadId(void)
90 /* FIXME: should probably use %fs register here */
91 assert( pCurrentThread );
92 return (DWORD)pCurrentThread;
96 /**********************************************************************
97 * GetLastError (KERNEL.148) (KERNEL32.227)
99 DWORD GetLastError(void)
101 THDB *thread = (THDB *)GetCurrentThreadId();
102 return thread->last_error;
106 /**********************************************************************
107 * SetLastError (KERNEL.147) (KERNEL32.497)
109 void SetLastError( DWORD error )
112 if (!pCurrentThread) return; /* FIXME */
113 thread = (THDB *)GetCurrentThreadId();
114 thread->last_error = error;
118 /**********************************************************************
119 * SetLastErrorEx (USER32.484)
121 void SetLastErrorEx( DWORD error, DWORD type )
123 /* FIXME: what about 'type'? */
124 SetLastError( error );
128 /**********************************************************************
129 * TlsAlloc (KERNEL32.530)
133 DWORD i, mask, ret = 0;
134 THDB *thread = (THDB *)GetCurrentThreadId();
135 DWORD *bits = thread->process->tls_bits;
136 EnterCriticalSection( &thread->process->crit_section );
137 if (*bits == 0xffffffff)
141 if (*bits == 0xffffffff)
143 LeaveCriticalSection( &thread->process->crit_section );
144 SetLastError( ERROR_NO_MORE_ITEMS );
148 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
150 LeaveCriticalSection( &thread->process->crit_section );
155 /**********************************************************************
156 * TlsFree (KERNEL32.531)
158 BOOL32 TlsFree( DWORD index )
161 THDB *thread = (THDB *)GetCurrentThreadId();
162 DWORD *bits = thread->process->tls_bits;
165 SetLastError( ERROR_INVALID_PARAMETER );
168 EnterCriticalSection( &thread->process->crit_section );
169 if (index >= 32) bits++;
170 mask = (1 << (index & 31));
171 if (!(*bits & mask)) /* already free? */
173 LeaveCriticalSection( &thread->process->crit_section );
174 SetLastError( ERROR_INVALID_PARAMETER );
178 thread->tls_array[index] = 0;
179 /* FIXME: should zero all other thread values */
180 LeaveCriticalSection( &thread->process->crit_section );
185 /**********************************************************************
186 * TlsGetValue (KERNEL32.532)
188 LPVOID TlsGetValue( DWORD index )
190 THDB *thread = (THDB *)GetCurrentThreadId();
193 SetLastError( ERROR_INVALID_PARAMETER );
196 SetLastError( ERROR_SUCCESS );
197 return thread->tls_array[index];
201 /**********************************************************************
202 * TlsSetValue (KERNEL32.533)
204 BOOL32 TlsSetValue( DWORD index, LPVOID value )
206 THDB *thread = (THDB *)GetCurrentThreadId();
209 SetLastError( ERROR_INVALID_PARAMETER );
212 thread->tls_array[index] = value;
217 /***********************************************************************
218 * GetThreadContext (KERNEL32.294)
220 BOOL32 GetThreadContext( HANDLE32 handle, CONTEXT *context )
226 /**********************************************************************
227 * NtCurrentTeb (NTDLL.89)
229 void NtCurrentTeb( CONTEXT *context )
231 EAX_reg(context) = GetSelectorBase( FS_reg(context) );
234 /**********************************************************************
235 * GetThreadPriority (KERNEL32.296)
238 GetThreadPriority(HANDLE32 hthread) {
243 thread = (THDB*)PROCESS_GetObjPtr(hthread,K32OBJ_THREAD);
245 ret = thread->delta_priority;
246 K32OBJ_DecCount((K32OBJ*)thread);
251 /**********************************************************************
252 * SetThreadPriority (KERNEL32.514)
255 SetThreadPriority(HANDLE32 hthread,INT32 priority) {
258 thread = (THDB*)PROCESS_GetObjPtr(hthread,K32OBJ_THREAD);
260 thread->delta_priority = priority;
261 K32OBJ_DecCount((K32OBJ*)thread);