Documentation ordinal fixes.
[wine/multimedia.git] / scheduler / thread.c
blob12bbff6651b377d3a3719198e42066c8df665ab1
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 "wine/port.h"
18 #include "thread.h"
19 #include "task.h"
20 #include "module.h"
21 #include "winerror.h"
22 #include "heap.h"
23 #include "selectors.h"
24 #include "winnt.h"
25 #include "server.h"
26 #include "services.h"
27 #include "stackframe.h"
28 #include "builtin16.h"
29 #include "debugtools.h"
30 #include "winnls.h"
32 DEFAULT_DEBUG_CHANNEL(thread);
34 /* TEB of the initial thread */
35 static TEB initial_teb;
37 extern struct _PDB current_process;
39 /***********************************************************************
40 * THREAD_IsWin16
42 BOOL THREAD_IsWin16( TEB *teb )
44 return !teb || !(teb->tibflags & TEBF_WIN32);
47 /***********************************************************************
48 * THREAD_IdToTEB
50 * Convert a thread id to a TEB, making sure it is valid.
52 TEB *THREAD_IdToTEB( DWORD id )
54 TEB *ret = NULL;
56 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
58 SERVER_START_REQ
60 struct get_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
61 req->handle = 0;
62 req->tid_in = (void *)id;
63 if (!server_call_noerr( REQ_GET_THREAD_INFO )) ret = req->teb;
65 SERVER_END_REQ;
67 if (!ret)
69 /* Allow task handles to be used; convert to main thread */
70 if ( IsTask16( id ) )
72 TDB *pTask = (TDB *)GlobalLock16( id );
73 if (pTask) return pTask->teb;
75 SetLastError( ERROR_INVALID_PARAMETER );
77 return ret;
81 /***********************************************************************
82 * THREAD_InitTEB
84 * Initialization of a newly created TEB.
86 static BOOL THREAD_InitTEB( TEB *teb )
88 teb->except = (void *)~0UL;
89 teb->self = teb;
90 teb->tibflags = TEBF_WIN32;
91 teb->tls_ptr = teb->tls_array;
92 teb->exit_code = STILL_ACTIVE;
93 teb->socket = -1;
94 teb->request_fd = -1;
95 teb->reply_fd = -1;
96 teb->stack_top = (void *)~0UL;
97 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
98 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
99 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, WINE_LDT_FLAGS_DATA|WINE_LDT_FLAGS_32BIT );
100 return (teb->teb_sel != 0);
104 /***********************************************************************
105 * THREAD_FreeTEB
107 * Free data structures associated with a thread.
108 * Must be called from the context of another thread.
110 static void CALLBACK THREAD_FreeTEB( TEB *teb )
112 TRACE("(%p) called\n", teb );
113 if (teb->cleanup) SERVICE_Delete( teb->cleanup );
115 /* Free the associated memory */
117 if (teb->socket != -1) close( teb->socket );
118 close( teb->request_fd );
119 close( teb->reply_fd );
120 if (teb->stack_sel) FreeSelector16( teb->stack_sel );
121 FreeSelector16( teb->teb_sel );
122 if (teb->buffer) munmap( (void *)teb->buffer,
123 (char *)(teb->buffer_info+1) - (char *)teb->buffer );
124 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
125 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
129 /***********************************************************************
130 * THREAD_InitStack
132 * Allocate the stack of a thread.
134 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size, BOOL alloc_stack16 )
136 DWORD old_prot, total_size;
137 DWORD page_size = getpagesize();
138 void *base;
140 /* Allocate the stack */
142 if (stack_size >= 16*1024*1024)
143 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
145 /* if size is smaller than default, get stack size from parent */
146 if (stack_size < 1024 * 1024)
148 if (teb)
149 stack_size = 1024 * 1024; /* no parent */
150 else
151 stack_size = ((char *)NtCurrentTeb()->stack_top - (char *)NtCurrentTeb()->stack_base
152 - SIGNAL_STACK_SIZE - 3 * page_size);
155 /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
156 stack_size += 64 * 1024;
158 /* Memory layout in allocated block:
160 * size contents
161 * 1 page NOACCESS guard page
162 * SIGNAL_STACK_SIZE signal stack
163 * 1 page NOACCESS guard page
164 * 1 page PAGE_GUARD guard page
165 * stack_size normal stack
166 * 64Kb 16-bit stack (optional)
167 * 1 page TEB (except for initial thread)
170 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
171 total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
172 if (alloc_stack16) total_size += 0x10000;
173 if (!teb) total_size += page_size;
175 if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
176 return NULL;
178 if (!teb)
180 teb = (TEB *)((char *)base + total_size - page_size);
181 if (!THREAD_InitTEB( teb ))
183 VirtualFree( base, 0, MEM_RELEASE );
184 return NULL;
188 teb->stack_low = base;
189 teb->stack_base = base;
190 teb->signal_stack = (char *)base + page_size;
191 teb->stack_top = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
193 /* Setup guard pages */
195 VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
196 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
197 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
198 PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
200 /* Allocate the 16-bit stack selector */
202 if (alloc_stack16)
204 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, WINE_LDT_FLAGS_DATA );
205 if (!teb->stack_sel) goto error;
206 teb->cur_stack = MAKESEGPTR( teb->stack_sel, 0x10000 - sizeof(STACK16FRAME) );
208 return teb;
210 error:
211 THREAD_FreeTEB( teb );
212 return NULL;
216 /***********************************************************************
217 * thread_errno_location
219 * Get the per-thread errno location.
221 static int *thread_errno_location(void)
223 return &NtCurrentTeb()->thread_errno;
226 /***********************************************************************
227 * thread_h_errno_location
229 * Get the per-thread h_errno location.
231 static int *thread_h_errno_location(void)
233 return &NtCurrentTeb()->thread_h_errno;
236 /***********************************************************************
237 * THREAD_Init
239 * Setup the initial thread.
241 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
243 void THREAD_Init(void)
245 if (!initial_teb.self) /* do it only once */
247 THREAD_InitTEB( &initial_teb );
248 assert( initial_teb.teb_sel );
249 initial_teb.process = &current_process;
250 SYSDEPS_SetCurThread( &initial_teb );
251 wine_errno_location = thread_errno_location;
252 wine_h_errno_location = thread_h_errno_location;
256 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
258 /***********************************************************************
259 * THREAD_Create
262 TEB *THREAD_Create( int fd, DWORD stack_size, BOOL alloc_stack16 )
264 TEB *teb;
266 if ((teb = THREAD_InitStack( NULL, stack_size, alloc_stack16 )))
268 teb->tibflags = TEBF_WIN32;
269 teb->process = NtCurrentTeb()->process;
270 teb->socket = fd;
271 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
272 TRACE("(%p) succeeded\n", teb);
274 return teb;
278 /***********************************************************************
279 * THREAD_Start
281 * Start execution of a newly created thread. Does not return.
283 static void THREAD_Start(void)
285 HANDLE cleanup_object;
286 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
288 /* install cleanup handler */
289 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
290 GetCurrentProcess(), &cleanup_object,
291 0, FALSE, DUPLICATE_SAME_ACCESS ))
292 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
293 (ULONG_PTR)NtCurrentTeb() );
295 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
296 PE_InitTls();
297 MODULE_DllThreadAttach( NULL );
298 ExitThread( func( NtCurrentTeb()->entry_arg ) );
302 /***********************************************************************
303 * CreateThread (KERNEL32.@)
305 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
306 LPTHREAD_START_ROUTINE start, LPVOID param,
307 DWORD flags, LPDWORD id )
309 int socket = -1;
310 HANDLE handle = 0;
311 TEB *teb;
312 void *tid = 0;
314 SERVER_START_REQ
316 struct new_thread_request *req = server_alloc_req( sizeof(*req), 0 );
318 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
319 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
320 if (!server_call( REQ_NEW_THREAD ))
322 handle = req->handle;
323 tid = req->tid;
324 socket = wine_server_recv_fd( handle, 0 );
327 SERVER_END_REQ;
328 if (!handle) return 0;
330 if (!(teb = THREAD_Create( socket, stack, TRUE )))
332 close( socket );
333 return 0;
335 teb->entry_point = start;
336 teb->entry_arg = param;
337 teb->startup = THREAD_Start;
338 teb->htask16 = GetCurrentTask();
339 if (id) *id = (DWORD)tid;
340 if (SYSDEPS_SpawnThread( teb ) == -1)
342 CloseHandle( handle );
343 THREAD_FreeTEB( teb );
344 return 0;
346 return handle;
349 /***********************************************************************
350 * CreateThread16 (KERNEL.441)
352 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
354 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
355 DWORD param = ((DWORD *)threadArgs)[1];
356 HeapFree( GetProcessHeap(), 0, threadArgs );
358 ((LPDWORD)CURRENT_STACK16)[-1] = param;
359 return wine_call_to_16_long( start, sizeof(DWORD) );
361 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
362 FARPROC16 start, SEGPTR param,
363 DWORD flags, LPDWORD id )
365 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
366 if (!threadArgs) return INVALID_HANDLE_VALUE;
367 threadArgs[0] = (DWORD)start;
368 threadArgs[1] = (DWORD)param;
370 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
374 /***********************************************************************
375 * ExitThread [KERNEL32.215] Ends a thread
377 * RETURNS
378 * None
380 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
382 BOOL last;
383 SERVER_START_REQ
385 struct terminate_thread_request *req = server_alloc_req( sizeof(*req), 0 );
387 /* send the exit code to the server */
388 req->handle = GetCurrentThread();
389 req->exit_code = code;
390 server_call( REQ_TERMINATE_THREAD );
391 last = req->last;
393 SERVER_END_REQ;
395 if (last)
397 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
398 exit( code );
400 else
402 MODULE_DllThreadDetach( NULL );
403 if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
404 SYSDEPS_ExitThread( code );
409 /***********************************************************************
410 * SetThreadContext [KERNEL32.670] Sets context of thread.
412 * RETURNS
413 * Success: TRUE
414 * Failure: FALSE
416 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
417 const CONTEXT *context ) /* [in] Address of context structure */
419 BOOL ret;
420 SERVER_START_REQ
422 struct set_thread_context_request *req = server_alloc_req( sizeof(*req),
423 sizeof(*context) );
424 req->handle = handle;
425 req->flags = context->ContextFlags;
426 memcpy( server_data_ptr(req), context, sizeof(*context) );
427 ret = !server_call( REQ_SET_THREAD_CONTEXT );
429 SERVER_END_REQ;
430 return ret;
434 /***********************************************************************
435 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
437 * RETURNS
438 * Success: TRUE
439 * Failure: FALSE
441 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
442 CONTEXT *context ) /* [out] Address of context structure */
444 BOOL ret;
445 SERVER_START_REQ
447 struct get_thread_context_request *req = server_alloc_req( sizeof(*req),
448 sizeof(*context) );
449 req->handle = handle;
450 req->flags = context->ContextFlags;
451 memcpy( server_data_ptr(req), context, sizeof(*context) );
452 if ((ret = !server_call( REQ_GET_THREAD_CONTEXT )))
453 memcpy( context, server_data_ptr(req), sizeof(*context) );
455 SERVER_END_REQ;
456 return ret;
460 /**********************************************************************
461 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
463 * RETURNS
464 * Success: Thread's priority level.
465 * Failure: THREAD_PRIORITY_ERROR_RETURN
467 INT WINAPI GetThreadPriority(
468 HANDLE hthread) /* [in] Handle to thread */
470 INT ret = THREAD_PRIORITY_ERROR_RETURN;
471 SERVER_START_REQ
473 struct get_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
474 req->handle = hthread;
475 req->tid_in = 0;
476 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
478 SERVER_END_REQ;
479 return ret;
483 /**********************************************************************
484 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
486 * RETURNS
487 * Success: TRUE
488 * Failure: FALSE
490 BOOL WINAPI SetThreadPriority(
491 HANDLE hthread, /* [in] Handle to thread */
492 INT priority) /* [in] Thread priority level */
494 BOOL ret;
495 SERVER_START_REQ
497 struct set_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
498 req->handle = hthread;
499 req->priority = priority;
500 req->mask = SET_THREAD_INFO_PRIORITY;
501 ret = !server_call( REQ_SET_THREAD_INFO );
503 SERVER_END_REQ;
504 return ret;
508 /**********************************************************************
509 * GetThreadPriorityBoost [KERNEL32.877] Returns priority boost for thread.
511 * Always reports that priority boost is disabled.
513 * RETURNS
514 * Success: TRUE.
515 * Failure: FALSE
517 BOOL WINAPI GetThreadPriorityBoost(
518 HANDLE hthread, /* [in] Handle to thread */
519 PBOOL pstate) /* [out] pointer to var that receives the boost state */
521 if (pstate) *pstate = FALSE;
522 return NO_ERROR;
526 /**********************************************************************
527 * SetThreadPriorityBoost [KERNEL32.893] Sets priority boost for thread.
529 * Priority boost is not implemented. Thsi function always returns
530 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
532 * RETURNS
533 * Always returns FALSE to indicate a failure
535 BOOL WINAPI SetThreadPriorityBoost(
536 HANDLE hthread, /* [in] Handle to thread */
537 BOOL disable) /* [in] TRUE to disable priority boost */
539 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
540 return FALSE;
544 /**********************************************************************
545 * SetThreadAffinityMask (KERNEL32.669)
547 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
549 DWORD ret;
550 SERVER_START_REQ
552 struct set_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
553 req->handle = hThread;
554 req->affinity = dwThreadAffinityMask;
555 req->mask = SET_THREAD_INFO_AFFINITY;
556 ret = !server_call( REQ_SET_THREAD_INFO );
557 /* FIXME: should return previous value */
559 SERVER_END_REQ;
560 return ret;
564 /**********************************************************************
565 * TerminateThread [KERNEL32.685] Terminates a thread
567 * RETURNS
568 * Success: TRUE
569 * Failure: FALSE
571 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
572 DWORD exit_code) /* [in] Exit code for thread */
574 NTSTATUS status = NtTerminateThread( handle, exit_code );
575 if (status) SetLastError( RtlNtStatusToDosError(status) );
576 return !status;
580 /**********************************************************************
581 * GetExitCodeThread (KERNEL32.@)
582 * GetExitCodeThread (WIN32S16.13)
584 * Gets termination status of thread.
586 * RETURNS
587 * Success: TRUE
588 * Failure: FALSE
590 BOOL WINAPI GetExitCodeThread(
591 HANDLE hthread, /* [in] Handle to thread */
592 LPDWORD exitcode) /* [out] Address to receive termination status */
594 BOOL ret;
595 SERVER_START_REQ
597 struct get_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
598 req->handle = hthread;
599 req->tid_in = 0;
600 ret = !server_call( REQ_GET_THREAD_INFO );
601 if (ret && exitcode) *exitcode = req->exit_code;
603 SERVER_END_REQ;
604 return ret;
608 /**********************************************************************
609 * ResumeThread [KERNEL32.587] Resumes a thread.
611 * Decrements a thread's suspend count. When count is zero, the
612 * execution of the thread is resumed.
614 * RETURNS
615 * Success: Previous suspend count
616 * Failure: 0xFFFFFFFF
617 * Already running: 0
619 DWORD WINAPI ResumeThread(
620 HANDLE hthread) /* [in] Identifies thread to restart */
622 DWORD ret = 0xffffffff;
623 SERVER_START_REQ
625 struct resume_thread_request *req = server_alloc_req( sizeof(*req), 0 );
626 req->handle = hthread;
627 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
629 SERVER_END_REQ;
630 return ret;
634 /**********************************************************************
635 * SuspendThread [KERNEL32.681] Suspends a thread.
637 * RETURNS
638 * Success: Previous suspend count
639 * Failure: 0xFFFFFFFF
641 DWORD WINAPI SuspendThread(
642 HANDLE hthread) /* [in] Handle to the thread */
644 DWORD ret = 0xffffffff;
645 SERVER_START_REQ
647 struct suspend_thread_request *req = server_alloc_req( sizeof(*req), 0 );
648 req->handle = hthread;
649 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
651 SERVER_END_REQ;
652 return ret;
656 /***********************************************************************
657 * QueueUserAPC (KERNEL32.566)
659 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
661 DWORD ret;
662 SERVER_START_REQ
664 struct queue_apc_request *req = server_alloc_req( sizeof(*req), 0 );
665 req->handle = hthread;
666 req->user = 1;
667 req->func = func;
668 req->param = (void *)data;
669 ret = !server_call( REQ_QUEUE_APC );
671 SERVER_END_REQ;
672 return ret;
676 /**********************************************************************
677 * GetThreadTimes [KERNEL32.@] Obtains timing information.
679 * NOTES
680 * What are the fields where these values are stored?
682 * RETURNS
683 * Success: TRUE
684 * Failure: FALSE
686 BOOL WINAPI GetThreadTimes(
687 HANDLE thread, /* [in] Specifies the thread of interest */
688 LPFILETIME creationtime, /* [out] When the thread was created */
689 LPFILETIME exittime, /* [out] When the thread was destroyed */
690 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
691 LPFILETIME usertime) /* [out] Time thread spent in user mode */
693 FIXME("(0x%08x): stub\n",thread);
694 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
695 return FALSE;
699 /**********************************************************************
700 * VWin32_BoostThreadGroup [KERNEL.535]
702 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
704 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
707 /**********************************************************************
708 * VWin32_BoostThreadStatic [KERNEL.536]
710 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
712 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
716 /***********************************************************************
717 * GetThreadLocale (KERNEL32.@)
719 LCID WINAPI GetThreadLocale(void)
721 LCID ret = NtCurrentTeb()->CurrentLocale;
722 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
723 return ret;
727 /**********************************************************************
728 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
730 * RETURNS
731 * Success: TRUE
732 * Failure: FALSE
734 * FIXME
735 * check if lcid is a valid cp
737 BOOL WINAPI SetThreadLocale(
738 LCID lcid) /* [in] Locale identifier */
740 switch (lcid)
742 case LOCALE_SYSTEM_DEFAULT:
743 lcid = GetSystemDefaultLCID();
744 break;
745 case LOCALE_USER_DEFAULT:
746 case LOCALE_NEUTRAL:
747 lcid = GetUserDefaultLCID();
748 break;
750 NtCurrentTeb()->CurrentLocale = lcid;
751 return TRUE;
755 /***********************************************************************
756 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
758 * RETURNS
759 * Pseudohandle for the current thread
761 #undef GetCurrentThread
762 HANDLE WINAPI GetCurrentThread(void)
764 return 0xfffffffe;
768 /***********************************************************************
769 * ProcessIdToSessionId (KERNEL32.@)
770 * This function is available on Terminal Server 4SP4 and Windows 2000
772 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
774 /* According to MSDN, if the calling process is not in a terminal
775 * services environment, then the sessionid returned is zero.
777 *sessionid_ptr = 0;
778 return TRUE;
782 #ifdef __i386__
784 /* void WINAPI SetLastError( DWORD error ); */
785 __ASM_GLOBAL_FUNC( SetLastError,
786 "movl 4(%esp),%eax\n\t"
787 ".byte 0x64\n\t"
788 "movl %eax,0x60\n\t"
789 "ret $4" );
791 /* DWORD WINAPI GetLastError(void); */
792 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
794 /* DWORD WINAPI GetCurrentProcessId(void) */
795 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
797 /* DWORD WINAPI GetCurrentThreadId(void) */
798 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
800 #else /* __i386__ */
802 /**********************************************************************
803 * SetLastError (KERNEL.147)
804 * SetLastError (KERNEL32.@)
806 * Sets the last-error code.
808 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
810 NtCurrentTeb()->last_error = error;
813 /**********************************************************************
814 * GetLastError (KERNEL.10)
815 * GetLastError (KERNEL32.@)
816 * GetLastError (WIN32S16.10)
818 * Returns last-error code.
820 DWORD WINAPI GetLastError(void)
822 return NtCurrentTeb()->last_error;
825 /***********************************************************************
826 * GetCurrentProcessId (KERNEL.471)
827 * GetCurrentProcessId (KERNEL32.@)
829 * Returns process identifier.
831 DWORD WINAPI GetCurrentProcessId(void)
833 return (DWORD)NtCurrentTeb()->pid;
836 /***********************************************************************
837 * GetCurrentThreadId (KERNEL.462)
838 * GetCurrentThreadId (KERNEL32.@)
840 * Returns thread identifier.
842 DWORD WINAPI GetCurrentThreadId(void)
844 return (DWORD)NtCurrentTeb()->tid;
847 #endif /* __i386__ */