Test pointer before it is dereferenced in CreateEventW().
[wine.git] / scheduler / thread.c
blob7aca5d2ac8b6a3f553c752e411b6bdae4ee2b3d5
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 = -1;
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_Init
219 * Setup the initial thread.
221 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
223 void THREAD_Init(void)
225 if (!initial_teb.self) /* do it only once */
227 THREAD_InitTEB( &initial_teb );
228 assert( initial_teb.teb_sel );
229 initial_teb.process = &current_process;
230 SYSDEPS_SetCurThread( &initial_teb );
234 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
236 /***********************************************************************
237 * THREAD_Create
240 TEB *THREAD_Create( int fd, DWORD stack_size, BOOL alloc_stack16 )
242 TEB *teb;
244 if ((teb = THREAD_InitStack( NULL, stack_size, alloc_stack16 )))
246 teb->tibflags = TEBF_WIN32;
247 teb->process = NtCurrentTeb()->process;
248 teb->socket = fd;
249 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
250 TRACE("(%p) succeeded\n", teb);
252 return teb;
256 /***********************************************************************
257 * THREAD_Start
259 * Start execution of a newly created thread. Does not return.
261 static void THREAD_Start(void)
263 HANDLE cleanup_object;
264 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
266 /* install cleanup handler */
267 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
268 GetCurrentProcess(), &cleanup_object,
269 0, FALSE, DUPLICATE_SAME_ACCESS ))
270 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
271 (ULONG_PTR)NtCurrentTeb() );
273 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
274 PE_InitTls();
275 MODULE_DllThreadAttach( NULL );
276 ExitThread( func( NtCurrentTeb()->entry_arg ) );
280 /***********************************************************************
281 * CreateThread (KERNEL32.63)
283 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
284 LPTHREAD_START_ROUTINE start, LPVOID param,
285 DWORD flags, LPDWORD id )
287 int socket = -1, handle = -1;
288 TEB *teb;
289 void *tid = 0;
291 SERVER_START_REQ
293 struct new_thread_request *req = server_alloc_req( sizeof(*req), 0 );
295 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
296 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
297 if (!server_call( REQ_NEW_THREAD ))
299 handle = req->handle;
300 tid = req->tid;
301 socket = wine_server_recv_fd( handle, 0 );
304 SERVER_END_REQ;
305 if (handle == -1) return 0;
307 if (!(teb = THREAD_Create( socket, stack, TRUE )))
309 close( socket );
310 return 0;
312 teb->entry_point = start;
313 teb->entry_arg = param;
314 teb->startup = THREAD_Start;
315 teb->htask16 = GetCurrentTask();
316 if (id) *id = (DWORD)tid;
317 if (SYSDEPS_SpawnThread( teb ) == -1)
319 CloseHandle( handle );
320 THREAD_FreeTEB( teb );
321 return 0;
323 return handle;
326 /***********************************************************************
327 * CreateThread16 (KERNEL.441)
329 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
331 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
332 DWORD param = ((DWORD *)threadArgs)[1];
333 HeapFree( GetProcessHeap(), 0, threadArgs );
335 ((LPDWORD)CURRENT_STACK16)[-1] = param;
336 return wine_call_to_16_long( start, sizeof(DWORD) );
338 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
339 FARPROC16 start, SEGPTR param,
340 DWORD flags, LPDWORD id )
342 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
343 if (!threadArgs) return INVALID_HANDLE_VALUE;
344 threadArgs[0] = (DWORD)start;
345 threadArgs[1] = (DWORD)param;
347 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
351 /***********************************************************************
352 * ExitThread [KERNEL32.215] Ends a thread
354 * RETURNS
355 * None
357 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
359 BOOL last;
360 SERVER_START_REQ
362 struct terminate_thread_request *req = server_alloc_req( sizeof(*req), 0 );
364 /* send the exit code to the server */
365 req->handle = GetCurrentThread();
366 req->exit_code = code;
367 server_call( REQ_TERMINATE_THREAD );
368 last = req->last;
370 SERVER_END_REQ;
372 if (last)
374 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
375 exit( code );
377 else
379 MODULE_DllThreadDetach( NULL );
380 if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
381 SYSDEPS_ExitThread( code );
386 /***********************************************************************
387 * SetThreadContext [KERNEL32.670] Sets context of thread.
389 * RETURNS
390 * Success: TRUE
391 * Failure: FALSE
393 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
394 const CONTEXT *context ) /* [in] Address of context structure */
396 BOOL ret;
397 SERVER_START_REQ
399 struct set_thread_context_request *req = server_alloc_req( sizeof(*req),
400 sizeof(*context) );
401 req->handle = handle;
402 req->flags = context->ContextFlags;
403 memcpy( server_data_ptr(req), context, sizeof(*context) );
404 ret = !server_call( REQ_SET_THREAD_CONTEXT );
406 SERVER_END_REQ;
407 return ret;
411 /***********************************************************************
412 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
414 * RETURNS
415 * Success: TRUE
416 * Failure: FALSE
418 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
419 CONTEXT *context ) /* [out] Address of context structure */
421 BOOL ret;
422 SERVER_START_REQ
424 struct get_thread_context_request *req = server_alloc_req( sizeof(*req),
425 sizeof(*context) );
426 req->handle = handle;
427 req->flags = context->ContextFlags;
428 memcpy( server_data_ptr(req), context, sizeof(*context) );
429 if ((ret = !server_call( REQ_GET_THREAD_CONTEXT )))
430 memcpy( context, server_data_ptr(req), sizeof(*context) );
432 SERVER_END_REQ;
433 return ret;
437 /**********************************************************************
438 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
440 * RETURNS
441 * Success: Thread's priority level.
442 * Failure: THREAD_PRIORITY_ERROR_RETURN
444 INT WINAPI GetThreadPriority(
445 HANDLE hthread) /* [in] Handle to thread */
447 INT ret = THREAD_PRIORITY_ERROR_RETURN;
448 SERVER_START_REQ
450 struct get_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
451 req->handle = hthread;
452 req->tid_in = 0;
453 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
455 SERVER_END_REQ;
456 return ret;
460 /**********************************************************************
461 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
463 * RETURNS
464 * Success: TRUE
465 * Failure: FALSE
467 BOOL WINAPI SetThreadPriority(
468 HANDLE hthread, /* [in] Handle to thread */
469 INT priority) /* [in] Thread priority level */
471 BOOL ret;
472 SERVER_START_REQ
474 struct set_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
475 req->handle = hthread;
476 req->priority = priority;
477 req->mask = SET_THREAD_INFO_PRIORITY;
478 ret = !server_call( REQ_SET_THREAD_INFO );
480 SERVER_END_REQ;
481 return ret;
485 /**********************************************************************
486 * GetThreadPriorityBoost [KERNEL32.877] Returns priority boost for thread.
488 * Always reports that priority boost is disabled.
490 * RETURNS
491 * Success: TRUE.
492 * Failure: FALSE
494 BOOL WINAPI GetThreadPriorityBoost(
495 HANDLE hthread, /* [in] Handle to thread */
496 PBOOL pstate) /* [out] pointer to var that receives the boost state */
498 if (pstate) *pstate = FALSE;
499 return NO_ERROR;
503 /**********************************************************************
504 * SetThreadPriorityBoost [KERNEL32.893] Sets priority boost for thread.
506 * Priority boost is not implemented. Thsi function always returns
507 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
509 * RETURNS
510 * Always returns FALSE to indicate a failure
512 BOOL WINAPI SetThreadPriorityBoost(
513 HANDLE hthread, /* [in] Handle to thread */
514 BOOL disable) /* [in] TRUE to disable priority boost */
516 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
517 return FALSE;
521 /**********************************************************************
522 * SetThreadAffinityMask (KERNEL32.669)
524 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
526 DWORD ret;
527 SERVER_START_REQ
529 struct set_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
530 req->handle = hThread;
531 req->affinity = dwThreadAffinityMask;
532 req->mask = SET_THREAD_INFO_AFFINITY;
533 ret = !server_call( REQ_SET_THREAD_INFO );
534 /* FIXME: should return previous value */
536 SERVER_END_REQ;
537 return ret;
541 /**********************************************************************
542 * TerminateThread [KERNEL32.685] Terminates a thread
544 * RETURNS
545 * Success: TRUE
546 * Failure: FALSE
548 BOOL WINAPI TerminateThread( HANDLE handle, /* [in] Handle to thread */
549 DWORD exit_code) /* [in] Exit code for thread */
551 NTSTATUS status = NtTerminateThread( handle, exit_code );
552 if (status) SetLastError( RtlNtStatusToDosError(status) );
553 return !status;
557 /**********************************************************************
558 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
560 * RETURNS
561 * Success: TRUE
562 * Failure: FALSE
564 BOOL WINAPI GetExitCodeThread(
565 HANDLE hthread, /* [in] Handle to thread */
566 LPDWORD exitcode) /* [out] Address to receive termination status */
568 BOOL ret;
569 SERVER_START_REQ
571 struct get_thread_info_request *req = server_alloc_req( sizeof(*req), 0 );
572 req->handle = hthread;
573 req->tid_in = 0;
574 ret = !server_call( REQ_GET_THREAD_INFO );
575 if (ret && exitcode) *exitcode = req->exit_code;
577 SERVER_END_REQ;
578 return ret;
582 /**********************************************************************
583 * ResumeThread [KERNEL32.587] Resumes a thread.
585 * Decrements a thread's suspend count. When count is zero, the
586 * execution of the thread is resumed.
588 * RETURNS
589 * Success: Previous suspend count
590 * Failure: 0xFFFFFFFF
591 * Already running: 0
593 DWORD WINAPI ResumeThread(
594 HANDLE hthread) /* [in] Identifies thread to restart */
596 DWORD ret = 0xffffffff;
597 SERVER_START_REQ
599 struct resume_thread_request *req = server_alloc_req( sizeof(*req), 0 );
600 req->handle = hthread;
601 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
603 SERVER_END_REQ;
604 return ret;
608 /**********************************************************************
609 * SuspendThread [KERNEL32.681] Suspends a thread.
611 * RETURNS
612 * Success: Previous suspend count
613 * Failure: 0xFFFFFFFF
615 DWORD WINAPI SuspendThread(
616 HANDLE hthread) /* [in] Handle to the thread */
618 DWORD ret = 0xffffffff;
619 SERVER_START_REQ
621 struct suspend_thread_request *req = server_alloc_req( sizeof(*req), 0 );
622 req->handle = hthread;
623 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
625 SERVER_END_REQ;
626 return ret;
630 /***********************************************************************
631 * QueueUserAPC (KERNEL32.566)
633 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
635 DWORD ret;
636 SERVER_START_REQ
638 struct queue_apc_request *req = server_alloc_req( sizeof(*req), 0 );
639 req->handle = hthread;
640 req->func = func;
641 req->param = (void *)data;
642 ret = !server_call( REQ_QUEUE_APC );
644 SERVER_END_REQ;
645 return ret;
649 /**********************************************************************
650 * GetThreadTimes [KERNEL32.???] Obtains timing information.
652 * NOTES
653 * What are the fields where these values are stored?
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 BOOL WINAPI GetThreadTimes(
660 HANDLE thread, /* [in] Specifies the thread of interest */
661 LPFILETIME creationtime, /* [out] When the thread was created */
662 LPFILETIME exittime, /* [out] When the thread was destroyed */
663 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
664 LPFILETIME usertime) /* [out] Time thread spent in user mode */
666 FIXME("(0x%08x): stub\n",thread);
667 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
668 return FALSE;
672 /**********************************************************************
673 * VWin32_BoostThreadGroup [KERNEL.535]
675 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
677 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
680 /**********************************************************************
681 * VWin32_BoostThreadStatic [KERNEL.536]
683 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
685 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
689 /***********************************************************************
690 * GetThreadLocale (KERNEL32.295)
692 LCID WINAPI GetThreadLocale(void)
694 LCID ret = NtCurrentTeb()->CurrentLocale;
695 if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
696 return ret;
700 /**********************************************************************
701 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
703 * RETURNS
704 * Success: TRUE
705 * Failure: FALSE
707 * FIXME
708 * check if lcid is a valid cp
710 BOOL WINAPI SetThreadLocale(
711 LCID lcid) /* [in] Locale identifier */
713 switch (lcid)
715 case LOCALE_SYSTEM_DEFAULT:
716 lcid = GetSystemDefaultLCID();
717 break;
718 case LOCALE_USER_DEFAULT:
719 case LOCALE_NEUTRAL:
720 lcid = GetUserDefaultLCID();
721 break;
723 NtCurrentTeb()->CurrentLocale = lcid;
724 return TRUE;
728 /***********************************************************************
729 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
731 * RETURNS
732 * Pseudohandle for the current thread
734 #undef GetCurrentThread
735 HANDLE WINAPI GetCurrentThread(void)
737 return 0xfffffffe;
741 /***********************************************************************
742 * ProcessIdToSessionId (KERNEL32)
743 * This function is available on Terminal Server 4SP4 and Windows 2000
745 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
747 /* According to MSDN, if the calling process is not in a terminal
748 * services environment, then the sessionid returned is zero.
750 *sessionid_ptr = 0;
751 return TRUE;
755 #ifdef __i386__
757 /* void WINAPI SetLastError( DWORD error ); */
758 __ASM_GLOBAL_FUNC( SetLastError,
759 "movl 4(%esp),%eax\n\t"
760 ".byte 0x64\n\t"
761 "movl %eax,0x60\n\t"
762 "ret $4" );
764 /* DWORD WINAPI GetLastError(void); */
765 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
767 /* DWORD WINAPI GetCurrentProcessId(void) */
768 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
770 /* DWORD WINAPI GetCurrentThreadId(void) */
771 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
773 #else /* __i386__ */
775 /**********************************************************************
776 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
778 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
780 NtCurrentTeb()->last_error = error;
783 /**********************************************************************
784 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
786 DWORD WINAPI GetLastError(void)
788 return NtCurrentTeb()->last_error;
791 /***********************************************************************
792 * GetCurrentProcessId [KERNEL32.199] Returns process identifier.
794 DWORD WINAPI GetCurrentProcessId(void)
796 return (DWORD)NtCurrentTeb()->pid;
799 /***********************************************************************
800 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
802 DWORD WINAPI GetCurrentThreadId(void)
804 return (DWORD)NtCurrentTeb()->tid;
807 #endif /* __i386__ */