Rearrange winver detection code and cache the winver value we
[wine.git] / scheduler / thread.c
blobd86295affe2aefbeed2c103544727cf679b7d199
1 /*
2 * Win32 threads
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12 #include "wine/winbase16.h"
13 #include "thread.h"
14 #include "process.h"
15 #include "task.h"
16 #include "module.h"
17 #include "user.h"
18 #include "winerror.h"
19 #include "heap.h"
20 #include "selectors.h"
21 #include "winnt.h"
22 #include "server.h"
23 #include "services.h"
24 #include "stackframe.h"
25 #include "debugtools.h"
26 #include "queue.h"
27 #include "hook.h"
29 DEFAULT_DEBUG_CHANNEL(thread)
31 /* THDB of the initial thread */
32 static THDB initial_thdb;
34 /* Global thread list (FIXME: not thread-safe) */
35 THDB *THREAD_First = &initial_thdb;
37 /***********************************************************************
38 * THREAD_IsWin16
40 BOOL THREAD_IsWin16( THDB *thdb )
42 return !thdb || !(thdb->teb.flags & TEBF_WIN32);
45 /***********************************************************************
46 * THREAD_IdToTHDB
48 * Convert a thread id to a THDB, making sure it is valid.
50 THDB *THREAD_IdToTHDB( DWORD id )
52 THDB *thdb = THREAD_First;
54 if (!id) return THREAD_Current();
55 while (thdb)
57 if ((DWORD)thdb->teb.tid == id) return thdb;
58 thdb = thdb->next;
60 /* Allow task handles to be used; convert to main thread */
61 if ( IsTask16( id ) )
63 TDB *pTask = (TDB *)GlobalLock16( id );
64 if (pTask) return pTask->thdb;
66 SetLastError( ERROR_INVALID_PARAMETER );
67 return NULL;
71 /***********************************************************************
72 * THREAD_InitTHDB
74 * Initialization of a newly created THDB.
76 static BOOL THREAD_InitTHDB( THDB *thdb, DWORD stack_size, BOOL alloc_stack16,
77 LPSECURITY_ATTRIBUTES sa )
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 thdb->stack_base = VirtualAlloc(NULL, stack_size + SIGNAL_STACK_SIZE +
96 (alloc_stack16 ? 0x10000 : 0),
97 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
98 if (!thdb->stack_base) goto error;
99 /* Set a guard page at the bottom of the stack */
100 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
101 &old_prot );
102 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
103 thdb->teb.stack_low = thdb->stack_base;
104 thdb->signal_stack = thdb->teb.stack_top; /* start of signal stack */
106 /* Allocate the 16-bit stack selector */
108 if (alloc_stack16)
110 thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
111 0x10000, SEGMENT_DATA,
112 FALSE, FALSE );
113 if (!thdb->teb.stack_sel) goto error;
114 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel,
115 0x10000 - sizeof(STACK16FRAME) );
116 thdb->signal_stack = (char *)thdb->signal_stack + 0x10000;
119 /* Create the thread event */
121 if (!(thdb->event = CreateEventA( NULL, FALSE, FALSE, NULL ))) goto error;
122 thdb->event = ConvertToGlobalHandle( thdb->event );
123 return TRUE;
125 error:
126 if (thdb->event) CloseHandle( thdb->event );
127 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
128 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
129 return FALSE;
133 /***********************************************************************
134 * THREAD_FreeTHDB
136 * Free data structures associated with a thread.
137 * Must be called from the context of another thread.
139 void CALLBACK THREAD_FreeTHDB( ULONG_PTR arg )
141 THDB *thdb = (THDB *)arg;
142 THDB **pptr = &THREAD_First;
144 TRACE("(%p) called\n", thdb );
145 SERVICE_Delete( thdb->cleanup );
147 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
149 CloseHandle( thdb->event );
150 while (*pptr && (*pptr != thdb)) pptr = &(*pptr)->next;
151 if (*pptr) *pptr = thdb->next;
153 /* Free the associated memory */
155 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
156 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
157 close( thdb->socket );
158 VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
159 HeapFree( SystemHeap, HEAP_NO_SERIALIZE, thdb );
163 /***********************************************************************
164 * THREAD_CreateInitialThread
166 * Create the initial thread.
168 THDB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
170 initial_thdb.process = pdb;
171 initial_thdb.teb.except = (void *)-1;
172 initial_thdb.teb.self = &initial_thdb.teb;
173 initial_thdb.teb.flags = /* TEBF_WIN32 */ 0;
174 initial_thdb.teb.tls_ptr = initial_thdb.tls_array;
175 initial_thdb.teb.process = pdb;
176 initial_thdb.exit_code = 0x103; /* STILL_ACTIVE */
177 initial_thdb.socket = server_fd;
179 /* Allocate the TEB selector (%fs register) */
181 if (!(initial_thdb.teb_sel = SELECTOR_AllocBlock( &initial_thdb.teb, 0x1000,
182 SEGMENT_DATA, TRUE, FALSE )))
184 MESSAGE("Could not allocate fs register for initial thread\n" );
185 return NULL;
187 SYSDEPS_SetCurThread( &initial_thdb );
189 /* Now proceed with normal initialization */
191 if (CLIENT_InitThread()) return NULL;
192 if (!THREAD_InitTHDB( &initial_thdb, 0, TRUE, NULL )) return NULL;
193 return &initial_thdb;
197 /***********************************************************************
198 * THREAD_Create
200 THDB *THREAD_Create( PDB *pdb, DWORD flags, DWORD stack_size, BOOL alloc_stack16,
201 LPSECURITY_ATTRIBUTES sa, int *server_handle )
203 struct new_thread_request request;
204 struct new_thread_reply reply = { NULL, -1 };
205 int fd[2];
206 HANDLE cleanup_object;
208 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
209 if (!thdb) return NULL;
210 thdb->process = pdb;
211 thdb->teb.except = (void *)-1;
212 thdb->teb.htask16 = pdb->task;
213 thdb->teb.self = &thdb->teb;
214 thdb->teb.flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
215 thdb->teb.tls_ptr = thdb->tls_array;
216 thdb->teb.process = pdb;
217 thdb->exit_code = 0x103; /* STILL_ACTIVE */
218 thdb->flags = flags;
219 thdb->socket = -1;
221 /* Allocate the TEB selector (%fs register) */
223 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
224 TRUE, FALSE );
225 if (!thdb->teb_sel) goto error;
227 /* Create the socket pair for server communication */
229 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
231 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
232 goto error;
234 thdb->socket = fd[0];
235 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
237 /* Create the thread on the server side */
239 request.pid = thdb->process->server_pid;
240 request.suspend = ((thdb->flags & CREATE_SUSPENDED) != 0);
241 request.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
242 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
243 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
244 thdb->teb.tid = reply.tid;
245 *server_handle = reply.handle;
247 /* Do the rest of the initialization */
249 if (!THREAD_InitTHDB( thdb, stack_size, alloc_stack16, sa )) goto error;
250 thdb->next = THREAD_First;
251 THREAD_First = thdb;
253 /* Install cleanup handler */
254 if ( !DuplicateHandle( GetCurrentProcess(), *server_handle,
255 GetCurrentProcess(), &cleanup_object,
256 0, FALSE, DUPLICATE_SAME_ACCESS ) ) goto error;
257 thdb->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTHDB, (ULONG_PTR)thdb );
259 return thdb;
261 error:
262 if (reply.handle != -1) CloseHandle( reply.handle );
263 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
264 HeapFree( SystemHeap, 0, thdb );
265 if (thdb->socket != -1) close( thdb->socket );
266 return NULL;
270 /***********************************************************************
271 * THREAD_Start
273 * Start execution of a newly created thread. Does not return.
275 static void THREAD_Start(void)
277 THDB *thdb = THREAD_Current();
278 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
279 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
280 PE_InitTls();
281 MODULE_DllThreadAttach( NULL );
283 if (thdb->process->flags & PDB32_DEBUGGED) DEBUG_SendCreateThreadEvent( func );
285 ExitThread( func( thdb->entry_arg ) );
289 /***********************************************************************
290 * CreateThread (KERNEL32.63)
292 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
293 LPTHREAD_START_ROUTINE start, LPVOID param,
294 DWORD flags, LPDWORD id )
296 int handle = -1;
297 THDB *thread = THREAD_Create( PROCESS_Current(), flags, stack, TRUE, sa, &handle );
298 if (!thread) return INVALID_HANDLE_VALUE;
299 thread->teb.flags |= TEBF_WIN32;
300 thread->entry_point = start;
301 thread->entry_arg = param;
302 thread->startup = THREAD_Start;
303 if (SYSDEPS_SpawnThread( thread ) == -1)
305 CloseHandle( handle );
306 return INVALID_HANDLE_VALUE;
308 if (id) *id = (DWORD)thread->teb.tid;
309 return handle;
313 /***********************************************************************
314 * ExitThread [KERNEL32.215] Ends a thread
316 * RETURNS
317 * None
319 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
321 MODULE_DllThreadDetach( NULL );
322 TerminateThread( GetCurrentThread(), code );
326 /***********************************************************************
327 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
329 * RETURNS
330 * Pseudohandle for the current thread
332 HANDLE WINAPI GetCurrentThread(void)
334 return CURRENT_THREAD_PSEUDOHANDLE;
338 /***********************************************************************
339 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
341 * RETURNS
342 * Thread identifier of calling thread
344 DWORD WINAPI GetCurrentThreadId(void)
346 return (DWORD)CURRENT()->tid;
350 /**********************************************************************
351 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
353 * RETURNS
354 * Calling thread's last error code value.
356 DWORD WINAPI GetLastError(void)
358 THDB *thread = THREAD_Current();
359 DWORD ret = thread->last_error;
360 TRACE("0x%lx\n",ret);
361 return ret;
365 /**********************************************************************
366 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
368 * RETURNS
369 * None.
371 void WINAPI SetLastError(
372 DWORD error) /* [in] Per-thread error code */
374 THDB *thread = THREAD_Current();
375 TRACE("%p error=0x%lx\n",thread,error);
376 thread->last_error = error;
380 /**********************************************************************
381 * SetLastErrorEx [USER32.485] Sets the last-error code.
383 * RETURNS
384 * None.
386 void WINAPI SetLastErrorEx(
387 DWORD error, /* [in] Per-thread error code */
388 DWORD type) /* [in] Error type */
390 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
391 switch(type) {
392 case 0:
393 break;
394 case SLE_ERROR:
395 case SLE_MINORERROR:
396 case SLE_WARNING:
397 /* Fall through for now */
398 default:
399 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
400 break;
402 SetLastError( error );
406 /**********************************************************************
407 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
409 * Allocates a thread local storage index
411 * RETURNS
412 * Success: TLS Index
413 * Failure: 0xFFFFFFFF
415 DWORD WINAPI TlsAlloc( void )
417 THDB *thread = THREAD_Current();
418 DWORD i, mask, ret = 0;
419 DWORD *bits = thread->process->tls_bits;
420 EnterCriticalSection( &thread->process->crit_section );
421 if (*bits == 0xffffffff)
423 bits++;
424 ret = 32;
425 if (*bits == 0xffffffff)
427 LeaveCriticalSection( &thread->process->crit_section );
428 SetLastError( ERROR_NO_MORE_ITEMS );
429 return 0xffffffff;
432 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
433 *bits |= mask;
434 LeaveCriticalSection( &thread->process->crit_section );
435 return ret + i;
439 /**********************************************************************
440 * TlsFree [KERNEL32.531] Releases a TLS index.
442 * Releases a thread local storage index, making it available for reuse
444 * RETURNS
445 * Success: TRUE
446 * Failure: FALSE
448 BOOL WINAPI TlsFree(
449 DWORD index) /* [in] TLS Index to free */
451 DWORD mask;
452 THDB *thread = THREAD_Current();
453 DWORD *bits = thread->process->tls_bits;
454 if (index >= 64)
456 SetLastError( ERROR_INVALID_PARAMETER );
457 return FALSE;
459 EnterCriticalSection( &thread->process->crit_section );
460 if (index >= 32) bits++;
461 mask = (1 << (index & 31));
462 if (!(*bits & mask)) /* already free? */
464 LeaveCriticalSection( &thread->process->crit_section );
465 SetLastError( ERROR_INVALID_PARAMETER );
466 return FALSE;
468 *bits &= ~mask;
469 thread->tls_array[index] = 0;
470 /* FIXME: should zero all other thread values */
471 LeaveCriticalSection( &thread->process->crit_section );
472 return TRUE;
476 /**********************************************************************
477 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
479 * RETURNS
480 * Success: Value stored in calling thread's TLS slot for index
481 * Failure: 0 and GetLastError returns NO_ERROR
483 LPVOID WINAPI TlsGetValue(
484 DWORD index) /* [in] TLS index to retrieve value for */
486 THDB *thread = THREAD_Current();
487 if (index >= 64)
489 SetLastError( ERROR_INVALID_PARAMETER );
490 return NULL;
492 SetLastError( ERROR_SUCCESS );
493 return thread->tls_array[index];
497 /**********************************************************************
498 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
500 * RETURNS
501 * Success: TRUE
502 * Failure: FALSE
504 BOOL WINAPI TlsSetValue(
505 DWORD index, /* [in] TLS index to set value for */
506 LPVOID value) /* [in] Value to be stored */
508 THDB *thread = THREAD_Current();
509 if (index >= 64)
511 SetLastError( ERROR_INVALID_PARAMETER );
512 return FALSE;
514 thread->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 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 WORD cs, ds;
547 FIXME("returning dummy info\n" );
549 /* make up some plausible values for segment registers */
550 GET_CS(cs);
551 GET_DS(ds);
552 context->SegCs = cs;
553 context->SegDs = ds;
554 context->SegEs = ds;
555 context->SegGs = ds;
556 context->SegSs = ds;
557 context->SegFs = ds;
558 return TRUE;
562 /**********************************************************************
563 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
565 * RETURNS
566 * Success: Thread's priority level.
567 * Failure: THREAD_PRIORITY_ERROR_RETURN
569 INT WINAPI GetThreadPriority(
570 HANDLE hthread) /* [in] Handle to thread */
572 struct get_thread_info_request req;
573 struct get_thread_info_reply reply;
574 req.handle = hthread;
575 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
576 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
577 return THREAD_PRIORITY_ERROR_RETURN;
578 return reply.priority;
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;
594 req.handle = hthread;
595 req.priority = priority;
596 req.mask = SET_THREAD_INFO_PRIORITY;
597 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
598 return !CLIENT_WaitReply( NULL, NULL, 0 );
602 /**********************************************************************
603 * SetThreadAffinityMask (KERNEL32.669)
605 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
607 struct set_thread_info_request req;
608 req.handle = hThread;
609 req.affinity = dwThreadAffinityMask;
610 req.mask = SET_THREAD_INFO_AFFINITY;
611 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
612 if (CLIENT_WaitReply( NULL, NULL, 0 )) return 0;
613 return 1; /* FIXME: should return previous value */
617 /**********************************************************************
618 * TerminateThread [KERNEL32.685] Terminates a thread
620 * RETURNS
621 * Success: TRUE
622 * Failure: FALSE
624 BOOL WINAPI TerminateThread(
625 HANDLE handle, /* [in] Handle to thread */
626 DWORD exitcode) /* [in] Exit code for thread */
628 struct terminate_thread_request req;
629 req.handle = handle;
630 req.exit_code = exitcode;
631 CLIENT_SendRequest( REQ_TERMINATE_THREAD, -1, 1, &req, sizeof(req) );
632 return !CLIENT_WaitReply( NULL, NULL, 0 );
636 /**********************************************************************
637 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
639 * RETURNS
640 * Success: TRUE
641 * Failure: FALSE
643 BOOL WINAPI GetExitCodeThread(
644 HANDLE hthread, /* [in] Handle to thread */
645 LPDWORD exitcode) /* [out] Address to receive termination status */
647 struct get_thread_info_request req;
648 struct get_thread_info_reply reply;
649 req.handle = hthread;
650 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
651 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
652 if (exitcode) *exitcode = reply.exit_code;
653 return TRUE;
657 /**********************************************************************
658 * ResumeThread [KERNEL32.587] Resumes a thread.
660 * Decrements a thread's suspend count. When count is zero, the
661 * execution of the thread is resumed.
663 * RETURNS
664 * Success: Previous suspend count
665 * Failure: 0xFFFFFFFF
666 * Already running: 0
668 DWORD WINAPI ResumeThread(
669 HANDLE hthread) /* [in] Identifies thread to restart */
671 struct resume_thread_request req;
672 struct resume_thread_reply reply;
673 req.handle = hthread;
674 CLIENT_SendRequest( REQ_RESUME_THREAD, -1, 1, &req, sizeof(req) );
675 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
676 return reply.count;
680 /**********************************************************************
681 * SuspendThread [KERNEL32.681] Suspends a thread.
683 * RETURNS
684 * Success: Previous suspend count
685 * Failure: 0xFFFFFFFF
687 DWORD WINAPI SuspendThread(
688 HANDLE hthread) /* [in] Handle to the thread */
690 struct suspend_thread_request req;
691 struct suspend_thread_reply reply;
692 req.handle = hthread;
693 CLIENT_SendRequest( REQ_SUSPEND_THREAD, -1, 1, &req, sizeof(req) );
694 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
695 return reply.count;
699 /***********************************************************************
700 * QueueUserAPC (KERNEL32.566)
702 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
704 struct queue_apc_request req;
705 req.handle = hthread;
706 req.func = func;
707 req.param = (void *)data;
708 CLIENT_SendRequest( REQ_QUEUE_APC, -1, 1, &req, sizeof(req) );
709 return !CLIENT_WaitReply( NULL, NULL, 0 );
713 /**********************************************************************
714 * GetThreadTimes [KERNEL32.???] Obtains timing information.
716 * NOTES
717 * What are the fields where these values are stored?
719 * RETURNS
720 * Success: TRUE
721 * Failure: FALSE
723 BOOL WINAPI GetThreadTimes(
724 HANDLE thread, /* [in] Specifies the thread of interest */
725 LPFILETIME creationtime, /* [out] When the thread was created */
726 LPFILETIME exittime, /* [out] When the thread was destroyed */
727 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
728 LPFILETIME usertime) /* [out] Time thread spent in user mode */
730 FIXME("(0x%08x): stub\n",thread);
731 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
732 return FALSE;
736 /**********************************************************************
737 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
739 * Attaches the input processing mechanism of one thread to that of
740 * another thread.
742 * RETURNS
743 * Success: TRUE
744 * Failure: FALSE
746 * TODO:
747 * 1. Reset the Key State (currenly per thread key state is not maintained)
749 BOOL WINAPI AttachThreadInput(
750 DWORD idAttach, /* [in] Thread to attach */
751 DWORD idAttachTo, /* [in] Thread to attach to */
752 BOOL fAttach) /* [in] Attach or detach */
754 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
755 BOOL16 bRet = 0;
757 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
759 /* A thread cannot attach to itself */
760 if ( idAttach == idAttachTo )
761 goto CLEANUP;
763 /* According to the docs this method should fail if a
764 * "Journal record" hook is installed. (attaches all input queues together)
766 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
767 goto CLEANUP;
769 /* Retrieve message queues corresponding to the thread id's */
770 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
771 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
773 /* Ensure we have message queues and that Src and Tgt threads
774 * are not system threads.
776 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
777 goto CLEANUP;
779 if (fAttach) /* Attach threads */
781 /* Only attach if currently detached */
782 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
784 /* First release the target threads perQData */
785 PERQDATA_Release( pTgtMsgQ->pQData );
787 /* Share a reference to the source threads perQDATA */
788 PERQDATA_Addref( pSrcMsgQ->pQData );
789 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
792 else /* Detach threads */
794 /* Only detach if currently attached */
795 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
797 /* First release the target threads perQData */
798 PERQDATA_Release( pTgtMsgQ->pQData );
800 /* Give the target thread its own private perQDATA once more */
801 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
805 /* TODO: Reset the Key State */
807 bRet = 1; /* Success */
809 CLEANUP:
811 /* Unlock the queues before returning */
812 if ( pSrcMsgQ )
813 QUEUE_Unlock( pSrcMsgQ );
814 if ( pTgtMsgQ )
815 QUEUE_Unlock( pTgtMsgQ );
817 return bRet;
820 /**********************************************************************
821 * VWin32_BoostThreadGroup [KERNEL.535]
823 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
825 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
828 /**********************************************************************
829 * VWin32_BoostThreadStatic [KERNEL.536]
831 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
833 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
836 /**********************************************************************
837 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
839 * RETURNS
840 * Success: TRUE
841 * Failure: FALSE
843 * NOTES
844 * Implemented in NT only (3.1 and above according to MS
846 BOOL WINAPI SetThreadLocale(
847 LCID lcid) /* [in] Locale identifier */
849 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
850 return FALSE;