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)
87 DWORD GetCurrentThreadId(void)
89 /* FIXME: should probably use %fs register here */
90 assert( pCurrentThread );
91 return (DWORD)pCurrentThread;
95 /**********************************************************************
96 * GetLastError (KERNEL.148) (KERNEL32.227)
98 DWORD GetLastError(void)
100 THDB *thread = (THDB *)GetCurrentThreadId();
101 return thread->last_error;
105 /**********************************************************************
106 * SetLastError (KERNEL.147) (KERNEL32.497)
108 void SetLastError( DWORD error )
111 if (!pCurrentThread) return; /* FIXME */
112 thread = (THDB *)GetCurrentThreadId();
113 thread->last_error = error;
117 /**********************************************************************
118 * SetLastErrorEx (USER32.484)
120 void SetLastErrorEx( DWORD error, DWORD type )
122 /* FIXME: what about 'type'? */
123 SetLastError( error );
127 /**********************************************************************
128 * TlsAlloc (KERNEL32.530)
132 DWORD i, mask, ret = 0;
133 THDB *thread = (THDB *)GetCurrentThreadId();
134 DWORD *bits = thread->process->tls_bits;
135 EnterCriticalSection( &thread->process->crit_section );
136 if (*bits == 0xffffffff)
140 if (*bits == 0xffffffff)
142 LeaveCriticalSection( &thread->process->crit_section );
143 SetLastError( ERROR_NO_MORE_ITEMS );
147 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
149 LeaveCriticalSection( &thread->process->crit_section );
154 /**********************************************************************
155 * TlsFree (KERNEL32.531)
157 BOOL32 TlsFree( DWORD index )
160 THDB *thread = (THDB *)GetCurrentThreadId();
161 DWORD *bits = thread->process->tls_bits;
164 SetLastError( ERROR_INVALID_PARAMETER );
167 EnterCriticalSection( &thread->process->crit_section );
168 if (index >= 32) bits++;
169 mask = (1 << (index & 31));
170 if (!(*bits & mask)) /* already free? */
172 LeaveCriticalSection( &thread->process->crit_section );
173 SetLastError( ERROR_INVALID_PARAMETER );
177 thread->tls_array[index] = 0;
178 /* FIXME: should zero all other thread values */
179 LeaveCriticalSection( &thread->process->crit_section );
184 /**********************************************************************
185 * TlsGetValue (KERNEL32.532)
187 LPVOID TlsGetValue( DWORD index )
189 THDB *thread = (THDB *)GetCurrentThreadId();
192 SetLastError( ERROR_INVALID_PARAMETER );
195 SetLastError( ERROR_SUCCESS );
196 return thread->tls_array[index];
200 /**********************************************************************
201 * TlsSetValue (KERNEL32.533)
203 BOOL32 TlsSetValue( DWORD index, LPVOID value )
205 THDB *thread = (THDB *)GetCurrentThreadId();
208 SetLastError( ERROR_INVALID_PARAMETER );
211 thread->tls_array[index] = value;
216 /***********************************************************************
217 * GetThreadContext (KERNEL32.294)
219 BOOL32 GetThreadContext( HANDLE32 handle, CONTEXT *context )
225 /**********************************************************************
226 * NtCurrentTeb (NTDLL.89)
228 void NtCurrentTeb( CONTEXT *context )
230 EAX_reg(context) = GetSelectorBase( FS_reg(context) );