kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / kernel32 / thread.c
blobac5d46546a15b40c922bf16fe6ecab0db71e5402
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 DECLSPEC_HOTPATCH CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack, LPTHREAD_START_ROUTINE start,
52 LPVOID param, DWORD flags, LPDWORD id )
54 return CreateRemoteThread( GetCurrentProcess(),
55 sa, stack, start, param, flags, id );
58 /***************************************************************************
59 * CreateRemoteThread (KERNEL32.@)
61 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
62 LPTHREAD_START_ROUTINE start, LPVOID param,
63 DWORD flags, DWORD *id )
65 return CreateRemoteThreadEx( hProcess, sa, stack, start, param, flags, NULL, id );
68 /***************************************************************************
69 * CreateRemoteThreadEx (KERNEL32.@)
71 * Creates a thread that runs in the address space of another process
73 * PARAMS
75 * RETURNS
76 * Success: Handle to the new thread.
77 * Failure: NULL. Use GetLastError() to find the error cause.
79 * BUGS
80 * Improper memory allocation: there's no ability to free new_thread_info
81 * in other process.
82 * Bad start address for RtlCreateUserThread because the library
83 * may be loaded at different address in other process.
85 HANDLE WINAPI CreateRemoteThreadEx( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
86 LPTHREAD_START_ROUTINE start, LPVOID param, DWORD flags,
87 LPPROC_THREAD_ATTRIBUTE_LIST attributes, DWORD *id )
89 HANDLE handle;
90 CLIENT_ID client_id;
91 NTSTATUS status;
92 SIZE_T stack_reserve = 0, stack_commit = 0;
94 if (attributes)
95 FIXME("thread attributes ignored\n");
97 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
98 else stack_commit = stack;
100 status = RtlCreateUserThread( hProcess, sa ? sa->lpSecurityDescriptor : NULL, TRUE,
101 NULL, stack_reserve, stack_commit,
102 (PRTL_THREAD_START_ROUTINE)start, param, &handle, &client_id );
103 if (status == STATUS_SUCCESS)
105 if (id) *id = HandleToULong(client_id.UniqueThread);
106 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
107 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
108 if (!(flags & CREATE_SUSPENDED))
110 ULONG ret;
111 if (NtResumeThread( handle, &ret ))
113 NtClose( handle );
114 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
115 handle = 0;
119 else
121 SetLastError( RtlNtStatusToDosError(status) );
122 handle = 0;
124 return handle;
128 /***********************************************************************
129 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
131 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
133 NTSTATUS status;
134 HANDLE handle;
135 OBJECT_ATTRIBUTES attr;
136 CLIENT_ID cid;
138 attr.Length = sizeof(attr);
139 attr.RootDirectory = 0;
140 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
141 attr.ObjectName = NULL;
142 attr.SecurityDescriptor = NULL;
143 attr.SecurityQualityOfService = NULL;
145 cid.UniqueProcess = 0; /* FIXME */
146 cid.UniqueThread = ULongToHandle(dwThreadId);
147 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
148 if (status)
150 SetLastError( RtlNtStatusToDosError(status) );
151 handle = 0;
153 return handle;
157 /***********************************************************************
158 * ExitThread [KERNEL32.@] Ends a thread
160 * RETURNS
161 * None
163 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
165 RtlExitUserThread( code );
169 /**********************************************************************
170 * TerminateThread [KERNEL32.@] Terminates a thread
172 * RETURNS
173 * Success: TRUE
174 * Failure: FALSE
176 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
177 DWORD exit_code) /* [in] Exit code for thread */
179 NTSTATUS status = NtTerminateThread( handle, exit_code );
180 if (status) SetLastError( RtlNtStatusToDosError(status) );
181 return !status;
185 /***********************************************************************
186 * FreeLibraryAndExitThread (KERNEL32.@)
188 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
190 FreeLibrary(hLibModule);
191 ExitThread(dwExitCode);
195 /**********************************************************************
196 * GetExitCodeThread (KERNEL32.@)
198 * Gets termination status of thread.
200 * RETURNS
201 * Success: TRUE
202 * Failure: FALSE
204 BOOL WINAPI GetExitCodeThread(
205 HANDLE hthread, /* [in] Handle to thread */
206 LPDWORD exitcode) /* [out] Address to receive termination status */
208 THREAD_BASIC_INFORMATION info;
209 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
210 &info, sizeof(info), NULL );
212 if (status)
214 SetLastError( RtlNtStatusToDosError(status) );
215 return FALSE;
217 if (exitcode) *exitcode = info.ExitStatus;
218 return TRUE;
222 /***********************************************************************
223 * SetThreadContext [KERNEL32.@] Sets context of thread.
225 * RETURNS
226 * Success: TRUE
227 * Failure: FALSE
229 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
230 const CONTEXT *context ) /* [in] Address of context structure */
232 NTSTATUS status = NtSetContextThread( handle, context );
233 if (status) SetLastError( RtlNtStatusToDosError(status) );
234 return !status;
238 /***********************************************************************
239 * Wow64SetThreadContext [KERNEL32.@]
241 BOOL WINAPI Wow64SetThreadContext( HANDLE handle, const WOW64_CONTEXT *context)
243 #ifdef __i386__
244 NTSTATUS status = NtSetContextThread( handle, (const CONTEXT *)context );
245 #elif defined(__x86_64__)
246 NTSTATUS status = RtlWow64SetThreadContext( handle, context );
247 #else
248 NTSTATUS status = STATUS_NOT_IMPLEMENTED;
249 FIXME("not implemented on this platform\n");
250 #endif
251 if (status) SetLastError( RtlNtStatusToDosError(status) );
252 return !status;
255 /***********************************************************************
256 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
258 * RETURNS
259 * Success: TRUE
260 * Failure: FALSE
262 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
263 CONTEXT *context ) /* [out] Address of context structure */
265 NTSTATUS status = NtGetContextThread( handle, context );
266 if (status) SetLastError( RtlNtStatusToDosError(status) );
267 return !status;
271 /***********************************************************************
272 * Wow64GetThreadContext [KERNEL32.@]
274 BOOL WINAPI Wow64GetThreadContext( HANDLE handle, WOW64_CONTEXT *context)
276 #ifdef __i386__
277 NTSTATUS status = NtGetContextThread( handle, (CONTEXT *)context );
278 #elif defined(__x86_64__)
279 NTSTATUS status = RtlWow64GetThreadContext( handle, context );
280 #else
281 NTSTATUS status = STATUS_NOT_IMPLEMENTED;
282 FIXME("not implemented on this platform\n");
283 #endif
284 if (status) SetLastError( RtlNtStatusToDosError(status) );
285 return !status;
289 /**********************************************************************
290 * SuspendThread [KERNEL32.@] Suspends a thread.
292 * RETURNS
293 * Success: Previous suspend count
294 * Failure: 0xFFFFFFFF
296 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
298 DWORD ret;
299 NTSTATUS status = NtSuspendThread( hthread, &ret );
301 if (status)
303 ret = ~0U;
304 SetLastError( RtlNtStatusToDosError(status) );
306 return ret;
310 /**********************************************************************
311 * ResumeThread [KERNEL32.@] Resumes a thread.
313 * Decrements a thread's suspend count. When count is zero, the
314 * execution of the thread is resumed.
316 * RETURNS
317 * Success: Previous suspend count
318 * Failure: 0xFFFFFFFF
319 * Already running: 0
321 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
323 DWORD ret;
324 NTSTATUS status = NtResumeThread( hthread, &ret );
326 if (status)
328 ret = ~0U;
329 SetLastError( RtlNtStatusToDosError(status) );
331 return ret;
335 /**********************************************************************
336 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
338 * RETURNS
339 * Success: Thread's priority level.
340 * Failure: THREAD_PRIORITY_ERROR_RETURN
342 INT WINAPI GetThreadPriority(
343 HANDLE hthread) /* [in] Handle to thread */
345 THREAD_BASIC_INFORMATION info;
346 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
347 &info, sizeof(info), NULL );
349 if (status)
351 SetLastError( RtlNtStatusToDosError(status) );
352 return THREAD_PRIORITY_ERROR_RETURN;
354 return info.Priority;
358 /**********************************************************************
359 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
361 * RETURNS
362 * Success: TRUE
363 * Failure: FALSE
365 BOOL WINAPI SetThreadPriority(
366 HANDLE hthread, /* [in] Handle to thread */
367 INT priority) /* [in] Thread priority level */
369 DWORD prio = priority;
370 NTSTATUS status;
372 status = NtSetInformationThread(hthread, ThreadBasePriority,
373 &prio, sizeof(prio));
375 if (status)
377 SetLastError( RtlNtStatusToDosError(status) );
378 return FALSE;
381 return TRUE;
385 /**********************************************************************
386 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
388 * Always reports that priority boost is disabled.
390 * RETURNS
391 * Success: TRUE.
392 * Failure: FALSE
394 BOOL WINAPI GetThreadPriorityBoost(
395 HANDLE hthread, /* [in] Handle to thread */
396 PBOOL pstate) /* [out] pointer to var that receives the boost state */
398 if (pstate) *pstate = FALSE;
399 return TRUE;
403 /**********************************************************************
404 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
406 * Priority boost is not implemented, but we return TRUE
407 * anyway because some games crash otherwise.
409 BOOL WINAPI SetThreadPriorityBoost(
410 HANDLE hthread, /* [in] Handle to thread */
411 BOOL disable) /* [in] TRUE to disable priority boost */
413 return TRUE;
417 /**********************************************************************
418 * SetThreadStackGuarantee (KERNEL32.@)
420 BOOL WINAPI SetThreadStackGuarantee(PULONG stacksize)
422 static int once;
423 if (once++ == 0)
424 FIXME("(%p): stub\n", stacksize);
425 return TRUE;
428 /***********************************************************************
429 * GetThreadGroupAffinity (KERNEL32.@)
431 BOOL WINAPI GetThreadGroupAffinity( HANDLE thread, GROUP_AFFINITY *affinity )
433 NTSTATUS status;
435 if (!affinity)
437 SetLastError( ERROR_INVALID_PARAMETER );
438 return FALSE;
441 status = NtQueryInformationThread( thread, ThreadGroupInformation,
442 affinity, sizeof(*affinity), NULL );
443 if (status)
445 SetLastError( RtlNtStatusToDosError(status) );
446 return FALSE;
449 return TRUE;
452 /***********************************************************************
453 * SetThreadGroupAffinity (KERNEL32.@)
455 BOOL WINAPI SetThreadGroupAffinity( HANDLE thread, const GROUP_AFFINITY *affinity_new,
456 GROUP_AFFINITY *affinity_old )
458 NTSTATUS status;
460 if (affinity_old && !GetThreadGroupAffinity( thread, affinity_old ))
461 return FALSE;
463 status = NtSetInformationThread( thread, ThreadGroupInformation,
464 affinity_new, sizeof(*affinity_new) );
465 if (status)
467 SetLastError( RtlNtStatusToDosError(status) );
468 return FALSE;
471 return TRUE;
474 /**********************************************************************
475 * SetThreadAffinityMask (KERNEL32.@)
477 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
479 NTSTATUS status;
480 THREAD_BASIC_INFORMATION tbi;
482 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
483 &tbi, sizeof(tbi), NULL );
484 if (status)
486 SetLastError( RtlNtStatusToDosError(status) );
487 return 0;
489 status = NtSetInformationThread( hThread, ThreadAffinityMask,
490 &dwThreadAffinityMask,
491 sizeof(dwThreadAffinityMask));
492 if (status)
494 SetLastError( RtlNtStatusToDosError(status) );
495 return 0;
497 return tbi.AffinityMask;
501 /**********************************************************************
502 * SetThreadIdealProcessor [KERNEL32.@] Sets preferred processor for thread.
504 * RETURNS
505 * Success: Value of last call to SetThreadIdealProcessor
506 * Failure: -1
508 DWORD WINAPI SetThreadIdealProcessor(
509 HANDLE hThread, /* [in] Specifies the thread of interest */
510 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
512 FIXME("(%p): stub\n",hThread);
513 if (dwIdealProcessor > MAXIMUM_PROCESSORS)
515 SetLastError(ERROR_INVALID_PARAMETER);
516 return ~0u;
518 return 0;
521 /***********************************************************************
522 * SetThreadIdealProcessorEx (KERNEL32.@)
524 BOOL WINAPI SetThreadIdealProcessorEx( HANDLE thread, PROCESSOR_NUMBER *ideal, PROCESSOR_NUMBER *previous )
526 FIXME("(%p %p %p): stub\n", thread, ideal, previous);
527 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
528 return FALSE;
531 /***********************************************************************
532 * GetThreadSelectorEntry (KERNEL32.@)
534 BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent )
536 THREAD_DESCRIPTOR_INFORMATION tdi;
537 NTSTATUS status;
539 tdi.Selector = sel;
540 status = NtQueryInformationThread( hthread, ThreadDescriptorTableEntry, &tdi, sizeof(tdi), NULL);
541 if (status)
543 SetLastError( RtlNtStatusToDosError(status) );
544 return FALSE;
546 *ldtent = tdi.Entry;
547 return TRUE;
551 /* callback for QueueUserAPC */
552 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
554 PAPCFUNC func = (PAPCFUNC)arg1;
555 func( arg2 );
558 /***********************************************************************
559 * QueueUserAPC (KERNEL32.@)
561 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
563 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
565 if (status) SetLastError( RtlNtStatusToDosError(status) );
566 return !status;
569 /***********************************************************************
570 * QueueUserWorkItem (KERNEL32.@)
572 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
574 NTSTATUS status;
576 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
578 status = RtlQueueWorkItem( Function, Context, Flags );
580 if (status) SetLastError( RtlNtStatusToDosError(status) );
581 return !status;
584 /**********************************************************************
585 * GetThreadTimes [KERNEL32.@] Obtains timing information.
587 * RETURNS
588 * Success: TRUE
589 * Failure: FALSE
591 BOOL WINAPI GetThreadTimes(
592 HANDLE thread, /* [in] Specifies the thread of interest */
593 LPFILETIME creationtime, /* [out] When the thread was created */
594 LPFILETIME exittime, /* [out] When the thread was destroyed */
595 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
596 LPFILETIME usertime) /* [out] Time thread spent in user mode */
598 KERNEL_USER_TIMES kusrt;
599 NTSTATUS status;
601 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
602 sizeof(kusrt), NULL);
603 if (status)
605 SetLastError( RtlNtStatusToDosError(status) );
606 return FALSE;
608 if (creationtime)
610 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
611 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
613 if (exittime)
615 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
616 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
618 if (kerneltime)
620 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
621 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
623 if (usertime)
625 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
626 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
629 return TRUE;
632 /**********************************************************************
633 * GetThreadId [KERNEL32.@]
635 * Retrieve the identifier of a thread.
637 * PARAMS
638 * Thread [I] The thread to retrieve the identifier of.
640 * RETURNS
641 * Success: Identifier of the target thread.
642 * Failure: 0
644 DWORD WINAPI GetThreadId(HANDLE Thread)
646 THREAD_BASIC_INFORMATION tbi;
647 NTSTATUS status;
649 TRACE("(%p)\n", Thread);
651 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
652 sizeof(tbi), NULL);
653 if (status)
655 SetLastError( RtlNtStatusToDosError(status) );
656 return 0;
659 return HandleToULong(tbi.ClientId.UniqueThread);
662 /**********************************************************************
663 * GetProcessIdOfThread [KERNEL32.@]
665 * Retrieve process identifier given thread belongs to.
667 * PARAMS
668 * Thread [I] The thread identifier.
670 * RETURNS
671 * Success: Process identifier
672 * Failure: 0
674 DWORD WINAPI GetProcessIdOfThread(HANDLE Thread)
676 THREAD_BASIC_INFORMATION tbi;
677 NTSTATUS status;
679 TRACE("(%p)\n", Thread);
681 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
682 sizeof(tbi), NULL);
683 if (status)
685 SetLastError( RtlNtStatusToDosError(status) );
686 return 0;
689 return HandleToULong(tbi.ClientId.UniqueProcess);
692 /***********************************************************************
693 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
695 * RETURNS
696 * Pseudohandle for the current thread
698 #undef GetCurrentThread
699 HANDLE WINAPI GetCurrentThread(void)
701 return (HANDLE)~(ULONG_PTR)1;
704 /***********************************************************************
705 * GetCurrentThreadStackLimits (KERNEL32.@)
707 void WINAPI GetCurrentThreadStackLimits(ULONG_PTR *low, ULONG_PTR *high)
709 *low = (ULONG_PTR)NtCurrentTeb()->DeallocationStack;
710 *high = (ULONG_PTR)NtCurrentTeb()->Tib.StackBase;
714 #ifdef __i386__
716 /***********************************************************************
717 * SetLastError (KERNEL32.@)
719 /* void WINAPI SetLastError( DWORD error ); */
720 __ASM_STDCALL_FUNC( SetLastError, 4,
721 "movl 4(%esp),%eax\n\t"
722 ".byte 0x64\n\t"
723 "movl %eax,0x34\n\t"
724 "ret $4" )
726 /***********************************************************************
727 * GetLastError (KERNEL32.@)
729 /* DWORD WINAPI GetLastError(void); */
730 __ASM_STDCALL_FUNC( GetLastError, 0, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
732 /***********************************************************************
733 * GetCurrentProcessId (KERNEL32.@)
735 /* DWORD WINAPI GetCurrentProcessId(void) */
736 __ASM_STDCALL_FUNC( GetCurrentProcessId, 0, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
738 /***********************************************************************
739 * GetCurrentThreadId (KERNEL32.@)
741 /* DWORD WINAPI GetCurrentThreadId(void) */
742 __ASM_STDCALL_FUNC( GetCurrentThreadId, 0, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
744 /***********************************************************************
745 * GetProcessHeap (KERNEL32.@)
747 /* HANDLE WINAPI GetProcessHeap(void) */
748 __ASM_STDCALL_FUNC( GetProcessHeap, 0, ".byte 0x64\n\tmovl 0x30,%eax\n\tmovl 0x18(%eax),%eax\n\tret");
750 #elif defined(__x86_64__)
752 #ifdef __APPLE__
754 /***********************************************************************
755 * SetLastError (KERNEL32.@)
757 /* void WINAPI SetLastError( DWORD error ); */
758 __ASM_STDCALL_FUNC( SetLastError, 8, ".byte 0x65\n\tmovq 0x30,%rax\n\tmovl %ecx,0x68(%rax)\n\tret" );
760 /***********************************************************************
761 * GetLastError (KERNEL32.@)
763 /* DWORD WINAPI GetLastError(void); */
764 __ASM_STDCALL_FUNC( GetLastError, 0, ".byte 0x65\n\tmovq 0x30,%rax\n\tmovl 0x68(%rax),%eax\n\tret" );
766 /***********************************************************************
767 * GetCurrentProcessId (KERNEL32.@)
769 /* DWORD WINAPI GetCurrentProcessId(void) */
770 __ASM_STDCALL_FUNC( GetCurrentProcessId, 0, ".byte 0x65\n\tmovq 0x30,%rax\n\tmovl 0x40(%rax),%eax\n\tret" );
772 /***********************************************************************
773 * GetCurrentThreadId (KERNEL32.@)
775 /* DWORD WINAPI GetCurrentThreadId(void) */
776 __ASM_STDCALL_FUNC( GetCurrentThreadId, 0, ".byte 0x65\n\tmovq 0x30,%rax\n\tmovl 0x48(%rax),%eax\n\tret" );
778 /***********************************************************************
779 * GetProcessHeap (KERNEL32.@)
781 /* HANDLE WINAPI GetProcessHeap(void) */
782 __ASM_STDCALL_FUNC( GetProcessHeap, 0, ".byte 0x65\n\tmovq 0x30,%rax\n\tmovq 0x60(%rax),%rax\n\tmovq 0x30(%rax),%rax\n\tret");
784 #else
786 /***********************************************************************
787 * SetLastError (KERNEL32.@)
789 /* void WINAPI SetLastError( DWORD error ); */
790 __ASM_STDCALL_FUNC( SetLastError, 8, ".byte 0x65\n\tmovl %ecx,0x68\n\tret" );
792 /***********************************************************************
793 * GetLastError (KERNEL32.@)
795 /* DWORD WINAPI GetLastError(void); */
796 __ASM_STDCALL_FUNC( GetLastError, 0, ".byte 0x65\n\tmovl 0x68,%eax\n\tret" );
798 /***********************************************************************
799 * GetCurrentProcessId (KERNEL32.@)
801 /* DWORD WINAPI GetCurrentProcessId(void) */
802 __ASM_STDCALL_FUNC( GetCurrentProcessId, 0, ".byte 0x65\n\tmovl 0x40,%eax\n\tret" );
804 /***********************************************************************
805 * GetCurrentThreadId (KERNEL32.@)
807 /* DWORD WINAPI GetCurrentThreadId(void) */
808 __ASM_STDCALL_FUNC( GetCurrentThreadId, 0, ".byte 0x65\n\tmovl 0x48,%eax\n\tret" );
810 /***********************************************************************
811 * GetProcessHeap (KERNEL32.@)
813 /* HANDLE WINAPI GetProcessHeap(void) */
814 __ASM_STDCALL_FUNC( GetProcessHeap, 0, ".byte 0x65\n\tmovq 0x60,%rax\n\tmovq 0x30(%rax),%rax\n\tret");
816 #endif /* __APPLE__ */
818 #else /* __x86_64__ */
820 /**********************************************************************
821 * SetLastError (KERNEL32.@)
823 * Sets the last-error code.
825 * RETURNS
826 * Nothing.
828 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
830 NtCurrentTeb()->LastErrorValue = error;
833 /**********************************************************************
834 * GetLastError (KERNEL32.@)
836 * Get the last-error code.
838 * RETURNS
839 * last-error code.
841 DWORD WINAPI GetLastError(void)
843 return NtCurrentTeb()->LastErrorValue;
846 /***********************************************************************
847 * GetCurrentProcessId (KERNEL32.@)
849 * Get the current process identifier.
851 * RETURNS
852 * current process identifier
854 DWORD WINAPI GetCurrentProcessId(void)
856 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
859 /***********************************************************************
860 * GetCurrentThreadId (KERNEL32.@)
862 * Get the current thread identifier.
864 * RETURNS
865 * current thread identifier
867 DWORD WINAPI GetCurrentThreadId(void)
869 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
872 /***********************************************************************
873 * GetProcessHeap (KERNEL32.@)
875 HANDLE WINAPI GetProcessHeap(void)
877 return NtCurrentTeb()->Peb->ProcessHeap;
880 #endif /* __i386__ */
882 /*************************************************************************
883 * rtlmode_to_win32mode
885 static DWORD rtlmode_to_win32mode( DWORD rtlmode )
887 DWORD win32mode = 0;
889 if (rtlmode & 0x10)
890 win32mode |= SEM_FAILCRITICALERRORS;
891 if (rtlmode & 0x20)
892 win32mode |= SEM_NOGPFAULTERRORBOX;
893 if (rtlmode & 0x40)
894 win32mode |= SEM_NOOPENFILEERRORBOX;
896 return win32mode;
899 /***********************************************************************
900 * SetThreadErrorMode (KERNEL32.@)
902 * Set the thread local error mode.
904 * PARAMS
905 * mode [I] The new error mode, a bitwise or of SEM_FAILCRITICALERRORS,
906 * SEM_NOGPFAULTERRORBOX and SEM_NOOPENFILEERRORBOX.
907 * oldmode [O] Destination of the old error mode (may be NULL)
909 * RETURNS
910 * Success: TRUE
911 * Failure: FALSE, check GetLastError
913 BOOL WINAPI SetThreadErrorMode( DWORD mode, LPDWORD oldmode )
915 NTSTATUS status;
916 DWORD tmp = 0;
918 if (mode & ~(SEM_FAILCRITICALERRORS |
919 SEM_NOGPFAULTERRORBOX |
920 SEM_NOOPENFILEERRORBOX))
922 SetLastError( ERROR_INVALID_PARAMETER );
923 return FALSE;
926 if (mode & SEM_FAILCRITICALERRORS)
927 tmp |= 0x10;
928 if (mode & SEM_NOGPFAULTERRORBOX)
929 tmp |= 0x20;
930 if (mode & SEM_NOOPENFILEERRORBOX)
931 tmp |= 0x40;
933 status = RtlSetThreadErrorMode( tmp, oldmode );
934 if (status)
936 SetLastError( RtlNtStatusToDosError(status) );
937 return FALSE;
940 if (oldmode)
941 *oldmode = rtlmode_to_win32mode(*oldmode);
943 return TRUE;
946 /***********************************************************************
947 * GetThreadErrorMode (KERNEL32.@)
949 * Get the thread local error mode.
951 * PARAMS
952 * None.
954 * RETURNS
955 * The current thread local error mode.
957 DWORD WINAPI GetThreadErrorMode( void )
959 return rtlmode_to_win32mode( RtlGetThreadErrorMode() );
962 /***********************************************************************
963 * GetThreadUILanguage (KERNEL32.@)
965 * Get the current thread's language identifier.
967 * PARAMS
968 * None.
970 * RETURNS
971 * The current thread's language identifier.
973 LANGID WINAPI GetThreadUILanguage( void )
975 LANGID lang;
976 NtQueryDefaultUILanguage( &lang );
977 FIXME(": stub, returning default language.\n");
978 return lang;
981 /***********************************************************************
982 * GetThreadIOPendingFlag (KERNEL32.@)
984 BOOL WINAPI GetThreadIOPendingFlag( HANDLE thread, PBOOL io_pending )
986 NTSTATUS status;
988 TRACE("%p, %p\n", thread, io_pending);
990 status = NtQueryInformationThread( thread, ThreadIsIoPending,
991 io_pending, sizeof(*io_pending), NULL );
992 if (status)
994 SetLastError( RtlNtStatusToDosError(status) );
995 return FALSE;
997 return TRUE;
1000 /***********************************************************************
1001 * CallbackMayRunLong (KERNEL32.@)
1003 BOOL WINAPI CallbackMayRunLong( TP_CALLBACK_INSTANCE *instance )
1005 NTSTATUS status;
1007 TRACE( "%p\n", instance );
1009 status = TpCallbackMayRunLong( instance );
1010 if (status)
1012 SetLastError( RtlNtStatusToDosError(status) );
1013 return FALSE;
1016 return TRUE;
1019 /***********************************************************************
1020 * CreateThreadpool (KERNEL32.@)
1022 PTP_POOL WINAPI CreateThreadpool( PVOID reserved )
1024 TP_POOL *pool;
1025 NTSTATUS status;
1027 TRACE( "%p\n", reserved );
1029 status = TpAllocPool( &pool, reserved );
1030 if (status)
1032 SetLastError( RtlNtStatusToDosError(status) );
1033 return NULL;
1036 return pool;
1039 /***********************************************************************
1040 * CreateThreadpoolCleanupGroup (KERNEL32.@)
1042 PTP_CLEANUP_GROUP WINAPI CreateThreadpoolCleanupGroup( void )
1044 TP_CLEANUP_GROUP *group;
1045 NTSTATUS status;
1047 TRACE( "\n" );
1049 status = TpAllocCleanupGroup( &group );
1050 if (status)
1052 SetLastError( RtlNtStatusToDosError(status) );
1053 return NULL;
1056 return group;
1059 /***********************************************************************
1060 * CreateThreadpoolIo (KERNEL32.@)
1062 PTP_IO WINAPI CreateThreadpoolIo( HANDLE handle, PTP_WIN32_IO_CALLBACK callback,
1063 PVOID userdata, TP_CALLBACK_ENVIRON *environment )
1065 FIXME("(%p, %p, %p, %p): stub\n", handle, callback, userdata, environment);
1066 return FALSE;
1069 /***********************************************************************
1070 * CreateThreadpoolTimer (KERNEL32.@)
1072 PTP_TIMER WINAPI CreateThreadpoolTimer( PTP_TIMER_CALLBACK callback, PVOID userdata,
1073 TP_CALLBACK_ENVIRON *environment )
1075 TP_TIMER *timer;
1076 NTSTATUS status;
1078 TRACE( "%p, %p, %p\n", callback, userdata, environment );
1080 status = TpAllocTimer( &timer, callback, userdata, environment );
1081 if (status)
1083 SetLastError( RtlNtStatusToDosError(status) );
1084 return NULL;
1087 return timer;
1090 /***********************************************************************
1091 * CreateThreadpoolWait (KERNEL32.@)
1093 PTP_WAIT WINAPI CreateThreadpoolWait( PTP_WAIT_CALLBACK callback, PVOID userdata,
1094 TP_CALLBACK_ENVIRON *environment )
1096 TP_WAIT *wait;
1097 NTSTATUS status;
1099 TRACE( "%p, %p, %p\n", callback, userdata, environment );
1101 status = TpAllocWait( &wait, callback, userdata, environment );
1102 if (status)
1104 SetLastError( RtlNtStatusToDosError(status) );
1105 return NULL;
1108 return wait;
1111 /***********************************************************************
1112 * CreateThreadpoolWork (KERNEL32.@)
1114 PTP_WORK WINAPI CreateThreadpoolWork( PTP_WORK_CALLBACK callback, PVOID userdata,
1115 TP_CALLBACK_ENVIRON *environment )
1117 TP_WORK *work;
1118 NTSTATUS status;
1120 TRACE( "%p, %p, %p\n", callback, userdata, environment );
1122 status = TpAllocWork( &work, callback, userdata, environment );
1123 if (status)
1125 SetLastError( RtlNtStatusToDosError(status) );
1126 return NULL;
1129 return work;
1132 /***********************************************************************
1133 * SetThreadpoolTimer (KERNEL32.@)
1135 VOID WINAPI SetThreadpoolTimer( TP_TIMER *timer, FILETIME *due_time,
1136 DWORD period, DWORD window_length )
1138 LARGE_INTEGER timeout;
1140 TRACE( "%p, %p, %u, %u\n", timer, due_time, period, window_length );
1142 if (due_time)
1144 timeout.u.LowPart = due_time->dwLowDateTime;
1145 timeout.u.HighPart = due_time->dwHighDateTime;
1148 TpSetTimer( timer, due_time ? &timeout : NULL, period, window_length );
1151 /***********************************************************************
1152 * SetThreadpoolWait (KERNEL32.@)
1154 VOID WINAPI SetThreadpoolWait( TP_WAIT *wait, HANDLE handle, FILETIME *due_time )
1156 LARGE_INTEGER timeout;
1158 TRACE( "%p, %p, %p\n", wait, handle, due_time );
1160 if (!handle)
1162 due_time = NULL;
1164 else if (due_time)
1166 timeout.u.LowPart = due_time->dwLowDateTime;
1167 timeout.u.HighPart = due_time->dwHighDateTime;
1170 TpSetWait( wait, handle, due_time ? &timeout : NULL );
1173 /***********************************************************************
1174 * TrySubmitThreadpoolCallback (KERNEL32.@)
1176 BOOL WINAPI TrySubmitThreadpoolCallback( PTP_SIMPLE_CALLBACK callback, PVOID userdata,
1177 TP_CALLBACK_ENVIRON *environment )
1179 NTSTATUS status;
1181 TRACE( "%p, %p, %p\n", callback, userdata, environment );
1183 status = TpSimpleTryPost( callback, userdata, environment );
1184 if (status)
1186 SetLastError( RtlNtStatusToDosError(status) );
1187 return FALSE;
1190 return TRUE;