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 THREAD_BASIC_INFORMATION tbi;
449 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
450 &tbi, sizeof(tbi), NULL );
453 SetLastError( RtlNtStatusToDosError(status) );
456 status = NtSetInformationThread( hThread, ThreadAffinityMask,
457 &dwThreadAffinityMask,
458 sizeof(dwThreadAffinityMask));
461 SetLastError( RtlNtStatusToDosError(status) );
464 return tbi.AffinityMask;
468 /**********************************************************************
469 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
472 * Success: Value of last call to SetThreadIdealProcessor
475 DWORD WINAPI SetThreadIdealProcessor(
476 HANDLE hThread, /* [in] Specifies the thread of interest */
477 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
479 FIXME("(%p): stub\n",hThread);
480 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
485 /* callback for QueueUserAPC */
486 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
488 PAPCFUNC func = (PAPCFUNC)arg1;
492 /***********************************************************************
493 * QueueUserAPC (KERNEL32.@)
495 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
497 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
499 if (status) SetLastError( RtlNtStatusToDosError(status) );
503 /***********************************************************************
504 * QueueUserWorkItem (KERNEL32.@)
506 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
508 FIXME("(%p,%p,0x%08lx): stub\n", Function, Context, Flags);
512 /**********************************************************************
513 * GetThreadTimes [KERNEL32.@] Obtains timing information.
519 BOOL WINAPI GetThreadTimes(
520 HANDLE thread, /* [in] Specifies the thread of interest */
521 LPFILETIME creationtime, /* [out] When the thread was created */
522 LPFILETIME exittime, /* [out] When the thread was destroyed */
523 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
524 LPFILETIME usertime) /* [out] Time thread spent in user mode */
526 KERNEL_USER_TIMES kusrt;
529 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
530 sizeof(kusrt), NULL);
533 SetLastError( RtlNtStatusToDosError(status) );
538 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
539 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
543 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
544 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
548 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
549 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
553 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
554 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
561 /**********************************************************************
562 * VWin32_BoostThreadGroup [KERNEL.535]
564 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
566 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
570 /**********************************************************************
571 * VWin32_BoostThreadStatic [KERNEL.536]
573 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
575 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
579 /***********************************************************************
580 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
583 * Pseudohandle for the current thread
585 #undef GetCurrentThread
586 HANDLE WINAPI GetCurrentThread(void)
588 return (HANDLE)0xfffffffe;
594 /***********************************************************************
595 * SetLastError (KERNEL.147)
596 * SetLastError (KERNEL32.@)
598 /* void WINAPI SetLastError( DWORD error ); */
599 __ASM_GLOBAL_FUNC( SetLastError,
600 "movl 4(%esp),%eax\n\t"
605 /***********************************************************************
606 * GetLastError (KERNEL.148)
607 * GetLastError (KERNEL32.@)
609 /* DWORD WINAPI GetLastError(void); */
610 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
612 /***********************************************************************
613 * GetCurrentProcessId (KERNEL.471)
614 * GetCurrentProcessId (KERNEL32.@)
616 /* DWORD WINAPI GetCurrentProcessId(void) */
617 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
619 /***********************************************************************
620 * GetCurrentThreadId (KERNEL.462)
621 * GetCurrentThreadId (KERNEL32.@)
623 /* DWORD WINAPI GetCurrentThreadId(void) */
624 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
628 /**********************************************************************
629 * SetLastError (KERNEL.147)
630 * SetLastError (KERNEL32.@)
632 * Sets the last-error code.
637 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
639 NtCurrentTeb()->LastErrorValue = error;
642 /**********************************************************************
643 * GetLastError (KERNEL.148)
644 * GetLastError (KERNEL32.@)
646 * Get the last-error code.
651 DWORD WINAPI GetLastError(void)
653 return NtCurrentTeb()->LastErrorValue;
656 /***********************************************************************
657 * GetCurrentProcessId (KERNEL.471)
658 * GetCurrentProcessId (KERNEL32.@)
660 * Get the current process identifier.
663 * current process identifier
665 DWORD WINAPI GetCurrentProcessId(void)
667 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
670 /***********************************************************************
671 * GetCurrentThreadId (KERNEL.462)
672 * GetCurrentThreadId (KERNEL32.@)
674 * Get the current thread identifier.
677 * current thread identifier
679 DWORD WINAPI GetCurrentThreadId(void)
681 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
684 #endif /* __i386__ */