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