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 BOOL last;
156 SERVER_START_REQ( terminate_thread )
157 {
158 /* send the exit code to the server */
159 req->handle = GetCurrentThread();
160 req->exit_code = code;
161 wine_server_call( req );
162 last = reply->last;
163 }
164 SERVER_END_REQ;
165
166 if (last)
167 {
168 LdrShutdownProcess();
169 exit( code );
170 }
171 else
172 {
173 RtlFreeThreadActivationContextStack();
174 RtlExitUserThread( code );
175 }
176 }
177
178
179 /**********************************************************************
180 * TerminateThread [KERNEL32.@] Terminates a thread
181 *
182 * RETURNS
183 * Success: TRUE
184 * Failure: FALSE
185 */
186 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
187 DWORD exit_code) /* [in] Exit code for thread */
188 {
189 NTSTATUS status = NtTerminateThread( handle, exit_code );
190 if (status) SetLastError( RtlNtStatusToDosError(status) );
191 return !status;
192 }
193
194
195 /***********************************************************************
196 * FreeLibraryAndExitThread (KERNEL32.@)
197 */
198 void WINAPI FreeLibraryAndExitThread(HINSTANCE hLibModule, DWORD dwExitCode)
199 {
200 FreeLibrary(hLibModule);
201 ExitThread(dwExitCode);
202 }
203
204
205 /**********************************************************************
206 * GetExitCodeThread (KERNEL32.@)
207 *
208 * Gets termination status of thread.
209 *
210 * RETURNS
211 * Success: TRUE
212 * Failure: FALSE
213 */
214 BOOL WINAPI GetExitCodeThread(
215 HANDLE hthread, /* [in] Handle to thread */
216 LPDWORD exitcode) /* [out] Address to receive termination status */
217 {
218 THREAD_BASIC_INFORMATION info;
219 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
220 &info, sizeof(info), NULL );
221
222 if (status)
223 {
224 SetLastError( RtlNtStatusToDosError(status) );
225 return FALSE;
226 }
227 if (exitcode) *exitcode = info.ExitStatus;
228 return TRUE;
229 }
230
231
232 /***********************************************************************
233 * SetThreadContext [KERNEL32.@] Sets context of thread.
234 *
235 * RETURNS
236 * Success: TRUE
237 * Failure: FALSE
238 */
239 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
240 const CONTEXT *context ) /* [in] Address of context structure */
241 {
242 NTSTATUS status = NtSetContextThread( handle, context );
243 if (status) SetLastError( RtlNtStatusToDosError(status) );
244 return !status;
245 }
246
247
248 /***********************************************************************
249 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
250 *
251 * RETURNS
252 * Success: TRUE
253 * Failure: FALSE
254 */
255 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
256 CONTEXT *context ) /* [out] Address of context structure */
257 {
258 NTSTATUS status = NtGetContextThread( handle, context );
259 if (status) SetLastError( RtlNtStatusToDosError(status) );
260 return !status;
261 }
262
263
264 /**********************************************************************
265 * SuspendThread [KERNEL32.@] Suspends a thread.
266 *
267 * RETURNS
268 * Success: Previous suspend count
269 * Failure: 0xFFFFFFFF
270 */
271 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
272 {
273 DWORD ret;
274 NTSTATUS status = NtSuspendThread( hthread, &ret );
275
276 if (status)
277 {
278 ret = ~0U;
279 SetLastError( RtlNtStatusToDosError(status) );
280 }
281 return ret;
282 }
283
284
285 /**********************************************************************
286 * ResumeThread [KERNEL32.@] Resumes a thread.
287 *
288 * Decrements a thread's suspend count. When count is zero, the
289 * execution of the thread is resumed.
290 *
291 * RETURNS
292 * Success: Previous suspend count
293 * Failure: 0xFFFFFFFF
294 * Already running: 0
295 */
296 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
297 {
298 DWORD ret;
299 NTSTATUS status = NtResumeThread( hthread, &ret );
300
301 if (status)
302 {
303 ret = ~0U;
304 SetLastError( RtlNtStatusToDosError(status) );
305 }
306 return ret;
307 }
308
309
310 /**********************************************************************
311 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
312 *
313 * RETURNS
314 * Success: Thread's priority level.
315 * Failure: THREAD_PRIORITY_ERROR_RETURN
316 */
317 INT WINAPI GetThreadPriority(
318 HANDLE hthread) /* [in] Handle to thread */
319 {
320 THREAD_BASIC_INFORMATION info;
321 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
322 &info, sizeof(info), NULL );
323
324 if (status)
325 {
326 SetLastError( RtlNtStatusToDosError(status) );
327 return THREAD_PRIORITY_ERROR_RETURN;
328 }
329 return info.Priority;
330 }
331
332
333 /**********************************************************************
334 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
335 *
336 * RETURNS
337 * Success: TRUE
338 * Failure: FALSE
339 */
340 BOOL WINAPI SetThreadPriority(
341 HANDLE hthread, /* [in] Handle to thread */
342 INT priority) /* [in] Thread priority level */
343 {
344 DWORD prio = priority;
345 NTSTATUS status;
346
347 status = NtSetInformationThread(hthread, ThreadBasePriority,
348 &prio, sizeof(prio));
349
350 if (status)
351 {
352 SetLastError( RtlNtStatusToDosError(status) );
353 return FALSE;
354 }
355
356 return TRUE;
357 }
358
359
360 /**********************************************************************
361 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
362 *
363 * Always reports that priority boost is disabled.
364 *
365 * RETURNS
366 * Success: TRUE.
367 * Failure: FALSE
368 */
369 BOOL WINAPI GetThreadPriorityBoost(
370 HANDLE hthread, /* [in] Handle to thread */
371 PBOOL pstate) /* [out] pointer to var that receives the boost state */
372 {
373 if (pstate) *pstate = FALSE;
374 return NO_ERROR;
375 }
376
377
378 /**********************************************************************
379 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
380 *
381 * Priority boost is not implemented. This function always returns
382 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
383 *
384 * RETURNS
385 * Always returns FALSE to indicate a failure
386 */
387 BOOL WINAPI SetThreadPriorityBoost(
388 HANDLE hthread, /* [in] Handle to thread */
389 BOOL disable) /* [in] TRUE to disable priority boost */
390 {
391 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
392 return FALSE;
393 }
394
395
396 /**********************************************************************
397 * SetThreadAffinityMask (KERNEL32.@)
398 */
399 DWORD_PTR WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD_PTR dwThreadAffinityMask )
400 {
401 NTSTATUS status;
402 THREAD_BASIC_INFORMATION tbi;
403
404 status = NtQueryInformationThread( hThread, ThreadBasicInformation,
405 &tbi, sizeof(tbi), NULL );
406 if (status)
407 {
408 SetLastError( RtlNtStatusToDosError(status) );
409 return 0;
410 }
411 status = NtSetInformationThread( hThread, ThreadAffinityMask,
412 &dwThreadAffinityMask,
413 sizeof(dwThreadAffinityMask));
414 if (status)
415 {
416 SetLastError( RtlNtStatusToDosError(status) );
417 return 0;
418 }
419 return tbi.AffinityMask;
420 }
421
422
423 /**********************************************************************
424 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
425 *
426 * RETURNS
427 * Success: Value of last call to SetThreadIdealProcessor
428 * Failure: -1
429 */
430 DWORD WINAPI SetThreadIdealProcessor(
431 HANDLE hThread, /* [in] Specifies the thread of interest */
432 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
433 {
434 FIXME("(%p): stub\n",hThread);
435 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
436 return -1L;
437 }
438
439
440 /* callback for QueueUserAPC */
441 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
442 {
443 PAPCFUNC func = (PAPCFUNC)arg1;
444 func( arg2 );
445 }
446
447 /***********************************************************************
448 * QueueUserAPC (KERNEL32.@)
449 */
450 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
451 {
452 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
453
454 if (status) SetLastError( RtlNtStatusToDosError(status) );
455 return !status;
456 }
457
458 /***********************************************************************
459 * QueueUserWorkItem (KERNEL32.@)
460 */
461 BOOL WINAPI QueueUserWorkItem( LPTHREAD_START_ROUTINE Function, PVOID Context, ULONG Flags )
462 {
463 NTSTATUS status;
464
465 TRACE("(%p,%p,0x%08x)\n", Function, Context, Flags);
466
467 status = RtlQueueWorkItem( Function, Context, Flags );
468
469 if (status) SetLastError( RtlNtStatusToDosError(status) );
470 return !status;
471 }
472
473 /**********************************************************************
474 * GetThreadTimes [KERNEL32.@] Obtains timing information.
475 *
476 * RETURNS
477 * Success: TRUE
478 * Failure: FALSE
479 */
480 BOOL WINAPI GetThreadTimes(
481 HANDLE thread, /* [in] Specifies the thread of interest */
482 LPFILETIME creationtime, /* [out] When the thread was created */
483 LPFILETIME exittime, /* [out] When the thread was destroyed */
484 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
485 LPFILETIME usertime) /* [out] Time thread spent in user mode */
486 {
487 KERNEL_USER_TIMES kusrt;
488 NTSTATUS status;
489
490 status = NtQueryInformationThread(thread, ThreadTimes, &kusrt,
491 sizeof(kusrt), NULL);
492 if (status)
493 {
494 SetLastError( RtlNtStatusToDosError(status) );
495 return FALSE;
496 }
497 if (creationtime)
498 {
499 creationtime->dwLowDateTime = kusrt.CreateTime.u.LowPart;
500 creationtime->dwHighDateTime = kusrt.CreateTime.u.HighPart;
501 }
502 if (exittime)
503 {
504 exittime->dwLowDateTime = kusrt.ExitTime.u.LowPart;
505 exittime->dwHighDateTime = kusrt.ExitTime.u.HighPart;
506 }
507 if (kerneltime)
508 {
509 kerneltime->dwLowDateTime = kusrt.KernelTime.u.LowPart;
510 kerneltime->dwHighDateTime = kusrt.KernelTime.u.HighPart;
511 }
512 if (usertime)
513 {
514 usertime->dwLowDateTime = kusrt.UserTime.u.LowPart;
515 usertime->dwHighDateTime = kusrt.UserTime.u.HighPart;
516 }
517
518 return TRUE;
519 }
520
521 /**********************************************************************
522 * GetThreadId [KERNEL32.@]
523 *
524 * Retrieve the identifier of a thread.
525 *
526 * PARAMS
527 * Thread [I] The thread to retrieve the identifier of.
528 *
529 * RETURNS
530 * Success: Identifier of the target thread.
531 * Failure: 0
532 */
533 DWORD WINAPI GetThreadId(HANDLE Thread)
534 {
535 THREAD_BASIC_INFORMATION tbi;
536 NTSTATUS status;
537
538 TRACE("(%p)\n", Thread);
539
540 status = NtQueryInformationThread(Thread, ThreadBasicInformation, &tbi,
541 sizeof(tbi), NULL);
542 if (status)
543 {
544 SetLastError( RtlNtStatusToDosError(status) );
545 return 0;
546 }
547
548 return HandleToULong(tbi.ClientId.UniqueThread);
549 }
550
551
552 /**********************************************************************
553 * VWin32_BoostThreadGroup [KERNEL.535]
554 */
555 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
556 {
557 FIXME("(0x%08x,%d): stub\n", threadId, boost);
558 }
559
560
561 /**********************************************************************
562 * VWin32_BoostThreadStatic [KERNEL.536]
563 */
564 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
565 {
566 FIXME("(0x%08x,%d): stub\n", threadId, boost);
567 }
568
569
570 /***********************************************************************
571 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
572 *
573 * RETURNS
574 * Pseudohandle for the current thread
575 */
576 #undef GetCurrentThread
577 HANDLE WINAPI GetCurrentThread(void)
578 {
579 return (HANDLE)0xfffffffe;
580 }
581
582
583 #ifdef __i386__
584
585 /***********************************************************************
586 * SetLastError (KERNEL.147)
587 * SetLastError (KERNEL32.@)
588 */
589 /* void WINAPI SetLastError( DWORD error ); */
590 __ASM_GLOBAL_FUNC( SetLastError,
591 "movl 4(%esp),%eax\n\t"
592 ".byte 0x64\n\t"
593 "movl %eax,0x34\n\t"
594 "ret $4" )
595
596 /***********************************************************************
597 * GetLastError (KERNEL.148)
598 * GetLastError (KERNEL32.@)
599 */
600 /* DWORD WINAPI GetLastError(void); */
601 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x34,%eax\n\tret" )
602
603 /***********************************************************************
604 * GetCurrentProcessId (KERNEL.471)
605 * GetCurrentProcessId (KERNEL32.@)
606 */
607 /* DWORD WINAPI GetCurrentProcessId(void) */
608 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" )
609
610 /***********************************************************************
611 * GetCurrentThreadId (KERNEL.462)
612 * GetCurrentThreadId (KERNEL32.@)
613 */
614 /* DWORD WINAPI GetCurrentThreadId(void) */
615 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" )
616
617 #else /* __i386__ */
618
619 /**********************************************************************
620 * SetLastError (KERNEL.147)
621 * SetLastError (KERNEL32.@)
622 *
623 * Sets the last-error code.
624 *
625 * RETURNS
626 * Nothing.
627 */
628 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
629 {
630 NtCurrentTeb()->LastErrorValue = error;
631 }
632
633 /**********************************************************************
634 * GetLastError (KERNEL.148)
635 * GetLastError (KERNEL32.@)
636 *
637 * Get the last-error code.
638 *
639 * RETURNS
640 * last-error code.
641 */
642 DWORD WINAPI GetLastError(void)
643 {
644 return NtCurrentTeb()->LastErrorValue;
645 }
646
647 /***********************************************************************
648 * GetCurrentProcessId (KERNEL.471)
649 * GetCurrentProcessId (KERNEL32.@)
650 *
651 * Get the current process identifier.
652 *
653 * RETURNS
654 * current process identifier
655 */
656 DWORD WINAPI GetCurrentProcessId(void)
657 {
658 return HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess);
659 }
660
661 /***********************************************************************
662 * GetCurrentThreadId (KERNEL.462)
663 * GetCurrentThreadId (KERNEL32.@)
664 *
665 * Get the current thread identifier.
666 *
667 * RETURNS
668 * current thread identifier
669 */
670 DWORD WINAPI GetCurrentThreadId(void)
671 {
672 return HandleToULong(NtCurrentTeb()->ClientId.UniqueThread);
673 }
674
675 #endif /* __i386__ */
676
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.