Do not assume that double byte font == unicode font.
[wine.git] / scheduler / thread.c
blob031fcc5eda5eaa1a9e85c217da52f4f9eb021663
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 "winerror.h"
23 #include "heap.h"
24 #include "selectors.h"
25 #include "winnt.h"
26 #include "server.h"
27 #include "services.h"
28 #include "stackframe.h"
29 #include "builtin16.h"
30 #include "debugtools.h"
31 #include "queue.h"
32 #include "hook.h"
33 #include "winnls.h"
35 DEFAULT_DEBUG_CHANNEL(thread);
37 /* TEB of the initial thread */
38 static TEB initial_teb;
40 /* The initial process PDB */
41 static PDB initial_pdb;
43 /***********************************************************************
44 * THREAD_IsWin16
46 BOOL THREAD_IsWin16( TEB *teb )
48 return !teb || !(teb->tibflags & TEBF_WIN32);
51 /***********************************************************************
52 * THREAD_IdToTEB
54 * Convert a thread id to a TEB, making sure it is valid.
56 TEB *THREAD_IdToTEB( DWORD id )
58 struct get_thread_info_request *req = get_req_buffer();
60 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
61 req->handle = -1;
62 req->tid_in = (void *)id;
63 if (!server_call_noerr( REQ_GET_THREAD_INFO )) return req->teb;
65 /* Allow task handles to be used; convert to main thread */
66 if ( IsTask16( id ) )
68 TDB *pTask = (TDB *)GlobalLock16( id );
69 if (pTask) return pTask->teb;
71 SetLastError( ERROR_INVALID_PARAMETER );
72 return NULL;
76 /***********************************************************************
77 * THREAD_InitTEB
79 * Initialization of a newly created TEB.
81 static BOOL THREAD_InitTEB( TEB *teb )
83 teb->except = (void *)~0UL;
84 teb->self = teb;
85 teb->tibflags = TEBF_WIN32;
86 teb->tls_ptr = teb->tls_array;
87 teb->exit_code = STILL_ACTIVE;
88 teb->socket = -1;
89 teb->stack_top = (void *)~0UL;
90 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
91 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
92 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
93 return (teb->teb_sel != 0);
97 /***********************************************************************
98 * THREAD_FreeTEB
100 * Free data structures associated with a thread.
101 * Must be called from the context of another thread.
103 static void CALLBACK THREAD_FreeTEB( TEB *teb )
105 TRACE("(%p) called\n", teb );
106 if (teb->cleanup) SERVICE_Delete( teb->cleanup );
108 /* Free the associated memory */
110 if (teb->socket != -1) close( teb->socket );
111 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
112 SELECTOR_FreeBlock( teb->teb_sel, 1 );
113 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
114 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
115 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
119 /***********************************************************************
120 * THREAD_InitStack
122 * Allocate the stack of a thread.
124 TEB *THREAD_InitStack( TEB *teb, DWORD stack_size, BOOL alloc_stack16 )
126 DWORD old_prot, total_size;
127 DWORD page_size = VIRTUAL_GetPageSize();
128 void *base;
130 /* Allocate the stack */
132 if (stack_size >= 16*1024*1024)
133 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
135 /* if size is smaller than default, get stack size from parent */
136 if (stack_size < 1024 * 1024)
138 if (teb)
139 stack_size = 1024 * 1024; /* no parent */
140 else
141 stack_size = ((char *)NtCurrentTeb()->stack_top - (char *)NtCurrentTeb()->stack_base
142 - SIGNAL_STACK_SIZE - 3 * page_size);
145 /* FIXME: some Wine functions use a lot of stack, so we add 64Kb here */
146 stack_size += 64 * 1024;
148 /* Memory layout in allocated block:
150 * size contents
151 * 1 page NOACCESS guard page
152 * SIGNAL_STACK_SIZE signal stack
153 * 1 page NOACCESS guard page
154 * 1 page PAGE_GUARD guard page
155 * stack_size normal stack
156 * 64Kb 16-bit stack (optional)
157 * 1 page TEB (except for initial thread)
160 stack_size = (stack_size + (page_size - 1)) & ~(page_size - 1);
161 total_size = stack_size + SIGNAL_STACK_SIZE + 3 * page_size;
162 if (alloc_stack16) total_size += 0x10000;
163 if (!teb) total_size += page_size;
165 if (!(base = VirtualAlloc( NULL, total_size, MEM_COMMIT, PAGE_EXECUTE_READWRITE )))
166 return NULL;
168 if (!teb)
170 teb = (TEB *)((char *)base + total_size - page_size);
171 if (!THREAD_InitTEB( teb ))
173 VirtualFree( base, 0, MEM_RELEASE );
174 return NULL;
178 teb->stack_low = base;
179 teb->stack_base = base;
180 teb->signal_stack = (char *)base + page_size;
181 teb->stack_top = (char *)base + 3 * page_size + SIGNAL_STACK_SIZE + stack_size;
183 /* Setup guard pages */
185 VirtualProtect( base, 1, PAGE_NOACCESS, &old_prot );
186 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE, 1, PAGE_NOACCESS, &old_prot );
187 VirtualProtect( (char *)teb->signal_stack + SIGNAL_STACK_SIZE + page_size, 1,
188 PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
190 /* Allocate the 16-bit stack selector */
192 if (alloc_stack16)
194 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
195 FALSE, FALSE );
196 if (!teb->stack_sel) goto error;
197 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
198 0x10000 - sizeof(STACK16FRAME) );
200 return teb;
202 error:
203 THREAD_FreeTEB( teb );
204 return NULL;
208 /***********************************************************************
209 * THREAD_Init
211 * Setup the initial thread.
213 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
215 void THREAD_Init(void)
217 if (!initial_teb.self) /* do it only once */
219 THREAD_InitTEB( &initial_teb );
220 assert( initial_teb.teb_sel );
221 initial_teb.process = &initial_pdb;
222 SYSDEPS_SetCurThread( &initial_teb );
226 DECL_GLOBAL_CONSTRUCTOR(thread_init) { THREAD_Init(); }
228 /***********************************************************************
229 * THREAD_Create
232 TEB *THREAD_Create( int fd, DWORD stack_size, BOOL alloc_stack16 )
234 TEB *teb;
236 if ((teb = THREAD_InitStack( NULL, stack_size, alloc_stack16 )))
238 teb->tibflags = (PROCESS_Current()->flags & PDB32_WIN16_PROC) ? 0 : TEBF_WIN32;
239 teb->process = PROCESS_Current();
240 teb->socket = fd;
241 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
242 TRACE("(%p) succeeded\n", teb);
244 return teb;
248 /***********************************************************************
249 * THREAD_Start
251 * Start execution of a newly created thread. Does not return.
253 static void THREAD_Start(void)
255 HANDLE cleanup_object;
256 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
258 /* install cleanup handler */
259 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
260 GetCurrentProcess(), &cleanup_object,
261 0, FALSE, DUPLICATE_SAME_ACCESS ))
262 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, (PAPCFUNC)THREAD_FreeTEB,
263 (ULONG_PTR)NtCurrentTeb() );
265 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
266 PE_InitTls();
267 MODULE_DllThreadAttach( NULL );
268 ExitThread( func( NtCurrentTeb()->entry_arg ) );
272 /***********************************************************************
273 * CreateThread (KERNEL32.63)
275 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
276 LPTHREAD_START_ROUTINE start, LPVOID param,
277 DWORD flags, LPDWORD id )
279 struct new_thread_request *req = get_req_buffer();
280 int socket, handle = -1;
281 TEB *teb;
282 void *tid;
284 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
285 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
286 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
287 handle = req->handle;
288 tid = req->tid;
290 if (!(teb = THREAD_Create( socket, stack, TRUE )))
292 close( socket );
293 return 0;
295 teb->tibflags |= TEBF_WIN32;
296 teb->entry_point = start;
297 teb->entry_arg = param;
298 teb->startup = THREAD_Start;
299 teb->htask16 = GetCurrentTask();
300 teb->CurrentLocale = GetUserDefaultLCID(); /* for threads in user context */
301 if (id) *id = (DWORD)tid;
302 if (SYSDEPS_SpawnThread( teb ) == -1)
304 CloseHandle( handle );
305 return 0;
307 return handle;
310 /***********************************************************************
311 * CreateThread16 (KERNEL.441)
313 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
315 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
316 DWORD param = ((DWORD *)threadArgs)[1];
317 HeapFree( GetProcessHeap(), 0, threadArgs );
319 ((LPDWORD)CURRENT_STACK16)[-1] = param;
320 return CallTo16Long( start, sizeof(DWORD) );
322 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
323 FARPROC16 start, SEGPTR param,
324 DWORD flags, LPDWORD id )
326 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
327 if (!threadArgs) return INVALID_HANDLE_VALUE;
328 threadArgs[0] = (DWORD)start;
329 threadArgs[1] = (DWORD)param;
331 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
335 /***********************************************************************
336 * ExitThread [KERNEL32.215] Ends a thread
338 * RETURNS
339 * None
341 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
343 struct terminate_thread_request *req = get_req_buffer();
345 /* send the exit code to the server */
346 req->handle = GetCurrentThread();
347 req->exit_code = code;
348 server_call( REQ_TERMINATE_THREAD );
349 if (req->last)
351 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
352 exit( code );
354 else
356 MODULE_DllThreadDetach( NULL );
357 if (!(NtCurrentTeb()->tibflags & TEBF_WIN32)) TASK_KillTask( 0 );
358 SYSDEPS_ExitThread( code );
363 /**********************************************************************
364 * SetLastErrorEx [USER32.485] Sets the last-error code.
366 * RETURNS
367 * None.
369 void WINAPI SetLastErrorEx(
370 DWORD error, /* [in] Per-thread error code */
371 DWORD type) /* [in] Error type */
373 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
374 switch(type) {
375 case 0:
376 break;
377 case SLE_ERROR:
378 case SLE_MINORERROR:
379 case SLE_WARNING:
380 /* Fall through for now */
381 default:
382 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
383 break;
385 SetLastError( error );
389 /**********************************************************************
390 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
392 * Allocates a thread local storage index
394 * RETURNS
395 * Success: TLS Index
396 * Failure: 0xFFFFFFFF
398 DWORD WINAPI TlsAlloc( void )
400 PDB *process = PROCESS_Current();
401 DWORD i, mask, ret = 0;
402 DWORD *bits = process->tls_bits;
403 EnterCriticalSection( &process->crit_section );
404 if (*bits == 0xffffffff)
406 bits++;
407 ret = 32;
408 if (*bits == 0xffffffff)
410 LeaveCriticalSection( &process->crit_section );
411 SetLastError( ERROR_NO_MORE_ITEMS );
412 return 0xffffffff;
415 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
416 *bits |= mask;
417 LeaveCriticalSection( &process->crit_section );
418 return ret + i;
422 /**********************************************************************
423 * TlsFree [KERNEL32.531] Releases a TLS index.
425 * Releases a thread local storage index, making it available for reuse
427 * RETURNS
428 * Success: TRUE
429 * Failure: FALSE
431 BOOL WINAPI TlsFree(
432 DWORD index) /* [in] TLS Index to free */
434 PDB *process = PROCESS_Current();
435 DWORD mask;
436 DWORD *bits = process->tls_bits;
437 if (index >= 64)
439 SetLastError( ERROR_INVALID_PARAMETER );
440 return FALSE;
442 EnterCriticalSection( &process->crit_section );
443 if (index >= 32) bits++;
444 mask = (1 << (index & 31));
445 if (!(*bits & mask)) /* already free? */
447 LeaveCriticalSection( &process->crit_section );
448 SetLastError( ERROR_INVALID_PARAMETER );
449 return FALSE;
451 *bits &= ~mask;
452 NtCurrentTeb()->tls_array[index] = 0;
453 /* FIXME: should zero all other thread values */
454 LeaveCriticalSection( &process->crit_section );
455 return TRUE;
459 /**********************************************************************
460 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
462 * RETURNS
463 * Success: Value stored in calling thread's TLS slot for index
464 * Failure: 0 and GetLastError returns NO_ERROR
466 LPVOID WINAPI TlsGetValue(
467 DWORD index) /* [in] TLS index to retrieve value for */
469 if (index >= 64)
471 SetLastError( ERROR_INVALID_PARAMETER );
472 return NULL;
474 SetLastError( ERROR_SUCCESS );
475 return NtCurrentTeb()->tls_array[index];
479 /**********************************************************************
480 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
482 * RETURNS
483 * Success: TRUE
484 * Failure: FALSE
486 BOOL WINAPI TlsSetValue(
487 DWORD index, /* [in] TLS index to set value for */
488 LPVOID value) /* [in] Value to be stored */
490 if (index >= 64)
492 SetLastError( ERROR_INVALID_PARAMETER );
493 return FALSE;
495 NtCurrentTeb()->tls_array[index] = value;
496 return TRUE;
500 /***********************************************************************
501 * SetThreadContext [KERNEL32.670] Sets context of thread.
503 * RETURNS
504 * Success: TRUE
505 * Failure: FALSE
507 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
508 const CONTEXT *context ) /* [in] Address of context structure */
510 struct set_thread_context_request *req = get_req_buffer();
511 req->handle = handle;
512 req->flags = context->ContextFlags;
513 memcpy( &req->context, context, sizeof(*context) );
514 return !server_call( REQ_SET_THREAD_CONTEXT );
518 /***********************************************************************
519 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
521 * RETURNS
522 * Success: TRUE
523 * Failure: FALSE
525 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
526 CONTEXT *context ) /* [out] Address of context structure */
528 struct get_thread_context_request *req = get_req_buffer();
529 req->handle = handle;
530 req->flags = context->ContextFlags;
531 memcpy( &req->context, context, sizeof(*context) );
532 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
533 memcpy( context, &req->context, sizeof(*context) );
534 return TRUE;
538 /**********************************************************************
539 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
541 * RETURNS
542 * Success: Thread's priority level.
543 * Failure: THREAD_PRIORITY_ERROR_RETURN
545 INT WINAPI GetThreadPriority(
546 HANDLE hthread) /* [in] Handle to thread */
548 INT ret = THREAD_PRIORITY_ERROR_RETURN;
549 struct get_thread_info_request *req = get_req_buffer();
550 req->handle = hthread;
551 req->tid_in = 0;
552 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
553 return ret;
557 /**********************************************************************
558 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
560 * RETURNS
561 * Success: TRUE
562 * Failure: FALSE
564 BOOL WINAPI SetThreadPriority(
565 HANDLE hthread, /* [in] Handle to thread */
566 INT priority) /* [in] Thread priority level */
568 struct set_thread_info_request *req = get_req_buffer();
569 req->handle = hthread;
570 req->priority = priority;
571 req->mask = SET_THREAD_INFO_PRIORITY;
572 return !server_call( REQ_SET_THREAD_INFO );
576 /**********************************************************************
577 * SetThreadAffinityMask (KERNEL32.669)
579 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
581 struct set_thread_info_request *req = get_req_buffer();
582 req->handle = hThread;
583 req->affinity = dwThreadAffinityMask;
584 req->mask = SET_THREAD_INFO_AFFINITY;
585 if (server_call( REQ_SET_THREAD_INFO )) return 0;
586 return 1; /* FIXME: should return previous value */
590 /**********************************************************************
591 * TerminateThread [KERNEL32.685] Terminates a thread
593 * RETURNS
594 * Success: TRUE
595 * Failure: FALSE
597 BOOL WINAPI TerminateThread(
598 HANDLE handle, /* [in] Handle to thread */
599 DWORD exitcode) /* [in] Exit code for thread */
601 BOOL ret;
602 struct terminate_thread_request *req = get_req_buffer();
603 req->handle = handle;
604 req->exit_code = exitcode;
605 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
607 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
608 if (req->last) exit( exitcode );
609 else SYSDEPS_ExitThread( exitcode );
611 return ret;
615 /**********************************************************************
616 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
618 * RETURNS
619 * Success: TRUE
620 * Failure: FALSE
622 BOOL WINAPI GetExitCodeThread(
623 HANDLE hthread, /* [in] Handle to thread */
624 LPDWORD exitcode) /* [out] Address to receive termination status */
626 BOOL ret = FALSE;
627 struct get_thread_info_request *req = get_req_buffer();
628 req->handle = hthread;
629 req->tid_in = 0;
630 if (!server_call( REQ_GET_THREAD_INFO ))
632 if (exitcode) *exitcode = req->exit_code;
633 ret = TRUE;
635 return ret;
639 /**********************************************************************
640 * ResumeThread [KERNEL32.587] Resumes a thread.
642 * Decrements a thread's suspend count. When count is zero, the
643 * execution of the thread is resumed.
645 * RETURNS
646 * Success: Previous suspend count
647 * Failure: 0xFFFFFFFF
648 * Already running: 0
650 DWORD WINAPI ResumeThread(
651 HANDLE hthread) /* [in] Identifies thread to restart */
653 DWORD ret = 0xffffffff;
654 struct resume_thread_request *req = get_req_buffer();
655 req->handle = hthread;
656 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
657 return ret;
661 /**********************************************************************
662 * SuspendThread [KERNEL32.681] Suspends a thread.
664 * RETURNS
665 * Success: Previous suspend count
666 * Failure: 0xFFFFFFFF
668 DWORD WINAPI SuspendThread(
669 HANDLE hthread) /* [in] Handle to the thread */
671 DWORD ret = 0xffffffff;
672 struct suspend_thread_request *req = get_req_buffer();
673 req->handle = hthread;
674 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
675 return ret;
679 /***********************************************************************
680 * QueueUserAPC (KERNEL32.566)
682 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
684 struct queue_apc_request *req = get_req_buffer();
685 req->handle = hthread;
686 req->func = func;
687 req->param = (void *)data;
688 return !server_call( REQ_QUEUE_APC );
692 /**********************************************************************
693 * GetThreadTimes [KERNEL32.???] Obtains timing information.
695 * NOTES
696 * What are the fields where these values are stored?
698 * RETURNS
699 * Success: TRUE
700 * Failure: FALSE
702 BOOL WINAPI GetThreadTimes(
703 HANDLE thread, /* [in] Specifies the thread of interest */
704 LPFILETIME creationtime, /* [out] When the thread was created */
705 LPFILETIME exittime, /* [out] When the thread was destroyed */
706 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
707 LPFILETIME usertime) /* [out] Time thread spent in user mode */
709 FIXME("(0x%08x): stub\n",thread);
710 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
711 return FALSE;
715 /**********************************************************************
716 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
718 * Attaches the input processing mechanism of one thread to that of
719 * another thread.
721 * RETURNS
722 * Success: TRUE
723 * Failure: FALSE
725 * TODO:
726 * 1. Reset the Key State (currenly per thread key state is not maintained)
728 BOOL WINAPI AttachThreadInput(
729 DWORD idAttach, /* [in] Thread to attach */
730 DWORD idAttachTo, /* [in] Thread to attach to */
731 BOOL fAttach) /* [in] Attach or detach */
733 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
734 BOOL16 bRet = 0;
736 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
738 /* A thread cannot attach to itself */
739 if ( idAttach == idAttachTo )
740 goto CLEANUP;
742 /* According to the docs this method should fail if a
743 * "Journal record" hook is installed. (attaches all input queues together)
745 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
746 goto CLEANUP;
748 /* Retrieve message queues corresponding to the thread id's */
749 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
750 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
752 /* Ensure we have message queues and that Src and Tgt threads
753 * are not system threads.
755 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
756 goto CLEANUP;
758 if (fAttach) /* Attach threads */
760 /* Only attach if currently detached */
761 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
763 /* First release the target threads perQData */
764 PERQDATA_Release( pTgtMsgQ->pQData );
766 /* Share a reference to the source threads perQDATA */
767 PERQDATA_Addref( pSrcMsgQ->pQData );
768 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
771 else /* Detach threads */
773 /* Only detach if currently attached */
774 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
776 /* First release the target threads perQData */
777 PERQDATA_Release( pTgtMsgQ->pQData );
779 /* Give the target thread its own private perQDATA once more */
780 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
784 /* TODO: Reset the Key State */
786 bRet = 1; /* Success */
788 CLEANUP:
790 /* Unlock the queues before returning */
791 if ( pSrcMsgQ )
792 QUEUE_Unlock( pSrcMsgQ );
793 if ( pTgtMsgQ )
794 QUEUE_Unlock( pTgtMsgQ );
796 return bRet;
799 /**********************************************************************
800 * VWin32_BoostThreadGroup [KERNEL.535]
802 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
804 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
807 /**********************************************************************
808 * VWin32_BoostThreadStatic [KERNEL.536]
810 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
812 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
816 /***********************************************************************
817 * GetThreadLocale (KERNEL32.295)
819 LCID WINAPI GetThreadLocale(void)
821 return NtCurrentTeb()->CurrentLocale;
825 /**********************************************************************
826 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
828 * RETURNS
829 * Success: TRUE
830 * Failure: FALSE
832 * FIXME
833 * check if lcid is a valid cp
835 BOOL WINAPI SetThreadLocale(
836 LCID lcid) /* [in] Locale identifier */
838 switch (lcid)
840 case LOCALE_SYSTEM_DEFAULT:
841 lcid = GetSystemDefaultLCID();
842 break;
843 case LOCALE_USER_DEFAULT:
844 case LOCALE_NEUTRAL:
845 lcid = GetUserDefaultLCID();
846 break;
848 NtCurrentTeb()->CurrentLocale = lcid;
849 return TRUE;
853 /***********************************************************************
854 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
856 * RETURNS
857 * Pseudohandle for the current thread
859 #undef GetCurrentThread
860 HANDLE WINAPI GetCurrentThread(void)
862 return 0xfffffffe;
866 #ifdef __i386__
868 /* void WINAPI SetLastError( DWORD error ); */
869 __ASM_GLOBAL_FUNC( SetLastError,
870 "movl 4(%esp),%eax\n\t"
871 ".byte 0x64\n\t"
872 "movl %eax,0x60\n\t"
873 "ret $4" );
875 /* DWORD WINAPI GetLastError(void); */
876 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
878 /* DWORD WINAPI GetCurrentProcessId(void) */
879 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
881 /* DWORD WINAPI GetCurrentThreadId(void) */
882 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
884 #else /* __i386__ */
886 /**********************************************************************
887 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
889 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
891 NtCurrentTeb()->last_error = error;
894 /**********************************************************************
895 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
897 DWORD WINAPI GetLastError(void)
899 return NtCurrentTeb()->last_error;
902 /***********************************************************************
903 * GetCurrentProcessId [KERNEL32.199] Returns process identifier.
905 DWORD WINAPI GetCurrentProcessId(void)
907 return (DWORD)NtCurrentTeb()->pid;
910 /***********************************************************************
911 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
913 DWORD WINAPI GetCurrentThreadId(void)
915 return (DWORD)NtCurrentTeb()->tid;
918 #endif /* __i386__ */