Improved Winelib apps initialisation code. No longer need to link
[wine.git] / scheduler / thread.c
blob046ae0438fde61a613a9ddd7bedd780760159836
1 /*
2 * Win32 threads
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #ifdef HAVE_SYS_MMAN_H
13 #include <sys/mman.h>
14 #endif
15 #include <unistd.h>
16 #include "wine/winbase16.h"
17 #include "thread.h"
18 #include "process.h"
19 #include "task.h"
20 #include "module.h"
21 #include "global.h"
22 #include "user.h"
23 #include "winerror.h"
24 #include "heap.h"
25 #include "selectors.h"
26 #include "winnt.h"
27 #include "server.h"
28 #include "services.h"
29 #include "stackframe.h"
30 #include "builtin16.h"
31 #include "debugtools.h"
32 #include "queue.h"
33 #include "hook.h"
35 DEFAULT_DEBUG_CHANNEL(thread);
37 /* TEB of the initial thread */
38 static TEB initial_teb;
40 /***********************************************************************
41 * THREAD_IsWin16
43 BOOL THREAD_IsWin16( TEB *teb )
45 return !teb || !(teb->tibflags & TEBF_WIN32);
48 /***********************************************************************
49 * THREAD_IdToTEB
51 * Convert a thread id to a TEB, making sure it is valid.
53 TEB *THREAD_IdToTEB( DWORD id )
55 struct get_thread_info_request *req = get_req_buffer();
57 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
58 req->handle = -1;
59 req->tid_in = (void *)id;
60 if (!server_call_noerr( REQ_GET_THREAD_INFO )) return req->teb;
62 /* Allow task handles to be used; convert to main thread */
63 if ( IsTask16( id ) )
65 TDB *pTask = (TDB *)GlobalLock16( id );
66 if (pTask) return pTask->teb;
68 SetLastError( ERROR_INVALID_PARAMETER );
69 return NULL;
73 /***********************************************************************
74 * THREAD_InitTEB
76 * Initialization of a newly created TEB.
78 static BOOL THREAD_InitTEB( TEB *teb, PDB *pdb )
80 teb->except = (void *)~0UL;
81 teb->htask16 = pdb->task;
82 teb->self = teb;
83 teb->tibflags = (pdb->flags & PDB32_WIN16_PROC) ? 0 : TEBF_WIN32;
84 teb->tls_ptr = teb->tls_array;
85 teb->process = pdb;
86 teb->exit_code = STILL_ACTIVE;
87 teb->socket = -1;
88 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
89 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
90 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
91 return (teb->teb_sel != 0);
95 /***********************************************************************
96 * THREAD_FreeTEB
98 * Free data structures associated with a thread.
99 * Must be called from the context of another thread.
101 static void CALLBACK THREAD_FreeTEB( TEB *teb )
103 TRACE("(%p) called\n", teb );
104 if (teb->cleanup) SERVICE_Delete( teb->cleanup );
106 /* Free the associated memory */
108 if (teb->socket != -1) close( teb->socket );
109 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
110 SELECTOR_FreeBlock( teb->teb_sel, 1 );
111 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
112 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
113 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
117 /***********************************************************************
118 * THREAD_InitStack
120 * Allocate the stack of a thread.
122 TEB *THREAD_InitStack( TEB *teb, PDB *pdb, DWORD stack_size, BOOL alloc_stack16 )
124 DWORD old_prot, total_size;
125 DWORD page_size = VIRTUAL_GetPageSize();
126 void *base;
128 /* Allocate the stack */
130 if (stack_size >= 16*1024*1024)
131 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
133 /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
134 stack_size += 64 * 1024;
136 /* Memory layout in allocated block:
138 * size contents
139 * 1 page NOACCESS guard page
140 * SIGNAL_STACK_SIZE signal stack
141 * 1 page NOACCESS guard page
142 * 1 page PAGE_GUARD guard page
143 * stack_size normal stack
144 * 64Kb 16-bit stack (optional)
145 * 1 page TEB (except for initial thread)
148 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
149 total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
150 if (alloc_stack16) total_size += 0x10000;
151 if (!teb) total_size += page_size;
153 if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
154 return NULL;
156 if (!teb)
158 teb = (TEB *)((char *)base + total_size - page_size);
159 if (!THREAD_InitTEB( teb, pdb ))
161 VirtualFree( base, 0, MEM_RELEASE );
162 return NULL;
166 teb->stack_low = base;
167 teb->stack_base = base;
168 teb->signal_stack = (char *)base + page_size;
169 teb->stack_top = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
171 /* Setup guard pages */
173 VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
174 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
175 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
176 PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
178 /* Allocate the 16-bit stack selector */
180 if (alloc_stack16)
182 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
183 FALSE, FALSE );
184 if (!teb->stack_sel) goto error;
185 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
186 0x10000 - sizeof(STACK16FRAME) );
188 return teb;
190 error:
191 THREAD_FreeTEB( teb );
192 return NULL;
196 /***********************************************************************
197 * THREAD_CreateInitialThread
199 * Create the initial thread.
201 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
203 TEB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
205 if (!THREAD_InitTEB( &initial_teb, pdb )) return NULL;
206 SYSDEPS_SetCurThread( &initial_teb );
207 initial_teb.socket = server_fd;
209 if (CLIENT_InitThread()) return NULL;
210 return THREAD_InitStack( &initial_teb, pdb, 0, TRUE );
214 /***********************************************************************
215 * THREAD_Create
218 TEB *THREAD_Create( PDB *pdb, void *pid, void *tid, int fd,
219 DWORD stack_size, BOOL alloc_stack16 )
221 TEB *teb;
223 if ((teb = THREAD_InitStack( NULL, pdb, stack_size, alloc_stack16 )))
225 teb->pid = pid;
226 teb->tid = tid;
227 teb->socket = fd;
228 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
229 TRACE("(%p) succeeded\n", teb);
231 return teb;
235 /***********************************************************************
236 * THREAD_Start
238 * Start execution of a newly created thread. Does not return.
240 static void THREAD_Start(void)
242 HANDLE cleanup_object;
243 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
245 /* install cleanup handler */
246 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
247 GetCurrentProcess(), &cleanup_object,
248 0, FALSE, DUPLICATE_SAME_ACCESS ))
249 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
250 (ULONG_PTR)NtCurrentTeb() );
252 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
253 PE_InitTls();
254 MODULE_DllThreadAttach( NULL );
255 ExitThread( func( NtCurrentTeb()->entry_arg ) );
259 /***********************************************************************
260 * CreateThread (KERNEL32.63)
262 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
263 LPTHREAD_START_ROUTINE start, LPVOID param,
264 DWORD flags, LPDWORD id )
266 struct new_thread_request *req = get_req_buffer();
267 int socket, handle = -1;
268 TEB *teb;
270 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
271 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
272 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
273 handle = req->handle;
275 if (!(teb = THREAD_Create( PROCESS_Current(), (void *)GetCurrentProcessId(),
276 req->tid, socket, stack, TRUE )))
278 close( socket );
279 return 0;
281 teb->tibflags |= TEBF_WIN32;
282 teb->entry_point = start;
283 teb->entry_arg = param;
284 teb->startup = THREAD_Start;
285 if (id) *id = (DWORD)teb->tid;
286 if (SYSDEPS_SpawnThread( teb ) == -1)
288 CloseHandle( handle );
289 return 0;
291 return handle;
294 /***********************************************************************
295 * CreateThread16 (KERNEL.441)
297 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
299 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
300 DWORD param = ((DWORD *)threadArgs)[1];
301 HeapFree( GetProcessHeap(), 0, threadArgs );
303 ((LPDWORD)CURRENT_STACK16)[-1] = param;
304 return CallTo16Long( start, sizeof(DWORD) );
306 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
307 FARPROC16 start, SEGPTR param,
308 DWORD flags, LPDWORD id )
310 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
311 if (!threadArgs) return INVALID_HANDLE_VALUE;
312 threadArgs[0] = (DWORD)start;
313 threadArgs[1] = (DWORD)param;
315 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
319 /***********************************************************************
320 * ExitThread [KERNEL32.215] Ends a thread
322 * RETURNS
323 * None
325 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
327 struct terminate_thread_request *req = get_req_buffer();
329 /* send the exit code to the server */
330 req->handle = GetCurrentThread();
331 req->exit_code = code;
332 server_call( REQ_TERMINATE_THREAD );
333 if (req->last)
335 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
336 TASK_KillTask( 0 );
337 exit( code );
339 else
341 MODULE_DllThreadDetach( NULL );
342 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
343 SYSDEPS_ExitThread( code );
348 /**********************************************************************
349 * SetLastErrorEx [USER32.485] Sets the last-error code.
351 * RETURNS
352 * None.
354 void WINAPI SetLastErrorEx(
355 DWORD error, /* [in] Per-thread error code */
356 DWORD type) /* [in] Error type */
358 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
359 switch(type) {
360 case 0:
361 break;
362 case SLE_ERROR:
363 case SLE_MINORERROR:
364 case SLE_WARNING:
365 /* Fall through for now */
366 default:
367 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
368 break;
370 SetLastError( error );
374 /**********************************************************************
375 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
377 * Allocates a thread local storage index
379 * RETURNS
380 * Success: TLS Index
381 * Failure: 0xFFFFFFFF
383 DWORD WINAPI TlsAlloc( void )
385 PDB *process = PROCESS_Current();
386 DWORD i, mask, ret = 0;
387 DWORD *bits = process->tls_bits;
388 EnterCriticalSection( &process->crit_section );
389 if (*bits == 0xffffffff)
391 bits++;
392 ret = 32;
393 if (*bits == 0xffffffff)
395 LeaveCriticalSection( &process->crit_section );
396 SetLastError( ERROR_NO_MORE_ITEMS );
397 return 0xffffffff;
400 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
401 *bits |= mask;
402 LeaveCriticalSection( &process->crit_section );
403 return ret + i;
407 /**********************************************************************
408 * TlsFree [KERNEL32.531] Releases a TLS index.
410 * Releases a thread local storage index, making it available for reuse
412 * RETURNS
413 * Success: TRUE
414 * Failure: FALSE
416 BOOL WINAPI TlsFree(
417 DWORD index) /* [in] TLS Index to free */
419 PDB *process = PROCESS_Current();
420 DWORD mask;
421 DWORD *bits = process->tls_bits;
422 if (index >= 64)
424 SetLastError( ERROR_INVALID_PARAMETER );
425 return FALSE;
427 EnterCriticalSection( &process->crit_section );
428 if (index >= 32) bits++;
429 mask = (1 << (index & 31));
430 if (!(*bits & mask)) /* already free? */
432 LeaveCriticalSection( &process->crit_section );
433 SetLastError( ERROR_INVALID_PARAMETER );
434 return FALSE;
436 *bits &= ~mask;
437 NtCurrentTeb()->tls_array[index] = 0;
438 /* FIXME: should zero all other thread values */
439 LeaveCriticalSection( &process->crit_section );
440 return TRUE;
444 /**********************************************************************
445 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
447 * RETURNS
448 * Success: Value stored in calling thread's TLS slot for index
449 * Failure: 0 and GetLastError returns NO_ERROR
451 LPVOID WINAPI TlsGetValue(
452 DWORD index) /* [in] TLS index to retrieve value for */
454 if (index >= 64)
456 SetLastError( ERROR_INVALID_PARAMETER );
457 return NULL;
459 SetLastError( ERROR_SUCCESS );
460 return NtCurrentTeb()->tls_array[index];
464 /**********************************************************************
465 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
467 * RETURNS
468 * Success: TRUE
469 * Failure: FALSE
471 BOOL WINAPI TlsSetValue(
472 DWORD index, /* [in] TLS index to set value for */
473 LPVOID value) /* [in] Value to be stored */
475 if (index >= 64)
477 SetLastError( ERROR_INVALID_PARAMETER );
478 return FALSE;
480 NtCurrentTeb()->tls_array[index] = value;
481 return TRUE;
485 /***********************************************************************
486 * SetThreadContext [KERNEL32.670] Sets context of thread.
488 * RETURNS
489 * Success: TRUE
490 * Failure: FALSE
492 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
493 const CONTEXT *context ) /* [in] Address of context structure */
495 struct set_thread_context_request *req = get_req_buffer();
496 req->handle = handle;
497 req->flags = context->ContextFlags;
498 memcpy( &req->context, context, sizeof(*context) );
499 return !server_call( REQ_SET_THREAD_CONTEXT );
503 /***********************************************************************
504 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
506 * RETURNS
507 * Success: TRUE
508 * Failure: FALSE
510 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
511 CONTEXT *context ) /* [out] Address of context structure */
513 struct get_thread_context_request *req = get_req_buffer();
514 req->handle = handle;
515 req->flags = context->ContextFlags;
516 memcpy( &req->context, context, sizeof(*context) );
517 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
518 memcpy( context, &req->context, sizeof(*context) );
519 return TRUE;
523 /**********************************************************************
524 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
526 * RETURNS
527 * Success: Thread's priority level.
528 * Failure: THREAD_PRIORITY_ERROR_RETURN
530 INT WINAPI GetThreadPriority(
531 HANDLE hthread) /* [in] Handle to thread */
533 INT ret = THREAD_PRIORITY_ERROR_RETURN;
534 struct get_thread_info_request *req = get_req_buffer();
535 req->handle = hthread;
536 req->tid_in = 0;
537 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
538 return ret;
542 /**********************************************************************
543 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
545 * RETURNS
546 * Success: TRUE
547 * Failure: FALSE
549 BOOL WINAPI SetThreadPriority(
550 HANDLE hthread, /* [in] Handle to thread */
551 INT priority) /* [in] Thread priority level */
553 struct set_thread_info_request *req = get_req_buffer();
554 req->handle = hthread;
555 req->priority = priority;
556 req->mask = SET_THREAD_INFO_PRIORITY;
557 return !server_call( REQ_SET_THREAD_INFO );
561 /**********************************************************************
562 * SetThreadAffinityMask (KERNEL32.669)
564 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
566 struct set_thread_info_request *req = get_req_buffer();
567 req->handle = hThread;
568 req->affinity = dwThreadAffinityMask;
569 req->mask = SET_THREAD_INFO_AFFINITY;
570 if (server_call( REQ_SET_THREAD_INFO )) return 0;
571 return 1; /* FIXME: should return previous value */
575 /**********************************************************************
576 * TerminateThread [KERNEL32.685] Terminates a thread
578 * RETURNS
579 * Success: TRUE
580 * Failure: FALSE
582 BOOL WINAPI TerminateThread(
583 HANDLE handle, /* [in] Handle to thread */
584 DWORD exitcode) /* [in] Exit code for thread */
586 BOOL ret;
587 struct terminate_thread_request *req = get_req_buffer();
588 req->handle = handle;
589 req->exit_code = exitcode;
590 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
592 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
593 if (req->last) exit( exitcode );
594 else SYSDEPS_ExitThread( exitcode );
596 return ret;
600 /**********************************************************************
601 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
603 * RETURNS
604 * Success: TRUE
605 * Failure: FALSE
607 BOOL WINAPI GetExitCodeThread(
608 HANDLE hthread, /* [in] Handle to thread */
609 LPDWORD exitcode) /* [out] Address to receive termination status */
611 BOOL ret = FALSE;
612 struct get_thread_info_request *req = get_req_buffer();
613 req->handle = hthread;
614 req->tid_in = 0;
615 if (!server_call( REQ_GET_THREAD_INFO ))
617 if (exitcode) *exitcode = req->exit_code;
618 ret = TRUE;
620 return ret;
624 /**********************************************************************
625 * ResumeThread [KERNEL32.587] Resumes a thread.
627 * Decrements a thread's suspend count. When count is zero, the
628 * execution of the thread is resumed.
630 * RETURNS
631 * Success: Previous suspend count
632 * Failure: 0xFFFFFFFF
633 * Already running: 0
635 DWORD WINAPI ResumeThread(
636 HANDLE hthread) /* [in] Identifies thread to restart */
638 DWORD ret = 0xffffffff;
639 struct resume_thread_request *req = get_req_buffer();
640 req->handle = hthread;
641 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
642 return ret;
646 /**********************************************************************
647 * SuspendThread [KERNEL32.681] Suspends a thread.
649 * RETURNS
650 * Success: Previous suspend count
651 * Failure: 0xFFFFFFFF
653 DWORD WINAPI SuspendThread(
654 HANDLE hthread) /* [in] Handle to the thread */
656 DWORD ret = 0xffffffff;
657 struct suspend_thread_request *req = get_req_buffer();
658 req->handle = hthread;
659 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
660 return ret;
664 /***********************************************************************
665 * QueueUserAPC (KERNEL32.566)
667 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
669 struct queue_apc_request *req = get_req_buffer();
670 req->handle = hthread;
671 req->func = func;
672 req->param = (void *)data;
673 return !server_call( REQ_QUEUE_APC );
677 /**********************************************************************
678 * GetThreadTimes [KERNEL32.???] Obtains timing information.
680 * NOTES
681 * What are the fields where these values are stored?
683 * RETURNS
684 * Success: TRUE
685 * Failure: FALSE
687 BOOL WINAPI GetThreadTimes(
688 HANDLE thread, /* [in] Specifies the thread of interest */
689 LPFILETIME creationtime, /* [out] When the thread was created */
690 LPFILETIME exittime, /* [out] When the thread was destroyed */
691 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
692 LPFILETIME usertime) /* [out] Time thread spent in user mode */
694 FIXME("(0x%08x): stub\n",thread);
695 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
696 return FALSE;
700 /**********************************************************************
701 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
703 * Attaches the input processing mechanism of one thread to that of
704 * another thread.
706 * RETURNS
707 * Success: TRUE
708 * Failure: FALSE
710 * TODO:
711 * 1. Reset the Key State (currenly per thread key state is not maintained)
713 BOOL WINAPI AttachThreadInput(
714 DWORD idAttach, /* [in] Thread to attach */
715 DWORD idAttachTo, /* [in] Thread to attach to */
716 BOOL fAttach) /* [in] Attach or detach */
718 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
719 BOOL16 bRet = 0;
721 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
723 /* A thread cannot attach to itself */
724 if ( idAttach == idAttachTo )
725 goto CLEANUP;
727 /* According to the docs this method should fail if a
728 * "Journal record" hook is installed. (attaches all input queues together)
730 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
731 goto CLEANUP;
733 /* Retrieve message queues corresponding to the thread id's */
734 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
735 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
737 /* Ensure we have message queues and that Src and Tgt threads
738 * are not system threads.
740 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
741 goto CLEANUP;
743 if (fAttach) /* Attach threads */
745 /* Only attach if currently detached */
746 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
748 /* First release the target threads perQData */
749 PERQDATA_Release( pTgtMsgQ->pQData );
751 /* Share a reference to the source threads perQDATA */
752 PERQDATA_Addref( pSrcMsgQ->pQData );
753 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
756 else /* Detach threads */
758 /* Only detach if currently attached */
759 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
761 /* First release the target threads perQData */
762 PERQDATA_Release( pTgtMsgQ->pQData );
764 /* Give the target thread its own private perQDATA once more */
765 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
769 /* TODO: Reset the Key State */
771 bRet = 1; /* Success */
773 CLEANUP:
775 /* Unlock the queues before returning */
776 if ( pSrcMsgQ )
777 QUEUE_Unlock( pSrcMsgQ );
778 if ( pTgtMsgQ )
779 QUEUE_Unlock( pTgtMsgQ );
781 return bRet;
784 /**********************************************************************
785 * VWin32_BoostThreadGroup [KERNEL.535]
787 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
789 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
792 /**********************************************************************
793 * VWin32_BoostThreadStatic [KERNEL.536]
795 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
797 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
800 /**********************************************************************
801 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
803 * RETURNS
804 * Success: TRUE
805 * Failure: FALSE
807 * NOTES
808 * Implemented in NT only (3.1 and above according to MS
810 BOOL WINAPI SetThreadLocale(
811 LCID lcid) /* [in] Locale identifier */
813 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
814 return FALSE;
818 /***********************************************************************
819 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
821 * RETURNS
822 * Pseudohandle for the current thread
824 #undef GetCurrentThread
825 HANDLE WINAPI GetCurrentThread(void)
827 return 0xfffffffe;
831 #ifdef __i386__
833 /* void WINAPI SetLastError( DWORD error ); */
834 __ASM_GLOBAL_FUNC( SetLastError,
835 "movl 4(%esp),%eax\n\t"
836 ".byte 0x64\n\t"
837 "movl %eax,0x60\n\t"
838 "ret $4" );
840 /* DWORD WINAPI GetLastError(void); */
841 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
843 /* DWORD WINAPI GetCurrentProcessId(void) */
844 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
846 /* DWORD WINAPI GetCurrentThreadId(void) */
847 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
849 #else /* __i386__ */
851 /**********************************************************************
852 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
854 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
856 NtCurrentTeb()->last_error = error;
859 /**********************************************************************
860 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
862 DWORD WINAPI GetLastError(void)
864 return NtCurrentTeb()->last_error;
867 /***********************************************************************
868 * GetCurrentProcessId [KERNEL32.199] Returns process identifier.
870 DWORD WINAPI GetCurrentProcessId(void)
872 return (DWORD)NtCurrentTeb()->pid;
875 /***********************************************************************
876 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
878 DWORD WINAPI GetCurrentThreadId(void)
880 return (DWORD)NtCurrentTeb()->tid;
883 #endif /* __i386__ */