1 /*
2 * Win32 threads
3 *
4 * Copyright 1996 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
31
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winternl.h"
38 #include "wine/winbase16.h"
39 #include "wine/exception.h"
40 #include "wine/library.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
43
44 #include "kernel_private.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(thread);
47
48
49 /***********************************************************************
50 * CreateThread (KERNEL32.@)
51 */
52 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
53 LPTHREAD_START_ROUTINE start, LPVOID param,
54 DWORD flags, LPDWORD id )
55 {
56 return CreateRemoteThread( GetCurrentProcess(),
57 sa, stack, start, param, flags, id );
58 }
59
60
61 /***************************************************************************
62 * CreateRemoteThread (KERNEL32.@)
63 *
64 * Creates a thread that runs in the address space of another process
65 *
66 * PARAMS
67 *
68 * RETURNS
69 * Success: Handle to the new thread.
70 * Failure: NULL. Use GetLastError() to find the error cause.
71 *
72 * BUGS
73 * Improper memory allocation: there's no ability to free new_thread_info
74 * in other process.
75 * Bad start address for RtlCreateUserThread because the library
76 * may be loaded at different address in other process.
77 */
78 HANDLE WINAPI CreateRemoteThread( HANDLE hProcess, SECURITY_ATTRIBUTES *sa, SIZE_T stack,
79 LPTHREAD_START_ROUTINE start, LPVOID param,
80 DWORD flags, LPDWORD id )
81 {
82 HANDLE handle;
83 CLIENT_ID client_id;
84 NTSTATUS status;
85 SIZE_T stack_reserve = 0, stack_commit = 0;
86
87 if (flags & STACK_SIZE_PARAM_IS_A_RESERVATION) stack_reserve = stack;
88 else stack_commit = stack;
89
90 status = RtlCreateUserThread( hProcess, NULL, TRUE,
91 NULL, stack_reserve, stack_commit,
92 (PRTL_THREAD_START_ROUTINE)start, param, &handle, &client_id );
93 if (status == STATUS_SUCCESS)
94 {
95 if (id) *id = HandleToULong(client_id.UniqueThread);
96 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
97 SetHandleInformation( handle, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
98 if (!(flags & CREATE_SUSPENDED))
99 {
100 ULONG ret;
101 if (NtResumeThread( handle, &ret ))
102 {
103 NtClose( handle );
104 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
105 handle = 0;
106 }
107 }
108 }
109 else
110 {
111 SetLastError( RtlNtStatusToDosError(status) );
112 handle = 0;
113 }
114 return handle;
115 }
116
117
118 /***********************************************************************
119 * OpenThread [KERNEL32.@] Retrieves a handle to a thread from its thread id
120 */
121 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
122 {
123 NTSTATUS status;
124 HANDLE handle;
125 OBJECT_ATTRIBUTES attr;
126 CLIENT_ID cid;
127
128 attr.Length = sizeof(attr);
129 attr.RootDirectory = 0;
130 attr.Attributes = bInheritHandle ? OBJ_INHERIT : 0;
131 attr.ObjectName = NULL;
132 attr.SecurityDescriptor = NULL;
133 attr.SecurityQualityOfService = NULL;
134
135 cid.UniqueProcess = 0; /* FIXME */
136 cid.UniqueThread = ULongToHandle(dwThreadId);
137 status = NtOpenThread( &handle, dwDesiredAccess, &attr, &cid );
138 if (status)
139 {
140 SetLastError( RtlNtStatusToDosError(status) );
141 handle = 0;
142 }
143 return handle;
144 }
145
146
147 /***********************************************************************
148 * ExitThread [KERNEL32.@] Ends a thread
149 *
150 * RETURNS
151 * None
152 */
153 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
154 {
155 RtlFreeThreadActivationContextStack();
156 RtlExitUserThread( code );
157 }
158
159
160 /**********************************************************************
161 * TerminateThread [KERNEL32.@] Terminates a thread
162 *
163 * RETURNS
164 * Success: TRUE
165 * Failure: FALSE
166 */
167 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
168 DWORD exit_code) /* [in] Exit code for thread */
169 {
170 NTSTATUS status = NtTerminateThread( handle, exit_code );
171 if (status) SetLastError( RtlNtStatusToDosError(status) );
172 return !status;
173 }
174
175
176 /***********************************************************************
177 * FreeLibraryAndExitThread (KERNEL32.@)
178 */
179 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
180 {
181 FreeLibrary(hLibModule);
182 ExitThread(dwExitCode);
183 }
184
185
186 /**********************************************************************
187 * GetExitCodeThread (KERNEL32.@)
188 *
189 * Gets termination status of thread.
190 *
191 * RETURNS
192 * Success: TRUE
193 * Failure: FALSE
194 */
195 BOOL WINAPI GetExitCodeThread(
196 HANDLE hthread, /* [in] Handle to thread */
197 LPDWORD exitcode) /* [out] Address to receive termination status */
198 {
199 THREAD_BASIC_INFORMATION info;
200 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
201 &info, sizeof(info), NULL );
202
203 if (status)
204 {
205 SetLastError( RtlNtStatusToDosError(status) );
206 return FALSE;
207 }
208 if (exitcode) *exitcode = info.ExitStatus;
209 return TRUE;
210 }
211
212
213 /***********************************************************************
214 * SetThreadContext [KERNEL32.@] Sets context of thread.
215 *
216 * RETURNS
217 * Success: TRUE
218 * Failure: FALSE
219 */
220 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
221 const CONTEXT *context ) /* [in] Address of context structure */
222 {
223 NTSTATUS status = NtSetContextThread( handle, context );
224 if (status) SetLastError( RtlNtStatusToDosError(status) );
225 return !status;
226 }
227
228
229 /***********************************************************************
230 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
231 *
232 * RETURNS
233 * Success: TRUE
234 * Failure: FALSE
235 */
236 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
237 CONTEXT *context ) /* [out] Address of context structure */
238 {
239 NTSTATUS status = NtGetContextThread( handle, context );
240 if (status) SetLastError( RtlNtStatusToDosError(status) );
241 return !status;
242 }
243
244
245 /**********************************************************************
246 * SuspendThread [KERNEL32.@] Suspends a thread.
247 *
248 * RETURNS
249 * Success: Previous suspend count
250 * Failure: 0xFFFFFFFF
251 */
252 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
253 {
254 DWORD ret;
255 NTSTATUS status = NtSuspendThread( hthread, &ret );
256
257 if (status)
258 {
259 ret = ~0U;
260 SetLastError( RtlNtStatusToDosError(status) );
261 }
262 return ret;
263 }
264
265
266 /**********************************************************************
267 * ResumeThread [KERNEL32.@] Resumes a thread.
268 *
269 * Decrements a thread's suspend count. When count is zero, the
270 * execution of the thread is resumed.
271 *
272 * RETURNS
273 * Success: Previous suspend count
274 * Failure: 0xFFFFFFFF
275 * Already running: 0
276 */
277 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
278 {
279 DWORD ret;
280 NTSTATUS status = NtResumeThread( hthread, &ret );
281
282 if (status)
283 {
284 ret = ~0U;
285 SetLastError( RtlNtStatusToDosError(status) );
286 }
287 return ret;
288 }
289
290
291 /**********************************************************************
292 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
293 *
294 * RETURNS
295 * Success: Thread's priority level.
296 * Failure: THREAD_PRIORITY_ERROR_RETURN
297 */
298 INT WINAPI GetThreadPriority(
299 HANDLE hthread) /* [in] Handle to thread */
300 {
301 THREAD_BASIC_INFORMATION info;
302 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
303 &info, sizeof(info), NULL );
304
305 if (status)
306 {
307 SetLastError( RtlNtStatusToDosError(status) );
308 return THREAD_PRIORITY_ERROR_RETURN;
309 }
310 return info.Priority;
311 }
312
313
314 /**********************************************************************
315 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
316 *
317 * RETURNS
318 * Success: TRUE
319 * Failure: FALSE
320 */
321 BOOL WINAPI SetThreadPriority(
322 HANDLE hthread, /* [in] Handle to thread */
323 INT priority) /* [in] Thread priority level */
324 {
325 DWORD prio = priority;
326 NTSTATUS status;
327
328 status = NtSetInformationThread(hthread, ThreadBasePriority,
329 &prio, sizeof(prio));
330
331 if (status)
332 {
333 SetLastError( RtlNtStatusToDosError(status) );
334 return FALSE;
335 }
336
337 return TRUE;
338 }
339
340
341 /**********************************************************************
342 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
343 *
344 * Always reports that priority boost is disabled.
345 *
346 * RETURNS
347 * Success: TRUE.
348 * Failure: FALSE
349 */
350 BOOL WINAPI GetThreadPriorityBoost(
351 HANDLE hthread, /* [in] Handle to thread */
352 PBOOL pstate) /* [out] pointer to var that receives the boost state */
353 {
354 if (pstate) *pstate = FALSE;
355 return NO_ERROR;
356 }
357
358
359 /**********************************************************************
360 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
361 *
362 * Priority boost is not implemented. This function always returns
363 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
364 *
365 * RETURNS
366 * Always returns FALSE to indicate a failure
367 */
368 BOOL WINAPI SetThreadPriorityBoost(
369 HANDLE hthread, /* [in] Handle to thread */
370 BOOL disable) /* [in] TRUE to disable priority boost */
371 {
372 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
373 return FALSE;
374 }
375
376
377 /**********************************************************************
378 * SetThreadAffinityMask (KERNEL32.@)
379 */
380 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
381 {
382 NTSTATUS status;
383 THREAD_BASIC_INFORMATION tbi;
384
385 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
386 &tbi, sizeof(tbi), NULL );
387 if (status)
388 {
389 SetLastError( RtlNtStatusToDosError(status) );
390 return 0;
391 }
392 status = NtSetInformationThread( hThread, ThreadAffinityMask,
393 &dwThreadAffinityMask,
394 sizeof(dwThreadAffinityMask));
395 if (status)
396 {
397 SetLastError( RtlNtStatusToDosError(status) );
398 return 0;
399 }
400 return tbi.AffinityMask;
401 }
402
403
404 /**********************************************************************
405 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
406 *
407 * RETURNS
408 * Success: Value of last call to SetThreadIdealProcessor
409 * Failure: -1
410 */
411 DWORD WINAPI SetThreadIdealProcessor(
412 HANDLE hThread, /* [in] Specifies the thread of interest */
413 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
414 {
415 FIXME("(%p): stub\n",hThread);
416 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
417 return -1L;
418 }
419
420
421 /* callback for QueueUserAPC */
422 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
423 {
424 PAPCFUNC func = (PAPCFUNC)arg1;
425 func( arg2 );
426 }
427
428 /***********************************************************************
429 * QueueUserAPC (KERNEL32.@)
430 */
431 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
432 {
433 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
434
435 if (status) SetLastError( RtlNtStatusToDosError(status) );
436 return !status;
437 }
438
439 /***********************************************************************
440 * QueueUserWorkItem (KERNEL32.@)
441 */
442 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
443 {
444 NTSTATUS status;
445
446 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
447
448 status = RtlQueueWorkItem( Function, Context, Flags );
449
450 if (status) SetLastError( RtlNtStatusToDosError(status) );
451 return !status;
452 }
453
454 /**********************************************************************
455 * GetThreadTimes [KERNEL32.@] Obtains timing information.
456 *
457 * RETURNS
458 * Success: TRUE
459 * Failure: FALSE
460 */
461 BOOL WINAPI GetThreadTimes(
462 HANDLE thread, /* [in] Specifies the thread of interest */
463 LPFILETIME creationtime, /* [out] When the thread was created */
464 LPFILETIME exittime, /* [out] When the thread was destroyed */
465 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
466 LPFILETIME usertime) /* [out] Time thread spent in user mode */
467 {
468 KERNEL_USER_TIMES kusrt;
469 NTSTATUS status;
470
471 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
472 sizeof(kusrt), NULL);
473 if (status)
474 {
475 SetLastError( RtlNtStatusToDosError(status) );
476 return FALSE;
477 }
478 if (creationtime)
479 {
480 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
481 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
482 }
483 if (exittime)
484 {
485 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
486 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
487 }
488 if (kerneltime)
489 {
490 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
491 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
492 }
493 if (usertime)
494 {
495 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
496 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
497 }
498
499 return TRUE;
500 }
501
502 /**********************************************************************
503 * GetThreadId [KERNEL32.@]
504 *
505 * Retrieve the identifier of a thread.
506 *
507 * PARAMS
508 * Thread [I] The thread to retrieve the identifier of.
509 *
510 * RETURNS
511 * Success: Identifier of the target thread.
512 * Failure: 0
513 */
514 DWORD WINAPI GetThreadId(HANDLE Thread)
515 {
516 THREAD_BASIC_INFORMATION tbi;
517 NTSTATUS status;
518
519 TRACE("(%p)\n", Thread);
520
521 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
522 sizeof(tbi), NULL);
523 if (status)
524 {
525 SetLastError( RtlNtStatusToDosError(status) );
526 return 0;
527 }
528
529 return HandleToULong(tbi.ClientId.UniqueThread);
530 }
531
532
533 /**********************************************************************
534 * VWin32_BoostThreadGroup [KERNEL.535]
535 */
536 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
537 {
538 FIXME("(0x%08x,%d): stub\n", threadId, boost);
539 }
540
541
542 /**********************************************************************
543 * VWin32_BoostThreadStatic [KERNEL.536]
544 */
545 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
546 {
547 FIXME("(0x%08x,%d): stub\n", threadId, boost);
548 }
549
550
551 /***********************************************************************
552 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
553 *
554 * RETURNS
555 * Pseudohandle for the current thread
556 */
557 #undef GetCurrentThread
558 HANDLE WINAPI GetCurrentThread(void)
559 {
560 return (HANDLE)0xfffffffe;
561 }
562
563
564 #ifdef __i386__
565
566 /***********************************************************************
567 * SetLastError (KERNEL.147)
568 * SetLastError (KERNEL32.@)
569 */
570 /* void WINAPI SetLastError( DWORD error ); */
571 __ASM_GLOBAL_FUNC( SetLastError,
572 "movl 4(%esp),%eax\n\t"
573 ".byte 0x64\n\t"
574 "movl %eax,0x34\n\t"
575 "ret $4" )
576
577 /***********************************************************************
578 * GetLastError (KERNEL.148)
579 * GetLastError (KERNEL32.@)
580 */
581 /* DWORD WINAPI GetLastError(void); */
582 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
583
584 /***********************************************************************
585 * GetCurrentProcessId (KERNEL.471)
586 * GetCurrentProcessId (KERNEL32.@)
587 */
588 /* DWORD WINAPI GetCurrentProcessId(void) */
589 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
590
591 /***********************************************************************
592 * GetCurrentThreadId (KERNEL.462)
593 * GetCurrentThreadId (KERNEL32.@)
594 */
595 /* DWORD WINAPI GetCurrentThreadId(void) */
596 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
597
598 #else /* __i386__ */
599
600 /**********************************************************************
601 * SetLastError (KERNEL.147)
602 * SetLastError (KERNEL32.@)
603 *
604 * Sets the last-error code.
605 *
606 * RETURNS
607 * Nothing.
608 */
609 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
610 {
611 NtCurrentTeb()->LastErrorValue = error;
612 }
613
614 /**********************************************************************
615 * GetLastError (KERNEL.148)
616 * GetLastError (KERNEL32.@)
617 *
618 * Get the last-error code.
619 *
620 * RETURNS
621 * last-error code.
622 */
623 DWORD WINAPI GetLastError(void)
624 {
625 return NtCurrentTeb()->LastErrorValue;
626 }
627
628 /***********************************************************************
629 * GetCurrentProcessId (KERNEL.471)
630 * GetCurrentProcessId (KERNEL32.@)
631 *
632 * Get the current process identifier.
633 *
634 * RETURNS
635 * current process identifier
636 */
637 DWORD WINAPI GetCurrentProcessId(void)
638 {
639 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
640 }
641
642 /***********************************************************************
643 * GetCurrentThreadId (KERNEL.462)
644 * GetCurrentThreadId (KERNEL32.@)
645 *
646 * Get the current thread identifier.
647 *
648 * RETURNS
649 * current thread identifier
650 */
651 DWORD WINAPI GetCurrentThreadId(void)
652 {
653 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
654 }
655
656 #endif /* __i386__ */
657
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.