gdi32: Use exec() and _exit() instead of system() and exit().
[wine/hacks.git] / dlls / kernel32 / thread.c
blobcdbe1e129f257ecd71c33069bde2526de038a92f
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 "winnls.h"
38 #include "thread.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);
50 /***********************************************************************
51 * CreateThread (KERNEL32.@)
53 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
54 LPTHREAD_START_ROUTINE start, LPVOID param,
55 DWORD flags, LPDWORD id )
57 return CreateRemoteThread( GetCurrentProcess(),
58 sa, stack, start, param, flags, id );
62 /***************************************************************************
63 * CreateRemoteThread (KERNEL32.@)
65 * Creates a thread that runs in the address space of another process
67 * PARAMS
69 * RETURNS
70 * Success: Handle to the new thread.
71 * Failure: NULL. Use GetLastError() to find the error cause.
73 * BUGS
74 * Improper memory allocation: there's no ability to free new_thread_info
75 * in other process.
76 * Bad start address for RtlCreateUserThread because the library
77 * may be loaded at different address in other process.
79 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
80 LPTHREAD_START_ROUTINE start, LPVOID param,
81 DWORD flags, LPDWORD id )
83 HANDLE handle;
84 CLIENT_ID client_id;
85 NTSTATUS status;
86 SIZE_T stack_reserve = 0, stack_commit = 0;
88 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
89 else stack_commit = stack;
91 status = RtlCreateUserThread( hProcess, NULL, TRUE,
92 NULL, stack_reserve, stack_commit,
93 (PRTL_THREAD_START_ROUTINE)start, param, &handle, &client_id );
94 if (status == STATUS_SUCCESS)
96 if (id) *id = (DWORD)client_id.UniqueThread;
97 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
98 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
99 if (!(flags & CREATE_SUSPENDED))
101 ULONG ret;
102 if (NtResumeThread( handle, &ret ))
104 NtClose( handle );
105 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
106 handle = 0;
110 else
112 SetLastError( RtlNtStatusToDosError(status) );
113 handle = 0;
115 return handle;
119 /***********************************************************************
120 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
122 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
124 NTSTATUS status;
125 HANDLE handle;
126 OBJECT_ATTRIBUTES attr;
127 CLIENT_ID cid;
129 attr.Length = sizeof(attr);
130 attr.RootDirectory = 0;
131 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
132 attr.ObjectName = NULL;
133 attr.SecurityDescriptor = NULL;
134 attr.SecurityQualityOfService = NULL;
136 cid.UniqueProcess = 0; /* FIXME */
137 cid.UniqueThread = (HANDLE)dwThreadId;
138 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
139 if (status)
141 SetLastError( RtlNtStatusToDosError(status) );
142 handle = 0;
144 return handle;
148 /***********************************************************************
149 * ExitThread [KERNEL32.@] Ends a thread
151 * RETURNS
152 * None
154 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
156 BOOL last;
157 SERVER_START_REQ( terminate_thread )
159 /* send the exit code to the server */
160 req->handle = GetCurrentThread();
161 req->exit_code = code;
162 wine_server_call( req );
163 last = reply->last;
165 SERVER_END_REQ;
167 if (last)
169 LdrShutdownProcess();
170 exit( code );
172 else RtlExitUserThread( code );
176 /**********************************************************************
177 * TerminateThread [KERNEL32.@] Terminates a thread
179 * RETURNS
180 * Success: TRUE
181 * Failure: FALSE
183 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
184 DWORD exit_code) /* [in] Exit code for thread */
186 NTSTATUS status = NtTerminateThread( handle, exit_code );
187 if (status) SetLastError( RtlNtStatusToDosError(status) );
188 return !status;
192 /***********************************************************************
193 * FreeLibraryAndExitThread (KERNEL32.@)
195 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
197 FreeLibrary(hLibModule);
198 ExitThread(dwExitCode);
202 /**********************************************************************
203 * GetExitCodeThread (KERNEL32.@)
205 * Gets termination status of thread.
207 * RETURNS
208 * Success: TRUE
209 * Failure: FALSE
211 BOOL WINAPI GetExitCodeThread(
212 HANDLE hthread, /* [in] Handle to thread */
213 LPDWORD exitcode) /* [out] Address to receive termination status */
215 THREAD_BASIC_INFORMATION info;
216 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
217 &info, sizeof(info), NULL );
219 if (status)
221 SetLastError( RtlNtStatusToDosError(status) );
222 return FALSE;
224 if (exitcode) *exitcode = info.ExitStatus;
225 return TRUE;
229 /***********************************************************************
230 * SetThreadContext [KERNEL32.@] Sets context of thread.
232 * RETURNS
233 * Success: TRUE
234 * Failure: FALSE
236 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
237 const CONTEXT *context ) /* [in] Address of context structure */
239 NTSTATUS status = NtSetContextThread( handle, context );
240 if (status) SetLastError( RtlNtStatusToDosError(status) );
241 return !status;
245 /***********************************************************************
246 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
248 * RETURNS
249 * Success: TRUE
250 * Failure: FALSE
252 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
253 CONTEXT *context ) /* [out] Address of context structure */
255 NTSTATUS status = NtGetContextThread( handle, context );
256 if (status) SetLastError( RtlNtStatusToDosError(status) );
257 return !status;
261 /**********************************************************************
262 * SuspendThread [KERNEL32.@] Suspends a thread.
264 * RETURNS
265 * Success: Previous suspend count
266 * Failure: 0xFFFFFFFF
268 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
270 DWORD ret;
271 NTSTATUS status = NtSuspendThread( hthread, &ret );
273 if (status)
275 ret = ~0U;
276 SetLastError( RtlNtStatusToDosError(status) );
278 return ret;
282 /**********************************************************************
283 * ResumeThread [KERNEL32.@] Resumes a thread.
285 * Decrements a thread's suspend count. When count is zero, the
286 * execution of the thread is resumed.
288 * RETURNS
289 * Success: Previous suspend count
290 * Failure: 0xFFFFFFFF
291 * Already running: 0
293 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
295 DWORD ret;
296 NTSTATUS status = NtResumeThread( hthread, &ret );
298 if (status)
300 ret = ~0U;
301 SetLastError( RtlNtStatusToDosError(status) );
303 return ret;
307 /**********************************************************************
308 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
310 * RETURNS
311 * Success: Thread's priority level.
312 * Failure: THREAD_PRIORITY_ERROR_RETURN
314 INT WINAPI GetThreadPriority(
315 HANDLE hthread) /* [in] Handle to thread */
317 THREAD_BASIC_INFORMATION info;
318 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
319 &info, sizeof(info), NULL );
321 if (status)
323 SetLastError( RtlNtStatusToDosError(status) );
324 return THREAD_PRIORITY_ERROR_RETURN;
326 return info.Priority;
330 /**********************************************************************
331 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
333 * RETURNS
334 * Success: TRUE
335 * Failure: FALSE
337 BOOL WINAPI SetThreadPriority(
338 HANDLE hthread, /* [in] Handle to thread */
339 INT priority) /* [in] Thread priority level */
341 DWORD prio = priority;
342 NTSTATUS status;
344 status = NtSetInformationThread(hthread, ThreadBasePriority,
345 &prio, sizeof(prio));
347 if (status)
349 SetLastError( RtlNtStatusToDosError(status) );
350 return FALSE;
353 return TRUE;
357 /**********************************************************************
358 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
360 * Always reports that priority boost is disabled.
362 * RETURNS
363 * Success: TRUE.
364 * Failure: FALSE
366 BOOL WINAPI GetThreadPriorityBoost(
367 HANDLE hthread, /* [in] Handle to thread */
368 PBOOL pstate) /* [out] pointer to var that receives the boost state */
370 if (pstate) *pstate = FALSE;
371 return NO_ERROR;
375 /**********************************************************************
376 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
378 * Priority boost is not implemented. Thsi function always returns
379 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
381 * RETURNS
382 * Always returns FALSE to indicate a failure
384 BOOL WINAPI SetThreadPriorityBoost(
385 HANDLE hthread, /* [in] Handle to thread */
386 BOOL disable) /* [in] TRUE to disable priority boost */
388 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
389 return FALSE;
393 /**********************************************************************
394 * SetThreadAffinityMask (KERNEL32.@)
396 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
398 NTSTATUS status;
399 THREAD_BASIC_INFORMATION tbi;
401 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
402 &tbi, sizeof(tbi), NULL );
403 if (status)
405 SetLastError( RtlNtStatusToDosError(status) );
406 return 0;
408 status = NtSetInformationThread( hThread, ThreadAffinityMask,
409 &dwThreadAffinityMask,
410 sizeof(dwThreadAffinityMask));
411 if (status)
413 SetLastError( RtlNtStatusToDosError(status) );
414 return 0;
416 return tbi.AffinityMask;
420 /**********************************************************************
421 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
423 * RETURNS
424 * Success: Value of last call to SetThreadIdealProcessor
425 * Failure: -1
427 DWORD WINAPI SetThreadIdealProcessor(
428 HANDLE hThread, /* [in] Specifies the thread of interest */
429 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
431 FIXME("(%p): stub\n",hThread);
432 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
433 return -1L;
437 /* callback for QueueUserAPC */
438 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
440 PAPCFUNC func = (PAPCFUNC)arg1;
441 func( arg2 );
444 /***********************************************************************
445 * QueueUserAPC (KERNEL32.@)
447 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
449 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
451 if (status) SetLastError( RtlNtStatusToDosError(status) );
452 return !status;
455 /***********************************************************************
456 * QueueUserWorkItem (KERNEL32.@)
458 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
460 NTSTATUS status;
462 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
464 status = RtlQueueWorkItem( Function, Context, Flags );
466 if (status) SetLastError( RtlNtStatusToDosError(status) );
467 return !status;
470 /**********************************************************************
471 * GetThreadTimes [KERNEL32.@] Obtains timing information.
473 * RETURNS
474 * Success: TRUE
475 * Failure: FALSE
477 BOOL WINAPI GetThreadTimes(
478 HANDLE thread, /* [in] Specifies the thread of interest */
479 LPFILETIME creationtime, /* [out] When the thread was created */
480 LPFILETIME exittime, /* [out] When the thread was destroyed */
481 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
482 LPFILETIME usertime) /* [out] Time thread spent in user mode */
484 KERNEL_USER_TIMES kusrt;
485 NTSTATUS status;
487 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
488 sizeof(kusrt), NULL);
489 if (status)
491 SetLastError( RtlNtStatusToDosError(status) );
492 return FALSE;
494 if (creationtime)
496 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
497 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
499 if (exittime)
501 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
502 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
504 if (kerneltime)
506 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
507 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
509 if (usertime)
511 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
512 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
515 return TRUE;
519 /**********************************************************************
520 * VWin32_BoostThreadGroup [KERNEL.535]
522 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
524 FIXME("(0x%08x,%d): stub\n", threadId, boost);
528 /**********************************************************************
529 * VWin32_BoostThreadStatic [KERNEL.536]
531 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
533 FIXME("(0x%08x,%d): stub\n", threadId, boost);
537 /***********************************************************************
538 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
540 * RETURNS
541 * Pseudohandle for the current thread
543 #undef GetCurrentThread
544 HANDLE WINAPI GetCurrentThread(void)
546 return (HANDLE)0xfffffffe;
550 #ifdef __i386__
552 /***********************************************************************
553 * SetLastError (KERNEL.147)
554 * SetLastError (KERNEL32.@)
556 /* void WINAPI SetLastError( DWORD error ); */
557 __ASM_GLOBAL_FUNC( SetLastError,
558 "movl 4(%esp),%eax\n\t"
559 ".byte 0x64\n\t"
560 "movl %eax,0x34\n\t"
561 "ret $4" )
563 /***********************************************************************
564 * GetLastError (KERNEL.148)
565 * GetLastError (KERNEL32.@)
567 /* DWORD WINAPI GetLastError(void); */
568 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
570 /***********************************************************************
571 * GetCurrentProcessId (KERNEL.471)
572 * GetCurrentProcessId (KERNEL32.@)
574 /* DWORD WINAPI GetCurrentProcessId(void) */
575 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
577 /***********************************************************************
578 * GetCurrentThreadId (KERNEL.462)
579 * GetCurrentThreadId (KERNEL32.@)
581 /* DWORD WINAPI GetCurrentThreadId(void) */
582 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
584 #else /* __i386__ */
586 /**********************************************************************
587 * SetLastError (KERNEL.147)
588 * SetLastError (KERNEL32.@)
590 * Sets the last-error code.
592 * RETURNS
593 * Nothing.
595 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
597 NtCurrentTeb()->LastErrorValue = error;
600 /**********************************************************************
601 * GetLastError (KERNEL.148)
602 * GetLastError (KERNEL32.@)
604 * Get the last-error code.
606 * RETURNS
607 * last-error code.
609 DWORD WINAPI GetLastError(void)
611 return NtCurrentTeb()->LastErrorValue;
614 /***********************************************************************
615 * GetCurrentProcessId (KERNEL.471)
616 * GetCurrentProcessId (KERNEL32.@)
618 * Get the current process identifier.
620 * RETURNS
621 * current process identifier
623 DWORD WINAPI GetCurrentProcessId(void)
625 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
628 /***********************************************************************
629 * GetCurrentThreadId (KERNEL.462)
630 * GetCurrentThreadId (KERNEL32.@)
632 * Get the current thread identifier.
634 * RETURNS
635 * current thread identifier
637 DWORD WINAPI GetCurrentThreadId(void)
639 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
642 #endif /* __i386__ */