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
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
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 */
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
);
167 LdrShutdownProcess();
172 RtlFreeThreadActivationContextStack();
173 RtlExitUserThread( code
);
178 /**********************************************************************
179 * TerminateThread [KERNEL32.@] Terminates a thread
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
) );
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.
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
);
223 SetLastError( RtlNtStatusToDosError(status
) );
226 if (exitcode
) *exitcode
= info
.ExitStatus
;
231 /***********************************************************************
232 * SetThreadContext [KERNEL32.@] Sets context of thread.
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
) );
247 /***********************************************************************
248 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
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
) );
263 /**********************************************************************
264 * SuspendThread [KERNEL32.@] Suspends a thread.
267 * Success: Previous suspend count
268 * Failure: 0xFFFFFFFF
270 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
273 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
278 SetLastError( RtlNtStatusToDosError(status
) );
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.
291 * Success: Previous suspend count
292 * Failure: 0xFFFFFFFF
295 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
298 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
303 SetLastError( RtlNtStatusToDosError(status
) );
309 /**********************************************************************
310 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
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
);
325 SetLastError( RtlNtStatusToDosError(status
) );
326 return THREAD_PRIORITY_ERROR_RETURN
;
328 return info
.Priority
;
332 /**********************************************************************
333 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
339 BOOL WINAPI
SetThreadPriority(
340 HANDLE hthread
, /* [in] Handle to thread */
341 INT priority
) /* [in] Thread priority level */
343 DWORD prio
= priority
;
346 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
347 &prio
, sizeof(prio
));
351 SetLastError( RtlNtStatusToDosError(status
) );
359 /**********************************************************************
360 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
362 * Always reports that priority boost is disabled.
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
;
377 /**********************************************************************
378 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
380 * Priority boost is not implemented. This function always returns
381 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
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
);
395 /**********************************************************************
396 * SetThreadAffinityMask (KERNEL32.@)
398 DWORD_PTR WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD_PTR dwThreadAffinityMask
)
401 THREAD_BASIC_INFORMATION tbi
;
403 status
= NtQueryInformationThread( hThread
, ThreadBasicInformation
,
404 &tbi
, sizeof(tbi
), NULL
);
407 SetLastError( RtlNtStatusToDosError(status
) );
410 status
= NtSetInformationThread( hThread
, ThreadAffinityMask
,
411 &dwThreadAffinityMask
,
412 sizeof(dwThreadAffinityMask
));
415 SetLastError( RtlNtStatusToDosError(status
) );
418 return tbi
.AffinityMask
;
422 /**********************************************************************
423 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
426 * Success: Value of last call to SetThreadIdealProcessor
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
);
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
;
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
) );
457 /***********************************************************************
458 * QueueUserWorkItem (KERNEL32.@)
460 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
464 TRACE("(%p,%p,0x%08x)\n", Function
, Context
, Flags
);
466 status
= RtlQueueWorkItem( Function
, Context
, Flags
);
468 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
472 /**********************************************************************
473 * GetThreadTimes [KERNEL32.@] Obtains timing information.
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
;
489 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
490 sizeof(kusrt
), NULL
);
493 SetLastError( RtlNtStatusToDosError(status
) );
498 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
499 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
503 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
504 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
508 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
509 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
513 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
514 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
520 /**********************************************************************
521 * GetThreadId [KERNEL32.@]
523 * Retrieve the identifier of a thread.
526 * Thread [I] The thread to retrieve the identifier of.
529 * Success: Identifier of the target thread.
532 DWORD WINAPI
GetThreadId(HANDLE Thread
)
534 THREAD_BASIC_INFORMATION tbi
;
537 TRACE("(%p)\n", Thread
);
539 status
= NtQueryInformationThread(Thread
, ThreadBasicInformation
, &tbi
,
543 SetLastError( RtlNtStatusToDosError(status
) );
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
573 * Pseudohandle for the current thread
575 #undef GetCurrentThread
576 HANDLE WINAPI
GetCurrentThread(void)
578 return (HANDLE
)0xfffffffe;
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"
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" )
618 /**********************************************************************
619 * SetLastError (KERNEL.147)
620 * SetLastError (KERNEL32.@)
622 * Sets the last-error code.
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.
641 DWORD WINAPI
GetLastError(void)
643 return NtCurrentTeb()->LastErrorValue
;
646 /***********************************************************************
647 * GetCurrentProcessId (KERNEL.471)
648 * GetCurrentProcessId (KERNEL32.@)
650 * Get the current process identifier.
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.
667 * current thread identifier
669 DWORD WINAPI
GetCurrentThreadId(void)
671 return HandleToULong(NtCurrentTeb()->ClientId
.UniqueThread
);
674 #endif /* __i386__ */