push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / kernel32 / thread.c
blob973279302e7e591585b76ec3519cf0f61e50a050
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 "wine/winbase16.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 BOOL last;
155 SERVER_START_REQ( terminate_thread )
157 /* send the exit code to the server */
158 req->handle = GetCurrentThread();
159 req->exit_code = code;
160 wine_server_call( req );
161 last = reply->last;
163 SERVER_END_REQ;
165 if (last)
167 LdrShutdownProcess();
168 exit( code );
170 else
172 RtlFreeThreadActivationContextStack();
173 RtlExitUserThread( code );
178 /**********************************************************************
179 * TerminateThread [KERNEL32.@] Terminates a thread
181 * RETURNS
182 * Success: TRUE
183 * Failure: FALSE
185 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
186 DWORD exit_code) /* [in] Exit code for thread */
188 NTSTATUS status = NtTerminateThread( handle, exit_code );
189 if (status) SetLastError( RtlNtStatusToDosError(status) );
190 return !status;
194 /***********************************************************************
195 * FreeLibraryAndExitThread (KERNEL32.@)
197 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
199 FreeLibrary(hLibModule);
200 ExitThread(dwExitCode);
204 /**********************************************************************
205 * GetExitCodeThread (KERNEL32.@)
207 * Gets termination status of thread.
209 * RETURNS
210 * Success: TRUE
211 * Failure: FALSE
213 BOOL WINAPI GetExitCodeThread(
214 HANDLE hthread, /* [in] Handle to thread */
215 LPDWORD exitcode) /* [out] Address to receive termination status */
217 THREAD_BASIC_INFORMATION info;
218 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
219 &info, sizeof(info), NULL );
221 if (status)
223 SetLastError( RtlNtStatusToDosError(status) );
224 return FALSE;
226 if (exitcode) *exitcode = info.ExitStatus;
227 return TRUE;
231 /***********************************************************************
232 * SetThreadContext [KERNEL32.@] Sets context of thread.
234 * RETURNS
235 * Success: TRUE
236 * Failure: FALSE
238 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
239 const CONTEXT *context ) /* [in] Address of context structure */
241 NTSTATUS status = NtSetContextThread( handle, context );
242 if (status) SetLastError( RtlNtStatusToDosError(status) );
243 return !status;
247 /***********************************************************************
248 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
250 * RETURNS
251 * Success: TRUE
252 * Failure: FALSE
254 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
255 CONTEXT *context ) /* [out] Address of context structure */
257 NTSTATUS status = NtGetContextThread( handle, context );
258 if (status) SetLastError( RtlNtStatusToDosError(status) );
259 return !status;
263 /**********************************************************************
264 * SuspendThread [KERNEL32.@] Suspends a thread.
266 * RETURNS
267 * Success: Previous suspend count
268 * Failure: 0xFFFFFFFF
270 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
272 DWORD ret;
273 NTSTATUS status = NtSuspendThread( hthread, &ret );
275 if (status)
277 ret = ~0U;
278 SetLastError( RtlNtStatusToDosError(status) );
280 return ret;
284 /**********************************************************************
285 * ResumeThread [KERNEL32.@] Resumes a thread.
287 * Decrements a thread's suspend count. When count is zero, the
288 * execution of the thread is resumed.
290 * RETURNS
291 * Success: Previous suspend count
292 * Failure: 0xFFFFFFFF
293 * Already running: 0
295 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
297 DWORD ret;
298 NTSTATUS status = NtResumeThread( hthread, &ret );
300 if (status)
302 ret = ~0U;
303 SetLastError( RtlNtStatusToDosError(status) );
305 return ret;
309 /**********************************************************************
310 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
312 * RETURNS
313 * Success: Thread's priority level.
314 * Failure: THREAD_PRIORITY_ERROR_RETURN
316 INT WINAPI GetThreadPriority(
317 HANDLE hthread) /* [in] Handle to thread */
319 THREAD_BASIC_INFORMATION info;
320 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
321 &info, sizeof(info), NULL );
323 if (status)
325 SetLastError( RtlNtStatusToDosError(status) );
326 return THREAD_PRIORITY_ERROR_RETURN;
328 return info.Priority;
332 /**********************************************************************
333 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
335 * RETURNS
336 * Success: TRUE
337 * Failure: FALSE
339 BOOL WINAPI SetThreadPriority(
340 HANDLE hthread, /* [in] Handle to thread */
341 INT priority) /* [in] Thread priority level */
343 DWORD prio = priority;
344 NTSTATUS status;
346 status = NtSetInformationThread(hthread, ThreadBasePriority,
347 &prio, sizeof(prio));
349 if (status)
351 SetLastError( RtlNtStatusToDosError(status) );
352 return FALSE;
355 return TRUE;
359 /**********************************************************************
360 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
362 * Always reports that priority boost is disabled.
364 * RETURNS
365 * Success: TRUE.
366 * Failure: FALSE
368 BOOL WINAPI GetThreadPriorityBoost(
369 HANDLE hthread, /* [in] Handle to thread */
370 PBOOL pstate) /* [out] pointer to var that receives the boost state */
372 if (pstate) *pstate = FALSE;
373 return NO_ERROR;
377 /**********************************************************************
378 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
380 * Priority boost is not implemented. Thsi function always returns
381 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
383 * RETURNS
384 * Always returns FALSE to indicate a failure
386 BOOL WINAPI SetThreadPriorityBoost(
387 HANDLE hthread, /* [in] Handle to thread */
388 BOOL disable) /* [in] TRUE to disable priority boost */
390 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
391 return FALSE;
395 /**********************************************************************
396 * SetThreadAffinityMask (KERNEL32.@)
398 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
400 NTSTATUS status;
401 THREAD_BASIC_INFORMATION tbi;
403 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
404 &tbi, sizeof(tbi), NULL );
405 if (status)
407 SetLastError( RtlNtStatusToDosError(status) );
408 return 0;
410 status = NtSetInformationThread( hThread, ThreadAffinityMask,
411 &dwThreadAffinityMask,
412 sizeof(dwThreadAffinityMask));
413 if (status)
415 SetLastError( RtlNtStatusToDosError(status) );
416 return 0;
418 return tbi.AffinityMask;
422 /**********************************************************************
423 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
425 * RETURNS
426 * Success: Value of last call to SetThreadIdealProcessor
427 * Failure: -1
429 DWORD WINAPI SetThreadIdealProcessor(
430 HANDLE hThread, /* [in] Specifies the thread of interest */
431 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
433 FIXME("(%p): stub\n",hThread);
434 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
435 return -1L;
439 /* callback for QueueUserAPC */
440 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
442 PAPCFUNC func = (PAPCFUNC)arg1;
443 func( arg2 );
446 /***********************************************************************
447 * QueueUserAPC (KERNEL32.@)
449 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
451 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
453 if (status) SetLastError( RtlNtStatusToDosError(status) );
454 return !status;
457 /***********************************************************************
458 * QueueUserWorkItem (KERNEL32.@)
460 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
462 NTSTATUS status;
464 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
466 status = RtlQueueWorkItem( Function, Context, Flags );
468 if (status) SetLastError( RtlNtStatusToDosError(status) );
469 return !status;
472 /**********************************************************************
473 * GetThreadTimes [KERNEL32.@] Obtains timing information.
475 * RETURNS
476 * Success: TRUE
477 * Failure: FALSE
479 BOOL WINAPI GetThreadTimes(
480 HANDLE thread, /* [in] Specifies the thread of interest */
481 LPFILETIME creationtime, /* [out] When the thread was created */
482 LPFILETIME exittime, /* [out] When the thread was destroyed */
483 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
484 LPFILETIME usertime) /* [out] Time thread spent in user mode */
486 KERNEL_USER_TIMES kusrt;
487 NTSTATUS status;
489 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
490 sizeof(kusrt), NULL);
491 if (status)
493 SetLastError( RtlNtStatusToDosError(status) );
494 return FALSE;
496 if (creationtime)
498 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
499 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
501 if (exittime)
503 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
504 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
506 if (kerneltime)
508 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
509 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
511 if (usertime)
513 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
514 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
517 return TRUE;
520 /**********************************************************************
521 * GetThreadId [KERNEL32.@]
523 * Retrieve the identifier of a thread.
525 * PARAMS
526 * Thread [I] The thread to retrieve the identifier of.
528 * RETURNS
529 * Success: Identifier of the target thread.
530 * Failure: 0
532 DWORD WINAPI GetThreadId(HANDLE Thread)
534 THREAD_BASIC_INFORMATION tbi;
535 NTSTATUS status;
537 TRACE("(%p)\n", Thread);
539 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
540 sizeof(tbi), NULL);
541 if (status)
543 SetLastError( RtlNtStatusToDosError(status) );
544 return 0;
547 return HandleToULong(tbi.ClientId.UniqueThread);
551 /**********************************************************************
552 * VWin32_BoostThreadGroup [KERNEL.535]
554 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
556 FIXME("(0x%08x,%d): stub\n", threadId, boost);
560 /**********************************************************************
561 * VWin32_BoostThreadStatic [KERNEL.536]
563 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
565 FIXME("(0x%08x,%d): stub\n", threadId, boost);
569 /***********************************************************************
570 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
572 * RETURNS
573 * Pseudohandle for the current thread
575 #undef GetCurrentThread
576 HANDLE WINAPI GetCurrentThread(void)
578 return (HANDLE)0xfffffffe;
582 #ifdef __i386__
584 /***********************************************************************
585 * SetLastError (KERNEL.147)
586 * SetLastError (KERNEL32.@)
588 /* void WINAPI SetLastError( DWORD error ); */
589 __ASM_GLOBAL_FUNC( SetLastError,
590 "movl 4(%esp),%eax\n\t"
591 ".byte 0x64\n\t"
592 "movl %eax,0x34\n\t"
593 "ret $4" )
595 /***********************************************************************
596 * GetLastError (KERNEL.148)
597 * GetLastError (KERNEL32.@)
599 /* DWORD WINAPI GetLastError(void); */
600 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
602 /***********************************************************************
603 * GetCurrentProcessId (KERNEL.471)
604 * GetCurrentProcessId (KERNEL32.@)
606 /* DWORD WINAPI GetCurrentProcessId(void) */
607 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
609 /***********************************************************************
610 * GetCurrentThreadId (KERNEL.462)
611 * GetCurrentThreadId (KERNEL32.@)
613 /* DWORD WINAPI GetCurrentThreadId(void) */
614 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
616 #else /* __i386__ */
618 /**********************************************************************
619 * SetLastError (KERNEL.147)
620 * SetLastError (KERNEL32.@)
622 * Sets the last-error code.
624 * RETURNS
625 * Nothing.
627 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
629 NtCurrentTeb()->LastErrorValue = error;
632 /**********************************************************************
633 * GetLastError (KERNEL.148)
634 * GetLastError (KERNEL32.@)
636 * Get the last-error code.
638 * RETURNS
639 * last-error code.
641 DWORD WINAPI GetLastError(void)
643 return NtCurrentTeb()->LastErrorValue;
646 /***********************************************************************
647 * GetCurrentProcessId (KERNEL.471)
648 * GetCurrentProcessId (KERNEL32.@)
650 * Get the current process identifier.
652 * RETURNS
653 * current process identifier
655 DWORD WINAPI GetCurrentProcessId(void)
657 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
660 /***********************************************************************
661 * GetCurrentThreadId (KERNEL.462)
662 * GetCurrentThreadId (KERNEL32.@)
664 * Get the current thread identifier.
666 * RETURNS
667 * current thread identifier
669 DWORD WINAPI GetCurrentThreadId(void)
671 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
674 #endif /* __i386__ */