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"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/times.h>
43 #include "wine/winbase16.h"
44 #include "wine/library.h"
45 #include "wine/pthread.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(thread);
50 WINE_DECLARE_DEBUG_CHANNEL(relay);
53 /***********************************************************************
56 * Allocate the stack of a thread.
58 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
61 DWORD page_size = getpagesize();
64 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
66 if (!(base = VirtualAlloc( NULL, stack_size, MEM_COMMIT, PAGE_EXECUTE_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_EXECUTE_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 )
125 SIZE_T stack_reserve = 0, stack_commit = 0;
126 struct new_thread_info *info;
128 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) )))
130 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
136 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
137 else stack_commit = stack;
139 status = RtlCreateUserThread( GetCurrentProcess(), NULL, (flags & CREATE_SUSPENDED) != 0,
140 NULL, stack_reserve, stack_commit,
141 THREAD_Start, info, &handle, &client_id );
142 if (status == STATUS_SUCCESS)
144 if (id) *id = (DWORD)client_id.UniqueThread;
145 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
146 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
150 RtlFreeHeap( GetProcessHeap(), 0, info );
151 SetLastError( RtlNtStatusToDosError(status) );
158 /***********************************************************************
159 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
161 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
164 SERVER_START_REQ( open_thread )
166 req->tid = dwThreadId;
167 req->access = dwDesiredAccess;
168 req->inherit = bInheritHandle;
169 if (!wine_server_call_err( req )) ret = reply->handle;
176 /***********************************************************************
177 * ExitThread [KERNEL32.@] Ends a thread
182 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
185 SERVER_START_REQ( terminate_thread )
187 /* send the exit code to the server */
188 req->handle = GetCurrentThread();
189 req->exit_code = code;
190 wine_server_call( req );
197 LdrShutdownProcess();
202 struct wine_pthread_thread_info info;
208 RemoveEntryList( &NtCurrentTeb()->TlsLinks );
211 info.stack_base = NtCurrentTeb()->DeallocationStack;
212 info.teb_base = NtCurrentTeb();
213 info.teb_sel = wine_get_fs();
214 info.exit_status = code;
217 NtFreeVirtualMemory( GetCurrentProcess(), &info.stack_base, &size, MEM_RELEASE | MEM_SYSTEM );
218 info.stack_size = size;
221 NtFreeVirtualMemory( GetCurrentProcess(), &info.teb_base, &size, MEM_RELEASE | MEM_SYSTEM );
222 info.teb_size = size;
224 /* block the async signals */
225 sigemptyset( &block_set );
226 sigaddset( &block_set, SIGALRM );
227 sigaddset( &block_set, SIGIO );
228 sigaddset( &block_set, SIGINT );
229 sigaddset( &block_set, SIGHUP );
230 sigaddset( &block_set, SIGUSR1 );
231 sigaddset( &block_set, SIGUSR2 );
232 sigaddset( &block_set, SIGTERM );
233 sigprocmask( SIG_BLOCK, &block_set, NULL );
235 close( NtCurrentTeb()->wait_fd[0] );
236 close( NtCurrentTeb()->wait_fd[1] );
237 close( NtCurrentTeb()->reply_fd );
238 close( NtCurrentTeb()->request_fd );
240 wine_pthread_exit_thread( &info );
245 /**********************************************************************
246 * TerminateThread [KERNEL32.@] Terminates a thread
252 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
253 DWORD exit_code) /* [in] Exit code for thread */
255 NTSTATUS status = NtTerminateThread( handle, exit_code );
256 if (status) SetLastError( RtlNtStatusToDosError(status) );
261 /***********************************************************************
262 * FreeLibraryAndExitThread (KERNEL32.@)
264 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
266 FreeLibrary(hLibModule);
267 ExitThread(dwExitCode);
271 /**********************************************************************
272 * GetExitCodeThread (KERNEL32.@)
274 * Gets termination status of thread.
280 BOOL WINAPI GetExitCodeThread(
281 HANDLE hthread, /* [in] Handle to thread */
282 LPDWORD exitcode) /* [out] Address to receive termination status */
284 THREAD_BASIC_INFORMATION info;
285 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
286 &info, sizeof(info), NULL );
290 SetLastError( RtlNtStatusToDosError(status) );
293 if (exitcode) *exitcode = info.ExitStatus;
298 /***********************************************************************
299 * SetThreadContext [KERNEL32.@] Sets context of thread.
305 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
306 const CONTEXT *context ) /* [in] Address of context structure */
308 NTSTATUS status = NtSetContextThread( handle, context );
309 if (status) SetLastError( RtlNtStatusToDosError(status) );
314 /***********************************************************************
315 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
321 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
322 CONTEXT *context ) /* [out] Address of context structure */
324 NTSTATUS status = NtGetContextThread( handle, context );
325 if (status) SetLastError( RtlNtStatusToDosError(status) );
330 /**********************************************************************
331 * SuspendThread [KERNEL32.@] Suspends a thread.
334 * Success: Previous suspend count
335 * Failure: 0xFFFFFFFF
337 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
340 NTSTATUS status = NtSuspendThread( hthread, &ret );
345 SetLastError( RtlNtStatusToDosError(status) );
351 /**********************************************************************
352 * ResumeThread [KERNEL32.@] Resumes a thread.
354 * Decrements a thread's suspend count. When count is zero, the
355 * execution of the thread is resumed.
358 * Success: Previous suspend count
359 * Failure: 0xFFFFFFFF
362 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
365 NTSTATUS status = NtResumeThread( hthread, &ret );
370 SetLastError( RtlNtStatusToDosError(status) );
376 /**********************************************************************
377 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
380 * Success: Thread's priority level.
381 * Failure: THREAD_PRIORITY_ERROR_RETURN
383 INT WINAPI GetThreadPriority(
384 HANDLE hthread) /* [in] Handle to thread */
386 THREAD_BASIC_INFORMATION info;
387 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
388 &info, sizeof(info), NULL );
392 SetLastError( RtlNtStatusToDosError(status) );
393 return THREAD_PRIORITY_ERROR_RETURN;
395 return info.Priority;
399 /**********************************************************************
400 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
406 BOOL WINAPI SetThreadPriority(
407 HANDLE hthread, /* [in] Handle to thread */
408 INT priority) /* [in] Thread priority level */
411 SERVER_START_REQ( set_thread_info )
413 req->handle = hthread;
414 req->priority = priority;
415 req->mask = SET_THREAD_INFO_PRIORITY;
416 ret = !wine_server_call_err( req );
423 /**********************************************************************
424 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
426 * Always reports that priority boost is disabled.
432 BOOL WINAPI GetThreadPriorityBoost(
433 HANDLE hthread, /* [in] Handle to thread */
434 PBOOL pstate) /* [out] pointer to var that receives the boost state */
436 if (pstate) *pstate = FALSE;
441 /**********************************************************************
442 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
444 * Priority boost is not implemented. Thsi function always returns
445 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
448 * Always returns FALSE to indicate a failure
450 BOOL WINAPI SetThreadPriorityBoost(
451 HANDLE hthread, /* [in] Handle to thread */
452 BOOL disable) /* [in] TRUE to disable priority boost */
454 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
459 /**********************************************************************
460 * SetThreadAffinityMask (KERNEL32.@)
462 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
465 SERVER_START_REQ( set_thread_info )
467 req->handle = hThread;
468 req->affinity = dwThreadAffinityMask;
469 req->mask = SET_THREAD_INFO_AFFINITY;
470 ret = !wine_server_call_err( req );
471 /* FIXME: should return previous value */
478 /**********************************************************************
479 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
482 * Success: Value of last call to SetThreadIdealProcessor
485 DWORD WINAPI SetThreadIdealProcessor(
486 HANDLE hThread, /* [in] Specifies the thread of interest */
487 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
489 FIXME("(%p): stub\n",hThread);
490 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
495 /* callback for QueueUserAPC */
496 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
498 PAPCFUNC func = (PAPCFUNC)arg1;
502 /***********************************************************************
503 * QueueUserAPC (KERNEL32.@)
505 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
507 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
509 if (status) SetLastError( RtlNtStatusToDosError(status) );
514 /**********************************************************************
515 * GetThreadTimes [KERNEL32.@] Obtains timing information.
521 BOOL WINAPI GetThreadTimes(
522 HANDLE thread, /* [in] Specifies the thread of interest */
523 LPFILETIME creationtime, /* [out] When the thread was created */
524 LPFILETIME exittime, /* [out] When the thread was destroyed */
525 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
526 LPFILETIME usertime) /* [out] Time thread spent in user mode */
530 if (creationtime || exittime)
532 /* We need to do a server call to get the creation time or exit time */
533 /* This works on any thread */
535 SERVER_START_REQ( get_thread_info )
537 req->handle = thread;
539 if ((ret = !wine_server_call_err( req )))
542 RtlSecondsSince1970ToTime( reply->creation_time, (LARGE_INTEGER*)creationtime );
544 RtlSecondsSince1970ToTime( reply->exit_time, (LARGE_INTEGER*)exittime );
549 if (ret && (kerneltime || usertime))
551 /* We call times(2) for kernel time or user time */
552 /* We can only (portably) do this for the current thread */
553 if (thread == GetCurrentThread())
557 long clocks_per_sec = sysconf(_SC_CLK_TCK);
562 time = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
563 kerneltime->dwHighDateTime = time >> 32;
564 kerneltime->dwLowDateTime = (DWORD)time;
568 time = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
569 usertime->dwHighDateTime = time >> 32;
570 usertime->dwLowDateTime = (DWORD)time;
575 if (kerneltime) kerneltime->dwHighDateTime = kerneltime->dwLowDateTime = 0;
576 if (usertime) usertime->dwHighDateTime = usertime->dwLowDateTime = 0;
577 FIXME("Cannot get kerneltime or usertime of other threads\n");
584 /**********************************************************************
585 * VWin32_BoostThreadGroup [KERNEL.535]
587 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
589 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
593 /**********************************************************************
594 * VWin32_BoostThreadStatic [KERNEL.536]
596 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
598 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
602 /***********************************************************************
603 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
606 * Pseudohandle for the current thread
608 #undef GetCurrentThread
609 HANDLE WINAPI GetCurrentThread(void)
611 return (HANDLE)0xfffffffe;
617 /***********************************************************************
618 * SetLastError (KERNEL.147)
619 * SetLastError (KERNEL32.@)
621 /* void WINAPI SetLastError( DWORD error ); */
622 __ASM_GLOBAL_FUNC( SetLastError,
623 "movl 4(%esp),%eax\n\t"
628 /***********************************************************************
629 * GetLastError (KERNEL.148)
630 * GetLastError (KERNEL32.@)
632 /* DWORD WINAPI GetLastError(void); */
633 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
635 /***********************************************************************
636 * GetCurrentProcessId (KERNEL.471)
637 * GetCurrentProcessId (KERNEL32.@)
639 /* DWORD WINAPI GetCurrentProcessId(void) */
640 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
642 /***********************************************************************
643 * GetCurrentThreadId (KERNEL.462)
644 * GetCurrentThreadId (KERNEL32.@)
646 /* DWORD WINAPI GetCurrentThreadId(void) */
647 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
651 /**********************************************************************
652 * SetLastError (KERNEL.147)
653 * SetLastError (KERNEL32.@)
655 * Sets the last-error code.
657 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
659 NtCurrentTeb()->last_error = error;
662 /**********************************************************************
663 * GetLastError (KERNEL.148)
664 * GetLastError (KERNEL32.@)
666 * Returns last-error code.
668 DWORD WINAPI GetLastError(void)
670 return NtCurrentTeb()->last_error;
673 /***********************************************************************
674 * GetCurrentProcessId (KERNEL.471)
675 * GetCurrentProcessId (KERNEL32.@)
677 * Returns process identifier.
679 DWORD WINAPI GetCurrentProcessId(void)
681 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
684 /***********************************************************************
685 * GetCurrentThreadId (KERNEL.462)
686 * GetCurrentThreadId (KERNEL32.@)
688 * Returns thread identifier.
690 DWORD WINAPI GetCurrentThreadId(void)
692 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
695 #endif /* __i386__ */