4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
36 #include "wine/winbase16.h"
40 #include "selectors.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 /* TEB of the initial thread */
50 static TEB initial_teb;
52 extern struct _PDB current_process;
54 /***********************************************************************
57 * Initialization of a newly created TEB.
59 static BOOL THREAD_InitTEB( TEB *teb )
61 teb->Tib.ExceptionList = (void *)~0UL;
62 teb->Tib.StackBase = (void *)~0UL;
63 teb->Tib.Self = &teb->Tib;
64 teb->tibflags = TEBF_WIN32;
65 teb->exit_code = STILL_ACTIVE;
70 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
71 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
72 teb->teb_sel = wine_ldt_alloc_fs();
73 return (teb->teb_sel != 0);
77 /***********************************************************************
80 * Free data structures associated with a thread.
81 * Must be called from the context of another thread.
83 static void THREAD_FreeTEB( TEB *teb )
85 TRACE("(%p) called\n", teb );
86 /* Free the associated memory */
87 wine_ldt_free_fs( teb->teb_sel );
88 VirtualFree( teb->DeallocationStack, 0, MEM_RELEASE );
92 /***********************************************************************
95 * Allocate the stack of a thread.
97 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
99 DWORD old_prot, total_size;
100 DWORD page_size = getpagesize();
103 /* Allocate the stack */
105 if (stack_size >= 16*1024*1024)
106 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
108 /* if size is smaller than default, get stack size from parent */
109 if (stack_size < 1024 * 1024)
112 stack_size = 1024 * 1024; /* no parent */
114 stack_size = ((char *)NtCurrentTeb()->Tib.StackBase - (char *)NtCurrentTeb()->DeallocationStack
115 - SIGNAL_STACK_SIZE - 3 * page_size);
118 /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
119 stack_size += 64 * 1024;
121 /* Memory layout in allocated block:
124 * 1 page NOACCESS guard page
125 * SIGNAL_STACK_SIZE signal stack
126 * 1 page NOACCESS guard page
127 * 1 page PAGE_GUARD guard page
128 * stack_size normal stack
129 * 1 page TEB (except for initial thread)
132 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
133 total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
134 if (!teb) total_size += page_size;
136 if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
141 teb = (TEB *)((char *)base + total_size - page_size);
142 if (!THREAD_InitTEB( teb ))
144 VirtualFree( base, 0, MEM_RELEASE );
149 teb->DeallocationStack = base;
150 teb->signal_stack = (char *)base + page_size;
151 teb->Tib.StackBase = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
152 teb->Tib.StackLimit = base; /* note: limit is lower than base since the stack grows down */
154 /* Setup guard pages */
156 VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
157 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
158 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
159 PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
164 /***********************************************************************
167 * Setup the initial thread.
169 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
171 void THREAD_Init(void)
173 static struct debug_info info; /* debug info for initial thread */
175 if (!initial_teb.Tib.Self) /* do it only once */
177 THREAD_InitTEB( &initial_teb );
178 assert( initial_teb.teb_sel );
179 info.str_pos = info.strings;
180 info.out_pos = info.output;
181 initial_teb.debug_info = &info;
182 initial_teb.Peb = (PEB *)¤t_process; /* FIXME */
183 SYSDEPS_SetCurThread( &initial_teb );
187 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
190 /***********************************************************************
193 * Start execution of a newly created thread. Does not return.
195 static void THREAD_Start( TEB *teb )
197 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)teb->entry_point;
198 struct debug_info info;
200 info.str_pos = info.strings;
201 info.out_pos = info.output;
202 teb->debug_info = &info;
204 SYSDEPS_SetCurThread( teb );
209 DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
213 MODULE_DllThreadAttach( NULL );
214 ExitThread( func( NtCurrentTeb()->entry_arg ) );
216 __EXCEPT(UnhandledExceptionFilter)
218 TerminateThread( GetCurrentThread(), GetExceptionCode() );
224 /***********************************************************************
225 * CreateThread (KERNEL32.@)
227 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
228 LPTHREAD_START_ROUTINE start, LPVOID param,
229 DWORD flags, LPDWORD id )
236 if (pipe( request_pipe ) == -1)
238 SetLastError( ERROR_TOO_MANY_OPEN_FILES );
241 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
242 wine_server_send_fd( request_pipe[0] );
244 SERVER_START_REQ( new_thread )
246 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
247 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
248 req->request_fd = request_pipe[0];
249 if (!wine_server_call_err( req ))
251 handle = reply->handle;
254 close( request_pipe[0] );
258 if (!handle || !(teb = THREAD_InitStack( NULL, stack )))
260 close( request_pipe[1] );
264 teb->Peb = NtCurrentTeb()->Peb;
265 teb->ClientId.UniqueThread = (HANDLE)tid;
266 teb->request_fd = request_pipe[1];
267 teb->entry_point = start;
268 teb->entry_arg = param;
269 teb->htask16 = GetCurrentTask();
272 if (SYSDEPS_SpawnThread( THREAD_Start, teb ) == -1)
274 CloseHandle( handle );
275 close( request_pipe[1] );
276 THREAD_FreeTEB( teb );
282 /***********************************************************************
283 * ExitThread [KERNEL32.@] Ends a thread
288 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
291 SERVER_START_REQ( terminate_thread )
293 /* send the exit code to the server */
294 req->handle = GetCurrentThread();
295 req->exit_code = code;
296 wine_server_call( req );
303 LdrShutdownProcess();
309 SYSDEPS_ExitThread( code );
313 /***********************************************************************
314 * OpenThread Retrieves a handle to a thread from its thread id
319 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
322 SERVER_START_REQ( open_thread )
324 req->tid = dwThreadId;
325 req->access = dwDesiredAccess;
326 req->inherit = bInheritHandle;
327 if (!wine_server_call_err( req )) ret = reply->handle;
333 /**********************************************************************
334 * TerminateThread [KERNEL32.@] Terminates a thread
340 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
341 DWORD exit_code) /* [in] Exit code for thread */
343 NTSTATUS status = NtTerminateThread( handle, exit_code );
344 if (status) SetLastError( RtlNtStatusToDosError(status) );
349 /**********************************************************************
350 * GetExitCodeThread (KERNEL32.@)
352 * Gets termination status of thread.
358 BOOL WINAPI GetExitCodeThread(
359 HANDLE hthread, /* [in] Handle to thread */
360 LPDWORD exitcode) /* [out] Address to receive termination status */
362 THREAD_BASIC_INFORMATION info;
363 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
364 &info, sizeof(info), NULL );
368 SetLastError( RtlNtStatusToDosError(status) );
371 if (exitcode) *exitcode = info.ExitStatus;
376 /* callback for QueueUserAPC */
377 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
379 PAPCFUNC func = (PAPCFUNC)arg1;
383 /***********************************************************************
384 * QueueUserAPC (KERNEL32.@)
386 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
388 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
390 if (status) SetLastError( RtlNtStatusToDosError(status) );
395 /***********************************************************************
396 * ProcessIdToSessionId (KERNEL32.@)
397 * This function is available on Terminal Server 4SP4 and Windows 2000
399 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
401 /* According to MSDN, if the calling process is not in a terminal
402 * services environment, then the sessionid returned is zero.
411 /***********************************************************************
412 * SetLastError (KERNEL.147)
413 * SetLastError (KERNEL32.@)
415 /* void WINAPI SetLastError( DWORD error ); */
416 __ASM_GLOBAL_FUNC( SetLastError,
417 "movl 4(%esp),%eax\n\t"
422 /***********************************************************************
423 * GetLastError (KERNEL.148)
424 * GetLastError (KERNEL32.@)
426 /* DWORD WINAPI GetLastError(void); */
427 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
429 /***********************************************************************
430 * GetCurrentProcessId (KERNEL.471)
431 * GetCurrentProcessId (KERNEL32.@)
433 /* DWORD WINAPI GetCurrentProcessId(void) */
434 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
436 /***********************************************************************
437 * GetCurrentThreadId (KERNEL.462)
438 * GetCurrentThreadId (KERNEL32.@)
440 /* DWORD WINAPI GetCurrentThreadId(void) */
441 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
445 /**********************************************************************
446 * SetLastError (KERNEL.147)
447 * SetLastError (KERNEL32.@)
449 * Sets the last-error code.
451 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
453 NtCurrentTeb()->last_error = error;
456 /**********************************************************************
457 * GetLastError (KERNEL.148)
458 * GetLastError (KERNEL32.@)
460 * Returns last-error code.
462 DWORD WINAPI GetLastError(void)
464 return NtCurrentTeb()->last_error;
467 /***********************************************************************
468 * GetCurrentProcessId (KERNEL.471)
469 * GetCurrentProcessId (KERNEL32.@)
471 * Returns process identifier.
473 DWORD WINAPI GetCurrentProcessId(void)
475 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
478 /***********************************************************************
479 * GetCurrentThreadId (KERNEL.462)
480 * GetCurrentThreadId (KERNEL32.@)
482 * Returns thread identifier.
484 DWORD WINAPI GetCurrentThreadId(void)
486 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
489 #endif /* __i386__ */