push 87b6981010d7405c33b14cddcceec21b47729eba
[wine/hacks.git] / dlls / kernel32 / thread.c
blobf6a2b0f2cc4236b70f5774d6c616a9b9c8d44d13
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winternl.h"
38 #include "wine/exception.h"
39 #include "wine/library.h"
40 #include "wine/server.h"
41 #include "wine/debug.h"
43 #include "kernel_private.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(thread);
48 /***********************************************************************
49 * CreateThread (KERNEL32.@)
51 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
52 LPTHREAD_START_ROUTINE start, LPVOID param,
53 DWORD flags, LPDWORD id )
55 return CreateRemoteThread( GetCurrentProcess(),
56 sa, stack, start, param, flags, id );
60 /***************************************************************************
61 * CreateRemoteThread (KERNEL32.@)
63 * Creates a thread that runs in the address space of another process
65 * PARAMS
67 * RETURNS
68 * Success: Handle to the new thread.
69 * Failure: NULL. Use GetLastError() to find the error cause.
71 * BUGS
72 * Improper memory allocation: there's no ability to free new_thread_info
73 * in other process.
74 * Bad start address for RtlCreateUserThread because the library
75 * may be loaded at different address in other process.
77 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
78 LPTHREAD_START_ROUTINE start, LPVOID param,
79 DWORD flags, LPDWORD id )
81 HANDLE handle;
82 CLIENT_ID client_id;
83 NTSTATUS status;
84 SIZE_T stack_reserve = 0, stack_commit = 0;
86 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
87 else stack_commit = stack;
89 status = RtlCreateUserThread( hProcess, NULL, TRUE,
90 NULL, stack_reserve, stack_commit,
91 (PRTL_THREAD_START_ROUTINE)start, param, &handle, &client_id );
92 if (status == STATUS_SUCCESS)
94 if (id) *id = HandleToULong(client_id.UniqueThread);
95 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
96 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
97 if (!(flags & CREATE_SUSPENDED))
99 ULONG ret;
100 if (NtResumeThread( handle, &ret ))
102 NtClose( handle );
103 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
104 handle = 0;
108 else
110 SetLastError( RtlNtStatusToDosError(status) );
111 handle = 0;
113 return handle;
117 /***********************************************************************
118 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
120 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
122 NTSTATUS status;
123 HANDLE handle;
124 OBJECT_ATTRIBUTES attr;
125 CLIENT_ID cid;
127 attr.Length = sizeof(attr);
128 attr.RootDirectory = 0;
129 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
130 attr.ObjectName = NULL;
131 attr.SecurityDescriptor = NULL;
132 attr.SecurityQualityOfService = NULL;
134 cid.UniqueProcess = 0; /* FIXME */
135 cid.UniqueThread = ULongToHandle(dwThreadId);
136 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
137 if (status)
139 SetLastError( RtlNtStatusToDosError(status) );
140 handle = 0;
142 return handle;
146 /***********************************************************************
147 * ExitThread [KERNEL32.@] Ends a thread
149 * RETURNS
150 * None
152 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
154 RtlFreeThreadActivationContextStack();
155 RtlExitUserThread( code );
159 /**********************************************************************
160 * TerminateThread [KERNEL32.@] Terminates a thread
162 * RETURNS
163 * Success: TRUE
164 * Failure: FALSE
166 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
167 DWORD exit_code) /* [in] Exit code for thread */
169 NTSTATUS status = NtTerminateThread( handle, exit_code );
170 if (status) SetLastError( RtlNtStatusToDosError(status) );
171 return !status;
175 /***********************************************************************
176 * FreeLibraryAndExitThread (KERNEL32.@)
178 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
180 FreeLibrary(hLibModule);
181 ExitThread(dwExitCode);
185 /**********************************************************************
186 * GetExitCodeThread (KERNEL32.@)
188 * Gets termination status of thread.
190 * RETURNS
191 * Success: TRUE
192 * Failure: FALSE
194 BOOL WINAPI GetExitCodeThread(
195 HANDLE hthread, /* [in] Handle to thread */
196 LPDWORD exitcode) /* [out] Address to receive termination status */
198 THREAD_BASIC_INFORMATION info;
199 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
200 &info, sizeof(info), NULL );
202 if (status)
204 SetLastError( RtlNtStatusToDosError(status) );
205 return FALSE;
207 if (exitcode) *exitcode = info.ExitStatus;
208 return TRUE;
212 /***********************************************************************
213 * SetThreadContext [KERNEL32.@] Sets context of thread.
215 * RETURNS
216 * Success: TRUE
217 * Failure: FALSE
219 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
220 const CONTEXT *context ) /* [in] Address of context structure */
222 NTSTATUS status = NtSetContextThread( handle, context );
223 if (status) SetLastError( RtlNtStatusToDosError(status) );
224 return !status;
228 /***********************************************************************
229 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
231 * RETURNS
232 * Success: TRUE
233 * Failure: FALSE
235 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
236 CONTEXT *context ) /* [out] Address of context structure */
238 NTSTATUS status = NtGetContextThread( handle, context );
239 if (status) SetLastError( RtlNtStatusToDosError(status) );
240 return !status;
244 /**********************************************************************
245 * SuspendThread [KERNEL32.@] Suspends a thread.
247 * RETURNS
248 * Success: Previous suspend count
249 * Failure: 0xFFFFFFFF
251 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
253 DWORD ret;
254 NTSTATUS status = NtSuspendThread( hthread, &ret );
256 if (status)
258 ret = ~0U;
259 SetLastError( RtlNtStatusToDosError(status) );
261 return ret;
265 /**********************************************************************
266 * ResumeThread [KERNEL32.@] Resumes a thread.
268 * Decrements a thread's suspend count. When count is zero, the
269 * execution of the thread is resumed.
271 * RETURNS
272 * Success: Previous suspend count
273 * Failure: 0xFFFFFFFF
274 * Already running: 0
276 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
278 DWORD ret;
279 NTSTATUS status = NtResumeThread( hthread, &ret );
281 if (status)
283 ret = ~0U;
284 SetLastError( RtlNtStatusToDosError(status) );
286 return ret;
290 /**********************************************************************
291 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
293 * RETURNS
294 * Success: Thread's priority level.
295 * Failure: THREAD_PRIORITY_ERROR_RETURN
297 INT WINAPI GetThreadPriority(
298 HANDLE hthread) /* [in] Handle to thread */
300 THREAD_BASIC_INFORMATION info;
301 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
302 &info, sizeof(info), NULL );
304 if (status)
306 SetLastError( RtlNtStatusToDosError(status) );
307 return THREAD_PRIORITY_ERROR_RETURN;
309 return info.Priority;
313 /**********************************************************************
314 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
316 * RETURNS
317 * Success: TRUE
318 * Failure: FALSE
320 BOOL WINAPI SetThreadPriority(
321 HANDLE hthread, /* [in] Handle to thread */
322 INT priority) /* [in] Thread priority level */
324 DWORD prio = priority;
325 NTSTATUS status;
327 status = NtSetInformationThread(hthread, ThreadBasePriority,
328 &prio, sizeof(prio));
330 if (status)
332 SetLastError( RtlNtStatusToDosError(status) );
333 return FALSE;
336 return TRUE;
340 /**********************************************************************
341 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
343 * Always reports that priority boost is disabled.
345 * RETURNS
346 * Success: TRUE.
347 * Failure: FALSE
349 BOOL WINAPI GetThreadPriorityBoost(
350 HANDLE hthread, /* [in] Handle to thread */
351 PBOOL pstate) /* [out] pointer to var that receives the boost state */
353 if (pstate) *pstate = FALSE;
354 return TRUE;
358 /**********************************************************************
359 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
361 * Priority boost is not implemented. This function always returns
362 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
364 * RETURNS
365 * Always returns FALSE to indicate a failure
367 BOOL WINAPI SetThreadPriorityBoost(
368 HANDLE hthread, /* [in] Handle to thread */
369 BOOL disable) /* [in] TRUE to disable priority boost */
371 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
372 return FALSE;
376 /**********************************************************************
377 * SetThreadAffinityMask (KERNEL32.@)
379 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
381 NTSTATUS status;
382 THREAD_BASIC_INFORMATION tbi;
384 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
385 &tbi, sizeof(tbi), NULL );
386 if (status)
388 SetLastError( RtlNtStatusToDosError(status) );
389 return 0;
391 status = NtSetInformationThread( hThread, ThreadAffinityMask,
392 &dwThreadAffinityMask,
393 sizeof(dwThreadAffinityMask));
394 if (status)
396 SetLastError( RtlNtStatusToDosError(status) );
397 return 0;
399 return tbi.AffinityMask;
403 /**********************************************************************
404 * SetThreadIdealProcessor [KERNEL32.@] Sets preferred processor for thread.
406 * RETURNS
407 * Success: Value of last call to SetThreadIdealProcessor
408 * Failure: -1
410 DWORD WINAPI SetThreadIdealProcessor(
411 HANDLE hThread, /* [in] Specifies the thread of interest */
412 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
414 FIXME("(%p): stub\n",hThread);
415 if (dwIdealProcessor > MAXIMUM_PROCESSORS)
417 SetLastError(ERROR_INVALID_PARAMETER);
418 return ~0u;
420 return 0;
424 /***********************************************************************
425 * GetThreadSelectorEntry (KERNEL32.@)
427 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
429 THREAD_DESCRIPTOR_INFORMATION tdi;
430 NTSTATUS status;
432 tdi.Selector = sel;
433 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry, &tdi, sizeof(tdi), NULL);
434 if (status)
436 SetLastError( RtlNtStatusToDosError(status) );
437 return FALSE;
439 *ldtent = tdi.Entry;
440 return TRUE;
444 /* callback for QueueUserAPC */
445 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
447 PAPCFUNC func = (PAPCFUNC)arg1;
448 func( arg2 );
451 /***********************************************************************
452 * QueueUserAPC (KERNEL32.@)
454 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
456 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
458 if (status) SetLastError( RtlNtStatusToDosError(status) );
459 return !status;
462 /***********************************************************************
463 * QueueUserWorkItem (KERNEL32.@)
465 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
467 NTSTATUS status;
469 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
471 status = RtlQueueWorkItem( Function, Context, Flags );
473 if (status) SetLastError( RtlNtStatusToDosError(status) );
474 return !status;
477 /**********************************************************************
478 * GetThreadTimes [KERNEL32.@] Obtains timing information.
480 * RETURNS
481 * Success: TRUE
482 * Failure: FALSE
484 BOOL WINAPI GetThreadTimes(
485 HANDLE thread, /* [in] Specifies the thread of interest */
486 LPFILETIME creationtime, /* [out] When the thread was created */
487 LPFILETIME exittime, /* [out] When the thread was destroyed */
488 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
489 LPFILETIME usertime) /* [out] Time thread spent in user mode */
491 KERNEL_USER_TIMES kusrt;
492 NTSTATUS status;
494 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
495 sizeof(kusrt), NULL);
496 if (status)
498 SetLastError( RtlNtStatusToDosError(status) );
499 return FALSE;
501 if (creationtime)
503 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
504 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
506 if (exittime)
508 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
509 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
511 if (kerneltime)
513 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
514 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
516 if (usertime)
518 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
519 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
522 return TRUE;
525 /**********************************************************************
526 * GetThreadId [KERNEL32.@]
528 * Retrieve the identifier of a thread.
530 * PARAMS
531 * Thread [I] The thread to retrieve the identifier of.
533 * RETURNS
534 * Success: Identifier of the target thread.
535 * Failure: 0
537 DWORD WINAPI GetThreadId(HANDLE Thread)
539 THREAD_BASIC_INFORMATION tbi;
540 NTSTATUS status;
542 TRACE("(%p)\n", Thread);
544 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
545 sizeof(tbi), NULL);
546 if (status)
548 SetLastError( RtlNtStatusToDosError(status) );
549 return 0;
552 return HandleToULong(tbi.ClientId.UniqueThread);
556 /***********************************************************************
557 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
559 * RETURNS
560 * Pseudohandle for the current thread
562 #undef GetCurrentThread
563 HANDLE WINAPI GetCurrentThread(void)
565 return (HANDLE)~(ULONG_PTR)1;
569 #ifdef __i386__
571 /***********************************************************************
572 * SetLastError (KERNEL32.@)
574 /* void WINAPI SetLastError( DWORD error ); */
575 __ASM_STDCALL_FUNC( SetLastError, 4,
576 "movl 4(%esp),%eax\n\t"
577 ".byte 0x64\n\t"
578 "movl %eax,0x34\n\t"
579 "ret $4" )
581 /***********************************************************************
582 * GetLastError (KERNEL32.@)
584 /* DWORD WINAPI GetLastError(void); */
585 __ASM_STDCALL_FUNC( GetLastError, 0, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
587 /***********************************************************************
588 * GetCurrentProcessId (KERNEL32.@)
590 /* DWORD WINAPI GetCurrentProcessId(void) */
591 __ASM_STDCALL_FUNC( GetCurrentProcessId, 0, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
593 /***********************************************************************
594 * GetCurrentThreadId (KERNEL32.@)
596 /* DWORD WINAPI GetCurrentThreadId(void) */
597 __ASM_STDCALL_FUNC( GetCurrentThreadId, 0, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
599 #else /* __i386__ */
601 /**********************************************************************
602 * SetLastError (KERNEL32.@)
604 * Sets the last-error code.
606 * RETURNS
607 * Nothing.
609 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
611 NtCurrentTeb()->LastErrorValue = error;
614 /**********************************************************************
615 * GetLastError (KERNEL32.@)
617 * Get the last-error code.
619 * RETURNS
620 * last-error code.
622 DWORD WINAPI GetLastError(void)
624 return NtCurrentTeb()->LastErrorValue;
627 /***********************************************************************
628 * GetCurrentProcessId (KERNEL32.@)
630 * Get the current process identifier.
632 * RETURNS
633 * current process identifier
635 DWORD WINAPI GetCurrentProcessId(void)
637 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
640 /***********************************************************************
641 * GetCurrentThreadId (KERNEL32.@)
643 * Get the current thread identifier.
645 * RETURNS
646 * current thread identifier
648 DWORD WINAPI GetCurrentThreadId(void)
650 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
653 #endif /* __i386__ */
655 /*************************************************************************
656 * rtlmode_to_win32mode
658 static DWORD rtlmode_to_win32mode( DWORD rtlmode )
660 DWORD win32mode = 0;
662 if (rtlmode & 0x10)
663 win32mode |= SEM_FAILCRITICALERRORS;
664 if (rtlmode & 0x20)
665 win32mode |= SEM_NOGPFAULTERRORBOX;
666 if (rtlmode & 0x40)
667 win32mode |= SEM_NOOPENFILEERRORBOX;
669 return win32mode;
672 /***********************************************************************
673 * SetThreadErrorMode (KERNEL32.@)
675 * Set the thread local error mode.
677 * PARAMS
678 * mode [I] The new error mode, a bitwise or of SEM_FAILCRITICALERRORS,
679 * SEM_NOGPFAULTERRORBOX and SEM_NOOPENFILEERRORBOX.
680 * oldmode [O] Destination of the old error mode (may be NULL)
682 * RETURNS
683 * Success: TRUE
684 * Failure: FALSE, check GetLastError
686 BOOL WINAPI SetThreadErrorMode( DWORD mode, LPDWORD oldmode )
688 NTSTATUS status;
689 DWORD tmp = 0;
691 if (mode & ~(SEM_FAILCRITICALERRORS |
692 SEM_NOGPFAULTERRORBOX |
693 SEM_NOOPENFILEERRORBOX))
695 SetLastError( ERROR_INVALID_PARAMETER );
696 return FALSE;
699 if (mode & SEM_FAILCRITICALERRORS)
700 tmp |= 0x10;
701 if (mode & SEM_NOGPFAULTERRORBOX)
702 tmp |= 0x20;
703 if (mode & SEM_NOOPENFILEERRORBOX)
704 tmp |= 0x40;
706 status = RtlSetThreadErrorMode( tmp, oldmode );
707 if (status)
709 SetLastError( RtlNtStatusToDosError(status) );
710 return FALSE;
713 if (oldmode)
714 *oldmode = rtlmode_to_win32mode(*oldmode);
716 return TRUE;
719 /***********************************************************************
720 * GetThreadErrorMode (KERNEL32.@)
722 * Get the thread local error mode.
724 * PARAMS
725 * None.
727 * RETURNS
728 * The current thread local error mode.
730 DWORD WINAPI GetThreadErrorMode( void )
732 return rtlmode_to_win32mode( RtlGetThreadErrorMode() );