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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 #include <sys/types.h>
33 #define WIN32_NO_STATUS
40 #include "wine/winbase16.h"
41 #include "wine/exception.h"
42 #include "wine/library.h"
43 #include "wine/server.h"
44 #include "wine/debug.h"
46 #include "kernel_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(thread
);
49 WINE_DECLARE_DEBUG_CHANNEL(relay
);
52 struct new_thread_info
54 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("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func
);
76 MODULE_DllThreadAttach( NULL
);
77 ExitThread( func( arg
) );
79 __EXCEPT(UnhandledExceptionFilter
)
81 TerminateThread( GetCurrentThread(), GetExceptionCode() );
87 /***********************************************************************
88 * CreateThread (KERNEL32.@)
90 HANDLE WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
91 LPTHREAD_START_ROUTINE start
, LPVOID param
,
92 DWORD flags
, LPDWORD id
)
94 return CreateRemoteThread( GetCurrentProcess(),
95 sa
, stack
, start
, param
, flags
, id
);
99 /***************************************************************************
100 * CreateRemoteThread (KERNEL32.@)
102 * Creates a thread that runs in the address space of another process
107 * Success: Handle to the new thread.
108 * Failure: NULL. Use GetLastError() to find the error cause.
111 * Improper memory allocation: there's no ability to free new_thread_info
113 * Bad start address for RtlCreateUserThread because the library
114 * may be loaded at different address in other process.
116 HANDLE WINAPI
CreateRemoteThread( HANDLE hProcess
, SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
117 LPTHREAD_START_ROUTINE start
, LPVOID param
,
118 DWORD flags
, LPDWORD id
)
123 SIZE_T stack_reserve
= 0, stack_commit
= 0;
124 struct new_thread_info
*info
;
126 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
128 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
134 if (flags
& STACK_SIZE_PARAM_IS_A_RESERVATION
) stack_reserve
= stack
;
135 else stack_commit
= stack
;
137 status
= RtlCreateUserThread( hProcess
, NULL
, TRUE
,
138 NULL
, stack_reserve
, stack_commit
,
139 THREAD_Start
, info
, &handle
, &client_id
);
140 if (status
== STATUS_SUCCESS
)
142 if (id
) *id
= (DWORD
)client_id
.UniqueThread
;
143 if (sa
&& (sa
->nLength
>= sizeof(*sa
)) && sa
->bInheritHandle
)
144 SetHandleInformation( handle
, HANDLE_FLAG_INHERIT
, HANDLE_FLAG_INHERIT
);
145 if (!(flags
& CREATE_SUSPENDED
))
148 if (NtResumeThread( handle
, &ret
))
151 RtlFreeHeap( GetProcessHeap(), 0, info
);
152 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
159 RtlFreeHeap( GetProcessHeap(), 0, info
);
160 SetLastError( RtlNtStatusToDosError(status
) );
167 /***********************************************************************
168 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
170 HANDLE WINAPI
OpenThread( DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
)
174 OBJECT_ATTRIBUTES attr
;
177 attr
.Length
= sizeof(attr
);
178 attr
.RootDirectory
= 0;
179 attr
.Attributes
= bInheritHandle
? OBJ_INHERIT
: 0;
180 attr
.ObjectName
= NULL
;
181 attr
.SecurityDescriptor
= NULL
;
182 attr
.SecurityQualityOfService
= NULL
;
184 cid
.UniqueProcess
= 0; /* FIXME */
185 cid
.UniqueThread
= (HANDLE
)dwThreadId
;
186 status
= NtOpenThread( &handle
, dwDesiredAccess
, &attr
, &cid
);
189 SetLastError( RtlNtStatusToDosError(status
) );
196 /***********************************************************************
197 * ExitThread [KERNEL32.@] Ends a thread
202 void WINAPI
ExitThread( DWORD code
) /* [in] Exit code for this thread */
205 SERVER_START_REQ( terminate_thread
)
207 /* send the exit code to the server */
208 req
->handle
= GetCurrentThread();
209 req
->exit_code
= code
;
210 wine_server_call( req
);
217 LdrShutdownProcess();
220 else RtlExitUserThread( code
);
224 /**********************************************************************
225 * TerminateThread [KERNEL32.@] Terminates a thread
231 BOOL WINAPI
TerminateThread( HANDLE handle
, /* [in] Handle to thread */
232 DWORD exit_code
) /* [in] Exit code for thread */
234 NTSTATUS status
= NtTerminateThread( handle
, exit_code
);
235 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
240 /***********************************************************************
241 * FreeLibraryAndExitThread (KERNEL32.@)
243 void WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
245 FreeLibrary(hLibModule
);
246 ExitThread(dwExitCode
);
250 /**********************************************************************
251 * GetExitCodeThread (KERNEL32.@)
253 * Gets termination status of thread.
259 BOOL WINAPI
GetExitCodeThread(
260 HANDLE hthread
, /* [in] Handle to thread */
261 LPDWORD exitcode
) /* [out] Address to receive termination status */
263 THREAD_BASIC_INFORMATION info
;
264 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
265 &info
, sizeof(info
), NULL
);
269 SetLastError( RtlNtStatusToDosError(status
) );
272 if (exitcode
) *exitcode
= info
.ExitStatus
;
277 /***********************************************************************
278 * SetThreadContext [KERNEL32.@] Sets context of thread.
284 BOOL WINAPI
SetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
285 const CONTEXT
*context
) /* [in] Address of context structure */
287 NTSTATUS status
= NtSetContextThread( handle
, context
);
288 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
293 /***********************************************************************
294 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
300 BOOL WINAPI
GetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
301 CONTEXT
*context
) /* [out] Address of context structure */
303 NTSTATUS status
= NtGetContextThread( handle
, context
);
304 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
309 /**********************************************************************
310 * SuspendThread [KERNEL32.@] Suspends a thread.
313 * Success: Previous suspend count
314 * Failure: 0xFFFFFFFF
316 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
319 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
324 SetLastError( RtlNtStatusToDosError(status
) );
330 /**********************************************************************
331 * ResumeThread [KERNEL32.@] Resumes a thread.
333 * Decrements a thread's suspend count. When count is zero, the
334 * execution of the thread is resumed.
337 * Success: Previous suspend count
338 * Failure: 0xFFFFFFFF
341 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
344 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
349 SetLastError( RtlNtStatusToDosError(status
) );
355 /**********************************************************************
356 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
359 * Success: Thread's priority level.
360 * Failure: THREAD_PRIORITY_ERROR_RETURN
362 INT WINAPI
GetThreadPriority(
363 HANDLE hthread
) /* [in] Handle to thread */
365 THREAD_BASIC_INFORMATION info
;
366 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
367 &info
, sizeof(info
), NULL
);
371 SetLastError( RtlNtStatusToDosError(status
) );
372 return THREAD_PRIORITY_ERROR_RETURN
;
374 return info
.Priority
;
378 /**********************************************************************
379 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
385 BOOL WINAPI
SetThreadPriority(
386 HANDLE hthread
, /* [in] Handle to thread */
387 INT priority
) /* [in] Thread priority level */
389 DWORD prio
= priority
;
392 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
393 &prio
, sizeof(prio
));
397 SetLastError( RtlNtStatusToDosError(status
) );
405 /**********************************************************************
406 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
408 * Always reports that priority boost is disabled.
414 BOOL WINAPI
GetThreadPriorityBoost(
415 HANDLE hthread
, /* [in] Handle to thread */
416 PBOOL pstate
) /* [out] pointer to var that receives the boost state */
418 if (pstate
) *pstate
= FALSE
;
423 /**********************************************************************
424 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
426 * Priority boost is not implemented. Thsi function always returns
427 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
430 * Always returns FALSE to indicate a failure
432 BOOL WINAPI
SetThreadPriorityBoost(
433 HANDLE hthread
, /* [in] Handle to thread */
434 BOOL disable
) /* [in] TRUE to disable priority boost */
436 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
441 /**********************************************************************
442 * SetThreadAffinityMask (KERNEL32.@)
444 DWORD WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD dwThreadAffinityMask
)
447 SERVER_START_REQ( set_thread_info
)
449 req
->handle
= hThread
;
450 req
->affinity
= dwThreadAffinityMask
;
451 req
->mask
= SET_THREAD_INFO_AFFINITY
;
452 ret
= !wine_server_call_err( req
);
453 /* FIXME: should return previous value */
460 /**********************************************************************
461 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
464 * Success: Value of last call to SetThreadIdealProcessor
467 DWORD WINAPI
SetThreadIdealProcessor(
468 HANDLE hThread
, /* [in] Specifies the thread of interest */
469 DWORD dwIdealProcessor
) /* [in] Specifies the new preferred processor */
471 FIXME("(%p): stub\n",hThread
);
472 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
477 /* callback for QueueUserAPC */
478 static void CALLBACK
call_user_apc( ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
)
480 PAPCFUNC func
= (PAPCFUNC
)arg1
;
484 /***********************************************************************
485 * QueueUserAPC (KERNEL32.@)
487 DWORD WINAPI
QueueUserAPC( PAPCFUNC func
, HANDLE hthread
, ULONG_PTR data
)
489 NTSTATUS status
= NtQueueApcThread( hthread
, call_user_apc
, (ULONG_PTR
)func
, data
, 0 );
491 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
495 /***********************************************************************
496 * QueueUserWorkItem (KERNEL32.@)
498 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
500 FIXME("(%p,%p,0x%08lx): stub\n", Function
, Context
, Flags
);
504 /**********************************************************************
505 * GetThreadTimes [KERNEL32.@] Obtains timing information.
511 BOOL WINAPI
GetThreadTimes(
512 HANDLE thread
, /* [in] Specifies the thread of interest */
513 LPFILETIME creationtime
, /* [out] When the thread was created */
514 LPFILETIME exittime
, /* [out] When the thread was destroyed */
515 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
516 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
518 KERNEL_USER_TIMES kusrt
;
521 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
522 sizeof(kusrt
), NULL
);
525 SetLastError( RtlNtStatusToDosError(status
) );
530 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
531 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
535 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
536 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
540 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
541 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
545 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
546 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
553 /**********************************************************************
554 * VWin32_BoostThreadGroup [KERNEL.535]
556 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT boost
)
558 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
562 /**********************************************************************
563 * VWin32_BoostThreadStatic [KERNEL.536]
565 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT boost
)
567 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
571 /***********************************************************************
572 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
575 * Pseudohandle for the current thread
577 #undef GetCurrentThread
578 HANDLE WINAPI
GetCurrentThread(void)
580 return (HANDLE
)0xfffffffe;
586 /***********************************************************************
587 * SetLastError (KERNEL.147)
588 * SetLastError (KERNEL32.@)
590 /* void WINAPI SetLastError( DWORD error ); */
591 __ASM_GLOBAL_FUNC( SetLastError
,
592 "movl 4(%esp),%eax\n\t"
597 /***********************************************************************
598 * GetLastError (KERNEL.148)
599 * GetLastError (KERNEL32.@)
601 /* DWORD WINAPI GetLastError(void); */
602 __ASM_GLOBAL_FUNC( GetLastError
, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
604 /***********************************************************************
605 * GetCurrentProcessId (KERNEL.471)
606 * GetCurrentProcessId (KERNEL32.@)
608 /* DWORD WINAPI GetCurrentProcessId(void) */
609 __ASM_GLOBAL_FUNC( GetCurrentProcessId
, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
611 /***********************************************************************
612 * GetCurrentThreadId (KERNEL.462)
613 * GetCurrentThreadId (KERNEL32.@)
615 /* DWORD WINAPI GetCurrentThreadId(void) */
616 __ASM_GLOBAL_FUNC( GetCurrentThreadId
, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
620 /**********************************************************************
621 * SetLastError (KERNEL.147)
622 * SetLastError (KERNEL32.@)
624 * Sets the last-error code.
629 void WINAPI
SetLastError( DWORD error
) /* [in] Per-thread error code */
631 NtCurrentTeb()->LastErrorValue
= error
;
634 /**********************************************************************
635 * GetLastError (KERNEL.148)
636 * GetLastError (KERNEL32.@)
638 * Get the last-error code.
643 DWORD WINAPI
GetLastError(void)
645 return NtCurrentTeb()->LastErrorValue
;
648 /***********************************************************************
649 * GetCurrentProcessId (KERNEL.471)
650 * GetCurrentProcessId (KERNEL32.@)
652 * Get the current process identifier.
655 * current process identifier
657 DWORD WINAPI
GetCurrentProcessId(void)
659 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueProcess
;
662 /***********************************************************************
663 * GetCurrentThreadId (KERNEL.462)
664 * GetCurrentThreadId (KERNEL32.@)
666 * Get the current thread identifier.
669 * current thread identifier
671 DWORD WINAPI
GetCurrentThreadId(void)
673 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueThread
;
676 #endif /* __i386__ */