ole32: Return error instead of asserting if storage file is corrupt.
[wine/testsucceed.git] / dlls / kernel / thread.c
blobc9270376991c09a0ca35bc175ce00136e13e88fa
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 DWORD ret;
447 SERVER_START_REQ( set_thread_info )
449 req->handle = hThread;
450 req->affinity = dwThreadAffinityMask;
451 req->mask = SET_THREAD_INFO_AFFINITY;
452 ret = !wine_server_call_err( req );
453 /* FIXME: should return previous value */
455 SERVER_END_REQ;
456 return ret;
460 /**********************************************************************
461 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
463 * RETURNS
464 * Success: Value of last call to SetThreadIdealProcessor
465 * Failure: -1
467 DWORD WINAPI SetThreadIdealProcessor(
468 HANDLE hThread, /* [in] Specifies the thread of interest */
469 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
471 FIXME("(%p): stub\n",hThread);
472 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
473 return -1L;
477 /* callback for QueueUserAPC */
478 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
480 PAPCFUNC func = (PAPCFUNC)arg1;
481 func( arg2 );
484 /***********************************************************************
485 * QueueUserAPC (KERNEL32.@)
487 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
489 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
491 if (status) SetLastError( RtlNtStatusToDosError(status) );
492 return !status;
495 /***********************************************************************
496 * QueueUserWorkItem (KERNEL32.@)
498 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
500 FIXME("(%p,%p,0x%08lx): stub\n", Function, Context, Flags);
501 return FALSE;
504 /**********************************************************************
505 * GetThreadTimes [KERNEL32.@] Obtains timing information.
507 * RETURNS
508 * Success: TRUE
509 * Failure: FALSE
511 BOOL WINAPI GetThreadTimes(
512 HANDLE thread, /* [in] Specifies the thread of interest */
513 LPFILETIME creationtime, /* [out] When the thread was created */
514 LPFILETIME exittime, /* [out] When the thread was destroyed */
515 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
516 LPFILETIME usertime) /* [out] Time thread spent in user mode */
518 KERNEL_USER_TIMES kusrt;
519 NTSTATUS status;
521 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
522 sizeof(kusrt), NULL);
523 if (status)
525 SetLastError( RtlNtStatusToDosError(status) );
526 return FALSE;
528 if (creationtime)
530 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
531 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
533 if (exittime)
535 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
536 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
538 if (kerneltime)
540 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
541 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
543 if (usertime)
545 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
546 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
549 return TRUE;
553 /**********************************************************************
554 * VWin32_BoostThreadGroup [KERNEL.535]
556 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
558 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
562 /**********************************************************************
563 * VWin32_BoostThreadStatic [KERNEL.536]
565 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
567 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
571 /***********************************************************************
572 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
574 * RETURNS
575 * Pseudohandle for the current thread
577 #undef GetCurrentThread
578 HANDLE WINAPI GetCurrentThread(void)
580 return (HANDLE)0xfffffffe;
584 #ifdef __i386__
586 /***********************************************************************
587 * SetLastError (KERNEL.147)
588 * SetLastError (KERNEL32.@)
590 /* void WINAPI SetLastError( DWORD error ); */
591 __ASM_GLOBAL_FUNC( SetLastError,
592 "movl 4(%esp),%eax\n\t"
593 ".byte 0x64\n\t"
594 "movl %eax,0x34\n\t"
595 "ret $4" )
597 /***********************************************************************
598 * GetLastError (KERNEL.148)
599 * GetLastError (KERNEL32.@)
601 /* DWORD WINAPI GetLastError(void); */
602 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
604 /***********************************************************************
605 * GetCurrentProcessId (KERNEL.471)
606 * GetCurrentProcessId (KERNEL32.@)
608 /* DWORD WINAPI GetCurrentProcessId(void) */
609 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
611 /***********************************************************************
612 * GetCurrentThreadId (KERNEL.462)
613 * GetCurrentThreadId (KERNEL32.@)
615 /* DWORD WINAPI GetCurrentThreadId(void) */
616 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
618 #else /* __i386__ */
620 /**********************************************************************
621 * SetLastError (KERNEL.147)
622 * SetLastError (KERNEL32.@)
624 * Sets the last-error code.
626 * RETURNS
627 * Nothing.
629 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
631 NtCurrentTeb()->LastErrorValue = error;
634 /**********************************************************************
635 * GetLastError (KERNEL.148)
636 * GetLastError (KERNEL32.@)
638 * Get the last-error code.
640 * RETURNS
641 * last-error code.
643 DWORD WINAPI GetLastError(void)
645 return NtCurrentTeb()->LastErrorValue;
648 /***********************************************************************
649 * GetCurrentProcessId (KERNEL.471)
650 * GetCurrentProcessId (KERNEL32.@)
652 * Get the current process identifier.
654 * RETURNS
655 * current process identifier
657 DWORD WINAPI GetCurrentProcessId(void)
659 return (DWORD)NtCurrentTeb()->ClientId.UniqueProcess;
662 /***********************************************************************
663 * GetCurrentThreadId (KERNEL.462)
664 * GetCurrentThreadId (KERNEL32.@)
666 * Get the current thread identifier.
668 * RETURNS
669 * current thread identifier
671 DWORD WINAPI GetCurrentThreadId(void)
673 return (DWORD)NtCurrentTeb()->ClientId.UniqueThread;
676 #endif /* __i386__ */