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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
27 #include <sys/types.h>
33 #define WIN32_NO_STATUS
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
);
50 /***********************************************************************
51 * CreateThread (KERNEL32.@)
53 HANDLE WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
54 LPTHREAD_START_ROUTINE start
, LPVOID param
,
55 DWORD flags
, LPDWORD id
)
57 return CreateRemoteThread( GetCurrentProcess(),
58 sa
, stack
, start
, param
, flags
, id
);
62 /***************************************************************************
63 * CreateRemoteThread (KERNEL32.@)
65 * Creates a thread that runs in the address space of another process
70 * Success: Handle to the new thread.
71 * Failure: NULL. Use GetLastError() to find the error cause.
74 * Improper memory allocation: there's no ability to free new_thread_info
76 * Bad start address for RtlCreateUserThread because the library
77 * may be loaded at different address in other process.
79 HANDLE WINAPI
CreateRemoteThread( HANDLE hProcess
, SECURITY_ATTRIBUTES
*sa
, SIZE_T stack
,
80 LPTHREAD_START_ROUTINE start
, LPVOID param
,
81 DWORD flags
, LPDWORD id
)
86 SIZE_T stack_reserve
= 0, stack_commit
= 0;
88 if (flags
& STACK_SIZE_PARAM_IS_A_RESERVATION
) stack_reserve
= stack
;
89 else stack_commit
= stack
;
91 status
= RtlCreateUserThread( hProcess
, NULL
, TRUE
,
92 NULL
, stack_reserve
, stack_commit
,
93 (PRTL_THREAD_START_ROUTINE
)start
, param
, &handle
, &client_id
);
94 if (status
== STATUS_SUCCESS
)
96 if (id
) *id
= (DWORD
)client_id
.UniqueThread
;
97 if (sa
&& (sa
->nLength
>= sizeof(*sa
)) && sa
->bInheritHandle
)
98 SetHandleInformation( handle
, HANDLE_FLAG_INHERIT
, HANDLE_FLAG_INHERIT
);
99 if (!(flags
& CREATE_SUSPENDED
))
102 if (NtResumeThread( handle
, &ret
))
105 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
112 SetLastError( RtlNtStatusToDosError(status
) );
119 /***********************************************************************
120 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
122 HANDLE WINAPI
OpenThread( DWORD dwDesiredAccess
, BOOL bInheritHandle
, DWORD dwThreadId
)
126 OBJECT_ATTRIBUTES attr
;
129 attr
.Length
= sizeof(attr
);
130 attr
.RootDirectory
= 0;
131 attr
.Attributes
= bInheritHandle
? OBJ_INHERIT
: 0;
132 attr
.ObjectName
= NULL
;
133 attr
.SecurityDescriptor
= NULL
;
134 attr
.SecurityQualityOfService
= NULL
;
136 cid
.UniqueProcess
= 0; /* FIXME */
137 cid
.UniqueThread
= (HANDLE
)dwThreadId
;
138 status
= NtOpenThread( &handle
, dwDesiredAccess
, &attr
, &cid
);
141 SetLastError( RtlNtStatusToDosError(status
) );
148 /***********************************************************************
149 * ExitThread [KERNEL32.@] Ends a thread
154 void WINAPI
ExitThread( DWORD code
) /* [in] Exit code for this thread */
157 SERVER_START_REQ( terminate_thread
)
159 /* send the exit code to the server */
160 req
->handle
= GetCurrentThread();
161 req
->exit_code
= code
;
162 wine_server_call( req
);
169 LdrShutdownProcess();
172 else RtlExitUserThread( code
);
176 /**********************************************************************
177 * TerminateThread [KERNEL32.@] Terminates a thread
183 BOOL WINAPI
TerminateThread( HANDLE handle
, /* [in] Handle to thread */
184 DWORD exit_code
) /* [in] Exit code for thread */
186 NTSTATUS status
= NtTerminateThread( handle
, exit_code
);
187 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
192 /***********************************************************************
193 * FreeLibraryAndExitThread (KERNEL32.@)
195 void WINAPI
FreeLibraryAndExitThread(HINSTANCE hLibModule
, DWORD dwExitCode
)
197 FreeLibrary(hLibModule
);
198 ExitThread(dwExitCode
);
202 /**********************************************************************
203 * GetExitCodeThread (KERNEL32.@)
205 * Gets termination status of thread.
211 BOOL WINAPI
GetExitCodeThread(
212 HANDLE hthread
, /* [in] Handle to thread */
213 LPDWORD exitcode
) /* [out] Address to receive termination status */
215 THREAD_BASIC_INFORMATION info
;
216 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
217 &info
, sizeof(info
), NULL
);
221 SetLastError( RtlNtStatusToDosError(status
) );
224 if (exitcode
) *exitcode
= info
.ExitStatus
;
229 /***********************************************************************
230 * SetThreadContext [KERNEL32.@] Sets context of thread.
236 BOOL WINAPI
SetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
237 const CONTEXT
*context
) /* [in] Address of context structure */
239 NTSTATUS status
= NtSetContextThread( handle
, context
);
240 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
245 /***********************************************************************
246 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
252 BOOL WINAPI
GetThreadContext( HANDLE handle
, /* [in] Handle to thread with context */
253 CONTEXT
*context
) /* [out] Address of context structure */
255 NTSTATUS status
= NtGetContextThread( handle
, context
);
256 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
261 /**********************************************************************
262 * SuspendThread [KERNEL32.@] Suspends a thread.
265 * Success: Previous suspend count
266 * Failure: 0xFFFFFFFF
268 DWORD WINAPI
SuspendThread( HANDLE hthread
) /* [in] Handle to the thread */
271 NTSTATUS status
= NtSuspendThread( hthread
, &ret
);
276 SetLastError( RtlNtStatusToDosError(status
) );
282 /**********************************************************************
283 * ResumeThread [KERNEL32.@] Resumes a thread.
285 * Decrements a thread's suspend count. When count is zero, the
286 * execution of the thread is resumed.
289 * Success: Previous suspend count
290 * Failure: 0xFFFFFFFF
293 DWORD WINAPI
ResumeThread( HANDLE hthread
) /* [in] Identifies thread to restart */
296 NTSTATUS status
= NtResumeThread( hthread
, &ret
);
301 SetLastError( RtlNtStatusToDosError(status
) );
307 /**********************************************************************
308 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
311 * Success: Thread's priority level.
312 * Failure: THREAD_PRIORITY_ERROR_RETURN
314 INT WINAPI
GetThreadPriority(
315 HANDLE hthread
) /* [in] Handle to thread */
317 THREAD_BASIC_INFORMATION info
;
318 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
319 &info
, sizeof(info
), NULL
);
323 SetLastError( RtlNtStatusToDosError(status
) );
324 return THREAD_PRIORITY_ERROR_RETURN
;
326 return info
.Priority
;
330 /**********************************************************************
331 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
337 BOOL WINAPI
SetThreadPriority(
338 HANDLE hthread
, /* [in] Handle to thread */
339 INT priority
) /* [in] Thread priority level */
341 DWORD prio
= priority
;
344 status
= NtSetInformationThread(hthread
, ThreadBasePriority
,
345 &prio
, sizeof(prio
));
349 SetLastError( RtlNtStatusToDosError(status
) );
357 /**********************************************************************
358 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
360 * Always reports that priority boost is disabled.
366 BOOL WINAPI
GetThreadPriorityBoost(
367 HANDLE hthread
, /* [in] Handle to thread */
368 PBOOL pstate
) /* [out] pointer to var that receives the boost state */
370 if (pstate
) *pstate
= FALSE
;
375 /**********************************************************************
376 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
378 * Priority boost is not implemented. Thsi function always returns
379 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
382 * Always returns FALSE to indicate a failure
384 BOOL WINAPI
SetThreadPriorityBoost(
385 HANDLE hthread
, /* [in] Handle to thread */
386 BOOL disable
) /* [in] TRUE to disable priority boost */
388 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
393 /**********************************************************************
394 * SetThreadAffinityMask (KERNEL32.@)
396 DWORD WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD dwThreadAffinityMask
)
399 THREAD_BASIC_INFORMATION tbi
;
401 status
= NtQueryInformationThread( hThread
, ThreadBasicInformation
,
402 &tbi
, sizeof(tbi
), NULL
);
405 SetLastError( RtlNtStatusToDosError(status
) );
408 status
= NtSetInformationThread( hThread
, ThreadAffinityMask
,
409 &dwThreadAffinityMask
,
410 sizeof(dwThreadAffinityMask
));
413 SetLastError( RtlNtStatusToDosError(status
) );
416 return tbi
.AffinityMask
;
420 /**********************************************************************
421 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
424 * Success: Value of last call to SetThreadIdealProcessor
427 DWORD WINAPI
SetThreadIdealProcessor(
428 HANDLE hThread
, /* [in] Specifies the thread of interest */
429 DWORD dwIdealProcessor
) /* [in] Specifies the new preferred processor */
431 FIXME("(%p): stub\n",hThread
);
432 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
437 /* callback for QueueUserAPC */
438 static void CALLBACK
call_user_apc( ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
)
440 PAPCFUNC func
= (PAPCFUNC
)arg1
;
444 /***********************************************************************
445 * QueueUserAPC (KERNEL32.@)
447 DWORD WINAPI
QueueUserAPC( PAPCFUNC func
, HANDLE hthread
, ULONG_PTR data
)
449 NTSTATUS status
= NtQueueApcThread( hthread
, call_user_apc
, (ULONG_PTR
)func
, data
, 0 );
451 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
455 /***********************************************************************
456 * QueueUserWorkItem (KERNEL32.@)
458 BOOL WINAPI
QueueUserWorkItem( LPTHREAD_START_ROUTINE Function
, PVOID Context
, ULONG Flags
)
462 TRACE("(%p,%p,0x%08x)\n", Function
, Context
, Flags
);
464 status
= RtlQueueWorkItem( Function
, Context
, Flags
);
466 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
470 /**********************************************************************
471 * GetThreadTimes [KERNEL32.@] Obtains timing information.
477 BOOL WINAPI
GetThreadTimes(
478 HANDLE thread
, /* [in] Specifies the thread of interest */
479 LPFILETIME creationtime
, /* [out] When the thread was created */
480 LPFILETIME exittime
, /* [out] When the thread was destroyed */
481 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
482 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
484 KERNEL_USER_TIMES kusrt
;
487 status
= NtQueryInformationThread(thread
, ThreadTimes
, &kusrt
,
488 sizeof(kusrt
), NULL
);
491 SetLastError( RtlNtStatusToDosError(status
) );
496 creationtime
->dwLowDateTime
= kusrt
.CreateTime
.u
.LowPart
;
497 creationtime
->dwHighDateTime
= kusrt
.CreateTime
.u
.HighPart
;
501 exittime
->dwLowDateTime
= kusrt
.ExitTime
.u
.LowPart
;
502 exittime
->dwHighDateTime
= kusrt
.ExitTime
.u
.HighPart
;
506 kerneltime
->dwLowDateTime
= kusrt
.KernelTime
.u
.LowPart
;
507 kerneltime
->dwHighDateTime
= kusrt
.KernelTime
.u
.HighPart
;
511 usertime
->dwLowDateTime
= kusrt
.UserTime
.u
.LowPart
;
512 usertime
->dwHighDateTime
= kusrt
.UserTime
.u
.HighPart
;
519 /**********************************************************************
520 * VWin32_BoostThreadGroup [KERNEL.535]
522 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT boost
)
524 FIXME("(0x%08x,%d): stub\n", threadId
, boost
);
528 /**********************************************************************
529 * VWin32_BoostThreadStatic [KERNEL.536]
531 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT boost
)
533 FIXME("(0x%08x,%d): stub\n", threadId
, boost
);
537 /***********************************************************************
538 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
541 * Pseudohandle for the current thread
543 #undef GetCurrentThread
544 HANDLE WINAPI
GetCurrentThread(void)
546 return (HANDLE
)0xfffffffe;
552 /***********************************************************************
553 * SetLastError (KERNEL.147)
554 * SetLastError (KERNEL32.@)
556 /* void WINAPI SetLastError( DWORD error ); */
557 __ASM_GLOBAL_FUNC( SetLastError
,
558 "movl 4(%esp),%eax\n\t"
563 /***********************************************************************
564 * GetLastError (KERNEL.148)
565 * GetLastError (KERNEL32.@)
567 /* DWORD WINAPI GetLastError(void); */
568 __ASM_GLOBAL_FUNC( GetLastError
, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
570 /***********************************************************************
571 * GetCurrentProcessId (KERNEL.471)
572 * GetCurrentProcessId (KERNEL32.@)
574 /* DWORD WINAPI GetCurrentProcessId(void) */
575 __ASM_GLOBAL_FUNC( GetCurrentProcessId
, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
577 /***********************************************************************
578 * GetCurrentThreadId (KERNEL.462)
579 * GetCurrentThreadId (KERNEL32.@)
581 /* DWORD WINAPI GetCurrentThreadId(void) */
582 __ASM_GLOBAL_FUNC( GetCurrentThreadId
, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
586 /**********************************************************************
587 * SetLastError (KERNEL.147)
588 * SetLastError (KERNEL32.@)
590 * Sets the last-error code.
595 void WINAPI
SetLastError( DWORD error
) /* [in] Per-thread error code */
597 NtCurrentTeb()->LastErrorValue
= error
;
600 /**********************************************************************
601 * GetLastError (KERNEL.148)
602 * GetLastError (KERNEL32.@)
604 * Get the last-error code.
609 DWORD WINAPI
GetLastError(void)
611 return NtCurrentTeb()->LastErrorValue
;
614 /***********************************************************************
615 * GetCurrentProcessId (KERNEL.471)
616 * GetCurrentProcessId (KERNEL32.@)
618 * Get the current process identifier.
621 * current process identifier
623 DWORD WINAPI
GetCurrentProcessId(void)
625 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueProcess
;
628 /***********************************************************************
629 * GetCurrentThreadId (KERNEL.462)
630 * GetCurrentThreadId (KERNEL32.@)
632 * Get the current thread identifier.
635 * current thread identifier
637 DWORD WINAPI
GetCurrentThreadId(void)
639 return (DWORD
)NtCurrentTeb()->ClientId
.UniqueThread
;
642 #endif /* __i386__ */