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
22 #include "wine/port.h"
27 #include <sys/types.h>
33 #define WIN32_NO_STATUS
38 #include "wine/winbase16.h"
39 #include "wine/exception.h"
40 #include "wine/library.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 #include "kernel_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread);
49 /***********************************************************************
50 * CreateThread (KERNEL32.@)
52 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
53 LPTHREAD_START_ROUTINE start, LPVOID param,
54 DWORD flags, LPDWORD id )
56 return CreateRemoteThread( GetCurrentProcess(),
57 sa, stack, start, param, flags, id );
61 /***************************************************************************
62 * CreateRemoteThread (KERNEL32.@)
64 * Creates a thread that runs in the address space of another process
69 * Success: Handle to the new thread.
70 * Failure: NULL. Use GetLastError() to find the error cause.
73 * Improper memory allocation: there's no ability to free new_thread_info
75 * Bad start address for RtlCreateUserThread because the library
76 * may be loaded at different address in other process.
78 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
79 LPTHREAD_START_ROUTINE start, LPVOID param,
80 DWORD flags, LPDWORD id )
85 SIZE_T stack_reserve = 0, stack_commit = 0;
87 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
88 else stack_commit = stack;
90 status = RtlCreateUserThread( hProcess, NULL, TRUE,
91 NULL, stack_reserve, stack_commit,
92 (PRTL_THREAD_START_ROUTINE)start, param, &handle, &client_id );
93 if (status == STATUS_SUCCESS)
95 if (id) *id = HandleToULong(client_id.UniqueThread);
96 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
97 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
98 if (!(flags & CREATE_SUSPENDED))
101 if (NtResumeThread( handle, &ret ))
104 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
111 SetLastError( RtlNtStatusToDosError(status) );
118 /***********************************************************************
119 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
121 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
125 OBJECT_ATTRIBUTES attr;
128 attr.Length = sizeof(attr);
129 attr.RootDirectory = 0;
130 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
131 attr.ObjectName = NULL;
132 attr.SecurityDescriptor = NULL;
133 attr.SecurityQualityOfService = NULL;
135 cid.UniqueProcess = 0; /* FIXME */
136 cid.UniqueThread = ULongToHandle(dwThreadId);
137 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
140 SetLastError( RtlNtStatusToDosError(status) );
147 /***********************************************************************
148 * ExitThread [KERNEL32.@] Ends a thread
153 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
155 RtlFreeThreadActivationContextStack();
156 RtlExitUserThread( code );
160 /**********************************************************************
161 * TerminateThread [KERNEL32.@] Terminates a thread
167 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
168 DWORD exit_code) /* [in] Exit code for thread */
170 NTSTATUS status = NtTerminateThread( handle, exit_code );
171 if (status) SetLastError( RtlNtStatusToDosError(status) );
176 /***********************************************************************
177 * FreeLibraryAndExitThread (KERNEL32.@)
179 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
181 FreeLibrary(hLibModule);
182 ExitThread(dwExitCode);
186 /**********************************************************************
187 * GetExitCodeThread (KERNEL32.@)
189 * Gets termination status of thread.
195 BOOL WINAPI GetExitCodeThread(
196 HANDLE hthread, /* [in] Handle to thread */
197 LPDWORD exitcode) /* [out] Address to receive termination status */
199 THREAD_BASIC_INFORMATION info;
200 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
201 &info, sizeof(info), NULL );
205 SetLastError( RtlNtStatusToDosError(status) );
208 if (exitcode) *exitcode = info.ExitStatus;
213 /***********************************************************************
214 * SetThreadContext [KERNEL32.@] Sets context of thread.
220 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
221 const CONTEXT *context ) /* [in] Address of context structure */
223 NTSTATUS status = NtSetContextThread( handle, context );
224 if (status) SetLastError( RtlNtStatusToDosError(status) );
229 /***********************************************************************
230 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
236 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
237 CONTEXT *context ) /* [out] Address of context structure */
239 NTSTATUS status = NtGetContextThread( handle, context );
240 if (status) SetLastError( RtlNtStatusToDosError(status) );
245 /**********************************************************************
246 * SuspendThread [KERNEL32.@] Suspends a thread.
249 * Success: Previous suspend count
250 * Failure: 0xFFFFFFFF
252 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
255 NTSTATUS status = NtSuspendThread( hthread, &ret );
260 SetLastError( RtlNtStatusToDosError(status) );
266 /**********************************************************************
267 * ResumeThread [KERNEL32.@] Resumes a thread.
269 * Decrements a thread's suspend count. When count is zero, the
270 * execution of the thread is resumed.
273 * Success: Previous suspend count
274 * Failure: 0xFFFFFFFF
277 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
280 NTSTATUS status = NtResumeThread( hthread, &ret );
285 SetLastError( RtlNtStatusToDosError(status) );
291 /**********************************************************************
292 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
295 * Success: Thread's priority level.
296 * Failure: THREAD_PRIORITY_ERROR_RETURN
298 INT WINAPI GetThreadPriority(
299 HANDLE hthread) /* [in] Handle to thread */
301 THREAD_BASIC_INFORMATION info;
302 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
303 &info, sizeof(info), NULL );
307 SetLastError( RtlNtStatusToDosError(status) );
308 return THREAD_PRIORITY_ERROR_RETURN;
310 return info.Priority;
314 /**********************************************************************
315 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
321 BOOL WINAPI SetThreadPriority(
322 HANDLE hthread, /* [in] Handle to thread */
323 INT priority) /* [in] Thread priority level */
325 DWORD prio = priority;
328 status = NtSetInformationThread(hthread, ThreadBasePriority,
329 &prio, sizeof(prio));
333 SetLastError( RtlNtStatusToDosError(status) );
341 /**********************************************************************
342 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
344 * Always reports that priority boost is disabled.
350 BOOL WINAPI GetThreadPriorityBoost(
351 HANDLE hthread, /* [in] Handle to thread */
352 PBOOL pstate) /* [out] pointer to var that receives the boost state */
354 if (pstate) *pstate = FALSE;
359 /**********************************************************************
360 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
362 * Priority boost is not implemented. This function always returns
363 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
366 * Always returns FALSE to indicate a failure
368 BOOL WINAPI SetThreadPriorityBoost(
369 HANDLE hthread, /* [in] Handle to thread */
370 BOOL disable) /* [in] TRUE to disable priority boost */
372 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
377 /**********************************************************************
378 * SetThreadAffinityMask (KERNEL32.@)
380 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
383 THREAD_BASIC_INFORMATION tbi;
385 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
386 &tbi, sizeof(tbi), NULL );
389 SetLastError( RtlNtStatusToDosError(status) );
392 status = NtSetInformationThread( hThread, ThreadAffinityMask,
393 &dwThreadAffinityMask,
394 sizeof(dwThreadAffinityMask));
397 SetLastError( RtlNtStatusToDosError(status) );
400 return tbi.AffinityMask;
404 /**********************************************************************
405 * SetThreadIdealProcessor [KERNEL32.@] Sets preferred processor for thread.
408 * Success: Value of last call to SetThreadIdealProcessor
411 DWORD WINAPI SetThreadIdealProcessor(
412 HANDLE hThread, /* [in] Specifies the thread of interest */
413 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
415 FIXME("(%p): stub\n",hThread);
416 if (dwIdealProcessor > MAXIMUM_PROCESSORS)
418 SetLastError(ERROR_INVALID_PARAMETER);
425 /* callback for QueueUserAPC */
426 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
428 PAPCFUNC func = (PAPCFUNC)arg1;
432 /***********************************************************************
433 * QueueUserAPC (KERNEL32.@)
435 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
437 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
439 if (status) SetLastError( RtlNtStatusToDosError(status) );
443 /***********************************************************************
444 * QueueUserWorkItem (KERNEL32.@)
446 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
450 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
452 status = RtlQueueWorkItem( Function, Context, Flags );
454 if (status) SetLastError( RtlNtStatusToDosError(status) );
458 /**********************************************************************
459 * GetThreadTimes [KERNEL32.@] Obtains timing information.
465 BOOL WINAPI GetThreadTimes(
466 HANDLE thread, /* [in] Specifies the thread of interest */
467 LPFILETIME creationtime, /* [out] When the thread was created */
468 LPFILETIME exittime, /* [out] When the thread was destroyed */
469 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
470 LPFILETIME usertime) /* [out] Time thread spent in user mode */
472 KERNEL_USER_TIMES kusrt;
475 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
476 sizeof(kusrt), NULL);
479 SetLastError( RtlNtStatusToDosError(status) );
484 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
485 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
489 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
490 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
494 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
495 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
499 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
500 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
506 /**********************************************************************
507 * GetThreadId [KERNEL32.@]
509 * Retrieve the identifier of a thread.
512 * Thread [I] The thread to retrieve the identifier of.
515 * Success: Identifier of the target thread.
518 DWORD WINAPI GetThreadId(HANDLE Thread)
520 THREAD_BASIC_INFORMATION tbi;
523 TRACE("(%p)\n", Thread);
525 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
529 SetLastError( RtlNtStatusToDosError(status) );
533 return HandleToULong(tbi.ClientId.UniqueThread);
537 /**********************************************************************
538 * VWin32_BoostThreadGroup [KERNEL.535]
540 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
542 FIXME("(0x%08x,%d): stub\n", threadId, boost);
546 /**********************************************************************
547 * VWin32_BoostThreadStatic [KERNEL.536]
549 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
551 FIXME("(0x%08x,%d): stub\n", threadId, boost);
555 /***********************************************************************
556 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
559 * Pseudohandle for the current thread
561 #undef GetCurrentThread
562 HANDLE WINAPI GetCurrentThread(void)
564 return (HANDLE)~(ULONG_PTR)1;
570 /***********************************************************************
571 * SetLastError (KERNEL.147)
572 * SetLastError (KERNEL32.@)
574 /* void WINAPI SetLastError( DWORD error ); */
575 __ASM_STDCALL_FUNC( SetLastError, 4,
576 "movl 4(%esp),%eax\n\t"
581 /***********************************************************************
582 * GetLastError (KERNEL.148)
583 * GetLastError (KERNEL32.@)
585 /* DWORD WINAPI GetLastError(void); */
586 __ASM_STDCALL_FUNC( GetLastError, 0, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
588 /***********************************************************************
589 * GetCurrentProcessId (KERNEL.471)
590 * GetCurrentProcessId (KERNEL32.@)
592 /* DWORD WINAPI GetCurrentProcessId(void) */
593 __ASM_STDCALL_FUNC( GetCurrentProcessId, 0, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
595 /***********************************************************************
596 * GetCurrentThreadId (KERNEL.462)
597 * GetCurrentThreadId (KERNEL32.@)
599 /* DWORD WINAPI GetCurrentThreadId(void) */
600 __ASM_STDCALL_FUNC( GetCurrentThreadId, 0, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
604 /**********************************************************************
605 * SetLastError (KERNEL.147)
606 * SetLastError (KERNEL32.@)
608 * Sets the last-error code.
613 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
615 NtCurrentTeb()->LastErrorValue = error;
618 /**********************************************************************
619 * GetLastError (KERNEL.148)
620 * GetLastError (KERNEL32.@)
622 * Get the last-error code.
627 DWORD WINAPI GetLastError(void)
629 return NtCurrentTeb()->LastErrorValue;
632 /***********************************************************************
633 * GetCurrentProcessId (KERNEL.471)
634 * GetCurrentProcessId (KERNEL32.@)
636 * Get the current process identifier.
639 * current process identifier
641 DWORD WINAPI GetCurrentProcessId(void)
643 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
646 /***********************************************************************
647 * GetCurrentThreadId (KERNEL.462)
648 * GetCurrentThreadId (KERNEL32.@)
650 * Get the current thread identifier.
653 * current thread identifier
655 DWORD WINAPI GetCurrentThreadId(void)
657 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
660 #endif /* __i386__ */