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/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
68 * Success: Handle to the new thread.
69 * Failure: NULL. Use GetLastError() to find the error cause.
72 * Improper memory allocation: there's no ability to free new_thread_info
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
)
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
))
100 if (NtResumeThread( handle
, &ret
))
103 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
110 SetLastError( RtlNtStatusToDosError(status
) );
117 /***********************************************************************
118 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
120 HANDLE WINAPI
OpenThread( DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
)
124 OBJECT_ATTRIBUTES attr
;
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
);
139 SetLastError( RtlNtStatusToDosError(status
) );
146 /***********************************************************************
147 * ExitThread [KERNEL32.@] Ends a thread
152 void WINAPI
ExitThread( DWORD code
) /* [in] Exit code for this thread */
154 RtlFreeThreadActivationContextStack();
155 RtlExitUserThread( code
);
159 /**********************************************************************
160 * TerminateThread [KERNEL32.@] Terminates a thread
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
) );
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.
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
);
204 SetLastError( RtlNtStatusToDosError(status
) );
207 if (exitcode
) *exitcode
= info
.ExitStatus
;
212 /***********************************************************************
213 * SetThreadContext [KERNEL32.@] Sets context of thread.
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
) );
228 /***********************************************************************
229 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
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
) );
244 /**********************************************************************
245 * SuspendThread [KERNEL32.@] Suspends a thread.
248 * Success: Previous suspend count
249 * Failure: 0xFFFFFFFF
251 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
254 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
259 SetLastError( RtlNtStatusToDosError(status
) );
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.
272 * Success: Previous suspend count
273 * Failure: 0xFFFFFFFF
276 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
279 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
284 SetLastError( RtlNtStatusToDosError(status
) );
290 /**********************************************************************
291 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
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
);
306 SetLastError( RtlNtStatusToDosError(status
) );
307 return THREAD_PRIORITY_ERROR_RETURN
;
309 return info
.Priority
;
313 /**********************************************************************
314 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
320 BOOL WINAPI
SetThreadPriority(
321 HANDLE hthread
, /* [in] Handle to thread */
322 INT priority
) /* [in] Thread priority level */
324 DWORD prio
= priority
;
327 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
328 &prio
, sizeof(prio
));
332 SetLastError( RtlNtStatusToDosError(status
) );
340 /**********************************************************************
341 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
343 * Always reports that priority boost is disabled.
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
;
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
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
);
376 /**********************************************************************
377 * SetThreadAffinityMask (KERNEL32.@)
379 DWORD_PTR WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD_PTR dwThreadAffinityMask
)
382 THREAD_BASIC_INFORMATION tbi
;
384 status
= NtQueryInformationThread( hThread
, ThreadBasicInformation
,
385 &tbi
, sizeof(tbi
), NULL
);
388 SetLastError( RtlNtStatusToDosError(status
) );
391 status
= NtSetInformationThread( hThread
, ThreadAffinityMask
,
392 &dwThreadAffinityMask
,
393 sizeof(dwThreadAffinityMask
));
396 SetLastError( RtlNtStatusToDosError(status
) );
399 return tbi
.AffinityMask
;
403 /**********************************************************************
404 * SetThreadIdealProcessor [KERNEL32.@] Sets preferred processor for thread.
407 * Success: Value of last call to SetThreadIdealProcessor
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
);
424 /***********************************************************************
425 * GetThreadSelectorEntry (KERNEL32.@)
427 BOOL WINAPI
GetThreadSelectorEntry( HANDLE hthread
, DWORD sel
, LPLDT_ENTRY ldtent
)
429 THREAD_DESCRIPTOR_INFORMATION tdi
;
433 status
= NtQueryInformationThread( hthread
, ThreadDescriptorTableEntry
, &tdi
, sizeof(tdi
), NULL
);
436 SetLastError( RtlNtStatusToDosError(status
) );
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
;
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
) );
462 /***********************************************************************
463 * QueueUserWorkItem (KERNEL32.@)
465 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
469 TRACE("(%p,%p,0x%08x)\n", Function
, Context
, Flags
);
471 status
= RtlQueueWorkItem( Function
, Context
, Flags
);
473 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
477 /**********************************************************************
478 * GetThreadTimes [KERNEL32.@] Obtains timing information.
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
;
494 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
495 sizeof(kusrt
), NULL
);
498 SetLastError( RtlNtStatusToDosError(status
) );
503 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
504 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
508 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
509 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
513 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
514 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
518 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
519 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
525 /**********************************************************************
526 * GetThreadId [KERNEL32.@]
528 * Retrieve the identifier of a thread.
531 * Thread [I] The thread to retrieve the identifier of.
534 * Success: Identifier of the target thread.
537 DWORD WINAPI
GetThreadId(HANDLE Thread
)
539 THREAD_BASIC_INFORMATION tbi
;
542 TRACE("(%p)\n", Thread
);
544 status
= NtQueryInformationThread(Thread
, ThreadBasicInformation
, &tbi
,
548 SetLastError( RtlNtStatusToDosError(status
) );
552 return HandleToULong(tbi
.ClientId
.UniqueThread
);
556 /***********************************************************************
557 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
560 * Pseudohandle for the current thread
562 #undef GetCurrentThread
563 HANDLE WINAPI
GetCurrentThread(void)
565 return (HANDLE
)~(ULONG_PTR
)1;
571 /***********************************************************************
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 (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 /***********************************************************************
600 * GetProcessHeap (KERNEL32.@)
602 /* HANDLE WINAPI GetProcessHeap(void) */
603 __ASM_STDCALL_FUNC( GetProcessHeap
, 0, ".byte 0x64\n\tmovl 0x30,%eax\n\tmovl 0x18(%eax),%eax\n\tret");
605 #elif defined(__x86_64__)
607 /***********************************************************************
608 * SetLastError (KERNEL32.@)
610 /* void WINAPI SetLastError( DWORD error ); */
611 __ASM_STDCALL_FUNC( SetLastError
, 8, ".byte 0x65\n\tmovl %ecx,0x68\n\tret" );
613 /***********************************************************************
614 * GetLastError (KERNEL32.@)
616 /* DWORD WINAPI GetLastError(void); */
617 __ASM_STDCALL_FUNC( GetLastError
, 0, ".byte 0x65\n\tmovl 0x68,%eax\n\tret" );
619 /***********************************************************************
620 * GetCurrentProcessId (KERNEL32.@)
622 /* DWORD WINAPI GetCurrentProcessId(void) */
623 __ASM_STDCALL_FUNC( GetCurrentProcessId
, 0, ".byte 0x65\n\tmovl 0x40,%eax\n\tret" );
625 /***********************************************************************
626 * GetCurrentThreadId (KERNEL32.@)
628 /* DWORD WINAPI GetCurrentThreadId(void) */
629 __ASM_STDCALL_FUNC( GetCurrentThreadId
, 0, ".byte 0x65\n\tmovl 0x48,%eax\n\tret" );
631 /***********************************************************************
632 * GetProcessHeap (KERNEL32.@)
634 /* HANDLE WINAPI GetProcessHeap(void) */
635 __ASM_STDCALL_FUNC( GetProcessHeap
, 0, ".byte 0x65\n\tmovq 0x60,%rax\n\tmovq 0x30(%rax),%rax\n\tret");
637 #else /* __x86_64__ */
639 /**********************************************************************
640 * SetLastError (KERNEL32.@)
642 * Sets the last-error code.
647 void WINAPI
SetLastError( DWORD error
) /* [in] Per-thread error code */
649 NtCurrentTeb()->LastErrorValue
= error
;
652 /**********************************************************************
653 * GetLastError (KERNEL32.@)
655 * Get the last-error code.
660 DWORD WINAPI
GetLastError(void)
662 return NtCurrentTeb()->LastErrorValue
;
665 /***********************************************************************
666 * GetCurrentProcessId (KERNEL32.@)
668 * Get the current process identifier.
671 * current process identifier
673 DWORD WINAPI
GetCurrentProcessId(void)
675 return HandleToULong(NtCurrentTeb()->ClientId
.UniqueProcess
);
678 /***********************************************************************
679 * GetCurrentThreadId (KERNEL32.@)
681 * Get the current thread identifier.
684 * current thread identifier
686 DWORD WINAPI
GetCurrentThreadId(void)
688 return HandleToULong(NtCurrentTeb()->ClientId
.UniqueThread
);
691 /***********************************************************************
692 * GetProcessHeap (KERNEL32.@)
694 HANDLE WINAPI
GetProcessHeap(void)
696 return NtCurrentTeb()->Peb
->ProcessHeap
;
699 #endif /* __i386__ */
701 /*************************************************************************
702 * rtlmode_to_win32mode
704 static DWORD
rtlmode_to_win32mode( DWORD rtlmode
)
709 win32mode
|= SEM_FAILCRITICALERRORS
;
711 win32mode
|= SEM_NOGPFAULTERRORBOX
;
713 win32mode
|= SEM_NOOPENFILEERRORBOX
;
718 /***********************************************************************
719 * SetThreadErrorMode (KERNEL32.@)
721 * Set the thread local error mode.
724 * mode [I] The new error mode, a bitwise or of SEM_FAILCRITICALERRORS,
725 * SEM_NOGPFAULTERRORBOX and SEM_NOOPENFILEERRORBOX.
726 * oldmode [O] Destination of the old error mode (may be NULL)
730 * Failure: FALSE, check GetLastError
732 BOOL WINAPI
SetThreadErrorMode( DWORD mode
, LPDWORD oldmode
)
737 if (mode
& ~(SEM_FAILCRITICALERRORS
|
738 SEM_NOGPFAULTERRORBOX
|
739 SEM_NOOPENFILEERRORBOX
))
741 SetLastError( ERROR_INVALID_PARAMETER
);
745 if (mode
& SEM_FAILCRITICALERRORS
)
747 if (mode
& SEM_NOGPFAULTERRORBOX
)
749 if (mode
& SEM_NOOPENFILEERRORBOX
)
752 status
= RtlSetThreadErrorMode( tmp
, oldmode
);
755 SetLastError( RtlNtStatusToDosError(status
) );
760 *oldmode
= rtlmode_to_win32mode(*oldmode
);
765 /***********************************************************************
766 * GetThreadErrorMode (KERNEL32.@)
768 * Get the thread local error mode.
774 * The current thread local error mode.
776 DWORD WINAPI
GetThreadErrorMode( void )
778 return rtlmode_to_win32mode( RtlGetThreadErrorMode() );