4 * Copyright 1996 Alexandre Julliard
12 #include "selectors.h"
16 THDB *pCurrentThread = NULL;
18 /***********************************************************************
21 * Return a pointer to a thread object. The object count must be decremented
22 * when no longer used.
24 static THDB *THREAD_GetPtr( HANDLE32 handle )
28 if (handle == 0xfffffffe) /* Self-thread handle */
30 thread = (THDB *)GetCurrentThreadId();
31 K32OBJ_IncCount( &thread->header );
33 else thread = (THDB *)PROCESS_GetObjPtr( handle, K32OBJ_THREAD );
38 /***********************************************************************
41 THDB *THREAD_Create( PDB32 *pdb, DWORD stack_size,
42 LPTHREAD_START_ROUTINE start_addr )
45 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
46 if (!thdb) return NULL;
47 thdb->header.type = K32OBJ_THREAD;
48 thdb->header.refcount = 1;
50 thdb->teb.except = (void *)-1;
51 thdb->teb.htask16 = 0; /* FIXME */
52 thdb->teb.stack_sel = 0; /* FIXME */
53 thdb->teb.self = &thdb->teb;
54 thdb->teb.tls_ptr = thdb->tls_array;
56 thdb->exit_code = 0x103; /* STILL_ACTIVE */
58 /* Allocate the stack */
60 if (!stack_size) stack_size = 1024 * 1024; /* default size = 1Mb */
61 thdb->stack_base = VirtualAlloc( NULL, stack_size, MEM_COMMIT,
62 PAGE_EXECUTE_READWRITE );
63 if (!thdb->stack_base) goto error;
64 /* Set a guard page at the bottom of the stack */
65 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
67 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
68 thdb->teb.stack_low = thdb->stack_base;
69 thdb->exit_stack = thdb->teb.stack_top;
71 /* Allocate the TEB selector (%fs register) */
73 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
75 if (!thdb->teb_sel) goto error;
77 /* Initialize the thread context */
79 thdb->pcontext = &thdb->context;
80 thdb->context.SegCs = WINE_CODE_SELECTOR;
81 thdb->context.SegDs = WINE_DATA_SELECTOR;
82 thdb->context.SegEs = WINE_DATA_SELECTOR;
83 thdb->context.SegFs = thdb->teb_sel;
84 thdb->context.SegGs = WINE_DATA_SELECTOR;
85 thdb->context.SegSs = WINE_DATA_SELECTOR;
86 thdb->context.Eip = (DWORD)start_addr;
87 thdb->context.Esp = (DWORD)thdb->teb.stack_top;
92 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
93 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
94 HeapFree( SystemHeap, 0, thdb );
99 /***********************************************************************
102 void THREAD_Destroy( K32OBJ *ptr )
104 THDB *thdb = (THDB *)ptr;
105 assert( ptr->type == K32OBJ_THREAD );
106 ptr->type = K32OBJ_UNKNOWN;
107 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
108 HeapFree( SystemHeap, 0, thdb );
112 /***********************************************************************
113 * CreateThread (KERNEL32.63)
115 * The only thing missing here is actually getting the thread to run ;-)
117 HANDLE32 CreateThread( LPSECURITY_ATTRIBUTES attribs, DWORD stack,
118 LPTHREAD_START_ROUTINE start, LPVOID param,
119 DWORD flags, LPDWORD id )
122 THDB *thread = THREAD_Create( pCurrentProcess, stack, start );
123 if (!thread) return INVALID_HANDLE_VALUE32;
124 handle = PROCESS_AllocHandle( &thread->header, 0 );
125 if (handle == INVALID_HANDLE_VALUE32)
127 THREAD_Destroy( &thread->header );
128 return INVALID_HANDLE_VALUE32;
131 fprintf( stderr, "CreateThread: stub\n" );
136 /***********************************************************************
137 * GetCurrentThread (KERNEL32.200)
139 HANDLE32 GetCurrentThread(void)
145 /***********************************************************************
146 * GetCurrentThreadId (KERNEL32.201)
147 * Returns crypted (xor'ed) pointer to THDB in Win95.
149 DWORD GetCurrentThreadId(void)
151 /* FIXME: should probably use %fs register here */
152 assert( pCurrentThread );
153 return (DWORD)pCurrentThread;
157 /**********************************************************************
158 * GetLastError (KERNEL.148) (KERNEL32.227)
160 DWORD GetLastError(void)
162 THDB *thread = (THDB *)GetCurrentThreadId();
163 return thread->last_error;
167 /**********************************************************************
168 * SetLastError (KERNEL.147) (KERNEL32.497)
170 void SetLastError( DWORD error )
173 if (!pCurrentThread) return; /* FIXME */
174 thread = (THDB *)GetCurrentThreadId();
175 thread->last_error = error;
179 /**********************************************************************
180 * SetLastErrorEx (USER32.484)
182 void SetLastErrorEx( DWORD error, DWORD type )
184 /* FIXME: what about 'type'? */
185 SetLastError( error );
189 /**********************************************************************
190 * TlsAlloc (KERNEL32.530)
194 DWORD i, mask, ret = 0;
195 THDB *thread = (THDB *)GetCurrentThreadId();
196 DWORD *bits = thread->process->tls_bits;
197 EnterCriticalSection( &thread->process->crit_section );
198 if (*bits == 0xffffffff)
202 if (*bits == 0xffffffff)
204 LeaveCriticalSection( &thread->process->crit_section );
205 SetLastError( ERROR_NO_MORE_ITEMS );
209 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
211 LeaveCriticalSection( &thread->process->crit_section );
216 /**********************************************************************
217 * TlsFree (KERNEL32.531)
219 BOOL32 TlsFree( DWORD index )
222 THDB *thread = (THDB *)GetCurrentThreadId();
223 DWORD *bits = thread->process->tls_bits;
226 SetLastError( ERROR_INVALID_PARAMETER );
229 EnterCriticalSection( &thread->process->crit_section );
230 if (index >= 32) bits++;
231 mask = (1 << (index & 31));
232 if (!(*bits & mask)) /* already free? */
234 LeaveCriticalSection( &thread->process->crit_section );
235 SetLastError( ERROR_INVALID_PARAMETER );
239 thread->tls_array[index] = 0;
240 /* FIXME: should zero all other thread values */
241 LeaveCriticalSection( &thread->process->crit_section );
246 /**********************************************************************
247 * TlsGetValue (KERNEL32.532)
249 LPVOID TlsGetValue( DWORD index )
251 THDB *thread = (THDB *)GetCurrentThreadId();
254 SetLastError( ERROR_INVALID_PARAMETER );
257 SetLastError( ERROR_SUCCESS );
258 return thread->tls_array[index];
262 /**********************************************************************
263 * TlsSetValue (KERNEL32.533)
265 BOOL32 TlsSetValue( DWORD index, LPVOID value )
267 THDB *thread = (THDB *)GetCurrentThreadId();
270 SetLastError( ERROR_INVALID_PARAMETER );
273 thread->tls_array[index] = value;
278 /***********************************************************************
279 * GetThreadContext (KERNEL32.294)
281 BOOL32 GetThreadContext( HANDLE32 handle, CONTEXT *context )
283 THDB *thread = (THDB*)PROCESS_GetObjPtr( handle, K32OBJ_THREAD );
284 if (!thread) return FALSE;
285 *context = thread->context;
286 K32OBJ_DecCount( &thread->header );
291 /**********************************************************************
292 * NtCurrentTeb (NTDLL.89)
294 void NtCurrentTeb( CONTEXT *context )
296 EAX_reg(context) = GetSelectorBase( FS_reg(context) );
300 /**********************************************************************
301 * GetThreadPriority (KERNEL32.296)
303 INT32 GetThreadPriority(HANDLE32 hthread)
308 if (!(thread = THREAD_GetPtr( hthread ))) return 0;
309 ret = thread->delta_priority;
310 K32OBJ_DecCount( &thread->header );
315 /**********************************************************************
316 * SetThreadPriority (KERNEL32.514)
318 BOOL32 SetThreadPriority(HANDLE32 hthread,INT32 priority)
322 if (!(thread = THREAD_GetPtr( hthread ))) return FALSE;
323 thread->delta_priority = priority;
324 K32OBJ_DecCount( &thread->header );