Release 20050930.
[wine/gsoc-2012-control.git] / dlls / kernel / thread.c
blob17bdd597cb569b34e857da37857d77ec9ae129f9
1 /*
2 * Win32 threads
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include "ntstatus.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "winnls.h"
37 #include "thread.h"
38 #include "module.h"
39 #include "wine/winbase16.h"
40 #include "wine/exception.h"
41 #include "wine/library.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 #include "kernel_private.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(thread);
48 WINE_DECLARE_DEBUG_CHANNEL(relay);
51 /***********************************************************************
52 * THREAD_InitStack
54 * Allocate the stack of a thread.
56 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
58 DWORD old_prot;
59 DWORD page_size = getpagesize();
60 void *base;
62 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
63 if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */
65 if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_READWRITE )))
66 return NULL;
68 teb->DeallocationStack = base;
69 teb->Tib.StackBase = (char *)base + stack_size;
70 teb->Tib.StackLimit = base; /* note: limit is lower than base since the stack grows down */
72 /* Setup guard pages */
74 VirtualProtect( base, 1, PAGE_READWRITE | PAGE_GUARD, &old_prot );
75 return teb;
79 struct new_thread_info
81 LPTHREAD_START_ROUTINE func;
82 void *arg;
85 /***********************************************************************
86 * THREAD_Start
88 * Start execution of a newly created thread. Does not return.
90 static void CALLBACK THREAD_Start( void *ptr )
92 struct new_thread_info *info = ptr;
93 LPTHREAD_START_ROUTINE func = info->func;
94 void *arg = info->arg;
96 RtlFreeHeap( GetProcessHeap(), 0, info );
98 if (TRACE_ON(relay))
99 DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
101 __TRY
103 MODULE_DllThreadAttach( NULL );
104 ExitThread( func( arg ) );
106 __EXCEPT(UnhandledExceptionFilter)
108 TerminateThread( GetCurrentThread(), GetExceptionCode() );
110 __ENDTRY
114 /***********************************************************************
115 * CreateThread (KERNEL32.@)
117 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
118 LPTHREAD_START_ROUTINE start, LPVOID param,
119 DWORD flags, LPDWORD id )
121 return CreateRemoteThread( GetCurrentProcess(),
122 sa, stack, start, param, flags, id );
126 /***************************************************************************
127 * CreateRemoteThread (KERNEL32.@)
129 * Creates a thread that runs in the address space of another process
131 * PARAMS
133 * RETURNS
134 * Success: Handle to the new thread.
135 * Failure: NULL. Use GetLastError() to find the error cause.
137 * BUGS
138 * Improper memory allocation: there's no ability to free new_thread_info
139 * in other process.
140 * Bad start address for RtlCreateUserThread because the library
141 * may be loaded at different address in other process.
143 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
144 LPTHREAD_START_ROUTINE start, LPVOID param,
145 DWORD flags, LPDWORD id )
147 HANDLE handle;
148 CLIENT_ID client_id;
149 NTSTATUS status;
150 SIZE_T stack_reserve = 0, stack_commit = 0;
151 struct new_thread_info *info;
153 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
155 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
156 return 0;
158 info->func = start;
159 info->arg = param;
161 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
162 else stack_commit = stack;
164 status = RtlCreateUserThread( hProcess, NULL, TRUE,
165 NULL, stack_reserve, stack_commit,
166 THREAD_Start, info, &handle, &client_id );
167 if (status == STATUS_SUCCESS)
169 if (id) *id = (DWORD)client_id.UniqueThread;
170 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
171 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
172 if (!(flags & CREATE_SUSPENDED))
174 ULONG ret;
175 if (NtResumeThread( handle, &ret ))
177 NtClose( handle );
178 RtlFreeHeap( GetProcessHeap(), 0, info );
179 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
180 handle = 0;
184 else
186 RtlFreeHeap( GetProcessHeap(), 0, info );
187 SetLastError( RtlNtStatusToDosError(status) );
188 handle = 0;
190 return handle;
194 /***********************************************************************
195 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
197 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
199 NTSTATUS status;
200 HANDLE handle;
201 OBJECT_ATTRIBUTES attr;
202 CLIENT_ID cid;
204 attr.Length = sizeof(attr);
205 attr.RootDirectory = 0;
206 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
207 attr.ObjectName = NULL;
208 attr.SecurityDescriptor = NULL;
209 attr.SecurityQualityOfService = NULL;
211 cid.UniqueProcess = 0; /* FIXME */
212 cid.UniqueThread = (HANDLE)dwThreadId;
213 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
214 if (status)
216 SetLastError( RtlNtStatusToDosError(status) );
217 handle = 0;
219 return handle;
223 /***********************************************************************
224 * ExitThread [KERNEL32.@] Ends a thread
226 * RETURNS
227 * None
229 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
231 BOOL last;
232 SERVER_START_REQ( terminate_thread )
234 /* send the exit code to the server */
235 req->handle = GetCurrentThread();
236 req->exit_code = code;
237 wine_server_call( req );
238 last = reply->last;
240 SERVER_END_REQ;
242 if (last)
244 LdrShutdownProcess();
245 exit( code );
247 else RtlExitUserThread( code );
251 /**********************************************************************
252 * TerminateThread [KERNEL32.@] Terminates a thread
254 * RETURNS
255 * Success: TRUE
256 * Failure: FALSE
258 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
259 DWORD exit_code) /* [in] Exit code for thread */
261 NTSTATUS status = NtTerminateThread( handle, exit_code );
262 if (status) SetLastError( RtlNtStatusToDosError(status) );
263 return !status;
267 /***********************************************************************
268 * FreeLibraryAndExitThread (KERNEL32.@)
270 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
272 FreeLibrary(hLibModule);
273 ExitThread(dwExitCode);
277 /**********************************************************************
278 * GetExitCodeThread (KERNEL32.@)
280 * Gets termination status of thread.
282 * RETURNS
283 * Success: TRUE
284 * Failure: FALSE
286 BOOL WINAPI GetExitCodeThread(
287 HANDLE hthread, /* [in] Handle to thread */
288 LPDWORD exitcode) /* [out] Address to receive termination status */
290 THREAD_BASIC_INFORMATION info;
291 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
292 &info, sizeof(info), NULL );
294 if (status)
296 SetLastError( RtlNtStatusToDosError(status) );
297 return FALSE;
299 if (exitcode) *exitcode = info.ExitStatus;
300 return TRUE;
304 /***********************************************************************
305 * SetThreadContext [KERNEL32.@] Sets context of thread.
307 * RETURNS
308 * Success: TRUE
309 * Failure: FALSE
311 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
312 const CONTEXT *context ) /* [in] Address of context structure */
314 NTSTATUS status = NtSetContextThread( handle, context );
315 if (status) SetLastError( RtlNtStatusToDosError(status) );
316 return !status;
320 /***********************************************************************
321 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
323 * RETURNS
324 * Success: TRUE
325 * Failure: FALSE
327 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
328 CONTEXT *context ) /* [out] Address of context structure */
330 NTSTATUS status = NtGetContextThread( handle, context );
331 if (status) SetLastError( RtlNtStatusToDosError(status) );
332 return !status;
336 /**********************************************************************
337 * SuspendThread [KERNEL32.@] Suspends a thread.
339 * RETURNS
340 * Success: Previous suspend count
341 * Failure: 0xFFFFFFFF
343 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
345 DWORD ret;
346 NTSTATUS status = NtSuspendThread( hthread, &ret );
348 if (status)
350 ret = ~0U;
351 SetLastError( RtlNtStatusToDosError(status) );
353 return ret;
357 /**********************************************************************
358 * ResumeThread [KERNEL32.@] Resumes a thread.
360 * Decrements a thread's suspend count. When count is zero, the
361 * execution of the thread is resumed.
363 * RETURNS
364 * Success: Previous suspend count
365 * Failure: 0xFFFFFFFF
366 * Already running: 0
368 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
370 DWORD ret;
371 NTSTATUS status = NtResumeThread( hthread, &ret );
373 if (status)
375 ret = ~0U;
376 SetLastError( RtlNtStatusToDosError(status) );
378 return ret;
382 /**********************************************************************
383 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
385 * RETURNS
386 * Success: Thread's priority level.
387 * Failure: THREAD_PRIORITY_ERROR_RETURN
389 INT WINAPI GetThreadPriority(
390 HANDLE hthread) /* [in] Handle to thread */
392 THREAD_BASIC_INFORMATION info;
393 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
394 &info, sizeof(info), NULL );
396 if (status)
398 SetLastError( RtlNtStatusToDosError(status) );
399 return THREAD_PRIORITY_ERROR_RETURN;
401 return info.Priority;
405 /**********************************************************************
406 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
408 * RETURNS
409 * Success: TRUE
410 * Failure: FALSE
412 BOOL WINAPI SetThreadPriority(
413 HANDLE hthread, /* [in] Handle to thread */
414 INT priority) /* [in] Thread priority level */
416 DWORD prio = priority;
417 NTSTATUS status;
419 status = NtSetInformationThread(hthread, ThreadBasePriority,
420 &prio, sizeof(prio));
422 if (status)
424 SetLastError( RtlNtStatusToDosError(status) );
425 return FALSE;
428 return TRUE;
432 /**********************************************************************
433 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
435 * Always reports that priority boost is disabled.
437 * RETURNS
438 * Success: TRUE.
439 * Failure: FALSE
441 BOOL WINAPI GetThreadPriorityBoost(
442 HANDLE hthread, /* [in] Handle to thread */
443 PBOOL pstate) /* [out] pointer to var that receives the boost state */
445 if (pstate) *pstate = FALSE;
446 return NO_ERROR;
450 /**********************************************************************
451 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
453 * Priority boost is not implemented. Thsi function always returns
454 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
456 * RETURNS
457 * Always returns FALSE to indicate a failure
459 BOOL WINAPI SetThreadPriorityBoost(
460 HANDLE hthread, /* [in] Handle to thread */
461 BOOL disable) /* [in] TRUE to disable priority boost */
463 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
464 return FALSE;
468 /**********************************************************************
469 * SetThreadAffinityMask (KERNEL32.@)
471 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
473 DWORD ret;
474 SERVER_START_REQ( set_thread_info )
476 req->handle = hThread;
477 req->affinity = dwThreadAffinityMask;
478 req->mask = SET_THREAD_INFO_AFFINITY;
479 ret = !wine_server_call_err( req );
480 /* FIXME: should return previous value */
482 SERVER_END_REQ;
483 return ret;
487 /**********************************************************************
488 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
490 * RETURNS
491 * Success: Value of last call to SetThreadIdealProcessor
492 * Failure: -1
494 DWORD WINAPI SetThreadIdealProcessor(
495 HANDLE hThread, /* [in] Specifies the thread of interest */
496 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
498 FIXME("(%p): stub\n",hThread);
499 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
500 return -1L;
504 /* callback for QueueUserAPC */
505 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
507 PAPCFUNC func = (PAPCFUNC)arg1;
508 func( arg2 );
511 /***********************************************************************
512 * QueueUserAPC (KERNEL32.@)
514 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
516 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
518 if (status) SetLastError( RtlNtStatusToDosError(status) );
519 return !status;
522 /***********************************************************************
523 * QueueUserWorkItem (KERNEL32.@)
525 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
527 FIXME("(%p,%p,0x%08lx): stub\n", Function, Context, Flags);
528 return FALSE;
531 /**********************************************************************
532 * GetThreadTimes [KERNEL32.@] Obtains timing information.
534 * RETURNS
535 * Success: TRUE
536 * Failure: FALSE
538 BOOL WINAPI GetThreadTimes(
539 HANDLE thread, /* [in] Specifies the thread of interest */
540 LPFILETIME creationtime, /* [out] When the thread was created */
541 LPFILETIME exittime, /* [out] When the thread was destroyed */
542 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
543 LPFILETIME usertime) /* [out] Time thread spent in user mode */
545 KERNEL_USER_TIMES kusrt;
546 NTSTATUS status;
548 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
549 sizeof(kusrt), NULL);
550 if (status)
552 SetLastError( RtlNtStatusToDosError(status) );
553 return FALSE;
555 if (creationtime)
557 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
558 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
560 if (exittime)
562 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
563 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
565 if (kerneltime)
567 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
568 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
570 if (usertime)
572 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
573 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
576 return TRUE;
580 /**********************************************************************
581 * VWin32_BoostThreadGroup [KERNEL.535]
583 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
585 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
589 /**********************************************************************
590 * VWin32_BoostThreadStatic [KERNEL.536]
592 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
594 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
598 /***********************************************************************
599 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
601 * RETURNS
602 * Pseudohandle for the current thread
604 #undef GetCurrentThread
605 HANDLE WINAPI GetCurrentThread(void)
607 return (HANDLE)0xfffffffe;
611 #ifdef __i386__
613 /***********************************************************************
614 * SetLastError (KERNEL.147)
615 * SetLastError (KERNEL32.@)
617 /* void WINAPI SetLastError( DWORD error ); */
618 __ASM_GLOBAL_FUNC( SetLastError,
619 "movl 4(%esp),%eax\n\t"
620 ".byte 0x64\n\t"
621 "movl %eax,0x34\n\t"
622 "ret $4" )
624 /***********************************************************************
625 * GetLastError (KERNEL.148)
626 * GetLastError (KERNEL32.@)
628 /* DWORD WINAPI GetLastError(void); */
629 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
631 /***********************************************************************
632 * GetCurrentProcessId (KERNEL.471)
633 * GetCurrentProcessId (KERNEL32.@)
635 /* DWORD WINAPI GetCurrentProcessId(void) */
636 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
638 /***********************************************************************
639 * GetCurrentThreadId (KERNEL.462)
640 * GetCurrentThreadId (KERNEL32.@)
642 /* DWORD WINAPI GetCurrentThreadId(void) */
643 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
645 #else /* __i386__ */
647 /**********************************************************************
648 * SetLastError (KERNEL.147)
649 * SetLastError (KERNEL32.@)
651 * Sets the last-error code.
653 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
655 NtCurrentTeb()->LastErrorValue = error;
658 /**********************************************************************
659 * GetLastError (KERNEL.148)
660 * GetLastError (KERNEL32.@)
662 * Returns last-error code.
664 DWORD WINAPI GetLastError(void)
666 return NtCurrentTeb()->LastErrorValue;
669 /***********************************************************************
670 * GetCurrentProcessId (KERNEL.471)
671 * GetCurrentProcessId (KERNEL32.@)
673 * Returns process identifier.
675 DWORD WINAPI GetCurrentProcessId(void)
677 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
680 /***********************************************************************
681 * GetCurrentThreadId (KERNEL.462)
682 * GetCurrentThreadId (KERNEL32.@)
684 * Returns thread identifier.
686 DWORD WINAPI GetCurrentThreadId(void)
688 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
691 #endif /* __i386__ */