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
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
);
48 WINE_DECLARE_DEBUG_CHANNEL(relay
);
51 struct new_thread_info
53 LPTHREAD_START_ROUTINE func
;
58 /***********************************************************************
61 * Start execution of a newly created thread. Does not return.
63 static void CALLBACK
THREAD_Start( void *ptr
)
65 struct new_thread_info
*info
= ptr
;
66 LPTHREAD_START_ROUTINE func
= info
->func
;
67 void *arg
= info
->arg
;
69 RtlFreeHeap( GetProcessHeap(), 0, info
);
72 DPRINTF("%04x:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func
);
76 ExitThread( func( arg
) );
78 __EXCEPT(UnhandledExceptionFilter
)
80 TerminateThread( GetCurrentThread(), GetExceptionCode() );
86 /***********************************************************************
87 * CreateThread (KERNEL32.@)
89 HANDLE WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
90 LPTHREAD_START_ROUTINE start
, LPVOID param
,
91 DWORD flags
, LPDWORD id
)
93 return CreateRemoteThread( GetCurrentProcess(),
94 sa
, stack
, start
, param
, flags
, id
);
98 /***************************************************************************
99 * CreateRemoteThread (KERNEL32.@)
101 * Creates a thread that runs in the address space of another process
106 * Success: Handle to the new thread.
107 * Failure: NULL. Use GetLastError() to find the error cause.
110 * Improper memory allocation: there's no ability to free new_thread_info
112 * Bad start address for RtlCreateUserThread because the library
113 * may be loaded at different address in other process.
115 HANDLE WINAPI
CreateRemoteThread( HANDLE hProcess
, SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
116 LPTHREAD_START_ROUTINE start
, LPVOID param
,
117 DWORD flags
, LPDWORD id
)
122 SIZE_T stack_reserve
= 0, stack_commit
= 0;
123 struct new_thread_info
*info
;
125 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
127 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
133 if (flags
& STACK_SIZE_PARAM_IS_A_RESERVATION
) stack_reserve
= stack
;
134 else stack_commit
= stack
;
136 status
= RtlCreateUserThread( hProcess
, NULL
, TRUE
,
137 NULL
, stack_reserve
, stack_commit
,
138 THREAD_Start
, info
, &handle
, &client_id
);
139 if (status
== STATUS_SUCCESS
)
141 if (id
) *id
= (DWORD
)client_id
.UniqueThread
;
142 if (sa
&& (sa
->nLength
>= sizeof(*sa
)) && sa
->bInheritHandle
)
143 SetHandleInformation( handle
, HANDLE_FLAG_INHERIT
, HANDLE_FLAG_INHERIT
);
144 if (!(flags
& CREATE_SUSPENDED
))
147 if (NtResumeThread( handle
, &ret
))
150 RtlFreeHeap( GetProcessHeap(), 0, info
);
151 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
158 RtlFreeHeap( GetProcessHeap(), 0, info
);
159 SetLastError( RtlNtStatusToDosError(status
) );
166 /***********************************************************************
167 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
169 HANDLE WINAPI
OpenThread( DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
)
173 OBJECT_ATTRIBUTES attr
;
176 attr
.Length
= sizeof(attr
);
177 attr
.RootDirectory
= 0;
178 attr
.Attributes
= bInheritHandle
? OBJ_INHERIT
: 0;
179 attr
.ObjectName
= NULL
;
180 attr
.SecurityDescriptor
= NULL
;
181 attr
.SecurityQualityOfService
= NULL
;
183 cid
.UniqueProcess
= 0; /* FIXME */
184 cid
.UniqueThread
= (HANDLE
)dwThreadId
;
185 status
= NtOpenThread( &handle
, dwDesiredAccess
, &attr
, &cid
);
188 SetLastError( RtlNtStatusToDosError(status
) );
195 /***********************************************************************
196 * ExitThread [KERNEL32.@] Ends a thread
201 void WINAPI
ExitThread( DWORD code
) /* [in] Exit code for this thread */
204 SERVER_START_REQ( terminate_thread
)
206 /* send the exit code to the server */
207 req
->handle
= GetCurrentThread();
208 req
->exit_code
= code
;
209 wine_server_call( req
);
216 LdrShutdownProcess();
219 else RtlExitUserThread( code
);
223 /**********************************************************************
224 * TerminateThread [KERNEL32.@] Terminates a thread
230 BOOL WINAPI
TerminateThread( HANDLE handle
, /* [in] Handle to thread */
231 DWORD exit_code
) /* [in] Exit code for thread */
233 NTSTATUS status
= NtTerminateThread( handle
, exit_code
);
234 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
239 /***********************************************************************
240 * FreeLibraryAndExitThread (KERNEL32.@)
242 void WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
244 FreeLibrary(hLibModule
);
245 ExitThread(dwExitCode
);
249 /**********************************************************************
250 * GetExitCodeThread (KERNEL32.@)
252 * Gets termination status of thread.
258 BOOL WINAPI
GetExitCodeThread(
259 HANDLE hthread
, /* [in] Handle to thread */
260 LPDWORD exitcode
) /* [out] Address to receive termination status */
262 THREAD_BASIC_INFORMATION info
;
263 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
264 &info
, sizeof(info
), NULL
);
268 SetLastError( RtlNtStatusToDosError(status
) );
271 if (exitcode
) *exitcode
= info
.ExitStatus
;
276 /***********************************************************************
277 * SetThreadContext [KERNEL32.@] Sets context of thread.
283 BOOL WINAPI
SetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
284 const CONTEXT
*context
) /* [in] Address of context structure */
286 NTSTATUS status
= NtSetContextThread( handle
, context
);
287 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
292 /***********************************************************************
293 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
299 BOOL WINAPI
GetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
300 CONTEXT
*context
) /* [out] Address of context structure */
302 NTSTATUS status
= NtGetContextThread( handle
, context
);
303 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
308 /**********************************************************************
309 * SuspendThread [KERNEL32.@] Suspends a thread.
312 * Success: Previous suspend count
313 * Failure: 0xFFFFFFFF
315 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
318 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
323 SetLastError( RtlNtStatusToDosError(status
) );
329 /**********************************************************************
330 * ResumeThread [KERNEL32.@] Resumes a thread.
332 * Decrements a thread's suspend count. When count is zero, the
333 * execution of the thread is resumed.
336 * Success: Previous suspend count
337 * Failure: 0xFFFFFFFF
340 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
343 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
348 SetLastError( RtlNtStatusToDosError(status
) );
354 /**********************************************************************
355 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
358 * Success: Thread's priority level.
359 * Failure: THREAD_PRIORITY_ERROR_RETURN
361 INT WINAPI
GetThreadPriority(
362 HANDLE hthread
) /* [in] Handle to thread */
364 THREAD_BASIC_INFORMATION info
;
365 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
366 &info
, sizeof(info
), NULL
);
370 SetLastError( RtlNtStatusToDosError(status
) );
371 return THREAD_PRIORITY_ERROR_RETURN
;
373 return info
.Priority
;
377 /**********************************************************************
378 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
384 BOOL WINAPI
SetThreadPriority(
385 HANDLE hthread
, /* [in] Handle to thread */
386 INT priority
) /* [in] Thread priority level */
388 DWORD prio
= priority
;
391 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
392 &prio
, sizeof(prio
));
396 SetLastError( RtlNtStatusToDosError(status
) );
404 /**********************************************************************
405 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
407 * Always reports that priority boost is disabled.
413 BOOL WINAPI
GetThreadPriorityBoost(
414 HANDLE hthread
, /* [in] Handle to thread */
415 PBOOL pstate
) /* [out] pointer to var that receives the boost state */
417 if (pstate
) *pstate
= FALSE
;
422 /**********************************************************************
423 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
425 * Priority boost is not implemented. Thsi function always returns
426 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
429 * Always returns FALSE to indicate a failure
431 BOOL WINAPI
SetThreadPriorityBoost(
432 HANDLE hthread
, /* [in] Handle to thread */
433 BOOL disable
) /* [in] TRUE to disable priority boost */
435 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
440 /**********************************************************************
441 * SetThreadAffinityMask (KERNEL32.@)
443 DWORD WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD dwThreadAffinityMask
)
446 THREAD_BASIC_INFORMATION tbi
;
448 status
= NtQueryInformationThread( hThread
, ThreadBasicInformation
,
449 &tbi
, sizeof(tbi
), NULL
);
452 SetLastError( RtlNtStatusToDosError(status
) );
455 status
= NtSetInformationThread( hThread
, ThreadAffinityMask
,
456 &dwThreadAffinityMask
,
457 sizeof(dwThreadAffinityMask
));
460 SetLastError( RtlNtStatusToDosError(status
) );
463 return tbi
.AffinityMask
;
467 /**********************************************************************
468 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
471 * Success: Value of last call to SetThreadIdealProcessor
474 DWORD WINAPI
SetThreadIdealProcessor(
475 HANDLE hThread
, /* [in] Specifies the thread of interest */
476 DWORD dwIdealProcessor
) /* [in] Specifies the new preferred processor */
478 FIXME("(%p): stub\n",hThread
);
479 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
484 /* callback for QueueUserAPC */
485 static void CALLBACK
call_user_apc( ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
)
487 PAPCFUNC func
= (PAPCFUNC
)arg1
;
491 /***********************************************************************
492 * QueueUserAPC (KERNEL32.@)
494 DWORD WINAPI
QueueUserAPC( PAPCFUNC func
, HANDLE hthread
, ULONG_PTR data
)
496 NTSTATUS status
= NtQueueApcThread( hthread
, call_user_apc
, (ULONG_PTR
)func
, data
, 0 );
498 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
502 /***********************************************************************
503 * QueueUserWorkItem (KERNEL32.@)
505 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
509 TRACE("(%p,%p,0x%08x)\n", Function
, Context
, Flags
);
511 status
= RtlQueueWorkItem( Function
, Context
, Flags
);
513 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
517 /**********************************************************************
518 * GetThreadTimes [KERNEL32.@] Obtains timing information.
524 BOOL WINAPI
GetThreadTimes(
525 HANDLE thread
, /* [in] Specifies the thread of interest */
526 LPFILETIME creationtime
, /* [out] When the thread was created */
527 LPFILETIME exittime
, /* [out] When the thread was destroyed */
528 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
529 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
531 KERNEL_USER_TIMES kusrt
;
534 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
535 sizeof(kusrt
), NULL
);
538 SetLastError( RtlNtStatusToDosError(status
) );
543 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
544 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
548 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
549 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
553 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
554 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
558 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
559 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
566 /**********************************************************************
567 * VWin32_BoostThreadGroup [KERNEL.535]
569 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT boost
)
571 FIXME("(0x%08x,%d): stub\n", threadId
, boost
);
575 /**********************************************************************
576 * VWin32_BoostThreadStatic [KERNEL.536]
578 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT boost
)
580 FIXME("(0x%08x,%d): stub\n", threadId
, boost
);
584 /***********************************************************************
585 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
588 * Pseudohandle for the current thread
590 #undef GetCurrentThread
591 HANDLE WINAPI
GetCurrentThread(void)
593 return (HANDLE
)0xfffffffe;
599 /***********************************************************************
600 * SetLastError (KERNEL.147)
601 * SetLastError (KERNEL32.@)
603 /* void WINAPI SetLastError( DWORD error ); */
604 __ASM_GLOBAL_FUNC( SetLastError
,
605 "movl 4(%esp),%eax\n\t"
610 /***********************************************************************
611 * GetLastError (KERNEL.148)
612 * GetLastError (KERNEL32.@)
614 /* DWORD WINAPI GetLastError(void); */
615 __ASM_GLOBAL_FUNC( GetLastError
, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
617 /***********************************************************************
618 * GetCurrentProcessId (KERNEL.471)
619 * GetCurrentProcessId (KERNEL32.@)
621 /* DWORD WINAPI GetCurrentProcessId(void) */
622 __ASM_GLOBAL_FUNC( GetCurrentProcessId
, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
624 /***********************************************************************
625 * GetCurrentThreadId (KERNEL.462)
626 * GetCurrentThreadId (KERNEL32.@)
628 /* DWORD WINAPI GetCurrentThreadId(void) */
629 __ASM_GLOBAL_FUNC( GetCurrentThreadId
, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
633 /**********************************************************************
634 * SetLastError (KERNEL.147)
635 * SetLastError (KERNEL32.@)
637 * Sets the last-error code.
642 void WINAPI
SetLastError( DWORD error
) /* [in] Per-thread error code */
644 NtCurrentTeb()->LastErrorValue
= error
;
647 /**********************************************************************
648 * GetLastError (KERNEL.148)
649 * GetLastError (KERNEL32.@)
651 * Get the last-error code.
656 DWORD WINAPI
GetLastError(void)
658 return NtCurrentTeb()->LastErrorValue
;
661 /***********************************************************************
662 * GetCurrentProcessId (KERNEL.471)
663 * GetCurrentProcessId (KERNEL32.@)
665 * Get the current process identifier.
668 * current process identifier
670 DWORD WINAPI
GetCurrentProcessId(void)
672 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueProcess
;
675 /***********************************************************************
676 * GetCurrentThreadId (KERNEL.462)
677 * GetCurrentThreadId (KERNEL32.@)
679 * Get the current thread identifier.
682 * current thread identifier
684 DWORD WINAPI
GetCurrentThreadId(void)
686 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueThread
;
689 #endif /* __i386__ */