Made debug events and contexts standard objects.
[wine/multimedia.git] / scheduler / thread.c
blob591723eef4cba087cf7b1ae11b56ffd08b63fc30
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, (DWORD)teb->tid, 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 VirtualFree( teb, 0, MEM_FREE );
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 * NOTES:
207 * Native NT dlls are using the space left on the allocated page
208 * the first allocated TEB on NT is at 0x7ffde000, since we can't
209 * allocate in this area and don't support a granularity of 4kb
210 * yet we leave it to VirtualAlloc to choose an address.
212 TEB *THREAD_Create( PDB *pdb, DWORD flags, DWORD stack_size, BOOL alloc_stack16,
213 LPSECURITY_ATTRIBUTES sa, int *server_handle )
215 struct new_thread_request *req = get_req_buffer();
216 int fd[2];
217 HANDLE cleanup_object;
219 TEB *teb = VirtualAlloc(0, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
220 if (!teb) return NULL;
221 teb->except = (void *)-1;
222 teb->htask16 = pdb->task;
223 teb->self = teb;
224 teb->flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
225 teb->tls_ptr = teb->tls_array;
226 teb->process = pdb;
227 teb->exit_code = 0x103; /* STILL_ACTIVE */
228 teb->socket = -1;
230 /* Allocate the TEB selector (%fs register) */
232 *server_handle = -1;
233 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
234 if (!teb->teb_sel) goto error;
236 /* Create the socket pair for server communication */
238 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
240 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
241 goto error;
243 teb->socket = fd[0];
244 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
246 /* Create the thread on the server side */
248 req->pid = teb->process->server_pid;
249 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
250 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
251 if (server_call_fd( REQ_NEW_THREAD, fd[1], NULL )) goto error;
252 teb->tid = req->tid;
253 *server_handle = req->handle;
255 /* Do the rest of the initialization */
257 if (!THREAD_InitTEB( teb, stack_size, alloc_stack16, sa )) goto error;
258 teb->next = THREAD_First;
259 THREAD_First = teb;
261 /* Install cleanup handler */
262 if ( !DuplicateHandle( GetCurrentProcess(), *server_handle,
263 GetCurrentProcess(), &cleanup_object,
264 0, FALSE, DUPLICATE_SAME_ACCESS ) ) goto error;
265 teb->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTEB, (ULONG_PTR)teb );
267 TRACE("(%p) succeeded\n", teb);
268 return teb;
270 error:
271 if (*server_handle != -1) CloseHandle( *server_handle );
272 if (teb->teb_sel) SELECTOR_FreeBlock( teb->teb_sel, 1 );
273 if (teb->socket != -1) close( teb->socket );
274 VirtualFree( teb, 0, MEM_FREE );
275 return NULL;
279 /***********************************************************************
280 * THREAD_Start
282 * Start execution of a newly created thread. Does not return.
284 static void THREAD_Start(void)
286 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
287 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, (DWORD)NtCurrentTeb()->tid, 0 );
288 PE_InitTls();
289 MODULE_DllThreadAttach( NULL );
291 if (NtCurrentTeb()->process->flags & PDB32_DEBUGGED) DEBUG_SendCreateThreadEvent( func );
293 ExitThread( func( NtCurrentTeb()->entry_arg ) );
297 /***********************************************************************
298 * CreateThread (KERNEL32.63)
300 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
301 LPTHREAD_START_ROUTINE start, LPVOID param,
302 DWORD flags, LPDWORD id )
304 int handle = -1;
305 TEB *teb = THREAD_Create( PROCESS_Current(), flags, stack, TRUE, sa, &handle );
306 if (!teb) return 0;
307 teb->flags |= TEBF_WIN32;
308 teb->entry_point = start;
309 teb->entry_arg = param;
310 teb->startup = THREAD_Start;
311 if (SYSDEPS_SpawnThread( teb ) == -1)
313 CloseHandle( handle );
314 return 0;
316 if (id) *id = (DWORD)teb->tid;
317 return handle;
320 /***********************************************************************
321 * CreateThread16 (KERNEL.441)
323 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
325 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
326 DWORD param = ((DWORD *)threadArgs)[1];
327 HeapFree( GetProcessHeap(), 0, threadArgs );
329 ((LPDWORD)CURRENT_STACK16)[-1] = param;
330 return CallTo16Long( start, sizeof(DWORD) );
332 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
333 FARPROC16 start, SEGPTR param,
334 DWORD flags, LPDWORD id )
336 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
337 if (!threadArgs) return INVALID_HANDLE_VALUE;
338 threadArgs[0] = (DWORD)start;
339 threadArgs[1] = (DWORD)param;
341 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
345 /***********************************************************************
346 * ExitThread [KERNEL32.215] Ends a thread
348 * RETURNS
349 * None
351 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
353 MODULE_DllThreadDetach( NULL );
354 TerminateThread( GetCurrentThread(), code );
358 /***********************************************************************
359 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
361 * RETURNS
362 * Pseudohandle for the current thread
364 HANDLE WINAPI GetCurrentThread(void)
366 return CURRENT_THREAD_PSEUDOHANDLE;
370 /**********************************************************************
371 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
373 * RETURNS
374 * Calling thread's last error code value.
376 DWORD WINAPI GetLastError(void)
378 return NtCurrentTeb()->last_error;
382 /**********************************************************************
383 * SetLastErrorEx [USER32.485] Sets the last-error code.
385 * RETURNS
386 * None.
388 void WINAPI SetLastErrorEx(
389 DWORD error, /* [in] Per-thread error code */
390 DWORD type) /* [in] Error type */
392 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
393 switch(type) {
394 case 0:
395 break;
396 case SLE_ERROR:
397 case SLE_MINORERROR:
398 case SLE_WARNING:
399 /* Fall through for now */
400 default:
401 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
402 break;
404 SetLastError( error );
408 /**********************************************************************
409 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
411 * Allocates a thread local storage index
413 * RETURNS
414 * Success: TLS Index
415 * Failure: 0xFFFFFFFF
417 DWORD WINAPI TlsAlloc( void )
419 PDB *process = PROCESS_Current();
420 DWORD i, mask, ret = 0;
421 DWORD *bits = process->tls_bits;
422 EnterCriticalSection( &process->crit_section );
423 if (*bits == 0xffffffff)
425 bits++;
426 ret = 32;
427 if (*bits == 0xffffffff)
429 LeaveCriticalSection( &process->crit_section );
430 SetLastError( ERROR_NO_MORE_ITEMS );
431 return 0xffffffff;
434 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
435 *bits |= mask;
436 LeaveCriticalSection( &process->crit_section );
437 return ret + i;
441 /**********************************************************************
442 * TlsFree [KERNEL32.531] Releases a TLS index.
444 * Releases a thread local storage index, making it available for reuse
446 * RETURNS
447 * Success: TRUE
448 * Failure: FALSE
450 BOOL WINAPI TlsFree(
451 DWORD index) /* [in] TLS Index to free */
453 PDB *process = PROCESS_Current();
454 DWORD mask;
455 DWORD *bits = process->tls_bits;
456 if (index >= 64)
458 SetLastError( ERROR_INVALID_PARAMETER );
459 return FALSE;
461 EnterCriticalSection( &process->crit_section );
462 if (index >= 32) bits++;
463 mask = (1 << (index & 31));
464 if (!(*bits & mask)) /* already free? */
466 LeaveCriticalSection( &process->crit_section );
467 SetLastError( ERROR_INVALID_PARAMETER );
468 return FALSE;
470 *bits &= ~mask;
471 NtCurrentTeb()->tls_array[index] = 0;
472 /* FIXME: should zero all other thread values */
473 LeaveCriticalSection( &process->crit_section );
474 return TRUE;
478 /**********************************************************************
479 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
481 * RETURNS
482 * Success: Value stored in calling thread's TLS slot for index
483 * Failure: 0 and GetLastError returns NO_ERROR
485 LPVOID WINAPI TlsGetValue(
486 DWORD index) /* [in] TLS index to retrieve value for */
488 if (index >= 64)
490 SetLastError( ERROR_INVALID_PARAMETER );
491 return NULL;
493 SetLastError( ERROR_SUCCESS );
494 return NtCurrentTeb()->tls_array[index];
498 /**********************************************************************
499 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
501 * RETURNS
502 * Success: TRUE
503 * Failure: FALSE
505 BOOL WINAPI TlsSetValue(
506 DWORD index, /* [in] TLS index to set value for */
507 LPVOID value) /* [in] Value to be stored */
509 if (index >= 64)
511 SetLastError( ERROR_INVALID_PARAMETER );
512 return FALSE;
514 NtCurrentTeb()->tls_array[index] = value;
515 return TRUE;
519 /***********************************************************************
520 * SetThreadContext [KERNEL32.670] Sets context of thread.
522 * RETURNS
523 * Success: TRUE
524 * Failure: FALSE
526 BOOL WINAPI SetThreadContext(
527 HANDLE handle, /* [in] Handle to thread with context */
528 const CONTEXT *context) /* [out] Address of context structure */
530 FIXME("not implemented\n" );
531 return TRUE;
534 /***********************************************************************
535 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
537 * RETURNS
538 * Success: TRUE
539 * Failure: FALSE
541 BOOL WINAPI GetThreadContext(
542 HANDLE handle, /* [in] Handle to thread with context */
543 CONTEXT *context) /* [out] Address of context structure */
545 #ifdef __i386__
546 WORD cs, ds;
548 FIXME("returning dummy info\n" );
550 /* make up some plausible values for segment registers */
551 GET_CS(cs);
552 GET_DS(ds);
553 context->SegCs = cs;
554 context->SegDs = ds;
555 context->SegEs = ds;
556 context->SegGs = ds;
557 context->SegSs = ds;
558 context->SegFs = ds;
559 #endif
560 return TRUE;
564 /**********************************************************************
565 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
567 * RETURNS
568 * Success: Thread's priority level.
569 * Failure: THREAD_PRIORITY_ERROR_RETURN
571 INT WINAPI GetThreadPriority(
572 HANDLE hthread) /* [in] Handle to thread */
574 INT ret = THREAD_PRIORITY_ERROR_RETURN;
575 struct get_thread_info_request *req = get_req_buffer();
576 req->handle = hthread;
577 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
578 return ret;
582 /**********************************************************************
583 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
585 * RETURNS
586 * Success: TRUE
587 * Failure: FALSE
589 BOOL WINAPI SetThreadPriority(
590 HANDLE hthread, /* [in] Handle to thread */
591 INT priority) /* [in] Thread priority level */
593 struct set_thread_info_request *req = get_req_buffer();
594 req->handle = hthread;
595 req->priority = priority;
596 req->mask = SET_THREAD_INFO_PRIORITY;
597 return !server_call( REQ_SET_THREAD_INFO );
601 /**********************************************************************
602 * SetThreadAffinityMask (KERNEL32.669)
604 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
606 struct set_thread_info_request *req = get_req_buffer();
607 req->handle = hThread;
608 req->affinity = dwThreadAffinityMask;
609 req->mask = SET_THREAD_INFO_AFFINITY;
610 if (server_call( REQ_SET_THREAD_INFO )) return 0;
611 return 1; /* FIXME: should return previous value */
615 /**********************************************************************
616 * TerminateThread [KERNEL32.685] Terminates a thread
618 * RETURNS
619 * Success: TRUE
620 * Failure: FALSE
622 BOOL WINAPI TerminateThread(
623 HANDLE handle, /* [in] Handle to thread */
624 DWORD exitcode) /* [in] Exit code for thread */
626 struct terminate_thread_request *req = get_req_buffer();
627 req->handle = handle;
628 req->exit_code = exitcode;
629 return !server_call( REQ_TERMINATE_THREAD );
633 /**********************************************************************
634 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
636 * RETURNS
637 * Success: TRUE
638 * Failure: FALSE
640 BOOL WINAPI GetExitCodeThread(
641 HANDLE hthread, /* [in] Handle to thread */
642 LPDWORD exitcode) /* [out] Address to receive termination status */
644 BOOL ret = FALSE;
645 struct get_thread_info_request *req = get_req_buffer();
646 req->handle = hthread;
647 if (!server_call( REQ_GET_THREAD_INFO ))
649 if (exitcode) *exitcode = req->exit_code;
650 ret = TRUE;
652 return ret;
656 /**********************************************************************
657 * ResumeThread [KERNEL32.587] Resumes a thread.
659 * Decrements a thread's suspend count. When count is zero, the
660 * execution of the thread is resumed.
662 * RETURNS
663 * Success: Previous suspend count
664 * Failure: 0xFFFFFFFF
665 * Already running: 0
667 DWORD WINAPI ResumeThread(
668 HANDLE hthread) /* [in] Identifies thread to restart */
670 DWORD ret = 0xffffffff;
671 struct resume_thread_request *req = get_req_buffer();
672 req->handle = hthread;
673 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
674 return ret;
678 /**********************************************************************
679 * SuspendThread [KERNEL32.681] Suspends a thread.
681 * RETURNS
682 * Success: Previous suspend count
683 * Failure: 0xFFFFFFFF
685 DWORD WINAPI SuspendThread(
686 HANDLE hthread) /* [in] Handle to the thread */
688 DWORD ret = 0xffffffff;
689 struct suspend_thread_request *req = get_req_buffer();
690 req->handle = hthread;
691 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
692 return ret;
696 /***********************************************************************
697 * QueueUserAPC (KERNEL32.566)
699 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
701 struct queue_apc_request *req = get_req_buffer();
702 req->handle = hthread;
703 req->func = func;
704 req->param = (void *)data;
705 return !server_call( REQ_QUEUE_APC );
709 /**********************************************************************
710 * GetThreadTimes [KERNEL32.???] Obtains timing information.
712 * NOTES
713 * What are the fields where these values are stored?
715 * RETURNS
716 * Success: TRUE
717 * Failure: FALSE
719 BOOL WINAPI GetThreadTimes(
720 HANDLE thread, /* [in] Specifies the thread of interest */
721 LPFILETIME creationtime, /* [out] When the thread was created */
722 LPFILETIME exittime, /* [out] When the thread was destroyed */
723 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
724 LPFILETIME usertime) /* [out] Time thread spent in user mode */
726 FIXME("(0x%08x): stub\n",thread);
727 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
728 return FALSE;
732 /**********************************************************************
733 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
735 * Attaches the input processing mechanism of one thread to that of
736 * another thread.
738 * RETURNS
739 * Success: TRUE
740 * Failure: FALSE
742 * TODO:
743 * 1. Reset the Key State (currenly per thread key state is not maintained)
745 BOOL WINAPI AttachThreadInput(
746 DWORD idAttach, /* [in] Thread to attach */
747 DWORD idAttachTo, /* [in] Thread to attach to */
748 BOOL fAttach) /* [in] Attach or detach */
750 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
751 BOOL16 bRet = 0;
753 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
755 /* A thread cannot attach to itself */
756 if ( idAttach == idAttachTo )
757 goto CLEANUP;
759 /* According to the docs this method should fail if a
760 * "Journal record" hook is installed. (attaches all input queues together)
762 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
763 goto CLEANUP;
765 /* Retrieve message queues corresponding to the thread id's */
766 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
767 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
769 /* Ensure we have message queues and that Src and Tgt threads
770 * are not system threads.
772 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
773 goto CLEANUP;
775 if (fAttach) /* Attach threads */
777 /* Only attach if currently detached */
778 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
780 /* First release the target threads perQData */
781 PERQDATA_Release( pTgtMsgQ->pQData );
783 /* Share a reference to the source threads perQDATA */
784 PERQDATA_Addref( pSrcMsgQ->pQData );
785 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
788 else /* Detach threads */
790 /* Only detach if currently attached */
791 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
793 /* First release the target threads perQData */
794 PERQDATA_Release( pTgtMsgQ->pQData );
796 /* Give the target thread its own private perQDATA once more */
797 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
801 /* TODO: Reset the Key State */
803 bRet = 1; /* Success */
805 CLEANUP:
807 /* Unlock the queues before returning */
808 if ( pSrcMsgQ )
809 QUEUE_Unlock( pSrcMsgQ );
810 if ( pTgtMsgQ )
811 QUEUE_Unlock( pTgtMsgQ );
813 return bRet;
816 /**********************************************************************
817 * VWin32_BoostThreadGroup [KERNEL.535]
819 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
821 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
824 /**********************************************************************
825 * VWin32_BoostThreadStatic [KERNEL.536]
827 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
829 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
832 /**********************************************************************
833 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
835 * RETURNS
836 * Success: TRUE
837 * Failure: FALSE
839 * NOTES
840 * Implemented in NT only (3.1 and above according to MS
842 BOOL WINAPI SetThreadLocale(
843 LCID lcid) /* [in] Locale identifier */
845 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
846 return FALSE;
850 /**********************************************************************
851 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
853 * RETURNS
854 * None.
856 #undef SetLastError
857 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
859 NtCurrentTeb()->last_error = error;
863 /***********************************************************************
864 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
866 * RETURNS
867 * Thread identifier of calling thread
869 #undef GetCurrentThreadId
870 DWORD WINAPI GetCurrentThreadId(void)
872 return (DWORD)NtCurrentTeb()->tid;