Added cvdump tool to dump CodeView symbol information.
[wine/gsoc-2012-control.git] / scheduler / thread.c
blob2dfd1cf43b35c90ae9f98160f96d5377348e606f
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 "user.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"
34 DEFAULT_DEBUG_CHANNEL(thread);
36 /* TEB of the initial thread */
37 static TEB initial_teb;
39 /***********************************************************************
40 * THREAD_IsWin16
42 BOOL THREAD_IsWin16( TEB *teb )
44 return !teb || !(teb->tibflags & TEBF_WIN32);
47 /***********************************************************************
48 * THREAD_IdToTEB
50 * Convert a thread id to a TEB, making sure it is valid.
52 TEB *THREAD_IdToTEB( DWORD id )
54 struct get_thread_info_request *req = get_req_buffer();
56 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
57 req->handle = -1;
58 req->tid_in = (void *)id;
59 if (!server_call_noerr( REQ_GET_THREAD_INFO )) return req->teb;
61 /* Allow task handles to be used; convert to main thread */
62 if ( IsTask16( id ) )
64 TDB *pTask = (TDB *)GlobalLock16( id );
65 if (pTask) return pTask->teb;
67 SetLastError( ERROR_INVALID_PARAMETER );
68 return NULL;
72 /***********************************************************************
73 * THREAD_InitTEB
75 * Initialization of a newly created TEB.
77 static BOOL THREAD_InitTEB( TEB *teb, DWORD stack_size, BOOL alloc_stack16 )
79 DWORD old_prot;
81 /* Allocate the stack */
83 /* FIXME:
84 * If stacksize smaller than 1 MB, allocate 1MB
85 * (one program wanted only 10 kB, which is recommendable, but some WINE
86 * functions, noteably in the files subdir, push HUGE structures and
87 * arrays on the stack. They probably shouldn't.)
88 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
89 * but this could give more or less unexplainable crashes.)
91 if (stack_size<1024*1024)
92 stack_size = 1024 * 1024;
93 if (stack_size >= 16*1024*1024)
94 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
95 teb->stack_base = VirtualAlloc(NULL, stack_size + SIGNAL_STACK_SIZE +
96 (alloc_stack16 ? 0x10000 : 0),
97 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
98 if (!teb->stack_base) goto error;
99 /* Set a guard page at the bottom of the stack */
100 VirtualProtect( teb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
101 teb->stack_top = (char *)teb->stack_base + stack_size;
102 teb->stack_low = teb->stack_base;
103 teb->signal_stack = teb->stack_top; /* start of signal stack */
105 /* Allocate the 16-bit stack selector */
107 if (alloc_stack16)
109 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
110 FALSE, FALSE );
111 if (!teb->stack_sel) goto error;
112 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
113 0x10000 - sizeof(STACK16FRAME) );
114 teb->signal_stack = (char *)teb->signal_stack + 0x10000;
117 /* StaticUnicodeString */
119 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
120 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
122 return TRUE;
124 error:
125 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
126 if (teb->stack_base) VirtualFree( teb->stack_base, 0, MEM_RELEASE );
127 return FALSE;
131 /***********************************************************************
132 * THREAD_FreeTEB
134 * Free data structures associated with a thread.
135 * Must be called from the context of another thread.
137 void CALLBACK THREAD_FreeTEB( ULONG_PTR arg )
139 TEB *teb = (TEB *)arg;
141 TRACE("(%p) called\n", teb );
142 SERVICE_Delete( teb->cleanup );
144 /* Free the associated memory */
146 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
147 SELECTOR_FreeBlock( teb->teb_sel, 1 );
148 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
149 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
150 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
151 VirtualFree( teb, 0, MEM_RELEASE );
155 /***********************************************************************
156 * THREAD_CreateInitialThread
158 * Create the initial thread.
160 TEB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
162 initial_teb.except = (void *)-1;
163 initial_teb.self = &initial_teb;
164 initial_teb.tibflags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
165 initial_teb.tls_ptr = initial_teb.tls_array;
166 initial_teb.process = pdb;
167 initial_teb.exit_code = STILL_ACTIVE;
168 initial_teb.socket = server_fd;
170 /* Allocate the TEB selector (%fs register) */
172 if (!(initial_teb.teb_sel = SELECTOR_AllocBlock( &initial_teb, 0x1000,
173 SEGMENT_DATA, TRUE, FALSE )))
175 MESSAGE("Could not allocate fs register for initial thread\n" );
176 return NULL;
178 SYSDEPS_SetCurThread( &initial_teb );
180 /* Now proceed with normal initialization */
182 if (CLIENT_InitThread()) return NULL;
183 if (!THREAD_InitTEB( &initial_teb, 0, TRUE )) return NULL;
184 return &initial_teb;
188 /***********************************************************************
189 * THREAD_Create
191 * NOTES:
192 * Native NT dlls are using the space left on the allocated page
193 * the first allocated TEB on NT is at 0x7ffde000, since we can't
194 * allocate in this area and don't support a granularity of 4kb
195 * yet we leave it to VirtualAlloc to choose an address.
197 TEB *THREAD_Create( PDB *pdb, void *tid, int fd, DWORD flags,
198 DWORD stack_size, BOOL alloc_stack16 )
200 TEB *teb = VirtualAlloc(0, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
201 if (!teb) return NULL;
202 teb->except = (void *)-1;
203 teb->htask16 = pdb->task;
204 teb->self = teb;
205 teb->tibflags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
206 teb->tls_ptr = teb->tls_array;
207 teb->process = pdb;
208 teb->exit_code = STILL_ACTIVE;
209 teb->socket = fd;
210 teb->tid = tid;
211 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
213 /* Allocate the TEB selector (%fs register) */
215 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
216 if (!teb->teb_sel) goto error;
218 /* Do the rest of the initialization */
220 if (!THREAD_InitTEB( teb, stack_size, alloc_stack16 )) goto error;
222 TRACE("(%p) succeeded\n", teb);
223 return teb;
225 error:
226 if (teb->teb_sel) SELECTOR_FreeBlock( teb->teb_sel, 1 );
227 VirtualFree( teb, 0, MEM_RELEASE );
228 return NULL;
232 /***********************************************************************
233 * THREAD_Start
235 * Start execution of a newly created thread. Does not return.
237 static void THREAD_Start(void)
239 HANDLE cleanup_object;
240 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
242 /* install cleanup handler */
243 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
244 GetCurrentProcess(), &cleanup_object,
245 0, FALSE, DUPLICATE_SAME_ACCESS ))
246 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTEB,
247 (ULONG_PTR)NtCurrentTeb() );
249 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
250 PE_InitTls();
251 MODULE_DllThreadAttach( NULL );
252 ExitThread( func( NtCurrentTeb()->entry_arg ) );
256 /***********************************************************************
257 * CreateThread (KERNEL32.63)
259 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
260 LPTHREAD_START_ROUTINE start, LPVOID param,
261 DWORD flags, LPDWORD id )
263 struct new_thread_request *req = get_req_buffer();
264 int socket, handle = -1;
265 TEB *teb;
267 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
268 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
269 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
270 handle = req->handle;
272 if (!(teb = THREAD_Create( PROCESS_Current(), req->tid, socket, flags, stack, TRUE )))
274 close( socket );
275 return 0;
277 teb->tibflags |= TEBF_WIN32;
278 teb->entry_point = start;
279 teb->entry_arg = param;
280 teb->startup = THREAD_Start;
281 if (SYSDEPS_SpawnThread( teb ) == -1)
283 CloseHandle( handle );
284 return 0;
286 if (id) *id = (DWORD)teb->tid;
287 return handle;
290 /***********************************************************************
291 * CreateThread16 (KERNEL.441)
293 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
295 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
296 DWORD param = ((DWORD *)threadArgs)[1];
297 HeapFree( GetProcessHeap(), 0, threadArgs );
299 ((LPDWORD)CURRENT_STACK16)[-1] = param;
300 return CallTo16Long( start, sizeof(DWORD) );
302 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
303 FARPROC16 start, SEGPTR param,
304 DWORD flags, LPDWORD id )
306 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
307 if (!threadArgs) return INVALID_HANDLE_VALUE;
308 threadArgs[0] = (DWORD)start;
309 threadArgs[1] = (DWORD)param;
311 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
315 /***********************************************************************
316 * ExitThread [KERNEL32.215] Ends a thread
318 * RETURNS
319 * None
321 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
323 struct terminate_thread_request *req = get_req_buffer();
325 /* send the exit code to the server */
326 req->handle = GetCurrentThread();
327 req->exit_code = code;
328 server_call( REQ_TERMINATE_THREAD );
329 if (req->last)
331 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
332 TASK_KillTask( 0 );
333 exit( code );
335 else
337 MODULE_DllThreadDetach( NULL );
338 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
339 SYSDEPS_ExitThread( code );
344 /***********************************************************************
345 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
347 * RETURNS
348 * Pseudohandle for the current thread
350 HANDLE WINAPI GetCurrentThread(void)
352 return CURRENT_THREAD_PSEUDOHANDLE;
356 /**********************************************************************
357 * SetLastErrorEx [USER32.485] Sets the last-error code.
359 * RETURNS
360 * None.
362 void WINAPI SetLastErrorEx(
363 DWORD error, /* [in] Per-thread error code */
364 DWORD type) /* [in] Error type */
366 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
367 switch(type) {
368 case 0:
369 break;
370 case SLE_ERROR:
371 case SLE_MINORERROR:
372 case SLE_WARNING:
373 /* Fall through for now */
374 default:
375 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
376 break;
378 SetLastError( error );
382 /**********************************************************************
383 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
385 * Allocates a thread local storage index
387 * RETURNS
388 * Success: TLS Index
389 * Failure: 0xFFFFFFFF
391 DWORD WINAPI TlsAlloc( void )
393 PDB *process = PROCESS_Current();
394 DWORD i, mask, ret = 0;
395 DWORD *bits = process->tls_bits;
396 EnterCriticalSection( &process->crit_section );
397 if (*bits == 0xffffffff)
399 bits++;
400 ret = 32;
401 if (*bits == 0xffffffff)
403 LeaveCriticalSection( &process->crit_section );
404 SetLastError( ERROR_NO_MORE_ITEMS );
405 return 0xffffffff;
408 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
409 *bits |= mask;
410 LeaveCriticalSection( &process->crit_section );
411 return ret + i;
415 /**********************************************************************
416 * TlsFree [KERNEL32.531] Releases a TLS index.
418 * Releases a thread local storage index, making it available for reuse
420 * RETURNS
421 * Success: TRUE
422 * Failure: FALSE
424 BOOL WINAPI TlsFree(
425 DWORD index) /* [in] TLS Index to free */
427 PDB *process = PROCESS_Current();
428 DWORD mask;
429 DWORD *bits = process->tls_bits;
430 if (index >= 64)
432 SetLastError( ERROR_INVALID_PARAMETER );
433 return FALSE;
435 EnterCriticalSection( &process->crit_section );
436 if (index >= 32) bits++;
437 mask = (1 << (index & 31));
438 if (!(*bits & mask)) /* already free? */
440 LeaveCriticalSection( &process->crit_section );
441 SetLastError( ERROR_INVALID_PARAMETER );
442 return FALSE;
444 *bits &= ~mask;
445 NtCurrentTeb()->tls_array[index] = 0;
446 /* FIXME: should zero all other thread values */
447 LeaveCriticalSection( &process->crit_section );
448 return TRUE;
452 /**********************************************************************
453 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
455 * RETURNS
456 * Success: Value stored in calling thread's TLS slot for index
457 * Failure: 0 and GetLastError returns NO_ERROR
459 LPVOID WINAPI TlsGetValue(
460 DWORD index) /* [in] TLS index to retrieve value for */
462 if (index >= 64)
464 SetLastError( ERROR_INVALID_PARAMETER );
465 return NULL;
467 SetLastError( ERROR_SUCCESS );
468 return NtCurrentTeb()->tls_array[index];
472 /**********************************************************************
473 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
475 * RETURNS
476 * Success: TRUE
477 * Failure: FALSE
479 BOOL WINAPI TlsSetValue(
480 DWORD index, /* [in] TLS index to set value for */
481 LPVOID value) /* [in] Value to be stored */
483 if (index >= 64)
485 SetLastError( ERROR_INVALID_PARAMETER );
486 return FALSE;
488 NtCurrentTeb()->tls_array[index] = value;
489 return TRUE;
493 /***********************************************************************
494 * SetThreadContext [KERNEL32.670] Sets context of thread.
496 * RETURNS
497 * Success: TRUE
498 * Failure: FALSE
500 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
501 const CONTEXT *context ) /* [in] Address of context structure */
503 struct set_thread_context_request *req = get_req_buffer();
504 req->handle = handle;
505 req->flags = context->ContextFlags;
506 memcpy( &req->context, context, sizeof(*context) );
507 return !server_call( REQ_SET_THREAD_CONTEXT );
511 /***********************************************************************
512 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
514 * RETURNS
515 * Success: TRUE
516 * Failure: FALSE
518 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
519 CONTEXT *context ) /* [out] Address of context structure */
521 struct get_thread_context_request *req = get_req_buffer();
522 req->handle = handle;
523 req->flags = context->ContextFlags;
524 memcpy( &req->context, context, sizeof(*context) );
525 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
526 memcpy( context, &req->context, sizeof(*context) );
527 return TRUE;
531 /**********************************************************************
532 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
534 * RETURNS
535 * Success: Thread's priority level.
536 * Failure: THREAD_PRIORITY_ERROR_RETURN
538 INT WINAPI GetThreadPriority(
539 HANDLE hthread) /* [in] Handle to thread */
541 INT ret = THREAD_PRIORITY_ERROR_RETURN;
542 struct get_thread_info_request *req = get_req_buffer();
543 req->handle = hthread;
544 req->tid_in = 0;
545 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
546 return ret;
550 /**********************************************************************
551 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
553 * RETURNS
554 * Success: TRUE
555 * Failure: FALSE
557 BOOL WINAPI SetThreadPriority(
558 HANDLE hthread, /* [in] Handle to thread */
559 INT priority) /* [in] Thread priority level */
561 struct set_thread_info_request *req = get_req_buffer();
562 req->handle = hthread;
563 req->priority = priority;
564 req->mask = SET_THREAD_INFO_PRIORITY;
565 return !server_call( REQ_SET_THREAD_INFO );
569 /**********************************************************************
570 * SetThreadAffinityMask (KERNEL32.669)
572 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
574 struct set_thread_info_request *req = get_req_buffer();
575 req->handle = hThread;
576 req->affinity = dwThreadAffinityMask;
577 req->mask = SET_THREAD_INFO_AFFINITY;
578 if (server_call( REQ_SET_THREAD_INFO )) return 0;
579 return 1; /* FIXME: should return previous value */
583 /**********************************************************************
584 * TerminateThread [KERNEL32.685] Terminates a thread
586 * RETURNS
587 * Success: TRUE
588 * Failure: FALSE
590 BOOL WINAPI TerminateThread(
591 HANDLE handle, /* [in] Handle to thread */
592 DWORD exitcode) /* [in] Exit code for thread */
594 BOOL ret;
595 struct terminate_thread_request *req = get_req_buffer();
596 req->handle = handle;
597 req->exit_code = exitcode;
598 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
600 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
601 if (req->last) exit( exitcode );
602 else SYSDEPS_ExitThread( exitcode );
604 return ret;
608 /**********************************************************************
609 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
611 * RETURNS
612 * Success: TRUE
613 * Failure: FALSE
615 BOOL WINAPI GetExitCodeThread(
616 HANDLE hthread, /* [in] Handle to thread */
617 LPDWORD exitcode) /* [out] Address to receive termination status */
619 BOOL ret = FALSE;
620 struct get_thread_info_request *req = get_req_buffer();
621 req->handle = hthread;
622 req->tid_in = 0;
623 if (!server_call( REQ_GET_THREAD_INFO ))
625 if (exitcode) *exitcode = req->exit_code;
626 ret = TRUE;
628 return ret;
632 /**********************************************************************
633 * ResumeThread [KERNEL32.587] Resumes a thread.
635 * Decrements a thread's suspend count. When count is zero, the
636 * execution of the thread is resumed.
638 * RETURNS
639 * Success: Previous suspend count
640 * Failure: 0xFFFFFFFF
641 * Already running: 0
643 DWORD WINAPI ResumeThread(
644 HANDLE hthread) /* [in] Identifies thread to restart */
646 DWORD ret = 0xffffffff;
647 struct resume_thread_request *req = get_req_buffer();
648 req->handle = hthread;
649 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
650 return ret;
654 /**********************************************************************
655 * SuspendThread [KERNEL32.681] Suspends a thread.
657 * RETURNS
658 * Success: Previous suspend count
659 * Failure: 0xFFFFFFFF
661 DWORD WINAPI SuspendThread(
662 HANDLE hthread) /* [in] Handle to the thread */
664 DWORD ret = 0xffffffff;
665 struct suspend_thread_request *req = get_req_buffer();
666 req->handle = hthread;
667 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
668 return ret;
672 /***********************************************************************
673 * QueueUserAPC (KERNEL32.566)
675 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
677 struct queue_apc_request *req = get_req_buffer();
678 req->handle = hthread;
679 req->func = func;
680 req->param = (void *)data;
681 return !server_call( REQ_QUEUE_APC );
685 /**********************************************************************
686 * GetThreadTimes [KERNEL32.???] Obtains timing information.
688 * NOTES
689 * What are the fields where these values are stored?
691 * RETURNS
692 * Success: TRUE
693 * Failure: FALSE
695 BOOL WINAPI GetThreadTimes(
696 HANDLE thread, /* [in] Specifies the thread of interest */
697 LPFILETIME creationtime, /* [out] When the thread was created */
698 LPFILETIME exittime, /* [out] When the thread was destroyed */
699 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
700 LPFILETIME usertime) /* [out] Time thread spent in user mode */
702 FIXME("(0x%08x): stub\n",thread);
703 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
704 return FALSE;
708 /**********************************************************************
709 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
711 * Attaches the input processing mechanism of one thread to that of
712 * another thread.
714 * RETURNS
715 * Success: TRUE
716 * Failure: FALSE
718 * TODO:
719 * 1. Reset the Key State (currenly per thread key state is not maintained)
721 BOOL WINAPI AttachThreadInput(
722 DWORD idAttach, /* [in] Thread to attach */
723 DWORD idAttachTo, /* [in] Thread to attach to */
724 BOOL fAttach) /* [in] Attach or detach */
726 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
727 BOOL16 bRet = 0;
729 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
731 /* A thread cannot attach to itself */
732 if ( idAttach == idAttachTo )
733 goto CLEANUP;
735 /* According to the docs this method should fail if a
736 * "Journal record" hook is installed. (attaches all input queues together)
738 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
739 goto CLEANUP;
741 /* Retrieve message queues corresponding to the thread id's */
742 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
743 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
745 /* Ensure we have message queues and that Src and Tgt threads
746 * are not system threads.
748 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
749 goto CLEANUP;
751 if (fAttach) /* Attach threads */
753 /* Only attach if currently detached */
754 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
756 /* First release the target threads perQData */
757 PERQDATA_Release( pTgtMsgQ->pQData );
759 /* Share a reference to the source threads perQDATA */
760 PERQDATA_Addref( pSrcMsgQ->pQData );
761 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
764 else /* Detach threads */
766 /* Only detach if currently attached */
767 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
769 /* First release the target threads perQData */
770 PERQDATA_Release( pTgtMsgQ->pQData );
772 /* Give the target thread its own private perQDATA once more */
773 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
777 /* TODO: Reset the Key State */
779 bRet = 1; /* Success */
781 CLEANUP:
783 /* Unlock the queues before returning */
784 if ( pSrcMsgQ )
785 QUEUE_Unlock( pSrcMsgQ );
786 if ( pTgtMsgQ )
787 QUEUE_Unlock( pTgtMsgQ );
789 return bRet;
792 /**********************************************************************
793 * VWin32_BoostThreadGroup [KERNEL.535]
795 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
797 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
800 /**********************************************************************
801 * VWin32_BoostThreadStatic [KERNEL.536]
803 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
805 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
808 /**********************************************************************
809 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
811 * RETURNS
812 * Success: TRUE
813 * Failure: FALSE
815 * NOTES
816 * Implemented in NT only (3.1 and above according to MS
818 BOOL WINAPI SetThreadLocale(
819 LCID lcid) /* [in] Locale identifier */
821 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
822 return FALSE;
826 #ifdef __i386__
828 /* void WINAPI SetLastError( DWORD error ); */
829 __ASM_GLOBAL_FUNC( SetLastError,
830 "movl 4(%esp),%eax\n\t"
831 ".byte 0x64\n\t"
832 "movl %eax,0x60\n\t"
833 "ret $4" );
835 /* DWORD WINAPI GetLastError(void); */
836 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
838 /* DWORD WINAPI GetCurrentThreadId(void) */
839 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
841 #else /* __i386__ */
843 /**********************************************************************
844 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
846 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
848 NtCurrentTeb()->last_error = error;
851 /**********************************************************************
852 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
854 DWORD WINAPI GetLastError(void)
856 return NtCurrentTeb()->last_error;
859 /***********************************************************************
860 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
862 DWORD WINAPI GetCurrentThreadId(void)
864 return (DWORD)NtCurrentTeb()->tid;
867 #endif /* __i386__ */