Removed check for union semun; cleaned up a bit.
[wine/multimedia.git] / scheduler / thread.c
blobc3a39b2c3bab2a3fe3f68911db3813ba1bc3a5f0
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 "stackframe.h"
24 #include "debug.h"
25 #include "queue.h"
26 #include "hook.h"
28 #ifndef __i386__
29 THDB *pCurrentThread;
30 #endif
32 /* Is threading code initialized? */
33 BOOL THREAD_InitDone = FALSE;
35 /* THDB of the initial thread */
36 static THDB initial_thdb;
38 /* Global thread list (FIXME: not thread-safe) */
39 THDB *THREAD_First = &initial_thdb;
41 /***********************************************************************
42 * THREAD_Current
44 * Return the current thread THDB pointer.
46 THDB *THREAD_Current(void)
48 if (!THREAD_InitDone) return NULL;
49 return (THDB *)((char *)NtCurrentTeb() - (int)&((THDB *)0)->teb);
52 /***********************************************************************
53 * THREAD_IsWin16
55 BOOL THREAD_IsWin16( THDB *thdb )
57 return !thdb || !(thdb->teb.flags & TEBF_WIN32);
60 /***********************************************************************
61 * THREAD_IdToTHDB
63 * Convert a thread id to a THDB, making sure it is valid.
65 THDB *THREAD_IdToTHDB( DWORD id )
67 THDB *thdb = THREAD_First;
69 if (!id) return THREAD_Current();
70 while (thdb)
72 if ((DWORD)thdb->server_tid == id) return thdb;
73 thdb = thdb->next;
75 /* Allow task handles to be used; convert to main thread */
76 if ( IsTask16( id ) )
78 TDB *pTask = (TDB *)GlobalLock16( id );
79 if (pTask) return pTask->thdb;
81 SetLastError( ERROR_INVALID_PARAMETER );
82 return NULL;
86 /***********************************************************************
87 * THREAD_InitTHDB
89 * Initialization of a newly created THDB.
91 static BOOL THREAD_InitTHDB( THDB *thdb, DWORD stack_size, BOOL alloc_stack16,
92 LPSECURITY_ATTRIBUTES sa )
94 DWORD old_prot;
96 /* Allocate the stack */
98 /* FIXME:
99 * If stacksize smaller than 1 MB, allocate 1MB
100 * (one program wanted only 10 kB, which is recommendable, but some WINE
101 * functions, noteably in the files subdir, push HUGE structures and
102 * arrays on the stack. They probably shouldn't.)
103 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
104 * but this could give more or less unexplainable crashes.)
106 if (stack_size<1024*1024)
107 stack_size = 1024 * 1024;
108 if (stack_size >= 16*1024*1024)
109 WARN(thread,"Thread stack size is %ld MB.\n",stack_size/1024/1024);
110 thdb->stack_base = VirtualAlloc(NULL,
111 stack_size + (alloc_stack16 ? 0x10000 : 0),
112 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
113 if (!thdb->stack_base) goto error;
114 /* Set a guard page at the bottom of the stack */
115 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
116 &old_prot );
117 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
118 thdb->teb.stack_low = thdb->stack_base;
119 thdb->exit_stack = thdb->teb.stack_top;
121 /* Allocate the 16-bit stack selector */
123 if (alloc_stack16)
125 thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
126 0x10000, SEGMENT_DATA,
127 FALSE, FALSE );
128 if (!thdb->teb.stack_sel) goto error;
129 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel,
130 0x10000 - sizeof(STACK16FRAME) );
133 /* Create the thread event */
135 if (!(thdb->event = CreateEventA( NULL, FALSE, FALSE, NULL ))) goto error;
136 thdb->event = ConvertToGlobalHandle( thdb->event );
137 return TRUE;
139 error:
140 if (thdb->event) CloseHandle( thdb->event );
141 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
142 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
143 return FALSE;
147 /***********************************************************************
148 * THREAD_FreeTHDB
150 * Free data structures associated with a thread.
151 * Must be called from the context of another thread.
153 void THREAD_FreeTHDB( THDB *thdb )
155 THDB **pptr = &THREAD_First;
157 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0, 0 );
159 CloseHandle( thdb->event );
160 while (*pptr && (*pptr != thdb)) pptr = &(*pptr)->next;
161 if (*pptr) *pptr = thdb->next;
163 /* Free the associated memory */
165 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
166 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
167 close( thdb->socket );
168 VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
169 HeapFree( SystemHeap, HEAP_NO_SERIALIZE, thdb );
173 /***********************************************************************
174 * THREAD_CreateInitialThread
176 * Create the initial thread.
178 THDB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
180 initial_thdb.process = pdb;
181 initial_thdb.teb.except = (void *)-1;
182 initial_thdb.teb.self = &initial_thdb.teb;
183 initial_thdb.teb.flags = /* TEBF_WIN32 */ 0;
184 initial_thdb.teb.tls_ptr = initial_thdb.tls_array;
185 initial_thdb.teb.process = pdb;
186 initial_thdb.exit_code = 0x103; /* STILL_ACTIVE */
187 initial_thdb.socket = server_fd;
189 /* Allocate the TEB selector (%fs register) */
191 if (!(initial_thdb.teb_sel = SELECTOR_AllocBlock( &initial_thdb.teb, 0x1000,
192 SEGMENT_DATA, TRUE, FALSE )))
194 MSG("Could not allocate fs register for initial thread\n" );
195 return NULL;
197 SET_CUR_THREAD( &initial_thdb );
198 THREAD_InitDone = TRUE;
200 /* Now proceed with normal initialization */
202 if (CLIENT_InitThread()) return NULL;
203 if (!THREAD_InitTHDB( &initial_thdb, 0, TRUE, NULL )) return NULL;
204 return &initial_thdb;
208 /***********************************************************************
209 * THREAD_Create
211 THDB *THREAD_Create( PDB *pdb, DWORD flags, DWORD stack_size, BOOL alloc_stack16,
212 LPSECURITY_ATTRIBUTES sa, int *server_handle )
214 struct new_thread_request request;
215 struct new_thread_reply reply = { NULL, -1 };
216 int fd[2];
218 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
219 if (!thdb) return NULL;
220 thdb->process = pdb;
221 thdb->teb.except = (void *)-1;
222 thdb->teb.htask16 = pdb->task;
223 thdb->teb.self = &thdb->teb;
224 thdb->teb.flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
225 thdb->teb.tls_ptr = thdb->tls_array;
226 thdb->teb.process = pdb;
227 thdb->exit_code = 0x103; /* STILL_ACTIVE */
228 thdb->flags = flags;
229 thdb->socket = -1;
231 /* Allocate the TEB selector (%fs register) */
233 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
234 TRUE, FALSE );
235 if (!thdb->teb_sel) goto error;
237 /* Create the socket pair for server communication */
239 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
241 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
242 goto error;
244 thdb->socket = fd[0];
245 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
247 /* Create the thread on the server side */
249 request.pid = thdb->process->server_pid;
250 request.suspend = ((thdb->flags & CREATE_SUSPENDED) != 0);
251 request.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
252 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
253 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
254 thdb->server_tid = reply.tid;
255 *server_handle = reply.handle;
257 /* Do the rest of the initialization */
259 if (!THREAD_InitTHDB( thdb, stack_size, alloc_stack16, sa )) goto error;
260 thdb->next = THREAD_First;
261 THREAD_First = thdb;
262 return thdb;
264 error:
265 if (reply.handle != -1) CloseHandle( reply.handle );
266 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
267 HeapFree( SystemHeap, 0, thdb );
268 if (thdb->socket != -1) close( thdb->socket );
269 return NULL;
273 /***********************************************************************
274 * THREAD_Start
276 * Start execution of a newly created thread. Does not return.
278 static void THREAD_Start(void)
280 THDB *thdb = THREAD_Current();
281 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
282 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0, 0 );
283 PE_InitTls();
284 MODULE_DllThreadAttach( NULL );
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->server_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 THDB *thdb = THREAD_Current();
347 assert( thdb );
348 assert( thdb->server_tid );
349 return (DWORD)thdb->server_tid;
353 /**********************************************************************
354 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
356 * RETURNS
357 * Calling thread's last error code value.
359 DWORD WINAPI GetLastError(void)
361 THDB *thread = THREAD_Current();
362 DWORD ret = thread->last_error;
363 TRACE(thread,"0x%lx\n",ret);
364 return ret;
368 /**********************************************************************
369 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
371 * RETURNS
372 * None.
374 void WINAPI SetLastError(
375 DWORD error) /* [in] Per-thread error code */
377 THDB *thread = THREAD_Current();
378 /* This one must work before we have a thread (FIXME) */
380 TRACE(thread,"%p error=0x%lx\n",thread,error);
382 if (thread)
383 thread->last_error = error;
387 /**********************************************************************
388 * SetLastErrorEx [USER32.485] Sets the last-error code.
390 * RETURNS
391 * None.
393 void WINAPI SetLastErrorEx(
394 DWORD error, /* [in] Per-thread error code */
395 DWORD type) /* [in] Error type */
397 TRACE(thread, "(0x%08lx, 0x%08lx)\n", error,type);
398 switch(type) {
399 case 0:
400 break;
401 case SLE_ERROR:
402 case SLE_MINORERROR:
403 case SLE_WARNING:
404 /* Fall through for now */
405 default:
406 FIXME(thread, "(error=%08lx, type=%08lx): Unhandled type\n", error,type);
407 break;
409 SetLastError( error );
413 /**********************************************************************
414 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
416 * Allocates a thread local storage index
418 * RETURNS
419 * Success: TLS Index
420 * Failure: 0xFFFFFFFF
422 DWORD WINAPI TlsAlloc( void )
424 THDB *thread = THREAD_Current();
425 DWORD i, mask, ret = 0;
426 DWORD *bits = thread->process->tls_bits;
427 EnterCriticalSection( &thread->process->crit_section );
428 if (*bits == 0xffffffff)
430 bits++;
431 ret = 32;
432 if (*bits == 0xffffffff)
434 LeaveCriticalSection( &thread->process->crit_section );
435 SetLastError( ERROR_NO_MORE_ITEMS );
436 return 0xffffffff;
439 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
440 *bits |= mask;
441 LeaveCriticalSection( &thread->process->crit_section );
442 return ret + i;
446 /**********************************************************************
447 * TlsFree [KERNEL32.531] Releases a TLS index.
449 * Releases a thread local storage index, making it available for reuse
451 * RETURNS
452 * Success: TRUE
453 * Failure: FALSE
455 BOOL WINAPI TlsFree(
456 DWORD index) /* [in] TLS Index to free */
458 DWORD mask;
459 THDB *thread = THREAD_Current();
460 DWORD *bits = thread->process->tls_bits;
461 if (index >= 64)
463 SetLastError( ERROR_INVALID_PARAMETER );
464 return FALSE;
466 EnterCriticalSection( &thread->process->crit_section );
467 if (index >= 32) bits++;
468 mask = (1 << (index & 31));
469 if (!(*bits & mask)) /* already free? */
471 LeaveCriticalSection( &thread->process->crit_section );
472 SetLastError( ERROR_INVALID_PARAMETER );
473 return FALSE;
475 *bits &= ~mask;
476 thread->tls_array[index] = 0;
477 /* FIXME: should zero all other thread values */
478 LeaveCriticalSection( &thread->process->crit_section );
479 return TRUE;
483 /**********************************************************************
484 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
486 * RETURNS
487 * Success: Value stored in calling thread's TLS slot for index
488 * Failure: 0 and GetLastError returns NO_ERROR
490 LPVOID WINAPI TlsGetValue(
491 DWORD index) /* [in] TLS index to retrieve value for */
493 THDB *thread = THREAD_Current();
494 if (index >= 64)
496 SetLastError( ERROR_INVALID_PARAMETER );
497 return NULL;
499 SetLastError( ERROR_SUCCESS );
500 return thread->tls_array[index];
504 /**********************************************************************
505 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
507 * RETURNS
508 * Success: TRUE
509 * Failure: FALSE
511 BOOL WINAPI TlsSetValue(
512 DWORD index, /* [in] TLS index to set value for */
513 LPVOID value) /* [in] Value to be stored */
515 THDB *thread = THREAD_Current();
516 if (index >= 64)
518 SetLastError( ERROR_INVALID_PARAMETER );
519 return FALSE;
521 thread->tls_array[index] = value;
522 return TRUE;
526 /***********************************************************************
527 * SetThreadContext [KERNEL32.670] Sets context of thread.
529 * RETURNS
530 * Success: TRUE
531 * Failure: FALSE
533 BOOL WINAPI SetThreadContext(
534 HANDLE handle, /* [in] Handle to thread with context */
535 CONTEXT *context) /* [out] Address of context structure */
537 FIXME( thread, "not implemented\n" );
538 return TRUE;
541 /***********************************************************************
542 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
544 * RETURNS
545 * Success: TRUE
546 * Failure: FALSE
548 BOOL WINAPI GetThreadContext(
549 HANDLE handle, /* [in] Handle to thread with context */
550 CONTEXT *context) /* [out] Address of context structure */
552 WORD cs, ds;
554 FIXME( thread, "returning dummy info\n" );
556 /* make up some plausible values for segment registers */
557 GET_CS(cs);
558 GET_DS(ds);
559 context->SegCs = cs;
560 context->SegDs = ds;
561 context->SegEs = ds;
562 context->SegGs = ds;
563 context->SegSs = ds;
564 context->SegFs = ds;
565 return TRUE;
569 /**********************************************************************
570 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
572 * RETURNS
573 * Success: Thread's priority level.
574 * Failure: THREAD_PRIORITY_ERROR_RETURN
576 INT WINAPI GetThreadPriority(
577 HANDLE hthread) /* [in] Handle to thread */
579 struct get_thread_info_request req;
580 struct get_thread_info_reply reply;
581 req.handle = hthread;
582 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
583 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
584 return THREAD_PRIORITY_ERROR_RETURN;
585 return reply.priority;
589 /**********************************************************************
590 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
592 * RETURNS
593 * Success: TRUE
594 * Failure: FALSE
596 BOOL WINAPI SetThreadPriority(
597 HANDLE hthread, /* [in] Handle to thread */
598 INT priority) /* [in] Thread priority level */
600 struct set_thread_info_request req;
601 req.handle = hthread;
602 req.priority = priority;
603 req.mask = SET_THREAD_INFO_PRIORITY;
604 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
605 return !CLIENT_WaitReply( NULL, NULL, 0 );
609 /**********************************************************************
610 * SetThreadAffinityMask (KERNEL32.669)
612 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
614 struct set_thread_info_request req;
615 req.handle = hThread;
616 req.affinity = dwThreadAffinityMask;
617 req.mask = SET_THREAD_INFO_AFFINITY;
618 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
619 if (CLIENT_WaitReply( NULL, NULL, 0 )) return 0;
620 return 1; /* FIXME: should return previous value */
624 /**********************************************************************
625 * TerminateThread [KERNEL32.685] Terminates a thread
627 * RETURNS
628 * Success: TRUE
629 * Failure: FALSE
631 BOOL WINAPI TerminateThread(
632 HANDLE handle, /* [in] Handle to thread */
633 DWORD exitcode) /* [in] Exit code for thread */
635 struct terminate_thread_request req;
636 req.handle = handle;
637 req.exit_code = exitcode;
638 CLIENT_SendRequest( REQ_TERMINATE_THREAD, -1, 1, &req, sizeof(req) );
639 return !CLIENT_WaitReply( NULL, NULL, 0 );
643 /**********************************************************************
644 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
646 * RETURNS
647 * Success: TRUE
648 * Failure: FALSE
650 BOOL WINAPI GetExitCodeThread(
651 HANDLE hthread, /* [in] Handle to thread */
652 LPDWORD exitcode) /* [out] Address to receive termination status */
654 struct get_thread_info_request req;
655 struct get_thread_info_reply reply;
656 req.handle = hthread;
657 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
658 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
659 if (exitcode) *exitcode = reply.exit_code;
660 return TRUE;
664 /**********************************************************************
665 * ResumeThread [KERNEL32.587] Resumes a thread.
667 * Decrements a thread's suspend count. When count is zero, the
668 * execution of the thread is resumed.
670 * RETURNS
671 * Success: Previous suspend count
672 * Failure: 0xFFFFFFFF
673 * Already running: 0
675 DWORD WINAPI ResumeThread(
676 HANDLE hthread) /* [in] Identifies thread to restart */
678 struct resume_thread_request req;
679 struct resume_thread_reply reply;
680 req.handle = hthread;
681 CLIENT_SendRequest( REQ_RESUME_THREAD, -1, 1, &req, sizeof(req) );
682 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
683 return reply.count;
687 /**********************************************************************
688 * SuspendThread [KERNEL32.681] Suspends a thread.
690 * RETURNS
691 * Success: Previous suspend count
692 * Failure: 0xFFFFFFFF
694 DWORD WINAPI SuspendThread(
695 HANDLE hthread) /* [in] Handle to the thread */
697 struct suspend_thread_request req;
698 struct suspend_thread_reply reply;
699 req.handle = hthread;
700 CLIENT_SendRequest( REQ_SUSPEND_THREAD, -1, 1, &req, sizeof(req) );
701 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
702 return reply.count;
706 /***********************************************************************
707 * QueueUserAPC (KERNEL32.566)
709 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
711 struct queue_apc_request req;
712 req.handle = hthread;
713 req.func = func;
714 req.param = (void *)data;
715 CLIENT_SendRequest( REQ_QUEUE_APC, -1, 1, &req, sizeof(req) );
716 return !CLIENT_WaitReply( NULL, NULL, 0 );
720 /**********************************************************************
721 * GetThreadTimes [KERNEL32.???] Obtains timing information.
723 * NOTES
724 * What are the fields where these values are stored?
726 * RETURNS
727 * Success: TRUE
728 * Failure: FALSE
730 BOOL WINAPI GetThreadTimes(
731 HANDLE thread, /* [in] Specifies the thread of interest */
732 LPFILETIME creationtime, /* [out] When the thread was created */
733 LPFILETIME exittime, /* [out] When the thread was destroyed */
734 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
735 LPFILETIME usertime) /* [out] Time thread spent in user mode */
737 FIXME(thread,"(0x%08x): stub\n",thread);
738 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
739 return FALSE;
743 /**********************************************************************
744 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
746 * Attaches the input processing mechanism of one thread to that of
747 * another thread.
749 * RETURNS
750 * Success: TRUE
751 * Failure: FALSE
753 * TODO:
754 * 1. Reset the Key State (currenly per thread key state is not maintained)
756 BOOL WINAPI AttachThreadInput(
757 DWORD idAttach, /* [in] Thread to attach */
758 DWORD idAttachTo, /* [in] Thread to attach to */
759 BOOL fAttach) /* [in] Attach or detach */
761 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
762 BOOL16 bRet = 0;
764 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
766 /* A thread cannot attach to itself */
767 if ( idAttach == idAttachTo )
768 goto CLEANUP;
770 /* According to the docs this method should fail if a
771 * "Journal record" hook is installed. (attaches all input queues together)
773 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
774 goto CLEANUP;
776 /* Retrieve message queues corresponding to the thread id's */
777 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
778 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
780 /* Ensure we have message queues and that Src and Tgt threads
781 * are not system threads.
783 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
784 goto CLEANUP;
786 if (fAttach) /* Attach threads */
788 /* Only attach if currently detached */
789 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
791 /* First release the target threads perQData */
792 PERQDATA_Release( pTgtMsgQ->pQData );
794 /* Share a reference to the source threads perQDATA */
795 PERQDATA_Addref( pSrcMsgQ->pQData );
796 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
799 else /* Detach threads */
801 /* Only detach if currently attached */
802 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
804 /* First release the target threads perQData */
805 PERQDATA_Release( pTgtMsgQ->pQData );
807 /* Give the target thread its own private perQDATA once more */
808 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
812 /* TODO: Reset the Key State */
814 bRet = 1; // Success
816 CLEANUP:
818 /* Unlock the queues before returning */
819 if ( pSrcMsgQ )
820 QUEUE_Unlock( pSrcMsgQ );
821 if ( pTgtMsgQ )
822 QUEUE_Unlock( pTgtMsgQ );
824 return bRet;
827 /**********************************************************************
828 * VWin32_BoostThreadGroup [KERNEL.535]
830 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
832 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
835 /**********************************************************************
836 * VWin32_BoostThreadStatic [KERNEL.536]
838 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
840 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
843 /**********************************************************************
844 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
846 * RETURNS
847 * Success: TRUE
848 * Failure: FALSE
850 * NOTES
851 * Implemented in NT only (3.1 and above according to MS
853 BOOL WINAPI SetThreadLocale(
854 LCID lcid) /* [in] Locale identifier */
856 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
857 return FALSE;