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 /***********************************************************************
55 * Allocate the stack of a thread.
57 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
60 DWORD page_size = getpagesize();
63 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
64 if (stack_size < 1024 * 1024) stack_size = 1024 * 1024; /* Xlib needs a large stack */
66 if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_READWRITE )))
69 teb->DeallocationStack = base;
70 teb->Tib.StackBase = (char *)base + stack_size;
71 teb->Tib.StackLimit = base; /* note: limit is lower than base since the stack grows down */
73 /* Setup guard pages */
75 VirtualProtect( base, 1, PAGE_READWRITE | PAGE_GUARD, &old_prot );
80 struct new_thread_info
82 LPTHREAD_START_ROUTINE func;
86 /***********************************************************************
89 * Start execution of a newly created thread. Does not return.
91 static void CALLBACK THREAD_Start( void *ptr )
93 struct new_thread_info *info = ptr;
94 LPTHREAD_START_ROUTINE func = info->func;
95 void *arg = info->arg;
97 RtlFreeHeap( GetProcessHeap(), 0, info );
100 DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
104 MODULE_DllThreadAttach( NULL );
105 ExitThread( func( arg ) );
107 __EXCEPT(UnhandledExceptionFilter)
109 TerminateThread( GetCurrentThread(), GetExceptionCode() );
115 /***********************************************************************
116 * CreateThread (KERNEL32.@)
118 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
119 LPTHREAD_START_ROUTINE start, LPVOID param,
120 DWORD flags, LPDWORD id )
122 return CreateRemoteThread( GetCurrentProcess(),
123 sa, stack, start, param, flags, id );
127 /***************************************************************************
128 * CreateRemoteThread (KERNEL32.@)
130 * Creates a thread that runs in the address space of another process
135 * Success: Handle to the new thread.
136 * Failure: NULL. Use GetLastError() to find the error cause.
139 * Improper memory allocation: there's no ability to free new_thread_info
141 * Bad start address for RtlCreateUserThread because the library
142 * may be loaded at different address in other process.
144 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
145 LPTHREAD_START_ROUTINE start, LPVOID param,
146 DWORD flags, LPDWORD id )
151 SIZE_T stack_reserve = 0, stack_commit = 0;
152 struct new_thread_info *info;
154 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
156 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
162 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
163 else stack_commit = stack;
165 status = RtlCreateUserThread( hProcess, NULL, TRUE,
166 NULL, stack_reserve, stack_commit,
167 THREAD_Start, info, &handle, &client_id );
168 if (status == STATUS_SUCCESS)
170 if (id) *id = (DWORD)client_id.UniqueThread;
171 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
172 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
173 if (!(flags & CREATE_SUSPENDED))
176 if (NtResumeThread( handle, &ret ))
179 RtlFreeHeap( GetProcessHeap(), 0, info );
180 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
187 RtlFreeHeap( GetProcessHeap(), 0, info );
188 SetLastError( RtlNtStatusToDosError(status) );
195 /***********************************************************************
196 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
198 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
202 OBJECT_ATTRIBUTES attr;
205 attr.Length = sizeof(attr);
206 attr.RootDirectory = 0;
207 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
208 attr.ObjectName = NULL;
209 attr.SecurityDescriptor = NULL;
210 attr.SecurityQualityOfService = NULL;
212 cid.UniqueProcess = 0; /* FIXME */
213 cid.UniqueThread = (HANDLE)dwThreadId;
214 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
217 SetLastError( RtlNtStatusToDosError(status) );
224 /***********************************************************************
225 * ExitThread [KERNEL32.@] Ends a thread
230 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
233 SERVER_START_REQ( terminate_thread )
235 /* send the exit code to the server */
236 req->handle = GetCurrentThread();
237 req->exit_code = code;
238 wine_server_call( req );
245 LdrShutdownProcess();
248 else RtlExitUserThread( code );
252 /**********************************************************************
253 * TerminateThread [KERNEL32.@] Terminates a thread
259 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
260 DWORD exit_code) /* [in] Exit code for thread */
262 NTSTATUS status = NtTerminateThread( handle, exit_code );
263 if (status) SetLastError( RtlNtStatusToDosError(status) );
268 /***********************************************************************
269 * FreeLibraryAndExitThread (KERNEL32.@)
271 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
273 FreeLibrary(hLibModule);
274 ExitThread(dwExitCode);
278 /**********************************************************************
279 * GetExitCodeThread (KERNEL32.@)
281 * Gets termination status of thread.
287 BOOL WINAPI GetExitCodeThread(
288 HANDLE hthread, /* [in] Handle to thread */
289 LPDWORD exitcode) /* [out] Address to receive termination status */
291 THREAD_BASIC_INFORMATION info;
292 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
293 &info, sizeof(info), NULL );
297 SetLastError( RtlNtStatusToDosError(status) );
300 if (exitcode) *exitcode = info.ExitStatus;
305 /***********************************************************************
306 * SetThreadContext [KERNEL32.@] Sets context of thread.
312 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
313 const CONTEXT *context ) /* [in] Address of context structure */
315 NTSTATUS status = NtSetContextThread( handle, context );
316 if (status) SetLastError( RtlNtStatusToDosError(status) );
321 /***********************************************************************
322 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
328 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
329 CONTEXT *context ) /* [out] Address of context structure */
331 NTSTATUS status = NtGetContextThread( handle, context );
332 if (status) SetLastError( RtlNtStatusToDosError(status) );
337 /**********************************************************************
338 * SuspendThread [KERNEL32.@] Suspends a thread.
341 * Success: Previous suspend count
342 * Failure: 0xFFFFFFFF
344 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
347 NTSTATUS status = NtSuspendThread( hthread, &ret );
352 SetLastError( RtlNtStatusToDosError(status) );
358 /**********************************************************************
359 * ResumeThread [KERNEL32.@] Resumes a thread.
361 * Decrements a thread's suspend count. When count is zero, the
362 * execution of the thread is resumed.
365 * Success: Previous suspend count
366 * Failure: 0xFFFFFFFF
369 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
372 NTSTATUS status = NtResumeThread( hthread, &ret );
377 SetLastError( RtlNtStatusToDosError(status) );
383 /**********************************************************************
384 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
387 * Success: Thread's priority level.
388 * Failure: THREAD_PRIORITY_ERROR_RETURN
390 INT WINAPI GetThreadPriority(
391 HANDLE hthread) /* [in] Handle to thread */
393 THREAD_BASIC_INFORMATION info;
394 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
395 &info, sizeof(info), NULL );
399 SetLastError( RtlNtStatusToDosError(status) );
400 return THREAD_PRIORITY_ERROR_RETURN;
402 return info.Priority;
406 /**********************************************************************
407 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
413 BOOL WINAPI SetThreadPriority(
414 HANDLE hthread, /* [in] Handle to thread */
415 INT priority) /* [in] Thread priority level */
417 DWORD prio = priority;
420 status = NtSetInformationThread(hthread, ThreadBasePriority,
421 &prio, sizeof(prio));
425 SetLastError( RtlNtStatusToDosError(status) );
433 /**********************************************************************
434 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
436 * Always reports that priority boost is disabled.
442 BOOL WINAPI GetThreadPriorityBoost(
443 HANDLE hthread, /* [in] Handle to thread */
444 PBOOL pstate) /* [out] pointer to var that receives the boost state */
446 if (pstate) *pstate = FALSE;
451 /**********************************************************************
452 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
454 * Priority boost is not implemented. Thsi function always returns
455 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
458 * Always returns FALSE to indicate a failure
460 BOOL WINAPI SetThreadPriorityBoost(
461 HANDLE hthread, /* [in] Handle to thread */
462 BOOL disable) /* [in] TRUE to disable priority boost */
464 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
469 /**********************************************************************
470 * SetThreadAffinityMask (KERNEL32.@)
472 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
475 SERVER_START_REQ( set_thread_info )
477 req->handle = hThread;
478 req->affinity = dwThreadAffinityMask;
479 req->mask = SET_THREAD_INFO_AFFINITY;
480 ret = !wine_server_call_err( req );
481 /* FIXME: should return previous value */
488 /**********************************************************************
489 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
492 * Success: Value of last call to SetThreadIdealProcessor
495 DWORD WINAPI SetThreadIdealProcessor(
496 HANDLE hThread, /* [in] Specifies the thread of interest */
497 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
499 FIXME("(%p): stub\n",hThread);
500 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
505 /* callback for QueueUserAPC */
506 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
508 PAPCFUNC func = (PAPCFUNC)arg1;
512 /***********************************************************************
513 * QueueUserAPC (KERNEL32.@)
515 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
517 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
519 if (status) SetLastError( RtlNtStatusToDosError(status) );
523 /***********************************************************************
524 * QueueUserWorkItem (KERNEL32.@)
526 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
528 FIXME("(%p,%p,0x%08lx): stub\n", Function, Context, Flags);
532 /**********************************************************************
533 * GetThreadTimes [KERNEL32.@] Obtains timing information.
539 BOOL WINAPI GetThreadTimes(
540 HANDLE thread, /* [in] Specifies the thread of interest */
541 LPFILETIME creationtime, /* [out] When the thread was created */
542 LPFILETIME exittime, /* [out] When the thread was destroyed */
543 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
544 LPFILETIME usertime) /* [out] Time thread spent in user mode */
546 KERNEL_USER_TIMES kusrt;
549 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
550 sizeof(kusrt), NULL);
553 SetLastError( RtlNtStatusToDosError(status) );
558 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
559 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
563 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
564 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
568 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
569 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
573 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
574 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
581 /**********************************************************************
582 * VWin32_BoostThreadGroup [KERNEL.535]
584 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
586 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
590 /**********************************************************************
591 * VWin32_BoostThreadStatic [KERNEL.536]
593 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
595 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
599 /***********************************************************************
600 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
603 * Pseudohandle for the current thread
605 #undef GetCurrentThread
606 HANDLE WINAPI GetCurrentThread(void)
608 return (HANDLE)0xfffffffe;
614 /***********************************************************************
615 * SetLastError (KERNEL.147)
616 * SetLastError (KERNEL32.@)
618 /* void WINAPI SetLastError( DWORD error ); */
619 __ASM_GLOBAL_FUNC( SetLastError,
620 "movl 4(%esp),%eax\n\t"
625 /***********************************************************************
626 * GetLastError (KERNEL.148)
627 * GetLastError (KERNEL32.@)
629 /* DWORD WINAPI GetLastError(void); */
630 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
632 /***********************************************************************
633 * GetCurrentProcessId (KERNEL.471)
634 * GetCurrentProcessId (KERNEL32.@)
636 /* DWORD WINAPI GetCurrentProcessId(void) */
637 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
639 /***********************************************************************
640 * GetCurrentThreadId (KERNEL.462)
641 * GetCurrentThreadId (KERNEL32.@)
643 /* DWORD WINAPI GetCurrentThreadId(void) */
644 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
648 /**********************************************************************
649 * SetLastError (KERNEL.147)
650 * SetLastError (KERNEL32.@)
652 * Sets the last-error code.
657 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
659 NtCurrentTeb()->LastErrorValue = error;
662 /**********************************************************************
663 * GetLastError (KERNEL.148)
664 * GetLastError (KERNEL32.@)
666 * Get the last-error code.
671 DWORD WINAPI GetLastError(void)
673 return NtCurrentTeb()->LastErrorValue;
676 /***********************************************************************
677 * GetCurrentProcessId (KERNEL.471)
678 * GetCurrentProcessId (KERNEL32.@)
680 * Get the current process identifier.
683 * current process identifier
685 DWORD WINAPI GetCurrentProcessId(void)
687 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
690 /***********************************************************************
691 * GetCurrentThreadId (KERNEL.462)
692 * GetCurrentThreadId (KERNEL32.@)
694 * Get the current thread identifier.
697 * current thread identifier
699 DWORD WINAPI GetCurrentThreadId(void)
701 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
704 #endif /* __i386__ */