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>
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 /***********************************************************************
54 * Allocate the stack of a thread.
56 TEB
*THREAD_InitStack( TEB
*teb
, DWORD stack_size
)
59 DWORD page_size
= getpagesize();
62 stack_size
= (stack_size
+ (page_size
- 1)) & ~(page_size
- 1);
63 if (stack_size
< 1024 * 1024) stack_size
= 1024 * 1024; /* Xlib needs a large stack */
65 if (!(base
= VirtualAlloc( NULL
, stack_size
, MEM_COMMIT
, PAGE_READWRITE
)))
68 teb
->DeallocationStack
= base
;
69 teb
->Tib
.StackBase
= (char *)base
+ stack_size
;
70 teb
->Tib
.StackLimit
= base
; /* note: limit is lower than base since the stack grows down */
72 /* Setup guard pages */
74 VirtualProtect( base
, 1, PAGE_READWRITE
| PAGE_GUARD
, &old_prot
);
79 struct new_thread_info
81 LPTHREAD_START_ROUTINE func
;
85 /***********************************************************************
88 * Start execution of a newly created thread. Does not return.
90 static void CALLBACK
THREAD_Start( void *ptr
)
92 struct new_thread_info
*info
= ptr
;
93 LPTHREAD_START_ROUTINE func
= info
->func
;
94 void *arg
= info
->arg
;
96 RtlFreeHeap( GetProcessHeap(), 0, info
);
99 DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func
);
103 MODULE_DllThreadAttach( NULL
);
104 ExitThread( func( arg
) );
106 __EXCEPT(UnhandledExceptionFilter
)
108 TerminateThread( GetCurrentThread(), GetExceptionCode() );
114 /***********************************************************************
115 * CreateThread (KERNEL32.@)
117 HANDLE WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
118 LPTHREAD_START_ROUTINE start
, LPVOID param
,
119 DWORD flags
, LPDWORD id
)
121 return CreateRemoteThread( GetCurrentProcess(),
122 sa
, stack
, start
, param
, flags
, id
);
126 /***************************************************************************
127 * CreateRemoteThread (KERNEL32.@)
129 * Creates a thread that runs in the address space of another process
134 * Success: Handle to the new thread.
135 * Failure: NULL. Use GetLastError() to find the error cause.
138 * Improper memory allocation: there's no ability to free new_thread_info
140 * Bad start address for RtlCreateUserThread because the library
141 * may be loaded at different address in other process.
143 HANDLE WINAPI
CreateRemoteThread( HANDLE hProcess
, SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
144 LPTHREAD_START_ROUTINE start
, LPVOID param
,
145 DWORD flags
, LPDWORD id
)
150 SIZE_T stack_reserve
= 0, stack_commit
= 0;
151 struct new_thread_info
*info
;
153 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info
) )))
155 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
161 if (flags
& STACK_SIZE_PARAM_IS_A_RESERVATION
) stack_reserve
= stack
;
162 else stack_commit
= stack
;
164 status
= RtlCreateUserThread( hProcess
, NULL
, TRUE
,
165 NULL
, stack_reserve
, stack_commit
,
166 THREAD_Start
, info
, &handle
, &client_id
);
167 if (status
== STATUS_SUCCESS
)
169 if (id
) *id
= (DWORD
)client_id
.UniqueThread
;
170 if (sa
&& (sa
->nLength
>= sizeof(*sa
)) && sa
->bInheritHandle
)
171 SetHandleInformation( handle
, HANDLE_FLAG_INHERIT
, HANDLE_FLAG_INHERIT
);
172 if (!(flags
& CREATE_SUSPENDED
))
175 if (NtResumeThread( handle
, &ret
))
178 RtlFreeHeap( GetProcessHeap(), 0, info
);
179 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
186 RtlFreeHeap( GetProcessHeap(), 0, info
);
187 SetLastError( RtlNtStatusToDosError(status
) );
194 /***********************************************************************
195 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
197 HANDLE WINAPI
OpenThread( DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
)
201 OBJECT_ATTRIBUTES attr
;
204 attr
.Length
= sizeof(attr
);
205 attr
.RootDirectory
= 0;
206 attr
.Attributes
= bInheritHandle
? OBJ_INHERIT
: 0;
207 attr
.ObjectName
= NULL
;
208 attr
.SecurityDescriptor
= NULL
;
209 attr
.SecurityQualityOfService
= NULL
;
211 cid
.UniqueProcess
= 0; /* FIXME */
212 cid
.UniqueThread
= (HANDLE
)dwThreadId
;
213 status
= NtOpenThread( &handle
, dwDesiredAccess
, &attr
, &cid
);
216 SetLastError( RtlNtStatusToDosError(status
) );
223 /***********************************************************************
224 * ExitThread [KERNEL32.@] Ends a thread
229 void WINAPI
ExitThread( DWORD code
) /* [in] Exit code for this thread */
232 SERVER_START_REQ( terminate_thread
)
234 /* send the exit code to the server */
235 req
->handle
= GetCurrentThread();
236 req
->exit_code
= code
;
237 wine_server_call( req
);
244 LdrShutdownProcess();
247 else RtlExitUserThread( code
);
251 /**********************************************************************
252 * TerminateThread [KERNEL32.@] Terminates a thread
258 BOOL WINAPI
TerminateThread( HANDLE handle
, /* [in] Handle to thread */
259 DWORD exit_code
) /* [in] Exit code for thread */
261 NTSTATUS status
= NtTerminateThread( handle
, exit_code
);
262 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
267 /***********************************************************************
268 * FreeLibraryAndExitThread (KERNEL32.@)
270 void WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
272 FreeLibrary(hLibModule
);
273 ExitThread(dwExitCode
);
277 /**********************************************************************
278 * GetExitCodeThread (KERNEL32.@)
280 * Gets termination status of thread.
286 BOOL WINAPI
GetExitCodeThread(
287 HANDLE hthread
, /* [in] Handle to thread */
288 LPDWORD exitcode
) /* [out] Address to receive termination status */
290 THREAD_BASIC_INFORMATION info
;
291 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
292 &info
, sizeof(info
), NULL
);
296 SetLastError( RtlNtStatusToDosError(status
) );
299 if (exitcode
) *exitcode
= info
.ExitStatus
;
304 /***********************************************************************
305 * SetThreadContext [KERNEL32.@] Sets context of thread.
311 BOOL WINAPI
SetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
312 const CONTEXT
*context
) /* [in] Address of context structure */
314 NTSTATUS status
= NtSetContextThread( handle
, context
);
315 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
320 /***********************************************************************
321 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
327 BOOL WINAPI
GetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
328 CONTEXT
*context
) /* [out] Address of context structure */
330 NTSTATUS status
= NtGetContextThread( handle
, context
);
331 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
336 /**********************************************************************
337 * SuspendThread [KERNEL32.@] Suspends a thread.
340 * Success: Previous suspend count
341 * Failure: 0xFFFFFFFF
343 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
346 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
351 SetLastError( RtlNtStatusToDosError(status
) );
357 /**********************************************************************
358 * ResumeThread [KERNEL32.@] Resumes a thread.
360 * Decrements a thread's suspend count. When count is zero, the
361 * execution of the thread is resumed.
364 * Success: Previous suspend count
365 * Failure: 0xFFFFFFFF
368 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
371 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
376 SetLastError( RtlNtStatusToDosError(status
) );
382 /**********************************************************************
383 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
386 * Success: Thread's priority level.
387 * Failure: THREAD_PRIORITY_ERROR_RETURN
389 INT WINAPI
GetThreadPriority(
390 HANDLE hthread
) /* [in] Handle to thread */
392 THREAD_BASIC_INFORMATION info
;
393 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
394 &info
, sizeof(info
), NULL
);
398 SetLastError( RtlNtStatusToDosError(status
) );
399 return THREAD_PRIORITY_ERROR_RETURN
;
401 return info
.Priority
;
405 /**********************************************************************
406 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
412 BOOL WINAPI
SetThreadPriority(
413 HANDLE hthread
, /* [in] Handle to thread */
414 INT priority
) /* [in] Thread priority level */
416 DWORD prio
= priority
;
419 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
420 &prio
, sizeof(prio
));
424 SetLastError( RtlNtStatusToDosError(status
) );
432 /**********************************************************************
433 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
435 * Always reports that priority boost is disabled.
441 BOOL WINAPI
GetThreadPriorityBoost(
442 HANDLE hthread
, /* [in] Handle to thread */
443 PBOOL pstate
) /* [out] pointer to var that receives the boost state */
445 if (pstate
) *pstate
= FALSE
;
450 /**********************************************************************
451 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
453 * Priority boost is not implemented. Thsi function always returns
454 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
457 * Always returns FALSE to indicate a failure
459 BOOL WINAPI
SetThreadPriorityBoost(
460 HANDLE hthread
, /* [in] Handle to thread */
461 BOOL disable
) /* [in] TRUE to disable priority boost */
463 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
468 /**********************************************************************
469 * SetThreadAffinityMask (KERNEL32.@)
471 DWORD WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD dwThreadAffinityMask
)
474 SERVER_START_REQ( set_thread_info
)
476 req
->handle
= hThread
;
477 req
->affinity
= dwThreadAffinityMask
;
478 req
->mask
= SET_THREAD_INFO_AFFINITY
;
479 ret
= !wine_server_call_err( req
);
480 /* FIXME: should return previous value */
487 /**********************************************************************
488 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
491 * Success: Value of last call to SetThreadIdealProcessor
494 DWORD WINAPI
SetThreadIdealProcessor(
495 HANDLE hThread
, /* [in] Specifies the thread of interest */
496 DWORD dwIdealProcessor
) /* [in] Specifies the new preferred processor */
498 FIXME("(%p): stub\n",hThread
);
499 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
504 /* callback for QueueUserAPC */
505 static void CALLBACK
call_user_apc( ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
)
507 PAPCFUNC func
= (PAPCFUNC
)arg1
;
511 /***********************************************************************
512 * QueueUserAPC (KERNEL32.@)
514 DWORD WINAPI
QueueUserAPC( PAPCFUNC func
, HANDLE hthread
, ULONG_PTR data
)
516 NTSTATUS status
= NtQueueApcThread( hthread
, call_user_apc
, (ULONG_PTR
)func
, data
, 0 );
518 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
522 /***********************************************************************
523 * QueueUserWorkItem (KERNEL32.@)
525 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
527 FIXME("(%p,%p,0x%08lx): stub\n", Function
, Context
, Flags
);
531 /**********************************************************************
532 * GetThreadTimes [KERNEL32.@] Obtains timing information.
538 BOOL WINAPI
GetThreadTimes(
539 HANDLE thread
, /* [in] Specifies the thread of interest */
540 LPFILETIME creationtime
, /* [out] When the thread was created */
541 LPFILETIME exittime
, /* [out] When the thread was destroyed */
542 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
543 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
545 KERNEL_USER_TIMES kusrt
;
548 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
549 sizeof(kusrt
), NULL
);
552 SetLastError( RtlNtStatusToDosError(status
) );
557 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
558 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
562 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
563 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
567 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
568 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
572 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
573 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
580 /**********************************************************************
581 * VWin32_BoostThreadGroup [KERNEL.535]
583 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT boost
)
585 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
589 /**********************************************************************
590 * VWin32_BoostThreadStatic [KERNEL.536]
592 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT boost
)
594 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
598 /***********************************************************************
599 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
602 * Pseudohandle for the current thread
604 #undef GetCurrentThread
605 HANDLE WINAPI
GetCurrentThread(void)
607 return (HANDLE
)0xfffffffe;
613 /***********************************************************************
614 * SetLastError (KERNEL.147)
615 * SetLastError (KERNEL32.@)
617 /* void WINAPI SetLastError( DWORD error ); */
618 __ASM_GLOBAL_FUNC( SetLastError
,
619 "movl 4(%esp),%eax\n\t"
624 /***********************************************************************
625 * GetLastError (KERNEL.148)
626 * GetLastError (KERNEL32.@)
628 /* DWORD WINAPI GetLastError(void); */
629 __ASM_GLOBAL_FUNC( GetLastError
, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
631 /***********************************************************************
632 * GetCurrentProcessId (KERNEL.471)
633 * GetCurrentProcessId (KERNEL32.@)
635 /* DWORD WINAPI GetCurrentProcessId(void) */
636 __ASM_GLOBAL_FUNC( GetCurrentProcessId
, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
638 /***********************************************************************
639 * GetCurrentThreadId (KERNEL.462)
640 * GetCurrentThreadId (KERNEL32.@)
642 /* DWORD WINAPI GetCurrentThreadId(void) */
643 __ASM_GLOBAL_FUNC( GetCurrentThreadId
, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
647 /**********************************************************************
648 * SetLastError (KERNEL.147)
649 * SetLastError (KERNEL32.@)
651 * Sets the last-error code.
653 void WINAPI
SetLastError( DWORD error
) /* [in] Per-thread error code */
655 NtCurrentTeb()->LastErrorValue
= error
;
658 /**********************************************************************
659 * GetLastError (KERNEL.148)
660 * GetLastError (KERNEL32.@)
662 * Returns last-error code.
664 DWORD WINAPI
GetLastError(void)
666 return NtCurrentTeb()->LastErrorValue
;
669 /***********************************************************************
670 * GetCurrentProcessId (KERNEL.471)
671 * GetCurrentProcessId (KERNEL32.@)
673 * Returns process identifier.
675 DWORD WINAPI
GetCurrentProcessId(void)
677 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueProcess
;
680 /***********************************************************************
681 * GetCurrentThreadId (KERNEL.462)
682 * GetCurrentThreadId (KERNEL32.@)
684 * Returns thread identifier.
686 DWORD WINAPI
GetCurrentThreadId(void)
688 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueThread
;
691 #endif /* __i386__ */