Don't load user32 too early on for 16-bit apps, so that app-specific
[wine] / scheduler / thread.c
1 /*
2  * Win32 threads
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include "config.h"
8
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"
31
32 DEFAULT_DEBUG_CHANNEL(thread);
33
34 /* TEB of the initial thread */
35 static TEB initial_teb;
36
37 extern struct _PDB current_process;
38
39 /***********************************************************************
40  *           THREAD_IsWin16
41  */
42 BOOL THREAD_IsWin16( TEB *teb )
43 {
44     return !teb || !(teb->tibflags & TEBF_WIN32);
45 }
46
47 /***********************************************************************
48  *           THREAD_IdToTEB
49  *
50  * Convert a thread id to a TEB, making sure it is valid.
51  */
52 TEB *THREAD_IdToTEB( DWORD id )
53 {
54     TEB *ret = NULL;
55
56     if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
57
58     SERVER_START_REQ( get_thread_info )
59     {
60         req->handle = 0;
61         req->tid_in = (void *)id;
62         if (!SERVER_CALL()) ret = req->teb;
63     }
64     SERVER_END_REQ;
65
66     if (!ret)
67     {
68         /* Allow task handles to be used; convert to main thread */
69         if ( IsTask16( id ) )
70         {
71             TDB *pTask = (TDB *)GlobalLock16( id );
72             if (pTask) return pTask->teb;
73         }
74         SetLastError( ERROR_INVALID_PARAMETER );
75     }
76     return ret;
77 }
78
79
80 /***********************************************************************
81  *           THREAD_InitTEB
82  *
83  * Initialization of a newly created TEB.
84  */
85 static BOOL THREAD_InitTEB( TEB *teb )
86 {
87     teb->except    = (void *)~0UL;
88     teb->self      = teb;
89     teb->tibflags  = TEBF_WIN32;
90     teb->tls_ptr   = teb->tls_array;
91     teb->exit_code = STILL_ACTIVE;
92     teb->request_fd = -1;
93     teb->reply_fd   = -1;
94     teb->wait_fd[0] = -1;
95     teb->wait_fd[1] = -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);
101 }
102
103
104 /***********************************************************************
105  *           THREAD_FreeTEB
106  *
107  * Free data structures associated with a thread.
108  * Must be called from the context of another thread.
109  */
110 static void CALLBACK THREAD_FreeTEB( TEB *teb )
111 {
112     TRACE("(%p) called\n", teb );
113     if (teb->cleanup) SERVICE_Delete( teb->cleanup );
114
115     /* Free the associated memory */
116
117     close( teb->request_fd );
118     close( teb->reply_fd );
119     close( teb->wait_fd[0] );
120     close( teb->wait_fd[1] );
121     if (teb->stack_sel) FreeSelector16( teb->stack_sel );
122     FreeSelector16( teb->teb_sel );
123     if (teb->buffer) munmap( (void *)teb->buffer, teb->buffer_size );
124     if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
125     VirtualFree( teb->stack_base, 0, MEM_RELEASE );
126 }
127
128
129 /***********************************************************************
130  *           THREAD_InitStack
131  *
132  * Allocate the stack of a thread.
133  */
134 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size )
135 {
136     DWORD old_prot, total_size;
137     DWORD page_size = getpagesize();
138     void *base;
139
140     /* Allocate the stack */
141
142     if (stack_size >= 16*1024*1024)
143         WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
144
145     /* if size is smaller than default, get stack size from parent */
146     if (stack_size < 1024 * 1024)
147     {
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);
153     }
154
155     /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
156     stack_size += 64 * 1024;
157
158     /* Memory layout in allocated block:
159      *
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)
168      */
169
170     stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
171     total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
172     total_size += 0x10000; /* 16-bit stack */
173     if (!teb) total_size += page_size;
174
175     if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
176         return NULL;
177
178     if (!teb)
179     {
180         teb = (TEB *)((char *)base + total_size - page_size);
181         if (!THREAD_InitTEB( teb ))
182         {
183             VirtualFree( base, 0, MEM_RELEASE );
184             return NULL;
185         }
186     }
187
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;
192
193     /* Setup guard pages */
194
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 );
199
200     /* Allocate the 16-bit stack selector */
201
202     teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, WINE_LDT_FLAGS_DATA );
203     if (!teb->stack_sel) goto error;
204     teb->cur_stack = MAKESEGPTR( teb->stack_sel, 0x10000 - sizeof(STACK16FRAME) );
205
206     return teb;
207
208 error:
209     THREAD_FreeTEB( teb );
210     return NULL;
211 }
212
213
214 /***********************************************************************
215  *           thread_errno_location
216  *
217  * Get the per-thread errno location.
218  */
219 static int *thread_errno_location(void)
220 {
221     return &NtCurrentTeb()->thread_errno;
222 }
223
224 /***********************************************************************
225  *           thread_h_errno_location
226  *
227  * Get the per-thread h_errno location.
228  */
229 static int *thread_h_errno_location(void)
230 {
231     return &NtCurrentTeb()->thread_h_errno;
232 }
233
234 /***********************************************************************
235  *           THREAD_Init
236  *
237  * Setup the initial thread.
238  *
239  * NOTES: The first allocated TEB on NT is at 0x7ffde000.
240  */
241 void THREAD_Init(void)
242 {
243     if (!initial_teb.self)  /* do it only once */
244     {
245         THREAD_InitTEB( &initial_teb );
246         assert( initial_teb.teb_sel );
247         initial_teb.process = &current_process;
248         SYSDEPS_SetCurThread( &initial_teb );
249         wine_errno_location = thread_errno_location;
250         wine_h_errno_location = thread_h_errno_location;
251     }
252 }
253
254 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
255
256
257 /***********************************************************************
258  *           THREAD_Start
259  *
260  * Start execution of a newly created thread. Does not return.
261  */
262 static void THREAD_Start(void)
263 {
264     HANDLE cleanup_object;
265     LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
266
267     /* install cleanup handler */
268     if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
269                          GetCurrentProcess(), &cleanup_object, 
270                          0, FALSE, DUPLICATE_SAME_ACCESS ))
271         NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
272                                                      (ULONG_PTR)NtCurrentTeb() );
273
274     PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
275     PE_InitTls();
276     MODULE_DllThreadAttach( NULL );
277     ExitThread( func( NtCurrentTeb()->entry_arg ) );
278 }
279
280
281 /***********************************************************************
282  *           CreateThread   (KERNEL32.@)
283  */
284 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
285                             LPTHREAD_START_ROUTINE start, LPVOID param,
286                             DWORD flags, LPDWORD id )
287 {
288     HANDLE handle = 0;
289     TEB *teb;
290     void *tid = 0;
291     int request_pipe[2];
292
293     if (pipe( request_pipe ) == -1)
294     {
295         SetLastError( ERROR_TOO_MANY_OPEN_FILES );
296         return 0;
297     }
298     fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
299     wine_server_send_fd( request_pipe[0] );
300
301     SERVER_START_REQ( new_thread )
302     {
303         req->suspend    = ((flags & CREATE_SUSPENDED) != 0);
304         req->inherit    = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
305         req->request_fd = request_pipe[0];
306         if (!SERVER_CALL_ERR())
307         {
308             handle = req->handle;
309             tid = req->tid;
310         }
311         close( request_pipe[0] );
312     }
313     SERVER_END_REQ;
314
315     if (!handle || !(teb = THREAD_InitStack( NULL, stack )))
316     {
317         close( request_pipe[1] );
318         return 0;
319     }
320
321     teb->process     = NtCurrentTeb()->process;
322     teb->tid         = tid;
323     teb->request_fd  = request_pipe[1];
324     teb->entry_point = start;
325     teb->entry_arg   = param;
326     teb->startup     = THREAD_Start;
327     teb->htask16     = GetCurrentTask();
328
329     if (id) *id = (DWORD)tid;
330     if (SYSDEPS_SpawnThread( teb ) == -1)
331     {
332         CloseHandle( handle );
333         THREAD_FreeTEB( teb );
334         return 0;
335     }
336     return handle;
337 }
338
339 /***********************************************************************
340  *           CreateThread16   (KERNEL.441)
341  */
342 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
343 {
344     FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
345     DWORD     param = ((DWORD *)threadArgs)[1];
346     HeapFree( GetProcessHeap(), 0, threadArgs );
347
348     ((LPDWORD)CURRENT_STACK16)[-1] = param;
349     return wine_call_to_16_long( start, sizeof(DWORD) );
350 }
351 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
352                               FARPROC16 start, SEGPTR param,
353                               DWORD flags, LPDWORD id )
354 {
355     DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
356     if (!threadArgs) return INVALID_HANDLE_VALUE;
357     threadArgs[0] = (DWORD)start;
358     threadArgs[1] = (DWORD)param;
359
360     return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
361 }
362
363
364 /***********************************************************************
365  * ExitThread [KERNEL32.215]  Ends a thread
366  *
367  * RETURNS
368  *    None
369  */
370 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
371 {
372     BOOL last;
373     SERVER_START_REQ( terminate_thread )
374     {
375         /* send the exit code to the server */
376         req->handle    = GetCurrentThread();
377         req->exit_code = code;
378         SERVER_CALL();
379         last = req->last;
380     }
381     SERVER_END_REQ;
382
383     if (last)
384     {
385         MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
386         exit( code );
387     }
388     else
389     {
390         MODULE_DllThreadDetach( NULL );
391         if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
392         SYSDEPS_ExitThread( code );
393     }
394 }
395
396
397 /***********************************************************************
398  * SetThreadContext [KERNEL32.670]  Sets context of thread.
399  *
400  * RETURNS
401  *    Success: TRUE
402  *    Failure: FALSE
403  */
404 BOOL WINAPI SetThreadContext( HANDLE handle,           /* [in]  Handle to thread with context */
405                               const CONTEXT *context ) /* [in] Address of context structure */
406 {
407     BOOL ret;
408     SERVER_START_VAR_REQ( set_thread_context, sizeof(*context) )
409     {
410         req->handle = handle;
411         req->flags = context->ContextFlags;
412         memcpy( server_data_ptr(req), context, sizeof(*context) );
413         ret = !SERVER_CALL_ERR();
414     }
415     SERVER_END_VAR_REQ;
416     return ret;
417 }
418
419
420 /***********************************************************************
421  * GetThreadContext [KERNEL32.@]  Retrieves context of thread.
422  *
423  * RETURNS
424  *    Success: TRUE
425  *    Failure: FALSE
426  */
427 BOOL WINAPI GetThreadContext( HANDLE handle,     /* [in]  Handle to thread with context */
428                               CONTEXT *context ) /* [out] Address of context structure */
429 {
430     BOOL ret;
431     SERVER_START_VAR_REQ( get_thread_context, sizeof(*context) )
432     {
433         req->handle = handle;
434         req->flags = context->ContextFlags;
435         memcpy( server_data_ptr(req), context, sizeof(*context) );
436         if ((ret = !SERVER_CALL_ERR()))
437             memcpy( context, server_data_ptr(req), sizeof(*context) );
438     }
439     SERVER_END_VAR_REQ;
440     return ret;
441 }
442
443
444 /**********************************************************************
445  * GetThreadPriority [KERNEL32.@]  Returns priority for thread.
446  *
447  * RETURNS
448  *    Success: Thread's priority level.
449  *    Failure: THREAD_PRIORITY_ERROR_RETURN
450  */
451 INT WINAPI GetThreadPriority(
452     HANDLE hthread) /* [in] Handle to thread */
453 {
454     INT ret = THREAD_PRIORITY_ERROR_RETURN;
455     SERVER_START_REQ( get_thread_info )
456     {
457         req->handle = hthread;
458         req->tid_in = 0;
459         if (!SERVER_CALL_ERR()) ret = req->priority;
460     }
461     SERVER_END_REQ;
462     return ret;
463 }
464
465
466 /**********************************************************************
467  * SetThreadPriority [KERNEL32.@]  Sets priority for thread.
468  *
469  * RETURNS
470  *    Success: TRUE
471  *    Failure: FALSE
472  */
473 BOOL WINAPI SetThreadPriority(
474     HANDLE hthread, /* [in] Handle to thread */
475     INT priority)   /* [in] Thread priority level */
476 {
477     BOOL ret;
478     SERVER_START_REQ( set_thread_info )
479     {
480         req->handle   = hthread;
481         req->priority = priority;
482         req->mask     = SET_THREAD_INFO_PRIORITY;
483         ret = !SERVER_CALL_ERR();
484     }
485     SERVER_END_REQ;
486     return ret;
487 }
488
489
490 /**********************************************************************
491  * GetThreadPriorityBoost [KERNEL32.877]  Returns priority boost for thread.
492  *
493  * Always reports that priority boost is disabled.
494  *
495  * RETURNS
496  *    Success: TRUE.
497  *    Failure: FALSE
498  */
499 BOOL WINAPI GetThreadPriorityBoost(
500     HANDLE hthread, /* [in] Handle to thread */
501     PBOOL pstate)   /* [out] pointer to var that receives the boost state */
502 {
503     if (pstate) *pstate = FALSE;
504     return NO_ERROR;
505 }
506
507
508 /**********************************************************************
509  * SetThreadPriorityBoost [KERNEL32.893]  Sets priority boost for thread.
510  *
511  * Priority boost is not implemented. Thsi function always returns 
512  * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
513  *
514  * RETURNS
515  *    Always returns FALSE to indicate a failure 
516  */
517 BOOL WINAPI SetThreadPriorityBoost(
518     HANDLE hthread, /* [in] Handle to thread */
519     BOOL disable)   /* [in] TRUE to disable priority boost */
520 {
521     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
522     return FALSE;
523 }
524
525
526 /**********************************************************************
527  *           SetThreadAffinityMask   (KERNEL32.669)
528  */
529 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
530 {
531     DWORD ret;
532     SERVER_START_REQ( set_thread_info )
533     {
534         req->handle   = hThread;
535         req->affinity = dwThreadAffinityMask;
536         req->mask     = SET_THREAD_INFO_AFFINITY;
537         ret = !SERVER_CALL_ERR();
538         /* FIXME: should return previous value */
539     }
540     SERVER_END_REQ;
541     return ret;
542 }
543
544
545 /**********************************************************************
546  * TerminateThread [KERNEL32.685]  Terminates a thread
547  *
548  * RETURNS
549  *    Success: TRUE
550  *    Failure: FALSE
551  */
552 BOOL WINAPI TerminateThread( HANDLE handle,    /* [in] Handle to thread */
553                              DWORD exit_code)  /* [in] Exit code for thread */
554 {
555     NTSTATUS status = NtTerminateThread( handle, exit_code );
556     if (status) SetLastError( RtlNtStatusToDosError(status) );
557     return !status;
558 }
559
560
561 /**********************************************************************
562  *              GetExitCodeThread (KERNEL32.@)
563  *              GetExitCodeThread (WIN32S16.13)
564  *
565  * Gets termination status of thread.
566  * 
567  * RETURNS
568  *    Success: TRUE
569  *    Failure: FALSE
570  */
571 BOOL WINAPI GetExitCodeThread(
572     HANDLE hthread, /* [in]  Handle to thread */
573     LPDWORD exitcode) /* [out] Address to receive termination status */
574 {
575     BOOL ret;
576     SERVER_START_REQ( get_thread_info )
577     {
578         req->handle = hthread;
579         req->tid_in = 0;
580         ret = !SERVER_CALL_ERR();
581         if (ret && exitcode) *exitcode = req->exit_code;
582     }
583     SERVER_END_REQ;
584     return ret;
585 }
586
587
588 /**********************************************************************
589  * ResumeThread [KERNEL32.587]  Resumes a thread.
590  *
591  * Decrements a thread's suspend count.  When count is zero, the
592  * execution of the thread is resumed.
593  *
594  * RETURNS
595  *    Success: Previous suspend count
596  *    Failure: 0xFFFFFFFF
597  *    Already running: 0
598  */
599 DWORD WINAPI ResumeThread(
600     HANDLE hthread) /* [in] Identifies thread to restart */
601 {
602     DWORD ret = 0xffffffff;
603     SERVER_START_REQ( resume_thread )
604     {
605         req->handle = hthread;
606         if (!SERVER_CALL_ERR()) ret = req->count;
607     }
608     SERVER_END_REQ;
609     return ret;
610 }
611
612
613 /**********************************************************************
614  * SuspendThread [KERNEL32.681]  Suspends a thread.
615  *
616  * RETURNS
617  *    Success: Previous suspend count
618  *    Failure: 0xFFFFFFFF
619  */
620 DWORD WINAPI SuspendThread(
621     HANDLE hthread) /* [in] Handle to the thread */
622 {
623     DWORD ret = 0xffffffff;
624     SERVER_START_REQ( suspend_thread )
625     {
626         req->handle = hthread;
627         if (!SERVER_CALL_ERR()) ret = req->count;
628     }
629     SERVER_END_REQ;
630     return ret;
631 }
632
633
634 /***********************************************************************
635  *              QueueUserAPC  (KERNEL32.566)
636  */
637 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
638 {
639     DWORD ret;
640     SERVER_START_REQ( queue_apc )
641     {
642         req->handle = hthread;
643         req->user   = 1;
644         req->func   = func;
645         req->param  = (void *)data;
646         ret = !SERVER_CALL_ERR();
647     }
648     SERVER_END_REQ;
649     return ret;
650 }
651
652
653 /**********************************************************************
654  * GetThreadTimes [KERNEL32.@]  Obtains timing information.
655  *
656  * NOTES
657  *    What are the fields where these values are stored?
658  *
659  * RETURNS
660  *    Success: TRUE
661  *    Failure: FALSE
662  */
663 BOOL WINAPI GetThreadTimes( 
664     HANDLE thread,         /* [in]  Specifies the thread of interest */
665     LPFILETIME creationtime, /* [out] When the thread was created */
666     LPFILETIME exittime,     /* [out] When the thread was destroyed */
667     LPFILETIME kerneltime,   /* [out] Time thread spent in kernel mode */
668     LPFILETIME usertime)     /* [out] Time thread spent in user mode */
669 {
670     FIXME("(0x%08x): stub\n",thread);
671     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
672     return FALSE;
673 }
674
675
676 /**********************************************************************
677  * VWin32_BoostThreadGroup [KERNEL.535]
678  */
679 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
680 {
681     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
682 }
683
684 /**********************************************************************
685  * VWin32_BoostThreadStatic [KERNEL.536]
686  */
687 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
688 {
689     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
690 }
691
692
693 /***********************************************************************
694  *           GetThreadLocale    (KERNEL32.@)
695  */
696 LCID WINAPI GetThreadLocale(void)
697 {
698     LCID ret = NtCurrentTeb()->CurrentLocale;
699     if (!ret) NtCurrentTeb()->CurrentLocale = ret = GetUserDefaultLCID();
700     return ret;
701 }
702
703
704 /**********************************************************************
705  * SetThreadLocale [KERNEL32.671]  Sets the calling threads current locale.
706  *
707  * RETURNS
708  *    Success: TRUE
709  *    Failure: FALSE
710  *
711  * FIXME
712  *  check if lcid is a valid cp
713  */
714 BOOL WINAPI SetThreadLocale(
715     LCID lcid)     /* [in] Locale identifier */
716 {
717     switch (lcid)
718     {
719       case LOCALE_SYSTEM_DEFAULT:
720         lcid = GetSystemDefaultLCID();
721         break;
722       case LOCALE_USER_DEFAULT:
723       case LOCALE_NEUTRAL:
724         lcid = GetUserDefaultLCID();
725         break;
726     }
727     NtCurrentTeb()->CurrentLocale = lcid;
728     return TRUE;
729 }
730
731
732 /***********************************************************************
733  * GetCurrentThread [KERNEL32.@]  Gets pseudohandle for current thread
734  *
735  * RETURNS
736  *    Pseudohandle for the current thread
737  */
738 #undef GetCurrentThread
739 HANDLE WINAPI GetCurrentThread(void)
740 {
741     return 0xfffffffe;
742 }
743
744
745 /***********************************************************************
746  * ProcessIdToSessionId   (KERNEL32.@)
747  * This function is available on Terminal Server 4SP4 and Windows 2000
748  */
749 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
750 {
751         /* According to MSDN, if the calling process is not in a terminal
752          * services environment, then the sessionid returned is zero.
753          */
754         *sessionid_ptr = 0;
755         return TRUE;
756 }
757
758 /***********************************************************************
759  * SetThreadExecutionState (KERNEL32.@)
760  *
761  * Informs the system that activity is taking place for
762  * power management purposes.
763  */ 
764 EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE flags)
765 {
766     FIXME("(%ld): stub\n", flags);
767     return 0;
768 }
769
770
771 #ifdef __i386__
772
773 /* void WINAPI SetLastError( DWORD error ); */
774 __ASM_GLOBAL_FUNC( SetLastError,
775                    "movl 4(%esp),%eax\n\t"
776                    ".byte 0x64\n\t"
777                    "movl %eax,0x60\n\t"
778                    "ret $4" );
779
780 /* DWORD WINAPI GetLastError(void); */
781 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
782
783 /* DWORD WINAPI GetCurrentProcessId(void) */
784 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
785                    
786 /* DWORD WINAPI GetCurrentThreadId(void) */
787 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
788                    
789 #else  /* __i386__ */
790
791 /**********************************************************************
792  *              SetLastError (KERNEL.147)
793  *              SetLastError (KERNEL32.@)
794  *
795  * Sets the last-error code.
796  */
797 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
798 {
799     NtCurrentTeb()->last_error = error;
800 }
801
802 /**********************************************************************
803  *              GetLastError (KERNEL.10)
804  *              GetLastError (KERNEL32.@)
805  *              GetLastError (WIN32S16.10)
806  *
807  * Returns last-error code.
808  */
809 DWORD WINAPI GetLastError(void)
810 {
811     return NtCurrentTeb()->last_error;
812 }
813
814 /***********************************************************************
815  *              GetCurrentProcessId (KERNEL.471)
816  *              GetCurrentProcessId (KERNEL32.@)
817  *
818  * Returns process identifier.
819  */
820 DWORD WINAPI GetCurrentProcessId(void)
821 {
822     return (DWORD)NtCurrentTeb()->pid;
823 }
824
825 /***********************************************************************
826  *              GetCurrentThreadId (KERNEL.462)
827  *              GetCurrentThreadId (KERNEL32.@)
828  *
829  * Returns thread identifier.
830  */
831 DWORD WINAPI GetCurrentThreadId(void)
832 {
833     return (DWORD)NtCurrentTeb()->tid;
834 }
835
836 #endif  /* __i386__ */