Implemented ACS_CENTER for animation controls.
[wine/testsucceed.git] / scheduler / thread.c
blob50ccc3cf7df435f2fbac59924bcf3f6451f5a34c
1 /*
2 * Win32 threads
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #ifdef HAVE_SYS_MMAN_H
13 #include <sys/mman.h>
14 #endif
15 #include <unistd.h>
16 #include "wine/winbase16.h"
17 #include "thread.h"
18 #include "process.h"
19 #include "task.h"
20 #include "module.h"
21 #include "global.h"
22 #include "winerror.h"
23 #include "heap.h"
24 #include "selectors.h"
25 #include "winnt.h"
26 #include "server.h"
27 #include "services.h"
28 #include "stackframe.h"
29 #include "builtin16.h"
30 #include "debugtools.h"
31 #include "winnls.h"
33 DEFAULT_DEBUG_CHANNEL(thread);
35 /* TEB of the initial thread */
36 static TEB initial_teb;
38 /***********************************************************************
39 * THREAD_IsWin16
41 BOOL THREAD_IsWin16( TEB *teb )
43 return !teb || !(teb->tibflags & TEBF_WIN32);
46 /***********************************************************************
47 * THREAD_IdToTEB
49 * Convert a thread id to a TEB, making sure it is valid.
51 TEB *THREAD_IdToTEB( DWORD id )
53 struct get_thread_info_request *req = get_req_buffer();
55 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
56 req->handle = -1;
57 req->tid_in = (void *)id;
58 if (!server_call_noerr( REQ_GET_THREAD_INFO )) return req->teb;
60 /* Allow task handles to be used; convert to main thread */
61 if ( IsTask16( id ) )
63 TDB *pTask = (TDB *)GlobalLock16( id );
64 if (pTask) return pTask->teb;
66 SetLastError( ERROR_INVALID_PARAMETER );
67 return NULL;
71 /***********************************************************************
72 * THREAD_InitTEB
74 * Initialization of a newly created TEB.
76 static BOOL THREAD_InitTEB( TEB *teb )
78 teb->except = (void *)~0UL;
79 teb->self = teb;
80 teb->tibflags = TEBF_WIN32;
81 teb->tls_ptr = teb->tls_array;
82 teb->exit_code = STILL_ACTIVE;
83 teb->socket = -1;
84 teb->stack_top = (void *)~0UL;
85 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
86 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
87 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
88 return (teb->teb_sel != 0);
92 /***********************************************************************
93 * THREAD_FreeTEB
95 * Free data structures associated with a thread.
96 * Must be called from the context of another thread.
98 static void CALLBACK THREAD_FreeTEB( TEB *teb )
100 TRACE("(%p) called\n", teb );
101 if (teb->cleanup) SERVICE_Delete( teb->cleanup );
103 /* Free the associated memory */
105 if (teb->socket != -1) close( teb->socket );
106 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
107 SELECTOR_FreeBlock( teb->teb_sel, 1 );
108 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
109 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
110 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
114 /***********************************************************************
115 * THREAD_InitStack
117 * Allocate the stack of a thread.
119 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size, BOOL alloc_stack16 )
121 DWORD old_prot, total_size;
122 DWORD page_size = VIRTUAL_GetPageSize();
123 void *base;
125 /* Allocate the stack */
127 if (stack_size >= 16*1024*1024)
128 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
130 /* if size is smaller than default, get stack size from parent */
131 if (stack_size < 1024 * 1024)
133 if (teb)
134 stack_size = 1024 * 1024; /* no parent */
135 else
136 stack_size = ((char *)NtCurrentTeb()->stack_top - (char *)NtCurrentTeb()->stack_base
137 - SIGNAL_STACK_SIZE - 3 * page_size);
140 /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
141 stack_size += 64 * 1024;
143 /* Memory layout in allocated block:
145 * size contents
146 * 1 page NOACCESS guard page
147 * SIGNAL_STACK_SIZE signal stack
148 * 1 page NOACCESS guard page
149 * 1 page PAGE_GUARD guard page
150 * stack_size normal stack
151 * 64Kb 16-bit stack (optional)
152 * 1 page TEB (except for initial thread)
155 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
156 total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
157 if (alloc_stack16) total_size += 0x10000;
158 if (!teb) total_size += page_size;
160 if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
161 return NULL;
163 if (!teb)
165 teb = (TEB *)((char *)base + total_size - page_size);
166 if (!THREAD_InitTEB( teb ))
168 VirtualFree( base, 0, MEM_RELEASE );
169 return NULL;
173 teb->stack_low = base;
174 teb->stack_base = base;
175 teb->signal_stack = (char *)base + page_size;
176 teb->stack_top = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
178 /* Setup guard pages */
180 VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
181 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
182 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
183 PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
185 /* Allocate the 16-bit stack selector */
187 if (alloc_stack16)
189 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
190 FALSE, FALSE );
191 if (!teb->stack_sel) goto error;
192 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
193 0x10000 - sizeof(STACK16FRAME) );
195 return teb;
197 error:
198 THREAD_FreeTEB( teb );
199 return NULL;
203 /***********************************************************************
204 * THREAD_Init
206 * Setup the initial thread.
208 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
210 void THREAD_Init(void)
212 if (!initial_teb.self) /* do it only once */
214 THREAD_InitTEB( &initial_teb );
215 assert( initial_teb.teb_sel );
216 initial_teb.process = &current_process;
217 SYSDEPS_SetCurThread( &initial_teb );
221 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
223 /***********************************************************************
224 * THREAD_Create
227 TEB *THREAD_Create( int fd, DWORD stack_size, BOOL alloc_stack16 )
229 TEB *teb;
231 if ((teb = THREAD_InitStack( NULL, stack_size, alloc_stack16 )))
233 teb->tibflags = (current_process.flags & PDB32_WIN16_PROC) ? 0 : TEBF_WIN32;
234 teb->process = &current_process;
235 teb->socket = fd;
236 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
237 TRACE("(%p) succeeded\n", teb);
239 return teb;
243 /***********************************************************************
244 * THREAD_Start
246 * Start execution of a newly created thread. Does not return.
248 static void THREAD_Start(void)
250 HANDLE cleanup_object;
251 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
253 /* install cleanup handler */
254 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
255 GetCurrentProcess(), &cleanup_object,
256 0, FALSE, DUPLICATE_SAME_ACCESS ))
257 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
258 (ULONG_PTR)NtCurrentTeb() );
260 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
261 PE_InitTls();
262 MODULE_DllThreadAttach( NULL );
263 ExitThread( func( NtCurrentTeb()->entry_arg ) );
267 /***********************************************************************
268 * CreateThread (KERNEL32.63)
270 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
271 LPTHREAD_START_ROUTINE start, LPVOID param,
272 DWORD flags, LPDWORD id )
274 struct new_thread_request *req = get_req_buffer();
275 int socket, handle = -1;
276 TEB *teb;
277 void *tid;
279 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
280 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
281 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
282 handle = req->handle;
283 tid = req->tid;
285 if (!(teb = THREAD_Create( socket, stack, TRUE )))
287 close( socket );
288 return 0;
290 teb->tibflags |= TEBF_WIN32;
291 teb->entry_point = start;
292 teb->entry_arg = param;
293 teb->startup = THREAD_Start;
294 teb->htask16 = GetCurrentTask();
295 if (id) *id = (DWORD)tid;
296 if (SYSDEPS_SpawnThread( teb ) == -1)
298 CloseHandle( handle );
299 return 0;
301 return handle;
304 /***********************************************************************
305 * CreateThread16 (KERNEL.441)
307 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
309 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
310 DWORD param = ((DWORD *)threadArgs)[1];
311 HeapFree( GetProcessHeap(), 0, threadArgs );
313 ((LPDWORD)CURRENT_STACK16)[-1] = param;
314 return CallTo16Long( start, sizeof(DWORD) );
316 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
317 FARPROC16 start, SEGPTR param,
318 DWORD flags, LPDWORD id )
320 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
321 if (!threadArgs) return INVALID_HANDLE_VALUE;
322 threadArgs[0] = (DWORD)start;
323 threadArgs[1] = (DWORD)param;
325 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
329 /***********************************************************************
330 * ExitThread [KERNEL32.215] Ends a thread
332 * RETURNS
333 * None
335 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
337 struct terminate_thread_request *req = get_req_buffer();
339 /* send the exit code to the server */
340 req->handle = GetCurrentThread();
341 req->exit_code = code;
342 server_call( REQ_TERMINATE_THREAD );
343 if (req->last)
345 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
346 exit( code );
348 else
350 MODULE_DllThreadDetach( NULL );
351 if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_KillTask( 0 );
352 SYSDEPS_ExitThread( code );
357 /**********************************************************************
358 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
360 * Allocates a thread local storage index
362 * RETURNS
363 * Success: TLS Index
364 * Failure: 0xFFFFFFFF
366 DWORD WINAPI TlsAlloc( void )
368 DWORD i, mask, ret = 0;
369 DWORD *bits = current_process.tls_bits;
370 EnterCriticalSection( &current_process.crit_section );
371 if (*bits == 0xffffffff)
373 bits++;
374 ret = 32;
375 if (*bits == 0xffffffff)
377 LeaveCriticalSection( &current_process.crit_section );
378 SetLastError( ERROR_NO_MORE_ITEMS );
379 return 0xffffffff;
382 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
383 *bits |= mask;
384 LeaveCriticalSection( &current_process.crit_section );
385 return ret + i;
389 /**********************************************************************
390 * TlsFree [KERNEL32.531] Releases a TLS index.
392 * Releases a thread local storage index, making it available for reuse
394 * RETURNS
395 * Success: TRUE
396 * Failure: FALSE
398 BOOL WINAPI TlsFree(
399 DWORD index) /* [in] TLS Index to free */
401 DWORD mask;
402 DWORD *bits = current_process.tls_bits;
403 if (index >= 64)
405 SetLastError( ERROR_INVALID_PARAMETER );
406 return FALSE;
408 EnterCriticalSection( &current_process.crit_section );
409 if (index >= 32) bits++;
410 mask = (1 << (index & 31));
411 if (!(*bits & mask)) /* already free? */
413 LeaveCriticalSection( &current_process.crit_section );
414 SetLastError( ERROR_INVALID_PARAMETER );
415 return FALSE;
417 *bits &= ~mask;
418 NtCurrentTeb()->tls_array[index] = 0;
419 /* FIXME: should zero all other thread values */
420 LeaveCriticalSection( &current_process.crit_section );
421 return TRUE;
425 /**********************************************************************
426 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
428 * RETURNS
429 * Success: Value stored in calling thread's TLS slot for index
430 * Failure: 0 and GetLastError returns NO_ERROR
432 LPVOID WINAPI TlsGetValue(
433 DWORD index) /* [in] TLS index to retrieve value for */
435 if (index >= 64)
437 SetLastError( ERROR_INVALID_PARAMETER );
438 return NULL;
440 SetLastError( ERROR_SUCCESS );
441 return NtCurrentTeb()->tls_array[index];
445 /**********************************************************************
446 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
448 * RETURNS
449 * Success: TRUE
450 * Failure: FALSE
452 BOOL WINAPI TlsSetValue(
453 DWORD index, /* [in] TLS index to set value for */
454 LPVOID value) /* [in] Value to be stored */
456 if (index >= 64)
458 SetLastError( ERROR_INVALID_PARAMETER );
459 return FALSE;
461 NtCurrentTeb()->tls_array[index] = value;
462 return TRUE;
466 /***********************************************************************
467 * SetThreadContext [KERNEL32.670] Sets context of thread.
469 * RETURNS
470 * Success: TRUE
471 * Failure: FALSE
473 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
474 const CONTEXT *context ) /* [in] Address of context structure */
476 struct set_thread_context_request *req = get_req_buffer();
477 req->handle = handle;
478 req->flags = context->ContextFlags;
479 memcpy( &req->context, context, sizeof(*context) );
480 return !server_call( REQ_SET_THREAD_CONTEXT );
484 /***********************************************************************
485 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
487 * RETURNS
488 * Success: TRUE
489 * Failure: FALSE
491 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
492 CONTEXT *context ) /* [out] Address of context structure */
494 struct get_thread_context_request *req = get_req_buffer();
495 req->handle = handle;
496 req->flags = context->ContextFlags;
497 memcpy( &req->context, context, sizeof(*context) );
498 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
499 memcpy( context, &req->context, sizeof(*context) );
500 return TRUE;
504 /**********************************************************************
505 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
507 * RETURNS
508 * Success: Thread's priority level.
509 * Failure: THREAD_PRIORITY_ERROR_RETURN
511 INT WINAPI GetThreadPriority(
512 HANDLE hthread) /* [in] Handle to thread */
514 INT ret = THREAD_PRIORITY_ERROR_RETURN;
515 struct get_thread_info_request *req = get_req_buffer();
516 req->handle = hthread;
517 req->tid_in = 0;
518 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
519 return ret;
523 /**********************************************************************
524 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
526 * RETURNS
527 * Success: TRUE
528 * Failure: FALSE
530 BOOL WINAPI SetThreadPriority(
531 HANDLE hthread, /* [in] Handle to thread */
532 INT priority) /* [in] Thread priority level */
534 struct set_thread_info_request *req = get_req_buffer();
535 req->handle = hthread;
536 req->priority = priority;
537 req->mask = SET_THREAD_INFO_PRIORITY;
538 return !server_call( REQ_SET_THREAD_INFO );
542 /**********************************************************************
543 * GetThreadPriorityBoost [KERNEL32.877] Returns priority boost for thread.
545 * Always reports that priority boost is disabled.
547 * RETURNS
548 * Success: TRUE.
549 * Failure: FALSE
551 BOOL WINAPI GetThreadPriorityBoost(
552 HANDLE hthread, /* [in] Handle to thread */
553 PBOOL pstate) /* [out] pointer to var that receives the boost state */
555 if (pstate) *pstate = FALSE;
556 return NO_ERROR;
560 /**********************************************************************
561 * SetThreadPriorityBoost [KERNEL32.893] Sets priority boost for thread.
563 * Priority boost is not implemented. Thsi function always returns
564 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
566 * RETURNS
567 * Always returns FALSE to indicate a failure
569 BOOL WINAPI SetThreadPriorityBoost(
570 HANDLE hthread, /* [in] Handle to thread */
571 BOOL disable) /* [in] TRUE to disable priority boost */
573 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
574 return FALSE;
578 /**********************************************************************
579 * SetThreadAffinityMask (KERNEL32.669)
581 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
583 struct set_thread_info_request *req = get_req_buffer();
584 req->handle = hThread;
585 req->affinity = dwThreadAffinityMask;
586 req->mask = SET_THREAD_INFO_AFFINITY;
587 if (server_call( REQ_SET_THREAD_INFO )) return 0;
588 return 1; /* FIXME: should return previous value */
592 /**********************************************************************
593 * TerminateThread [KERNEL32.685] Terminates a thread
595 * RETURNS
596 * Success: TRUE
597 * Failure: FALSE
599 BOOL WINAPI TerminateThread(
600 HANDLE handle, /* [in] Handle to thread */
601 DWORD exitcode) /* [in] Exit code for thread */
603 BOOL ret;
604 struct terminate_thread_request *req = get_req_buffer();
605 req->handle = handle;
606 req->exit_code = exitcode;
607 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
609 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
610 if (req->last) exit( exitcode );
611 else SYSDEPS_ExitThread( exitcode );
613 return ret;
617 /**********************************************************************
618 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
620 * RETURNS
621 * Success: TRUE
622 * Failure: FALSE
624 BOOL WINAPI GetExitCodeThread(
625 HANDLE hthread, /* [in] Handle to thread */
626 LPDWORD exitcode) /* [out] Address to receive termination status */
628 BOOL ret = FALSE;
629 struct get_thread_info_request *req = get_req_buffer();
630 req->handle = hthread;
631 req->tid_in = 0;
632 if (!server_call( REQ_GET_THREAD_INFO ))
634 if (exitcode) *exitcode = req->exit_code;
635 ret = TRUE;
637 return ret;
641 /**********************************************************************
642 * ResumeThread [KERNEL32.587] Resumes a thread.
644 * Decrements a thread's suspend count. When count is zero, the
645 * execution of the thread is resumed.
647 * RETURNS
648 * Success: Previous suspend count
649 * Failure: 0xFFFFFFFF
650 * Already running: 0
652 DWORD WINAPI ResumeThread(
653 HANDLE hthread) /* [in] Identifies thread to restart */
655 DWORD ret = 0xffffffff;
656 struct resume_thread_request *req = get_req_buffer();
657 req->handle = hthread;
658 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
659 return ret;
663 /**********************************************************************
664 * SuspendThread [KERNEL32.681] Suspends a thread.
666 * RETURNS
667 * Success: Previous suspend count
668 * Failure: 0xFFFFFFFF
670 DWORD WINAPI SuspendThread(
671 HANDLE hthread) /* [in] Handle to the thread */
673 DWORD ret = 0xffffffff;
674 struct suspend_thread_request *req = get_req_buffer();
675 req->handle = hthread;
676 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
677 return ret;
681 /***********************************************************************
682 * QueueUserAPC (KERNEL32.566)
684 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
686 struct queue_apc_request *req = get_req_buffer();
687 req->handle = hthread;
688 req->func = func;
689 req->param = (void *)data;
690 return !server_call( REQ_QUEUE_APC );
694 /**********************************************************************
695 * GetThreadTimes [KERNEL32.???] Obtains timing information.
697 * NOTES
698 * What are the fields where these values are stored?
700 * RETURNS
701 * Success: TRUE
702 * Failure: FALSE
704 BOOL WINAPI GetThreadTimes(
705 HANDLE thread, /* [in] Specifies the thread of interest */
706 LPFILETIME creationtime, /* [out] When the thread was created */
707 LPFILETIME exittime, /* [out] When the thread was destroyed */
708 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
709 LPFILETIME usertime) /* [out] Time thread spent in user mode */
711 FIXME("(0x%08x): stub\n",thread);
712 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
713 return FALSE;
717 /**********************************************************************
718 * VWin32_BoostThreadGroup [KERNEL.535]
720 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
722 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
725 /**********************************************************************
726 * VWin32_BoostThreadStatic [KERNEL.536]
728 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
730 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
734 /***********************************************************************
735 * GetThreadLocale (KERNEL32.295)
737 LCID WINAPI GetThreadLocale(void)
739 LCID ret = NtCurrentTeb()->CurrentLocale;
740 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
741 return ret;
745 /**********************************************************************
746 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
748 * RETURNS
749 * Success: TRUE
750 * Failure: FALSE
752 * FIXME
753 * check if lcid is a valid cp
755 BOOL WINAPI SetThreadLocale(
756 LCID lcid) /* [in] Locale identifier */
758 switch (lcid)
760 case LOCALE_SYSTEM_DEFAULT:
761 lcid = GetSystemDefaultLCID();
762 break;
763 case LOCALE_USER_DEFAULT:
764 case LOCALE_NEUTRAL:
765 lcid = GetUserDefaultLCID();
766 break;
768 NtCurrentTeb()->CurrentLocale = lcid;
769 return TRUE;
773 /***********************************************************************
774 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
776 * RETURNS
777 * Pseudohandle for the current thread
779 #undef GetCurrentThread
780 HANDLE WINAPI GetCurrentThread(void)
782 return 0xfffffffe;
786 #ifdef __i386__
788 /* void WINAPI SetLastError( DWORD error ); */
789 __ASM_GLOBAL_FUNC( SetLastError,
790 "movl 4(%esp),%eax\n\t"
791 ".byte 0x64\n\t"
792 "movl %eax,0x60\n\t"
793 "ret $4" );
795 /* DWORD WINAPI GetLastError(void); */
796 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
798 /* DWORD WINAPI GetCurrentProcessId(void) */
799 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
801 /* DWORD WINAPI GetCurrentThreadId(void) */
802 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
804 #else /* __i386__ */
806 /**********************************************************************
807 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
809 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
811 NtCurrentTeb()->last_error = error;
814 /**********************************************************************
815 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
817 DWORD WINAPI GetLastError(void)
819 return NtCurrentTeb()->last_error;
822 /***********************************************************************
823 * GetCurrentProcessId [KERNEL32.199] Returns process identifier.
825 DWORD WINAPI GetCurrentProcessId(void)
827 return (DWORD)NtCurrentTeb()->pid;
830 /***********************************************************************
831 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
833 DWORD WINAPI GetCurrentThreadId(void)
835 return (DWORD)NtCurrentTeb()->tid;
838 #endif /* __i386__ */