Got rid of THREAD_InitDone.
[wine/multimedia.git] / scheduler / thread.c
blob6c184b14ae93ae08b23d957fc4b585ecb2c04fea
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];
207 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
208 if (!thdb) return NULL;
209 thdb->process = pdb;
210 thdb->teb.except = (void *)-1;
211 thdb->teb.htask16 = pdb->task;
212 thdb->teb.self = &thdb->teb;
213 thdb->teb.flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
214 thdb->teb.tls_ptr = thdb->tls_array;
215 thdb->teb.process = pdb;
216 thdb->exit_code = 0x103; /* STILL_ACTIVE */
217 thdb->flags = flags;
218 thdb->socket = -1;
220 /* Allocate the TEB selector (%fs register) */
222 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
223 TRUE, FALSE );
224 if (!thdb->teb_sel) goto error;
226 /* Create the socket pair for server communication */
228 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
230 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
231 goto error;
233 thdb->socket = fd[0];
234 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
236 /* Create the thread on the server side */
238 request.pid = thdb->process->server_pid;
239 request.suspend = ((thdb->flags & CREATE_SUSPENDED) != 0);
240 request.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
241 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
242 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
243 thdb->teb.tid = reply.tid;
244 *server_handle = reply.handle;
246 /* Do the rest of the initialization */
248 if (!THREAD_InitTHDB( thdb, stack_size, alloc_stack16, sa )) goto error;
249 thdb->next = THREAD_First;
250 THREAD_First = thdb;
252 /* Install cleanup handler */
254 thdb->cleanup = SERVICE_AddObject( *server_handle,
255 THREAD_FreeTHDB, (ULONG_PTR)thdb );
256 return thdb;
258 error:
259 if (reply.handle != -1) CloseHandle( reply.handle );
260 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
261 HeapFree( SystemHeap, 0, thdb );
262 if (thdb->socket != -1) close( thdb->socket );
263 return NULL;
267 /***********************************************************************
268 * THREAD_Start
270 * Start execution of a newly created thread. Does not return.
272 static void THREAD_Start(void)
274 THDB *thdb = THREAD_Current();
275 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
276 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
277 PE_InitTls();
278 MODULE_DllThreadAttach( NULL );
280 if (thdb->process->flags & PDB32_DEBUGGED) DEBUG_SendCreateThreadEvent( func );
282 ExitThread( func( thdb->entry_arg ) );
286 /***********************************************************************
287 * CreateThread (KERNEL32.63)
289 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
290 LPTHREAD_START_ROUTINE start, LPVOID param,
291 DWORD flags, LPDWORD id )
293 int handle = -1;
294 THDB *thread = THREAD_Create( PROCESS_Current(), flags, stack, TRUE, sa, &handle );
295 if (!thread) return INVALID_HANDLE_VALUE;
296 thread->teb.flags |= TEBF_WIN32;
297 thread->entry_point = start;
298 thread->entry_arg = param;
299 thread->startup = THREAD_Start;
300 if (SYSDEPS_SpawnThread( thread ) == -1)
302 CloseHandle( handle );
303 return INVALID_HANDLE_VALUE;
305 if (id) *id = (DWORD)thread->teb.tid;
306 return handle;
310 /***********************************************************************
311 * ExitThread [KERNEL32.215] Ends a thread
313 * RETURNS
314 * None
316 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
318 MODULE_DllThreadDetach( NULL );
319 TerminateThread( GetCurrentThread(), code );
323 /***********************************************************************
324 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
326 * RETURNS
327 * Pseudohandle for the current thread
329 HANDLE WINAPI GetCurrentThread(void)
331 return CURRENT_THREAD_PSEUDOHANDLE;
335 /***********************************************************************
336 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
338 * RETURNS
339 * Thread identifier of calling thread
341 DWORD WINAPI GetCurrentThreadId(void)
343 return (DWORD)CURRENT()->tid;
347 /**********************************************************************
348 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
350 * RETURNS
351 * Calling thread's last error code value.
353 DWORD WINAPI GetLastError(void)
355 THDB *thread = THREAD_Current();
356 DWORD ret = thread->last_error;
357 TRACE("0x%lx\n",ret);
358 return ret;
362 /**********************************************************************
363 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
365 * RETURNS
366 * None.
368 void WINAPI SetLastError(
369 DWORD error) /* [in] Per-thread error code */
371 THDB *thread = THREAD_Current();
372 TRACE("%p error=0x%lx\n",thread,error);
373 thread->last_error = error;
377 /**********************************************************************
378 * SetLastErrorEx [USER32.485] Sets the last-error code.
380 * RETURNS
381 * None.
383 void WINAPI SetLastErrorEx(
384 DWORD error, /* [in] Per-thread error code */
385 DWORD type) /* [in] Error type */
387 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
388 switch(type) {
389 case 0:
390 break;
391 case SLE_ERROR:
392 case SLE_MINORERROR:
393 case SLE_WARNING:
394 /* Fall through for now */
395 default:
396 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
397 break;
399 SetLastError( error );
403 /**********************************************************************
404 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
406 * Allocates a thread local storage index
408 * RETURNS
409 * Success: TLS Index
410 * Failure: 0xFFFFFFFF
412 DWORD WINAPI TlsAlloc( void )
414 THDB *thread = THREAD_Current();
415 DWORD i, mask, ret = 0;
416 DWORD *bits = thread->process->tls_bits;
417 EnterCriticalSection( &thread->process->crit_section );
418 if (*bits == 0xffffffff)
420 bits++;
421 ret = 32;
422 if (*bits == 0xffffffff)
424 LeaveCriticalSection( &thread->process->crit_section );
425 SetLastError( ERROR_NO_MORE_ITEMS );
426 return 0xffffffff;
429 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
430 *bits |= mask;
431 LeaveCriticalSection( &thread->process->crit_section );
432 return ret + i;
436 /**********************************************************************
437 * TlsFree [KERNEL32.531] Releases a TLS index.
439 * Releases a thread local storage index, making it available for reuse
441 * RETURNS
442 * Success: TRUE
443 * Failure: FALSE
445 BOOL WINAPI TlsFree(
446 DWORD index) /* [in] TLS Index to free */
448 DWORD mask;
449 THDB *thread = THREAD_Current();
450 DWORD *bits = thread->process->tls_bits;
451 if (index >= 64)
453 SetLastError( ERROR_INVALID_PARAMETER );
454 return FALSE;
456 EnterCriticalSection( &thread->process->crit_section );
457 if (index >= 32) bits++;
458 mask = (1 << (index & 31));
459 if (!(*bits & mask)) /* already free? */
461 LeaveCriticalSection( &thread->process->crit_section );
462 SetLastError( ERROR_INVALID_PARAMETER );
463 return FALSE;
465 *bits &= ~mask;
466 thread->tls_array[index] = 0;
467 /* FIXME: should zero all other thread values */
468 LeaveCriticalSection( &thread->process->crit_section );
469 return TRUE;
473 /**********************************************************************
474 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
476 * RETURNS
477 * Success: Value stored in calling thread's TLS slot for index
478 * Failure: 0 and GetLastError returns NO_ERROR
480 LPVOID WINAPI TlsGetValue(
481 DWORD index) /* [in] TLS index to retrieve value for */
483 THDB *thread = THREAD_Current();
484 if (index >= 64)
486 SetLastError( ERROR_INVALID_PARAMETER );
487 return NULL;
489 SetLastError( ERROR_SUCCESS );
490 return thread->tls_array[index];
494 /**********************************************************************
495 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
497 * RETURNS
498 * Success: TRUE
499 * Failure: FALSE
501 BOOL WINAPI TlsSetValue(
502 DWORD index, /* [in] TLS index to set value for */
503 LPVOID value) /* [in] Value to be stored */
505 THDB *thread = THREAD_Current();
506 if (index >= 64)
508 SetLastError( ERROR_INVALID_PARAMETER );
509 return FALSE;
511 thread->tls_array[index] = value;
512 return TRUE;
516 /***********************************************************************
517 * SetThreadContext [KERNEL32.670] Sets context of thread.
519 * RETURNS
520 * Success: TRUE
521 * Failure: FALSE
523 BOOL WINAPI SetThreadContext(
524 HANDLE handle, /* [in] Handle to thread with context */
525 CONTEXT *context) /* [out] Address of context structure */
527 FIXME("not implemented\n" );
528 return TRUE;
531 /***********************************************************************
532 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
534 * RETURNS
535 * Success: TRUE
536 * Failure: FALSE
538 BOOL WINAPI GetThreadContext(
539 HANDLE handle, /* [in] Handle to thread with context */
540 CONTEXT *context) /* [out] Address of context structure */
542 WORD cs, ds;
544 FIXME("returning dummy info\n" );
546 /* make up some plausible values for segment registers */
547 GET_CS(cs);
548 GET_DS(ds);
549 context->SegCs = cs;
550 context->SegDs = ds;
551 context->SegEs = ds;
552 context->SegGs = ds;
553 context->SegSs = ds;
554 context->SegFs = ds;
555 return TRUE;
559 /**********************************************************************
560 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
562 * RETURNS
563 * Success: Thread's priority level.
564 * Failure: THREAD_PRIORITY_ERROR_RETURN
566 INT WINAPI GetThreadPriority(
567 HANDLE hthread) /* [in] Handle to thread */
569 struct get_thread_info_request req;
570 struct get_thread_info_reply reply;
571 req.handle = hthread;
572 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
573 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
574 return THREAD_PRIORITY_ERROR_RETURN;
575 return reply.priority;
579 /**********************************************************************
580 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
582 * RETURNS
583 * Success: TRUE
584 * Failure: FALSE
586 BOOL WINAPI SetThreadPriority(
587 HANDLE hthread, /* [in] Handle to thread */
588 INT priority) /* [in] Thread priority level */
590 struct set_thread_info_request req;
591 req.handle = hthread;
592 req.priority = priority;
593 req.mask = SET_THREAD_INFO_PRIORITY;
594 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
595 return !CLIENT_WaitReply( NULL, NULL, 0 );
599 /**********************************************************************
600 * SetThreadAffinityMask (KERNEL32.669)
602 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
604 struct set_thread_info_request req;
605 req.handle = hThread;
606 req.affinity = dwThreadAffinityMask;
607 req.mask = SET_THREAD_INFO_AFFINITY;
608 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
609 if (CLIENT_WaitReply( NULL, NULL, 0 )) return 0;
610 return 1; /* FIXME: should return previous value */
614 /**********************************************************************
615 * TerminateThread [KERNEL32.685] Terminates a thread
617 * RETURNS
618 * Success: TRUE
619 * Failure: FALSE
621 BOOL WINAPI TerminateThread(
622 HANDLE handle, /* [in] Handle to thread */
623 DWORD exitcode) /* [in] Exit code for thread */
625 struct terminate_thread_request req;
626 req.handle = handle;
627 req.exit_code = exitcode;
628 CLIENT_SendRequest( REQ_TERMINATE_THREAD, -1, 1, &req, sizeof(req) );
629 return !CLIENT_WaitReply( NULL, NULL, 0 );
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 struct get_thread_info_request req;
645 struct get_thread_info_reply reply;
646 req.handle = hthread;
647 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
648 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
649 if (exitcode) *exitcode = reply.exit_code;
650 return TRUE;
654 /**********************************************************************
655 * ResumeThread [KERNEL32.587] Resumes a thread.
657 * Decrements a thread's suspend count. When count is zero, the
658 * execution of the thread is resumed.
660 * RETURNS
661 * Success: Previous suspend count
662 * Failure: 0xFFFFFFFF
663 * Already running: 0
665 DWORD WINAPI ResumeThread(
666 HANDLE hthread) /* [in] Identifies thread to restart */
668 struct resume_thread_request req;
669 struct resume_thread_reply reply;
670 req.handle = hthread;
671 CLIENT_SendRequest( REQ_RESUME_THREAD, -1, 1, &req, sizeof(req) );
672 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
673 return reply.count;
677 /**********************************************************************
678 * SuspendThread [KERNEL32.681] Suspends a thread.
680 * RETURNS
681 * Success: Previous suspend count
682 * Failure: 0xFFFFFFFF
684 DWORD WINAPI SuspendThread(
685 HANDLE hthread) /* [in] Handle to the thread */
687 struct suspend_thread_request req;
688 struct suspend_thread_reply reply;
689 req.handle = hthread;
690 CLIENT_SendRequest( REQ_SUSPEND_THREAD, -1, 1, &req, sizeof(req) );
691 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
692 return reply.count;
696 /***********************************************************************
697 * QueueUserAPC (KERNEL32.566)
699 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
701 struct queue_apc_request req;
702 req.handle = hthread;
703 req.func = func;
704 req.param = (void *)data;
705 CLIENT_SendRequest( REQ_QUEUE_APC, -1, 1, &req, sizeof(req) );
706 return !CLIENT_WaitReply( NULL, NULL, 0 );
710 /**********************************************************************
711 * GetThreadTimes [KERNEL32.???] Obtains timing information.
713 * NOTES
714 * What are the fields where these values are stored?
716 * RETURNS
717 * Success: TRUE
718 * Failure: FALSE
720 BOOL WINAPI GetThreadTimes(
721 HANDLE thread, /* [in] Specifies the thread of interest */
722 LPFILETIME creationtime, /* [out] When the thread was created */
723 LPFILETIME exittime, /* [out] When the thread was destroyed */
724 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
725 LPFILETIME usertime) /* [out] Time thread spent in user mode */
727 FIXME("(0x%08x): stub\n",thread);
728 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
729 return FALSE;
733 /**********************************************************************
734 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
736 * Attaches the input processing mechanism of one thread to that of
737 * another thread.
739 * RETURNS
740 * Success: TRUE
741 * Failure: FALSE
743 * TODO:
744 * 1. Reset the Key State (currenly per thread key state is not maintained)
746 BOOL WINAPI AttachThreadInput(
747 DWORD idAttach, /* [in] Thread to attach */
748 DWORD idAttachTo, /* [in] Thread to attach to */
749 BOOL fAttach) /* [in] Attach or detach */
751 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
752 BOOL16 bRet = 0;
754 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
756 /* A thread cannot attach to itself */
757 if ( idAttach == idAttachTo )
758 goto CLEANUP;
760 /* According to the docs this method should fail if a
761 * "Journal record" hook is installed. (attaches all input queues together)
763 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
764 goto CLEANUP;
766 /* Retrieve message queues corresponding to the thread id's */
767 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
768 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
770 /* Ensure we have message queues and that Src and Tgt threads
771 * are not system threads.
773 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
774 goto CLEANUP;
776 if (fAttach) /* Attach threads */
778 /* Only attach if currently detached */
779 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
781 /* First release the target threads perQData */
782 PERQDATA_Release( pTgtMsgQ->pQData );
784 /* Share a reference to the source threads perQDATA */
785 PERQDATA_Addref( pSrcMsgQ->pQData );
786 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
789 else /* Detach threads */
791 /* Only detach if currently attached */
792 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
794 /* First release the target threads perQData */
795 PERQDATA_Release( pTgtMsgQ->pQData );
797 /* Give the target thread its own private perQDATA once more */
798 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
802 /* TODO: Reset the Key State */
804 bRet = 1; /* Success */
806 CLEANUP:
808 /* Unlock the queues before returning */
809 if ( pSrcMsgQ )
810 QUEUE_Unlock( pSrcMsgQ );
811 if ( pTgtMsgQ )
812 QUEUE_Unlock( pTgtMsgQ );
814 return bRet;
817 /**********************************************************************
818 * VWin32_BoostThreadGroup [KERNEL.535]
820 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
822 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
825 /**********************************************************************
826 * VWin32_BoostThreadStatic [KERNEL.536]
828 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
830 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
833 /**********************************************************************
834 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
836 * RETURNS
837 * Success: TRUE
838 * Failure: FALSE
840 * NOTES
841 * Implemented in NT only (3.1 and above according to MS
843 BOOL WINAPI SetThreadLocale(
844 LCID lcid) /* [in] Locale identifier */
846 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
847 return FALSE;