Added --with-nptl configure option and necessary changes to support
[wine] / scheduler / thread.c
1 /*
2  * Win32 threads
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/winbase16.h"
37 #include "thread.h"
38 #include "task.h"
39 #include "module.h"
40 #include "winerror.h"
41 #include "selectors.h"
42 #include "winnt.h"
43 #include "wine/server.h"
44 #include "stackframe.h"
45 #include "wine/debug.h"
46 #include "winnls.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(thread);
49 WINE_DECLARE_DEBUG_CHANNEL(relay);
50
51 /* TEB of the initial thread */
52 static TEB initial_teb;
53
54 extern struct _PDB current_process;
55
56 /***********************************************************************
57  *           THREAD_IdToTEB
58  *
59  * Convert a thread id to a TEB, making sure it is valid.
60  */
61 TEB *THREAD_IdToTEB( DWORD id )
62 {
63     TEB *ret = NULL;
64
65     if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
66
67     SERVER_START_REQ( get_thread_info )
68     {
69         req->handle = 0;
70         req->tid_in = id;
71         if (!wine_server_call( req )) ret = reply->teb;
72     }
73     SERVER_END_REQ;
74
75     if (!ret)
76     {
77         /* Allow task handles to be used; convert to main thread */
78         if ( IsTask16( id ) )
79         {
80             TDB *pTask = TASK_GetPtr( id );
81             if (pTask) return pTask->teb;
82         }
83         SetLastError( ERROR_INVALID_PARAMETER );
84     }
85     return ret;
86 }
87
88
89 /***********************************************************************
90  *           THREAD_InitTEB
91  *
92  * Initialization of a newly created TEB.
93  */
94 static BOOL THREAD_InitTEB( TEB *teb )
95 {
96     teb->except    = (void *)~0UL;
97     teb->self      = teb;
98     teb->tibflags  = TEBF_WIN32;
99     teb->tls_ptr   = teb->tls_array;
100     teb->exit_code = STILL_ACTIVE;
101     teb->request_fd = -1;
102     teb->reply_fd   = -1;
103     teb->wait_fd[0] = -1;
104     teb->wait_fd[1] = -1;
105     teb->stack_top  = (void *)~0UL;
106     teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
107     teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
108     teb->teb_sel = wine_ldt_alloc_fs();
109     return (teb->teb_sel != 0);
110 }
111
112
113 /***********************************************************************
114  *           THREAD_FreeTEB
115  *
116  * Free data structures associated with a thread.
117  * Must be called from the context of another thread.
118  */
119 static void THREAD_FreeTEB( TEB *teb )
120 {
121     TRACE("(%p) called\n", teb );
122     /* Free the associated memory */
123     wine_ldt_free_entries( teb->stack_sel, 1 );
124     wine_ldt_free_fs( teb->teb_sel );
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      * 1 page              debug info (except for initial thread)
169      */
170
171     stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
172     total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
173     total_size += 0x10000; /* 16-bit stack */
174     if (!teb) total_size += 2 * page_size;
175
176     if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
177         return NULL;
178
179     if (!teb)
180     {
181         teb = (TEB *)((char *)base + total_size - 2 * page_size);
182         if (!THREAD_InitTEB( teb )) goto error;
183         teb->debug_info = (char *)teb + page_size;
184     }
185
186     teb->stack_low    = base;
187     teb->stack_base   = base;
188     teb->signal_stack = (char *)base + page_size;
189     teb->stack_top    = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
190
191     /* Setup guard pages */
192
193     VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
194     VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
195     VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
196                     PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
197
198     /* Allocate the 16-bit stack selector */
199
200     teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, WINE_LDT_FLAGS_DATA );
201     if (!teb->stack_sel) goto error;
202     teb->cur_stack = MAKESEGPTR( teb->stack_sel, 0x10000 - sizeof(STACK16FRAME) );
203
204     return teb;
205
206 error:
207     wine_ldt_free_fs( teb->teb_sel );
208     VirtualFree( base, 0, MEM_RELEASE );
209     return NULL;
210 }
211
212
213 /***********************************************************************
214  *           THREAD_Init
215  *
216  * Setup the initial thread.
217  *
218  * NOTES: The first allocated TEB on NT is at 0x7ffde000.
219  */
220 void THREAD_Init(void)
221 {
222     if (!initial_teb.self)  /* do it only once */
223     {
224         THREAD_InitTEB( &initial_teb );
225         assert( initial_teb.teb_sel );
226         initial_teb.process = &current_process;
227         SYSDEPS_SetCurThread( &initial_teb );
228         SYSDEPS_InitErrno();
229     }
230 }
231
232 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
233
234
235 /***********************************************************************
236  *           THREAD_Start
237  *
238  * Start execution of a newly created thread. Does not return.
239  */
240 static void THREAD_Start(void)
241 {
242     LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
243
244     if (TRACE_ON(relay))
245         DPRINTF("%04lx:Starting thread (entryproc=%p)\n", GetCurrentThreadId(), func );
246
247     PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
248     MODULE_DllThreadAttach( NULL );
249     ExitThread( func( NtCurrentTeb()->entry_arg ) );
250 }
251
252
253 /***********************************************************************
254  *           CreateThread   (KERNEL32.@)
255  */
256 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, SIZE_T stack,
257                             LPTHREAD_START_ROUTINE start, LPVOID param,
258                             DWORD flags, LPDWORD id )
259 {
260     HANDLE handle = 0;
261     TEB *teb;
262     DWORD tid = 0;
263     int request_pipe[2];
264
265     if (pipe( request_pipe ) == -1)
266     {
267         SetLastError( ERROR_TOO_MANY_OPEN_FILES );
268         return 0;
269     }
270     fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
271     wine_server_send_fd( request_pipe[0] );
272
273     SERVER_START_REQ( new_thread )
274     {
275         req->suspend    = ((flags & CREATE_SUSPENDED) != 0);
276         req->inherit    = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
277         req->request_fd = request_pipe[0];
278         if (!wine_server_call_err( req ))
279         {
280             handle = reply->handle;
281             tid = reply->tid;
282         }
283         close( request_pipe[0] );
284     }
285     SERVER_END_REQ;
286
287     if (!handle || !(teb = THREAD_InitStack( NULL, stack )))
288     {
289         close( request_pipe[1] );
290         return 0;
291     }
292
293     teb->process     = NtCurrentTeb()->process;
294     teb->tid         = tid;
295     teb->request_fd  = request_pipe[1];
296     teb->entry_point = start;
297     teb->entry_arg   = param;
298     teb->startup     = THREAD_Start;
299     teb->htask16     = GetCurrentTask();
300
301     if (id) *id = tid;
302     if (SYSDEPS_SpawnThread( teb ) == -1)
303     {
304         CloseHandle( handle );
305         close( request_pipe[1] );
306         THREAD_FreeTEB( teb );
307         return 0;
308     }
309     return handle;
310 }
311
312 /***********************************************************************
313  * ExitThread [KERNEL32.@]  Ends a thread
314  *
315  * RETURNS
316  *    None
317  */
318 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
319 {
320     BOOL last;
321     SERVER_START_REQ( terminate_thread )
322     {
323         /* send the exit code to the server */
324         req->handle    = GetCurrentThread();
325         req->exit_code = code;
326         wine_server_call( req );
327         last = reply->last;
328     }
329     SERVER_END_REQ;
330
331     if (last)
332     {
333         LdrShutdownProcess();
334         exit( code );
335     }
336     else
337     {
338         LdrShutdownThread();
339         if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_ExitTask();
340         SYSDEPS_ExitThread( code );
341     }
342 }
343
344 /***********************************************************************
345  * OpenThread Retrieves a handle to a thread from its thread id
346  *
347  * RETURNS
348  *    None
349  */
350 HANDLE WINAPI OpenThread( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwThreadId )
351 {
352     HANDLE ret = 0;
353     SERVER_START_REQ( open_thread )
354     {
355         req->tid     = dwThreadId;
356         req->access  = dwDesiredAccess;
357         req->inherit = bInheritHandle;
358         if (!wine_server_call_err( req )) ret = reply->handle;
359     }
360     SERVER_END_REQ;
361     return ret;
362 }
363
364 /***********************************************************************
365  * SetThreadContext [KERNEL32.@]  Sets context of thread.
366  *
367  * RETURNS
368  *    Success: TRUE
369  *    Failure: FALSE
370  */
371 BOOL WINAPI SetThreadContext( HANDLE handle,           /* [in]  Handle to thread with context */
372                               const CONTEXT *context ) /* [in] Address of context structure */
373 {
374     BOOL ret;
375     SERVER_START_REQ( set_thread_context )
376     {
377         req->handle = handle;
378         req->flags = context->ContextFlags;
379         wine_server_add_data( req, context, sizeof(*context) );
380         ret = !wine_server_call_err( req );
381     }
382     SERVER_END_REQ;
383     return ret;
384 }
385
386
387 /***********************************************************************
388  * GetThreadContext [KERNEL32.@]  Retrieves context of thread.
389  *
390  * RETURNS
391  *    Success: TRUE
392  *    Failure: FALSE
393  */
394 BOOL WINAPI GetThreadContext( HANDLE handle,     /* [in]  Handle to thread with context */
395                               CONTEXT *context ) /* [out] Address of context structure */
396 {
397     BOOL ret;
398     SERVER_START_REQ( get_thread_context )
399     {
400         req->handle = handle;
401         req->flags = context->ContextFlags;
402         wine_server_add_data( req, context, sizeof(*context) );
403         wine_server_set_reply( req, context, sizeof(*context) );
404         ret = !wine_server_call_err( req );
405     }
406     SERVER_END_REQ;
407     return ret;
408 }
409
410
411 /**********************************************************************
412  * GetThreadPriority [KERNEL32.@]  Returns priority for thread.
413  *
414  * RETURNS
415  *    Success: Thread's priority level.
416  *    Failure: THREAD_PRIORITY_ERROR_RETURN
417  */
418 INT WINAPI GetThreadPriority(
419     HANDLE hthread) /* [in] Handle to thread */
420 {
421     INT ret = THREAD_PRIORITY_ERROR_RETURN;
422     SERVER_START_REQ( get_thread_info )
423     {
424         req->handle = hthread;
425         req->tid_in = 0;
426         if (!wine_server_call_err( req )) ret = reply->priority;
427     }
428     SERVER_END_REQ;
429     return ret;
430 }
431
432
433 /**********************************************************************
434  * SetThreadPriority [KERNEL32.@]  Sets priority for thread.
435  *
436  * RETURNS
437  *    Success: TRUE
438  *    Failure: FALSE
439  */
440 BOOL WINAPI SetThreadPriority(
441     HANDLE hthread, /* [in] Handle to thread */
442     INT priority)   /* [in] Thread priority level */
443 {
444     BOOL ret;
445     SERVER_START_REQ( set_thread_info )
446     {
447         req->handle   = hthread;
448         req->priority = priority;
449         req->mask     = SET_THREAD_INFO_PRIORITY;
450         ret = !wine_server_call_err( req );
451     }
452     SERVER_END_REQ;
453     return ret;
454 }
455
456
457 /**********************************************************************
458  * GetThreadPriorityBoost [KERNEL32.@]  Returns priority boost for thread.
459  *
460  * Always reports that priority boost is disabled.
461  *
462  * RETURNS
463  *    Success: TRUE.
464  *    Failure: FALSE
465  */
466 BOOL WINAPI GetThreadPriorityBoost(
467     HANDLE hthread, /* [in] Handle to thread */
468     PBOOL pstate)   /* [out] pointer to var that receives the boost state */
469 {
470     if (pstate) *pstate = FALSE;
471     return NO_ERROR;
472 }
473
474
475 /**********************************************************************
476  * SetThreadPriorityBoost [KERNEL32.@]  Sets priority boost for thread.
477  *
478  * Priority boost is not implemented. Thsi function always returns
479  * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
480  *
481  * RETURNS
482  *    Always returns FALSE to indicate a failure
483  */
484 BOOL WINAPI SetThreadPriorityBoost(
485     HANDLE hthread, /* [in] Handle to thread */
486     BOOL disable)   /* [in] TRUE to disable priority boost */
487 {
488     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
489     return FALSE;
490 }
491
492
493 /**********************************************************************
494  *           SetThreadAffinityMask   (KERNEL32.@)
495  */
496 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
497 {
498     DWORD ret;
499     SERVER_START_REQ( set_thread_info )
500     {
501         req->handle   = hThread;
502         req->affinity = dwThreadAffinityMask;
503         req->mask     = SET_THREAD_INFO_AFFINITY;
504         ret = !wine_server_call_err( req );
505         /* FIXME: should return previous value */
506     }
507     SERVER_END_REQ;
508     return ret;
509 }
510
511 /**********************************************************************
512  * SetThreadIdealProcessor [KERNEL32.@]  Obtains timing information.
513  *
514  * RETURNS
515  *    Success: Value of last call to SetThreadIdealProcessor
516  *    Failure: -1
517  */
518 DWORD WINAPI SetThreadIdealProcessor(
519     HANDLE hThread,          /* [in] Specifies the thread of interest */
520     DWORD dwIdealProcessor)  /* [in] Specifies the new preferred processor */
521 {
522     FIXME("(%p): stub\n",hThread);
523     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
524     return -1L;
525 }
526
527 /**********************************************************************
528  * TerminateThread [KERNEL32.@]  Terminates a thread
529  *
530  * RETURNS
531  *    Success: TRUE
532  *    Failure: FALSE
533  */
534 BOOL WINAPI TerminateThread( HANDLE handle,    /* [in] Handle to thread */
535                              DWORD exit_code)  /* [in] Exit code for thread */
536 {
537     NTSTATUS status = NtTerminateThread( handle, exit_code );
538     if (status) SetLastError( RtlNtStatusToDosError(status) );
539     return !status;
540 }
541
542
543 /**********************************************************************
544  *              GetExitCodeThread (KERNEL32.@)
545  *
546  * Gets termination status of thread.
547  *
548  * RETURNS
549  *    Success: TRUE
550  *    Failure: FALSE
551  */
552 BOOL WINAPI GetExitCodeThread(
553     HANDLE hthread, /* [in]  Handle to thread */
554     LPDWORD exitcode) /* [out] Address to receive termination status */
555 {
556     BOOL ret;
557     SERVER_START_REQ( get_thread_info )
558     {
559         req->handle = hthread;
560         req->tid_in = 0;
561         ret = !wine_server_call_err( req );
562         if (ret && exitcode) *exitcode = reply->exit_code;
563     }
564     SERVER_END_REQ;
565     return ret;
566 }
567
568
569 /**********************************************************************
570  * ResumeThread [KERNEL32.@]  Resumes a thread.
571  *
572  * Decrements a thread's suspend count.  When count is zero, the
573  * execution of the thread is resumed.
574  *
575  * RETURNS
576  *    Success: Previous suspend count
577  *    Failure: 0xFFFFFFFF
578  *    Already running: 0
579  */
580 DWORD WINAPI ResumeThread(
581     HANDLE hthread) /* [in] Identifies thread to restart */
582 {
583     DWORD ret = 0xffffffff;
584     SERVER_START_REQ( resume_thread )
585     {
586         req->handle = hthread;
587         if (!wine_server_call_err( req )) ret = reply->count;
588     }
589     SERVER_END_REQ;
590     return ret;
591 }
592
593
594 /**********************************************************************
595  * SuspendThread [KERNEL32.@]  Suspends a thread.
596  *
597  * RETURNS
598  *    Success: Previous suspend count
599  *    Failure: 0xFFFFFFFF
600  */
601 DWORD WINAPI SuspendThread(
602     HANDLE hthread) /* [in] Handle to the thread */
603 {
604     DWORD ret = 0xffffffff;
605     SERVER_START_REQ( suspend_thread )
606     {
607         req->handle = hthread;
608         if (!wine_server_call_err( req )) ret = reply->count;
609     }
610     SERVER_END_REQ;
611     return ret;
612 }
613
614
615 /***********************************************************************
616  *              QueueUserAPC  (KERNEL32.@)
617  */
618 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
619 {
620     DWORD ret;
621     SERVER_START_REQ( queue_apc )
622     {
623         req->handle = hthread;
624         req->user   = 1;
625         req->func   = func;
626         req->param  = (void *)data;
627         ret = !wine_server_call_err( req );
628     }
629     SERVER_END_REQ;
630     return ret;
631 }
632
633
634 /**********************************************************************
635  * GetThreadTimes [KERNEL32.@]  Obtains timing information.
636  *
637  * RETURNS
638  *    Success: TRUE
639  *    Failure: FALSE
640  */
641 BOOL WINAPI GetThreadTimes(
642     HANDLE thread,         /* [in]  Specifies the thread of interest */
643     LPFILETIME creationtime, /* [out] When the thread was created */
644     LPFILETIME exittime,     /* [out] When the thread was destroyed */
645     LPFILETIME kerneltime,   /* [out] Time thread spent in kernel mode */
646     LPFILETIME usertime)     /* [out] Time thread spent in user mode */
647 {
648     BOOL ret = TRUE;
649
650     if (creationtime || exittime)
651     {
652         /* We need to do a server call to get the creation time or exit time */
653         /* This works on any thread */
654
655         SERVER_START_REQ( get_thread_info )
656         {
657             req->handle = thread;
658             req->tid_in = 0;
659             if ((ret = !wine_server_call_err( req )))
660             {
661                 if (creationtime)
662                     RtlSecondsSince1970ToTime( reply->creation_time, (LARGE_INTEGER*)creationtime );
663                 if (exittime)
664                     RtlSecondsSince1970ToTime( reply->exit_time, (LARGE_INTEGER*)exittime );
665             }
666         }
667         SERVER_END_REQ;
668     }
669     if (ret && (kerneltime || usertime))
670     {
671         /* We call times(2) for kernel time or user time */
672         /* We can only (portably) do this for the current thread */
673         if (thread == GetCurrentThread())
674         {
675             ULONGLONG time;
676             struct tms time_buf;
677             long clocks_per_sec = sysconf(_SC_CLK_TCK);
678
679             times(&time_buf);
680             if (kerneltime)
681             {
682                 time = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
683                 kerneltime->dwHighDateTime = time >> 32;
684                 kerneltime->dwLowDateTime = (DWORD)time;
685             }
686             if (usertime)
687             {
688                 time = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
689                 usertime->dwHighDateTime = time >> 32;
690                 usertime->dwLowDateTime = (DWORD)time;
691             }
692         }
693         else
694         {
695             if (kerneltime) kerneltime->dwHighDateTime = kerneltime->dwLowDateTime = 0;
696             if (usertime) usertime->dwHighDateTime = usertime->dwLowDateTime = 0;
697             FIXME("Cannot get kerneltime or usertime of other threads\n");
698         }
699     }
700     return ret;
701 }
702
703
704 /**********************************************************************
705  * VWin32_BoostThreadGroup [KERNEL.535]
706  */
707 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
708 {
709     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
710 }
711
712 /**********************************************************************
713  * VWin32_BoostThreadStatic [KERNEL.536]
714  */
715 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
716 {
717     FIXME("(0x%08lx,%d): stub\n", threadId, boost);
718 }
719
720
721 /***********************************************************************
722  * GetCurrentThread [KERNEL32.@]  Gets pseudohandle for current thread
723  *
724  * RETURNS
725  *    Pseudohandle for the current thread
726  */
727 #undef GetCurrentThread
728 HANDLE WINAPI GetCurrentThread(void)
729 {
730     return (HANDLE)0xfffffffe;
731 }
732
733
734 /***********************************************************************
735  * ProcessIdToSessionId   (KERNEL32.@)
736  * This function is available on Terminal Server 4SP4 and Windows 2000
737  */
738 BOOL WINAPI ProcessIdToSessionId( DWORD procid, DWORD *sessionid_ptr )
739 {
740         /* According to MSDN, if the calling process is not in a terminal
741          * services environment, then the sessionid returned is zero.
742          */
743         *sessionid_ptr = 0;
744         return TRUE;
745 }
746
747 /***********************************************************************
748  * SetThreadExecutionState (KERNEL32.@)
749  *
750  * Informs the system that activity is taking place for
751  * power management purposes.
752  */
753 EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE flags)
754 {
755     static EXECUTION_STATE current =
756             ES_SYSTEM_REQUIRED|ES_DISPLAY_REQUIRED|ES_USER_PRESENT;
757     EXECUTION_STATE old = current;
758
759     if (!(current & ES_CONTINUOUS) || (flags & ES_CONTINUOUS))
760         current = flags;
761     FIXME("(0x%lx): stub, harmless (power management).\n", flags);
762     return old;
763 }
764
765
766 #ifdef __i386__
767
768 /***********************************************************************
769  *              SetLastError (KERNEL.147)
770  *              SetLastError (KERNEL32.@)
771  */
772 /* void WINAPI SetLastError( DWORD error ); */
773 __ASM_GLOBAL_FUNC( SetLastError,
774                    "movl 4(%esp),%eax\n\t"
775                    ".byte 0x64\n\t"
776                    "movl %eax,0x60\n\t"
777                    "ret $4" );
778
779 /***********************************************************************
780  *              GetLastError (KERNEL.148)
781  *              GetLastError (KERNEL32.@)
782  */
783 /* DWORD WINAPI GetLastError(void); */
784 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
785
786 /***********************************************************************
787  *              GetCurrentProcessId (KERNEL.471)
788  *              GetCurrentProcessId (KERNEL32.@)
789  */
790 /* DWORD WINAPI GetCurrentProcessId(void) */
791 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
792
793 /***********************************************************************
794  *              GetCurrentThreadId (KERNEL.462)
795  *              GetCurrentThreadId (KERNEL32.@)
796  */
797 /* DWORD WINAPI GetCurrentThreadId(void) */
798 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
799
800 #else  /* __i386__ */
801
802 /**********************************************************************
803  *              SetLastError (KERNEL.147)
804  *              SetLastError (KERNEL32.@)
805  *
806  * Sets the last-error code.
807  */
808 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
809 {
810     NtCurrentTeb()->last_error = error;
811 }
812
813 /**********************************************************************
814  *              GetLastError (KERNEL.148)
815  *              GetLastError (KERNEL32.@)
816  *
817  * Returns last-error code.
818  */
819 DWORD WINAPI GetLastError(void)
820 {
821     return NtCurrentTeb()->last_error;
822 }
823
824 /***********************************************************************
825  *              GetCurrentProcessId (KERNEL.471)
826  *              GetCurrentProcessId (KERNEL32.@)
827  *
828  * Returns process identifier.
829  */
830 DWORD WINAPI GetCurrentProcessId(void)
831 {
832     return (DWORD)NtCurrentTeb()->pid;
833 }
834
835 /***********************************************************************
836  *              GetCurrentThreadId (KERNEL.462)
837  *              GetCurrentThreadId (KERNEL32.@)
838  *
839  * Returns thread identifier.
840  */
841 DWORD WINAPI GetCurrentThreadId(void)
842 {
843     return NtCurrentTeb()->tid;
844 }
845
846 #endif  /* __i386__ */