- Added global data mutual exclusion
[wine.git] / scheduler / thread.c
blob7ba3a0c528c4bc8aa1c960da3495c8c4b413aa06
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_SOCKET_H
13 # include <sys/socket.h>
14 #endif
15 #ifdef HAVE_SYS_MMAN_H
16 #include <sys/mman.h>
17 #endif
18 #include <unistd.h>
19 #include "wine/winbase16.h"
20 #include "thread.h"
21 #include "process.h"
22 #include "task.h"
23 #include "module.h"
24 #include "user.h"
25 #include "winerror.h"
26 #include "heap.h"
27 #include "selectors.h"
28 #include "winnt.h"
29 #include "server.h"
30 #include "services.h"
31 #include "stackframe.h"
32 #include "builtin16.h"
33 #include "debugtools.h"
34 #include "queue.h"
35 #include "hook.h"
37 DEFAULT_DEBUG_CHANNEL(thread)
39 /* TEB of the initial thread */
40 static TEB initial_teb;
42 /* Global thread list (FIXME: not thread-safe) */
43 TEB *THREAD_First = &initial_teb;
45 /***********************************************************************
46 * THREAD_IsWin16
48 BOOL THREAD_IsWin16( TEB *teb )
50 return !teb || !(teb->flags & TEBF_WIN32);
53 /***********************************************************************
54 * THREAD_IdToTEB
56 * Convert a thread id to a TEB, making sure it is valid.
58 TEB *THREAD_IdToTEB( DWORD id )
60 TEB *teb = THREAD_First;
62 if (!id) return NtCurrentTeb();
63 while (teb)
65 if ((DWORD)teb->tid == id) return teb;
66 teb = teb->next;
68 /* Allow task handles to be used; convert to main thread */
69 if ( IsTask16( id ) )
71 TDB *pTask = (TDB *)GlobalLock16( id );
72 if (pTask) return pTask->teb;
74 SetLastError( ERROR_INVALID_PARAMETER );
75 return NULL;
79 /***********************************************************************
80 * THREAD_InitTEB
82 * Initialization of a newly created TEB.
84 static BOOL THREAD_InitTEB( TEB *teb, DWORD stack_size, BOOL alloc_stack16,
85 LPSECURITY_ATTRIBUTES sa )
87 DWORD old_prot;
89 /* Allocate the stack */
91 /* FIXME:
92 * If stacksize smaller than 1 MB, allocate 1MB
93 * (one program wanted only 10 kB, which is recommendable, but some WINE
94 * functions, noteably in the files subdir, push HUGE structures and
95 * arrays on the stack. They probably shouldn't.)
96 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
97 * but this could give more or less unexplainable crashes.)
99 if (stack_size<1024*1024)
100 stack_size = 1024 * 1024;
101 if (stack_size >= 16*1024*1024)
102 WARN("Thread stack size is %ld MB.\n",stack_size/1024/1024);
103 teb->stack_base = VirtualAlloc(NULL, stack_size + SIGNAL_STACK_SIZE +
104 (alloc_stack16 ? 0x10000 : 0),
105 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
106 if (!teb->stack_base) goto error;
107 /* Set a guard page at the bottom of the stack */
108 VirtualProtect( teb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
109 teb->stack_top = (char *)teb->stack_base + stack_size;
110 teb->stack_low = teb->stack_base;
111 teb->signal_stack = teb->stack_top; /* start of signal stack */
113 /* Allocate the 16-bit stack selector */
115 if (alloc_stack16)
117 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
118 FALSE, FALSE );
119 if (!teb->stack_sel) goto error;
120 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
121 0x10000 - sizeof(STACK16FRAME) );
122 teb->signal_stack = (char *)teb->signal_stack + 0x10000;
125 /* Create the thread event */
127 if (!(teb->event = CreateEventA( NULL, FALSE, FALSE, NULL ))) goto error;
128 teb->event = ConvertToGlobalHandle( teb->event );
129 return TRUE;
131 error:
132 if (teb->event) CloseHandle( teb->event );
133 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
134 if (teb->stack_base) VirtualFree( teb->stack_base, 0, MEM_RELEASE );
135 return FALSE;
139 /***********************************************************************
140 * THREAD_FreeTEB
142 * Free data structures associated with a thread.
143 * Must be called from the context of another thread.
145 void CALLBACK THREAD_FreeTEB( ULONG_PTR arg )
147 TEB *teb = (TEB *)arg;
148 TEB **pptr = &THREAD_First;
150 TRACE("(%p) called\n", teb );
151 SERVICE_Delete( teb->cleanup );
153 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
155 CloseHandle( teb->event );
156 while (*pptr && (*pptr != teb)) pptr = &(*pptr)->next;
157 if (*pptr) *pptr = teb->next;
159 /* Free the associated memory */
161 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
162 SELECTOR_FreeBlock( teb->teb_sel, 1 );
163 close( teb->socket );
164 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
165 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
166 HeapFree( SystemHeap, 0, teb );
170 /***********************************************************************
171 * THREAD_CreateInitialThread
173 * Create the initial thread.
175 TEB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
177 initial_teb.except = (void *)-1;
178 initial_teb.self = &initial_teb;
179 initial_teb.flags = /* TEBF_WIN32 */ 0;
180 initial_teb.tls_ptr = initial_teb.tls_array;
181 initial_teb.process = pdb;
182 initial_teb.exit_code = 0x103; /* STILL_ACTIVE */
183 initial_teb.socket = server_fd;
185 /* Allocate the TEB selector (%fs register) */
187 if (!(initial_teb.teb_sel = SELECTOR_AllocBlock( &initial_teb, 0x1000,
188 SEGMENT_DATA, TRUE, FALSE )))
190 MESSAGE("Could not allocate fs register for initial thread\n" );
191 return NULL;
193 SYSDEPS_SetCurThread( &initial_teb );
195 /* Now proceed with normal initialization */
197 if (CLIENT_InitThread()) return NULL;
198 if (!THREAD_InitTEB( &initial_teb, 0, TRUE, NULL )) return NULL;
199 return &initial_teb;
203 /***********************************************************************
204 * THREAD_Create
206 TEB *THREAD_Create( PDB *pdb, DWORD flags, DWORD stack_size, BOOL alloc_stack16,
207 LPSECURITY_ATTRIBUTES sa, int *server_handle )
209 struct new_thread_request *req = get_req_buffer();
210 int fd[2];
211 HANDLE cleanup_object;
213 TEB *teb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(TEB) );
214 if (!teb) return NULL;
215 teb->except = (void *)-1;
216 teb->htask16 = pdb->task;
217 teb->self = teb;
218 teb->flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
219 teb->tls_ptr = teb->tls_array;
220 teb->process = pdb;
221 teb->exit_code = 0x103; /* STILL_ACTIVE */
222 teb->socket = -1;
224 /* Allocate the TEB selector (%fs register) */
226 *server_handle = -1;
227 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
228 if (!teb->teb_sel) goto error;
230 /* Create the socket pair for server communication */
232 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
234 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
235 goto error;
237 teb->socket = fd[0];
238 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
240 /* Create the thread on the server side */
242 req->pid = teb->process->server_pid;
243 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
244 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
245 if (server_call_fd( REQ_NEW_THREAD, fd[1], NULL )) goto error;
246 teb->tid = req->tid;
247 *server_handle = req->handle;
249 /* Do the rest of the initialization */
251 if (!THREAD_InitTEB( teb, stack_size, alloc_stack16, sa )) goto error;
252 teb->next = THREAD_First;
253 THREAD_First = teb;
255 /* Install cleanup handler */
256 if ( !DuplicateHandle( GetCurrentProcess(), *server_handle,
257 GetCurrentProcess(), &cleanup_object,
258 0, FALSE, DUPLICATE_SAME_ACCESS ) ) goto error;
259 teb->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTEB, (ULONG_PTR)teb );
261 return teb;
263 error:
264 if (*server_handle != -1) CloseHandle( *server_handle );
265 if (teb->teb_sel) SELECTOR_FreeBlock( teb->teb_sel, 1 );
266 if (teb->socket != -1) close( teb->socket );
267 HeapFree( SystemHeap, 0, teb );
268 return NULL;
272 /***********************************************************************
273 * THREAD_Start
275 * Start execution of a newly created thread. Does not return.
277 static void THREAD_Start(void)
279 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
280 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
281 PE_InitTls();
282 MODULE_DllThreadAttach( NULL );
284 if (NtCurrentTeb()->process->flags & PDB32_DEBUGGED) DEBUG_SendCreateThreadEvent( func );
286 ExitThread( func( NtCurrentTeb()->entry_arg ) );
290 /***********************************************************************
291 * CreateThread (KERNEL32.63)
293 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
294 LPTHREAD_START_ROUTINE start, LPVOID param,
295 DWORD flags, LPDWORD id )
297 int handle = -1;
298 TEB *teb = THREAD_Create( PROCESS_Current(), flags, stack, TRUE, sa, &handle );
299 if (!teb) return 0;
300 teb->flags |= TEBF_WIN32;
301 teb->entry_point = start;
302 teb->entry_arg = param;
303 teb->startup = THREAD_Start;
304 if (SYSDEPS_SpawnThread( teb ) == -1)
306 CloseHandle( handle );
307 return 0;
309 if (id) *id = (DWORD)teb->tid;
310 return handle;
313 /***********************************************************************
314 * CreateThread16 (KERNEL.441)
316 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
318 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
319 DWORD param = ((DWORD *)threadArgs)[1];
320 HeapFree( GetProcessHeap(), 0, threadArgs );
322 ((LPDWORD)CURRENT_STACK16)[-1] = param;
323 return CallTo16Long( start, sizeof(DWORD) );
325 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
326 FARPROC16 start, SEGPTR param,
327 DWORD flags, LPDWORD id )
329 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
330 if (!threadArgs) return INVALID_HANDLE_VALUE;
331 threadArgs[0] = (DWORD)start;
332 threadArgs[1] = (DWORD)param;
334 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
338 /***********************************************************************
339 * ExitThread [KERNEL32.215] Ends a thread
341 * RETURNS
342 * None
344 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
346 MODULE_DllThreadDetach( NULL );
347 TerminateThread( GetCurrentThread(), code );
351 /***********************************************************************
352 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
354 * RETURNS
355 * Pseudohandle for the current thread
357 HANDLE WINAPI GetCurrentThread(void)
359 return CURRENT_THREAD_PSEUDOHANDLE;
363 /**********************************************************************
364 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
366 * RETURNS
367 * Calling thread's last error code value.
369 DWORD WINAPI GetLastError(void)
371 return NtCurrentTeb()->last_error;
375 /**********************************************************************
376 * SetLastErrorEx [USER32.485] Sets the last-error code.
378 * RETURNS
379 * None.
381 void WINAPI SetLastErrorEx(
382 DWORD error, /* [in] Per-thread error code */
383 DWORD type) /* [in] Error type */
385 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
386 switch(type) {
387 case 0:
388 break;
389 case SLE_ERROR:
390 case SLE_MINORERROR:
391 case SLE_WARNING:
392 /* Fall through for now */
393 default:
394 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
395 break;
397 SetLastError( error );
401 /**********************************************************************
402 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
404 * Allocates a thread local storage index
406 * RETURNS
407 * Success: TLS Index
408 * Failure: 0xFFFFFFFF
410 DWORD WINAPI TlsAlloc( void )
412 PDB *process = PROCESS_Current();
413 DWORD i, mask, ret = 0;
414 DWORD *bits = process->tls_bits;
415 EnterCriticalSection( &process->crit_section );
416 if (*bits == 0xffffffff)
418 bits++;
419 ret = 32;
420 if (*bits == 0xffffffff)
422 LeaveCriticalSection( &process->crit_section );
423 SetLastError( ERROR_NO_MORE_ITEMS );
424 return 0xffffffff;
427 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
428 *bits |= mask;
429 LeaveCriticalSection( &process->crit_section );
430 return ret + i;
434 /**********************************************************************
435 * TlsFree [KERNEL32.531] Releases a TLS index.
437 * Releases a thread local storage index, making it available for reuse
439 * RETURNS
440 * Success: TRUE
441 * Failure: FALSE
443 BOOL WINAPI TlsFree(
444 DWORD index) /* [in] TLS Index to free */
446 PDB *process = PROCESS_Current();
447 DWORD mask;
448 DWORD *bits = process->tls_bits;
449 if (index >= 64)
451 SetLastError( ERROR_INVALID_PARAMETER );
452 return FALSE;
454 EnterCriticalSection( &process->crit_section );
455 if (index >= 32) bits++;
456 mask = (1 << (index & 31));
457 if (!(*bits & mask)) /* already free? */
459 LeaveCriticalSection( &process->crit_section );
460 SetLastError( ERROR_INVALID_PARAMETER );
461 return FALSE;
463 *bits &= ~mask;
464 NtCurrentTeb()->tls_array[index] = 0;
465 /* FIXME: should zero all other thread values */
466 LeaveCriticalSection( &process->crit_section );
467 return TRUE;
471 /**********************************************************************
472 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
474 * RETURNS
475 * Success: Value stored in calling thread's TLS slot for index
476 * Failure: 0 and GetLastError returns NO_ERROR
478 LPVOID WINAPI TlsGetValue(
479 DWORD index) /* [in] TLS index to retrieve value for */
481 if (index >= 64)
483 SetLastError( ERROR_INVALID_PARAMETER );
484 return NULL;
486 SetLastError( ERROR_SUCCESS );
487 return NtCurrentTeb()->tls_array[index];
491 /**********************************************************************
492 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
494 * RETURNS
495 * Success: TRUE
496 * Failure: FALSE
498 BOOL WINAPI TlsSetValue(
499 DWORD index, /* [in] TLS index to set value for */
500 LPVOID value) /* [in] Value to be stored */
502 if (index >= 64)
504 SetLastError( ERROR_INVALID_PARAMETER );
505 return FALSE;
507 NtCurrentTeb()->tls_array[index] = value;
508 return TRUE;
512 /***********************************************************************
513 * SetThreadContext [KERNEL32.670] Sets context of thread.
515 * RETURNS
516 * Success: TRUE
517 * Failure: FALSE
519 BOOL WINAPI SetThreadContext(
520 HANDLE handle, /* [in] Handle to thread with context */
521 const CONTEXT *context) /* [out] Address of context structure */
523 FIXME("not implemented\n" );
524 return TRUE;
527 /***********************************************************************
528 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
530 * RETURNS
531 * Success: TRUE
532 * Failure: FALSE
534 BOOL WINAPI GetThreadContext(
535 HANDLE handle, /* [in] Handle to thread with context */
536 CONTEXT *context) /* [out] Address of context structure */
538 #ifdef __i386__
539 WORD cs, ds;
541 FIXME("returning dummy info\n" );
543 /* make up some plausible values for segment registers */
544 GET_CS(cs);
545 GET_DS(ds);
546 context->SegCs = cs;
547 context->SegDs = ds;
548 context->SegEs = ds;
549 context->SegGs = ds;
550 context->SegSs = ds;
551 context->SegFs = ds;
552 #endif
553 return TRUE;
557 /**********************************************************************
558 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
560 * RETURNS
561 * Success: Thread's priority level.
562 * Failure: THREAD_PRIORITY_ERROR_RETURN
564 INT WINAPI GetThreadPriority(
565 HANDLE hthread) /* [in] Handle to thread */
567 INT ret = THREAD_PRIORITY_ERROR_RETURN;
568 struct get_thread_info_request *req = get_req_buffer();
569 req->handle = hthread;
570 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
571 return ret;
575 /**********************************************************************
576 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
578 * RETURNS
579 * Success: TRUE
580 * Failure: FALSE
582 BOOL WINAPI SetThreadPriority(
583 HANDLE hthread, /* [in] Handle to thread */
584 INT priority) /* [in] Thread priority level */
586 struct set_thread_info_request *req = get_req_buffer();
587 req->handle = hthread;
588 req->priority = priority;
589 req->mask = SET_THREAD_INFO_PRIORITY;
590 return !server_call( REQ_SET_THREAD_INFO );
594 /**********************************************************************
595 * SetThreadAffinityMask (KERNEL32.669)
597 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
599 struct set_thread_info_request *req = get_req_buffer();
600 req->handle = hThread;
601 req->affinity = dwThreadAffinityMask;
602 req->mask = SET_THREAD_INFO_AFFINITY;
603 if (server_call( REQ_SET_THREAD_INFO )) return 0;
604 return 1; /* FIXME: should return previous value */
608 /**********************************************************************
609 * TerminateThread [KERNEL32.685] Terminates a thread
611 * RETURNS
612 * Success: TRUE
613 * Failure: FALSE
615 BOOL WINAPI TerminateThread(
616 HANDLE handle, /* [in] Handle to thread */
617 DWORD exitcode) /* [in] Exit code for thread */
619 struct terminate_thread_request *req = get_req_buffer();
620 req->handle = handle;
621 req->exit_code = exitcode;
622 return !server_call( REQ_TERMINATE_THREAD );
626 /**********************************************************************
627 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
629 * RETURNS
630 * Success: TRUE
631 * Failure: FALSE
633 BOOL WINAPI GetExitCodeThread(
634 HANDLE hthread, /* [in] Handle to thread */
635 LPDWORD exitcode) /* [out] Address to receive termination status */
637 BOOL ret = FALSE;
638 struct get_thread_info_request *req = get_req_buffer();
639 req->handle = hthread;
640 if (!server_call( REQ_GET_THREAD_INFO ))
642 if (exitcode) *exitcode = req->exit_code;
643 ret = TRUE;
645 return ret;
649 /**********************************************************************
650 * ResumeThread [KERNEL32.587] Resumes a thread.
652 * Decrements a thread's suspend count. When count is zero, the
653 * execution of the thread is resumed.
655 * RETURNS
656 * Success: Previous suspend count
657 * Failure: 0xFFFFFFFF
658 * Already running: 0
660 DWORD WINAPI ResumeThread(
661 HANDLE hthread) /* [in] Identifies thread to restart */
663 DWORD ret = 0xffffffff;
664 struct resume_thread_request *req = get_req_buffer();
665 req->handle = hthread;
666 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
667 return ret;
671 /**********************************************************************
672 * SuspendThread [KERNEL32.681] Suspends a thread.
674 * RETURNS
675 * Success: Previous suspend count
676 * Failure: 0xFFFFFFFF
678 DWORD WINAPI SuspendThread(
679 HANDLE hthread) /* [in] Handle to the thread */
681 DWORD ret = 0xffffffff;
682 struct suspend_thread_request *req = get_req_buffer();
683 req->handle = hthread;
684 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
685 return ret;
689 /***********************************************************************
690 * QueueUserAPC (KERNEL32.566)
692 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
694 struct queue_apc_request *req = get_req_buffer();
695 req->handle = hthread;
696 req->func = func;
697 req->param = (void *)data;
698 return !server_call( REQ_QUEUE_APC );
702 /**********************************************************************
703 * GetThreadTimes [KERNEL32.???] Obtains timing information.
705 * NOTES
706 * What are the fields where these values are stored?
708 * RETURNS
709 * Success: TRUE
710 * Failure: FALSE
712 BOOL WINAPI GetThreadTimes(
713 HANDLE thread, /* [in] Specifies the thread of interest */
714 LPFILETIME creationtime, /* [out] When the thread was created */
715 LPFILETIME exittime, /* [out] When the thread was destroyed */
716 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
717 LPFILETIME usertime) /* [out] Time thread spent in user mode */
719 FIXME("(0x%08x): stub\n",thread);
720 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
721 return FALSE;
725 /**********************************************************************
726 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
728 * Attaches the input processing mechanism of one thread to that of
729 * another thread.
731 * RETURNS
732 * Success: TRUE
733 * Failure: FALSE
735 * TODO:
736 * 1. Reset the Key State (currenly per thread key state is not maintained)
738 BOOL WINAPI AttachThreadInput(
739 DWORD idAttach, /* [in] Thread to attach */
740 DWORD idAttachTo, /* [in] Thread to attach to */
741 BOOL fAttach) /* [in] Attach or detach */
743 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
744 BOOL16 bRet = 0;
746 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
748 /* A thread cannot attach to itself */
749 if ( idAttach == idAttachTo )
750 goto CLEANUP;
752 /* According to the docs this method should fail if a
753 * "Journal record" hook is installed. (attaches all input queues together)
755 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
756 goto CLEANUP;
758 /* Retrieve message queues corresponding to the thread id's */
759 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
760 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
762 /* Ensure we have message queues and that Src and Tgt threads
763 * are not system threads.
765 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
766 goto CLEANUP;
768 if (fAttach) /* Attach threads */
770 /* Only attach if currently detached */
771 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
773 /* First release the target threads perQData */
774 PERQDATA_Release( pTgtMsgQ->pQData );
776 /* Share a reference to the source threads perQDATA */
777 PERQDATA_Addref( pSrcMsgQ->pQData );
778 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
781 else /* Detach threads */
783 /* Only detach if currently attached */
784 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
786 /* First release the target threads perQData */
787 PERQDATA_Release( pTgtMsgQ->pQData );
789 /* Give the target thread its own private perQDATA once more */
790 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
794 /* TODO: Reset the Key State */
796 bRet = 1; /* Success */
798 CLEANUP:
800 /* Unlock the queues before returning */
801 if ( pSrcMsgQ )
802 QUEUE_Unlock( pSrcMsgQ );
803 if ( pTgtMsgQ )
804 QUEUE_Unlock( pTgtMsgQ );
806 return bRet;
809 /**********************************************************************
810 * VWin32_BoostThreadGroup [KERNEL.535]
812 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
814 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
817 /**********************************************************************
818 * VWin32_BoostThreadStatic [KERNEL.536]
820 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
822 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
825 /**********************************************************************
826 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
828 * RETURNS
829 * Success: TRUE
830 * Failure: FALSE
832 * NOTES
833 * Implemented in NT only (3.1 and above according to MS
835 BOOL WINAPI SetThreadLocale(
836 LCID lcid) /* [in] Locale identifier */
838 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
839 return FALSE;
843 /**********************************************************************
844 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
846 * RETURNS
847 * None.
849 #undef SetLastError
850 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
852 NtCurrentTeb()->last_error = error;
856 /***********************************************************************
857 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
859 * RETURNS
860 * Thread identifier of calling thread
862 #undef GetCurrentThreadId
863 DWORD WINAPI GetCurrentThreadId(void)
865 return (DWORD)NtCurrentTeb()->tid;