server: Make the FILE_SHARE_DELETE sharing checks depend on DELETE
[wine/multimedia.git] / dlls / kernel / thread.c
blob09c3081e08027f1883363dddc041bf39c46858f6
1 /*
2 * Win32 threads
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <sys/types.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winnls.h"
38 #include "thread.h"
39 #include "module.h"
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;
55 void *arg;
58 /***********************************************************************
59 * THREAD_Start
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 );
71 if (TRACE_ON(relay))
72 DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
74 __TRY
76 MODULE_DllThreadAttach( NULL );
77 ExitThread( func( arg ) );
79 __EXCEPT(UnhandledExceptionFilter)
81 TerminateThread( GetCurrentThread(), GetExceptionCode() );
83 __ENDTRY
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
104 * PARAMS
106 * RETURNS
107 * Success: Handle to the new thread.
108 * Failure: NULL. Use GetLastError() to find the error cause.
110 * BUGS
111 * Improper memory allocation: there's no ability to free new_thread_info
112 * in other process.
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 )
120 HANDLE handle;
121 CLIENT_ID client_id;
122 NTSTATUS status;
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 );
129 return 0;
131 info->func = start;
132 info->arg = param;
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))
147 ULONG ret;
148 if (NtResumeThread( handle, &ret ))
150 NtClose( handle );
151 RtlFreeHeap( GetProcessHeap(), 0, info );
152 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
153 handle = 0;
157 else
159 RtlFreeHeap( GetProcessHeap(), 0, info );
160 SetLastError( RtlNtStatusToDosError(status) );
161 handle = 0;
163 return handle;
167 /***********************************************************************
168 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
170 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
172 NTSTATUS status;
173 HANDLE handle;
174 OBJECT_ATTRIBUTES attr;
175 CLIENT_ID cid;
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 );
187 if (status)
189 SetLastError( RtlNtStatusToDosError(status) );
190 handle = 0;
192 return handle;
196 /***********************************************************************
197 * ExitThread [KERNEL32.@] Ends a thread
199 * RETURNS
200 * None
202 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
204 BOOL last;
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 );
211 last = reply->last;
213 SERVER_END_REQ;
215 if (last)
217 LdrShutdownProcess();
218 exit( code );
220 else RtlExitUserThread( code );
224 /**********************************************************************
225 * TerminateThread [KERNEL32.@] Terminates a thread
227 * RETURNS
228 * Success: TRUE
229 * Failure: FALSE
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) );
236 return !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.
255 * RETURNS
256 * Success: TRUE
257 * Failure: FALSE
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 );
267 if (status)
269 SetLastError( RtlNtStatusToDosError(status) );
270 return FALSE;
272 if (exitcode) *exitcode = info.ExitStatus;
273 return TRUE;
277 /***********************************************************************
278 * SetThreadContext [KERNEL32.@] Sets context of thread.
280 * RETURNS
281 * Success: TRUE
282 * Failure: FALSE
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) );
289 return !status;
293 /***********************************************************************
294 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
296 * RETURNS
297 * Success: TRUE
298 * Failure: FALSE
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) );
305 return !status;
309 /**********************************************************************
310 * SuspendThread [KERNEL32.@] Suspends a thread.
312 * RETURNS
313 * Success: Previous suspend count
314 * Failure: 0xFFFFFFFF
316 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
318 DWORD ret;
319 NTSTATUS status = NtSuspendThread( hthread, &ret );
321 if (status)
323 ret = ~0U;
324 SetLastError( RtlNtStatusToDosError(status) );
326 return ret;
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.
336 * RETURNS
337 * Success: Previous suspend count
338 * Failure: 0xFFFFFFFF
339 * Already running: 0
341 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
343 DWORD ret;
344 NTSTATUS status = NtResumeThread( hthread, &ret );
346 if (status)
348 ret = ~0U;
349 SetLastError( RtlNtStatusToDosError(status) );
351 return ret;
355 /**********************************************************************
356 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
358 * RETURNS
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 );
369 if (status)
371 SetLastError( RtlNtStatusToDosError(status) );
372 return THREAD_PRIORITY_ERROR_RETURN;
374 return info.Priority;
378 /**********************************************************************
379 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
381 * RETURNS
382 * Success: TRUE
383 * Failure: FALSE
385 BOOL WINAPI SetThreadPriority(
386 HANDLE hthread, /* [in] Handle to thread */
387 INT priority) /* [in] Thread priority level */
389 DWORD prio = priority;
390 NTSTATUS status;
392 status = NtSetInformationThread(hthread, ThreadBasePriority,
393 &prio, sizeof(prio));
395 if (status)
397 SetLastError( RtlNtStatusToDosError(status) );
398 return FALSE;
401 return TRUE;
405 /**********************************************************************
406 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
408 * Always reports that priority boost is disabled.
410 * RETURNS
411 * Success: TRUE.
412 * Failure: FALSE
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;
419 return NO_ERROR;
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
429 * RETURNS
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);
437 return FALSE;
441 /**********************************************************************
442 * SetThreadAffinityMask (KERNEL32.@)
444 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
446 NTSTATUS status;
447 THREAD_BASIC_INFORMATION tbi;
449 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
450 &tbi, sizeof(tbi), NULL );
451 if (status)
453 SetLastError( RtlNtStatusToDosError(status) );
454 return 0;
456 status = NtSetInformationThread( hThread, ThreadAffinityMask,
457 &dwThreadAffinityMask,
458 sizeof(dwThreadAffinityMask));
459 if (status)
461 SetLastError( RtlNtStatusToDosError(status) );
462 return 0;
464 return tbi.AffinityMask;
468 /**********************************************************************
469 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
471 * RETURNS
472 * Success: Value of last call to SetThreadIdealProcessor
473 * Failure: -1
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);
481 return -1L;
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;
489 func( arg2 );
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) );
500 return !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);
509 return FALSE;
512 /**********************************************************************
513 * GetThreadTimes [KERNEL32.@] Obtains timing information.
515 * RETURNS
516 * Success: TRUE
517 * Failure: FALSE
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;
527 NTSTATUS status;
529 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
530 sizeof(kusrt), NULL);
531 if (status)
533 SetLastError( RtlNtStatusToDosError(status) );
534 return FALSE;
536 if (creationtime)
538 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
539 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
541 if (exittime)
543 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
544 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
546 if (kerneltime)
548 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
549 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
551 if (usertime)
553 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
554 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
557 return TRUE;
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
582 * RETURNS
583 * Pseudohandle for the current thread
585 #undef GetCurrentThread
586 HANDLE WINAPI GetCurrentThread(void)
588 return (HANDLE)0xfffffffe;
592 #ifdef __i386__
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"
601 ".byte 0x64\n\t"
602 "movl %eax,0x34\n\t"
603 "ret $4" )
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" )
626 #else /* __i386__ */
628 /**********************************************************************
629 * SetLastError (KERNEL.147)
630 * SetLastError (KERNEL32.@)
632 * Sets the last-error code.
634 * RETURNS
635 * Nothing.
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.
648 * RETURNS
649 * 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.
662 * RETURNS
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.
676 * RETURNS
677 * current thread identifier
679 DWORD WINAPI GetCurrentThreadId(void)
681 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
684 #endif /* __i386__ */