Test for static was reversed.
[wine.git] / scheduler / thread.c
blob87404f3a5a66efc76b69e86c6e982c12215bbb1c
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 DEFAULT_DEBUG_CHANNEL(thread)
30 #ifndef __i386__
31 THDB *pCurrentThread;
32 #endif
34 /* Is threading code initialized? */
35 BOOL THREAD_InitDone = FALSE;
37 /* THDB of the initial thread */
38 static THDB initial_thdb;
40 /* Global thread list (FIXME: not thread-safe) */
41 THDB *THREAD_First = &initial_thdb;
43 /***********************************************************************
44 * THREAD_Current
46 * Return the current thread THDB pointer.
48 THDB *THREAD_Current(void)
50 if (!THREAD_InitDone) return NULL;
51 return (THDB *)((char *)NtCurrentTeb() - (int)&((THDB *)0)->teb);
54 /***********************************************************************
55 * THREAD_IsWin16
57 BOOL THREAD_IsWin16( THDB *thdb )
59 return !thdb || !(thdb->teb.flags & TEBF_WIN32);
62 /***********************************************************************
63 * THREAD_IdToTHDB
65 * Convert a thread id to a THDB, making sure it is valid.
67 THDB *THREAD_IdToTHDB( DWORD id )
69 THDB *thdb = THREAD_First;
71 if (!id) return THREAD_Current();
72 while (thdb)
74 if ((DWORD)thdb->server_tid == id) return thdb;
75 thdb = thdb->next;
77 /* Allow task handles to be used; convert to main thread */
78 if ( IsTask16( id ) )
80 TDB *pTask = (TDB *)GlobalLock16( id );
81 if (pTask) return pTask->thdb;
83 SetLastError( ERROR_INVALID_PARAMETER );
84 return NULL;
88 /***********************************************************************
89 * THREAD_InitTHDB
91 * Initialization of a newly created THDB.
93 static BOOL THREAD_InitTHDB( THDB *thdb, DWORD stack_size, BOOL alloc_stack16,
94 LPSECURITY_ATTRIBUTES sa )
96 DWORD old_prot;
98 /* Allocate the stack */
100 /* FIXME:
101 * If stacksize smaller than 1 MB, allocate 1MB
102 * (one program wanted only 10 kB, which is recommendable, but some WINE
103 * functions, noteably in the files subdir, push HUGE structures and
104 * arrays on the stack. They probably shouldn't.)
105 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
106 * but this could give more or less unexplainable crashes.)
108 if (stack_size<1024*1024)
109 stack_size = 1024 * 1024;
110 if (stack_size >= 16*1024*1024)
111 WARN(thread,"Thread stack size is %ld MB.\n",stack_size/1024/1024);
112 thdb->stack_base = VirtualAlloc(NULL,
113 stack_size + (alloc_stack16 ? 0x10000 : 0),
114 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
115 if (!thdb->stack_base) goto error;
116 /* Set a guard page at the bottom of the stack */
117 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
118 &old_prot );
119 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
120 thdb->teb.stack_low = thdb->stack_base;
121 thdb->exit_stack = thdb->teb.stack_top;
123 /* Allocate the 16-bit stack selector */
125 if (alloc_stack16)
127 thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
128 0x10000, SEGMENT_DATA,
129 FALSE, FALSE );
130 if (!thdb->teb.stack_sel) goto error;
131 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel,
132 0x10000 - sizeof(STACK16FRAME) );
135 /* Create the thread event */
137 if (!(thdb->event = CreateEventA( NULL, FALSE, FALSE, NULL ))) goto error;
138 thdb->event = ConvertToGlobalHandle( thdb->event );
139 return TRUE;
141 error:
142 if (thdb->event) CloseHandle( thdb->event );
143 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
144 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
145 return FALSE;
149 /***********************************************************************
150 * THREAD_FreeTHDB
152 * Free data structures associated with a thread.
153 * Must be called from the context of another thread.
155 void THREAD_FreeTHDB( THDB *thdb )
157 THDB **pptr = &THREAD_First;
159 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0, 0 );
161 CloseHandle( thdb->event );
162 while (*pptr && (*pptr != thdb)) pptr = &(*pptr)->next;
163 if (*pptr) *pptr = thdb->next;
165 /* Free the associated memory */
167 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
168 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
169 close( thdb->socket );
170 VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
171 HeapFree( SystemHeap, HEAP_NO_SERIALIZE, thdb );
175 /***********************************************************************
176 * THREAD_CreateInitialThread
178 * Create the initial thread.
180 THDB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
182 initial_thdb.process = pdb;
183 initial_thdb.teb.except = (void *)-1;
184 initial_thdb.teb.self = &initial_thdb.teb;
185 initial_thdb.teb.flags = /* TEBF_WIN32 */ 0;
186 initial_thdb.teb.tls_ptr = initial_thdb.tls_array;
187 initial_thdb.teb.process = pdb;
188 initial_thdb.exit_code = 0x103; /* STILL_ACTIVE */
189 initial_thdb.socket = server_fd;
191 /* Allocate the TEB selector (%fs register) */
193 if (!(initial_thdb.teb_sel = SELECTOR_AllocBlock( &initial_thdb.teb, 0x1000,
194 SEGMENT_DATA, TRUE, FALSE )))
196 MSG("Could not allocate fs register for initial thread\n" );
197 return NULL;
199 SET_CUR_THREAD( &initial_thdb );
200 THREAD_InitDone = TRUE;
202 /* Now proceed with normal initialization */
204 if (CLIENT_InitThread()) return NULL;
205 if (!THREAD_InitTHDB( &initial_thdb, 0, TRUE, NULL )) return NULL;
206 return &initial_thdb;
210 /***********************************************************************
211 * THREAD_Create
213 THDB *THREAD_Create( PDB *pdb, DWORD flags, DWORD stack_size, BOOL alloc_stack16,
214 LPSECURITY_ATTRIBUTES sa, int *server_handle )
216 struct new_thread_request request;
217 struct new_thread_reply reply = { NULL, -1 };
218 int fd[2];
220 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
221 if (!thdb) return NULL;
222 thdb->process = pdb;
223 thdb->teb.except = (void *)-1;
224 thdb->teb.htask16 = pdb->task;
225 thdb->teb.self = &thdb->teb;
226 thdb->teb.flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
227 thdb->teb.tls_ptr = thdb->tls_array;
228 thdb->teb.process = pdb;
229 thdb->exit_code = 0x103; /* STILL_ACTIVE */
230 thdb->flags = flags;
231 thdb->socket = -1;
233 /* Allocate the TEB selector (%fs register) */
235 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
236 TRUE, FALSE );
237 if (!thdb->teb_sel) goto error;
239 /* Create the socket pair for server communication */
241 if (socketpair( AF_UNIX, SOCK_STREAM, 0, fd ) == -1)
243 SetLastError( ERROR_TOO_MANY_OPEN_FILES ); /* FIXME */
244 goto error;
246 thdb->socket = fd[0];
247 fcntl( fd[0], F_SETFD, 1 ); /* set close on exec flag */
249 /* Create the thread on the server side */
251 request.pid = thdb->process->server_pid;
252 request.suspend = ((thdb->flags & CREATE_SUSPENDED) != 0);
253 request.inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
254 CLIENT_SendRequest( REQ_NEW_THREAD, fd[1], 1, &request, sizeof(request) );
255 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) goto error;
256 thdb->server_tid = reply.tid;
257 *server_handle = reply.handle;
259 /* Do the rest of the initialization */
261 if (!THREAD_InitTHDB( thdb, stack_size, alloc_stack16, sa )) goto error;
262 thdb->next = THREAD_First;
263 THREAD_First = thdb;
264 return thdb;
266 error:
267 if (reply.handle != -1) CloseHandle( reply.handle );
268 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
269 HeapFree( SystemHeap, 0, thdb );
270 if (thdb->socket != -1) close( thdb->socket );
271 return NULL;
275 /***********************************************************************
276 * THREAD_Start
278 * Start execution of a newly created thread. Does not return.
280 static void THREAD_Start(void)
282 THDB *thdb = THREAD_Current();
283 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
284 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0, 0 );
285 PE_InitTls();
286 MODULE_DllThreadAttach( NULL );
287 ExitThread( func( thdb->entry_arg ) );
291 /***********************************************************************
292 * CreateThread (KERNEL32.63)
294 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
295 LPTHREAD_START_ROUTINE start, LPVOID param,
296 DWORD flags, LPDWORD id )
298 int handle = -1;
299 THDB *thread = THREAD_Create( PROCESS_Current(), flags, stack, TRUE, sa, &handle );
300 if (!thread) return INVALID_HANDLE_VALUE;
301 thread->teb.flags |= TEBF_WIN32;
302 thread->entry_point = start;
303 thread->entry_arg = param;
304 thread->startup = THREAD_Start;
305 if (SYSDEPS_SpawnThread( thread ) == -1)
307 CloseHandle( handle );
308 return INVALID_HANDLE_VALUE;
310 if (id) *id = (DWORD)thread->server_tid;
311 return handle;
315 /***********************************************************************
316 * ExitThread [KERNEL32.215] Ends a thread
318 * RETURNS
319 * None
321 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
323 MODULE_DllThreadDetach( NULL );
324 TerminateThread( GetCurrentThread(), code );
328 /***********************************************************************
329 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
331 * RETURNS
332 * Pseudohandle for the current thread
334 HANDLE WINAPI GetCurrentThread(void)
336 return CURRENT_THREAD_PSEUDOHANDLE;
340 /***********************************************************************
341 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
343 * RETURNS
344 * Thread identifier of calling thread
346 DWORD WINAPI GetCurrentThreadId(void)
348 THDB *thdb = THREAD_Current();
349 assert( thdb );
350 assert( thdb->server_tid );
351 return (DWORD)thdb->server_tid;
355 /**********************************************************************
356 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
358 * RETURNS
359 * Calling thread's last error code value.
361 DWORD WINAPI GetLastError(void)
363 THDB *thread = THREAD_Current();
364 DWORD ret = thread->last_error;
365 TRACE(thread,"0x%lx\n",ret);
366 return ret;
370 /**********************************************************************
371 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
373 * RETURNS
374 * None.
376 void WINAPI SetLastError(
377 DWORD error) /* [in] Per-thread error code */
379 THDB *thread = THREAD_Current();
380 /* This one must work before we have a thread (FIXME) */
382 TRACE(thread,"%p error=0x%lx\n",thread,error);
384 if (thread)
385 thread->last_error = error;
389 /**********************************************************************
390 * SetLastErrorEx [USER32.485] Sets the last-error code.
392 * RETURNS
393 * None.
395 void WINAPI SetLastErrorEx(
396 DWORD error, /* [in] Per-thread error code */
397 DWORD type) /* [in] Error type */
399 TRACE(thread, "(0x%08lx, 0x%08lx)\n", error,type);
400 switch(type) {
401 case 0:
402 break;
403 case SLE_ERROR:
404 case SLE_MINORERROR:
405 case SLE_WARNING:
406 /* Fall through for now */
407 default:
408 FIXME(thread, "(error=%08lx, type=%08lx): Unhandled type\n", error,type);
409 break;
411 SetLastError( error );
415 /**********************************************************************
416 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
418 * Allocates a thread local storage index
420 * RETURNS
421 * Success: TLS Index
422 * Failure: 0xFFFFFFFF
424 DWORD WINAPI TlsAlloc( void )
426 THDB *thread = THREAD_Current();
427 DWORD i, mask, ret = 0;
428 DWORD *bits = thread->process->tls_bits;
429 EnterCriticalSection( &thread->process->crit_section );
430 if (*bits == 0xffffffff)
432 bits++;
433 ret = 32;
434 if (*bits == 0xffffffff)
436 LeaveCriticalSection( &thread->process->crit_section );
437 SetLastError( ERROR_NO_MORE_ITEMS );
438 return 0xffffffff;
441 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
442 *bits |= mask;
443 LeaveCriticalSection( &thread->process->crit_section );
444 return ret + i;
448 /**********************************************************************
449 * TlsFree [KERNEL32.531] Releases a TLS index.
451 * Releases a thread local storage index, making it available for reuse
453 * RETURNS
454 * Success: TRUE
455 * Failure: FALSE
457 BOOL WINAPI TlsFree(
458 DWORD index) /* [in] TLS Index to free */
460 DWORD mask;
461 THDB *thread = THREAD_Current();
462 DWORD *bits = thread->process->tls_bits;
463 if (index >= 64)
465 SetLastError( ERROR_INVALID_PARAMETER );
466 return FALSE;
468 EnterCriticalSection( &thread->process->crit_section );
469 if (index >= 32) bits++;
470 mask = (1 << (index & 31));
471 if (!(*bits & mask)) /* already free? */
473 LeaveCriticalSection( &thread->process->crit_section );
474 SetLastError( ERROR_INVALID_PARAMETER );
475 return FALSE;
477 *bits &= ~mask;
478 thread->tls_array[index] = 0;
479 /* FIXME: should zero all other thread values */
480 LeaveCriticalSection( &thread->process->crit_section );
481 return TRUE;
485 /**********************************************************************
486 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
488 * RETURNS
489 * Success: Value stored in calling thread's TLS slot for index
490 * Failure: 0 and GetLastError returns NO_ERROR
492 LPVOID WINAPI TlsGetValue(
493 DWORD index) /* [in] TLS index to retrieve value for */
495 THDB *thread = THREAD_Current();
496 if (index >= 64)
498 SetLastError( ERROR_INVALID_PARAMETER );
499 return NULL;
501 SetLastError( ERROR_SUCCESS );
502 return thread->tls_array[index];
506 /**********************************************************************
507 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
509 * RETURNS
510 * Success: TRUE
511 * Failure: FALSE
513 BOOL WINAPI TlsSetValue(
514 DWORD index, /* [in] TLS index to set value for */
515 LPVOID value) /* [in] Value to be stored */
517 THDB *thread = THREAD_Current();
518 if (index >= 64)
520 SetLastError( ERROR_INVALID_PARAMETER );
521 return FALSE;
523 thread->tls_array[index] = value;
524 return TRUE;
528 /***********************************************************************
529 * SetThreadContext [KERNEL32.670] Sets context of thread.
531 * RETURNS
532 * Success: TRUE
533 * Failure: FALSE
535 BOOL WINAPI SetThreadContext(
536 HANDLE handle, /* [in] Handle to thread with context */
537 CONTEXT *context) /* [out] Address of context structure */
539 FIXME( thread, "not implemented\n" );
540 return TRUE;
543 /***********************************************************************
544 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
546 * RETURNS
547 * Success: TRUE
548 * Failure: FALSE
550 BOOL WINAPI GetThreadContext(
551 HANDLE handle, /* [in] Handle to thread with context */
552 CONTEXT *context) /* [out] Address of context structure */
554 WORD cs, ds;
556 FIXME( thread, "returning dummy info\n" );
558 /* make up some plausible values for segment registers */
559 GET_CS(cs);
560 GET_DS(ds);
561 context->SegCs = cs;
562 context->SegDs = ds;
563 context->SegEs = ds;
564 context->SegGs = ds;
565 context->SegSs = ds;
566 context->SegFs = ds;
567 return TRUE;
571 /**********************************************************************
572 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
574 * RETURNS
575 * Success: Thread's priority level.
576 * Failure: THREAD_PRIORITY_ERROR_RETURN
578 INT WINAPI GetThreadPriority(
579 HANDLE hthread) /* [in] Handle to thread */
581 struct get_thread_info_request req;
582 struct get_thread_info_reply reply;
583 req.handle = hthread;
584 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
585 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL ))
586 return THREAD_PRIORITY_ERROR_RETURN;
587 return reply.priority;
591 /**********************************************************************
592 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
594 * RETURNS
595 * Success: TRUE
596 * Failure: FALSE
598 BOOL WINAPI SetThreadPriority(
599 HANDLE hthread, /* [in] Handle to thread */
600 INT priority) /* [in] Thread priority level */
602 struct set_thread_info_request req;
603 req.handle = hthread;
604 req.priority = priority;
605 req.mask = SET_THREAD_INFO_PRIORITY;
606 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
607 return !CLIENT_WaitReply( NULL, NULL, 0 );
611 /**********************************************************************
612 * SetThreadAffinityMask (KERNEL32.669)
614 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
616 struct set_thread_info_request req;
617 req.handle = hThread;
618 req.affinity = dwThreadAffinityMask;
619 req.mask = SET_THREAD_INFO_AFFINITY;
620 CLIENT_SendRequest( REQ_SET_THREAD_INFO, -1, 1, &req, sizeof(req) );
621 if (CLIENT_WaitReply( NULL, NULL, 0 )) return 0;
622 return 1; /* FIXME: should return previous value */
626 /**********************************************************************
627 * TerminateThread [KERNEL32.685] Terminates a thread
629 * RETURNS
630 * Success: TRUE
631 * Failure: FALSE
633 BOOL WINAPI TerminateThread(
634 HANDLE handle, /* [in] Handle to thread */
635 DWORD exitcode) /* [in] Exit code for thread */
637 struct terminate_thread_request req;
638 req.handle = handle;
639 req.exit_code = exitcode;
640 CLIENT_SendRequest( REQ_TERMINATE_THREAD, -1, 1, &req, sizeof(req) );
641 return !CLIENT_WaitReply( NULL, NULL, 0 );
645 /**********************************************************************
646 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
648 * RETURNS
649 * Success: TRUE
650 * Failure: FALSE
652 BOOL WINAPI GetExitCodeThread(
653 HANDLE hthread, /* [in] Handle to thread */
654 LPDWORD exitcode) /* [out] Address to receive termination status */
656 struct get_thread_info_request req;
657 struct get_thread_info_reply reply;
658 req.handle = hthread;
659 CLIENT_SendRequest( REQ_GET_THREAD_INFO, -1, 1, &req, sizeof(req) );
660 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return FALSE;
661 if (exitcode) *exitcode = reply.exit_code;
662 return TRUE;
666 /**********************************************************************
667 * ResumeThread [KERNEL32.587] Resumes a thread.
669 * Decrements a thread's suspend count. When count is zero, the
670 * execution of the thread is resumed.
672 * RETURNS
673 * Success: Previous suspend count
674 * Failure: 0xFFFFFFFF
675 * Already running: 0
677 DWORD WINAPI ResumeThread(
678 HANDLE hthread) /* [in] Identifies thread to restart */
680 struct resume_thread_request req;
681 struct resume_thread_reply reply;
682 req.handle = hthread;
683 CLIENT_SendRequest( REQ_RESUME_THREAD, -1, 1, &req, sizeof(req) );
684 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
685 return reply.count;
689 /**********************************************************************
690 * SuspendThread [KERNEL32.681] Suspends a thread.
692 * RETURNS
693 * Success: Previous suspend count
694 * Failure: 0xFFFFFFFF
696 DWORD WINAPI SuspendThread(
697 HANDLE hthread) /* [in] Handle to the thread */
699 struct suspend_thread_request req;
700 struct suspend_thread_reply reply;
701 req.handle = hthread;
702 CLIENT_SendRequest( REQ_SUSPEND_THREAD, -1, 1, &req, sizeof(req) );
703 if (CLIENT_WaitSimpleReply( &reply, sizeof(reply), NULL )) return 0xffffffff;
704 return reply.count;
708 /***********************************************************************
709 * QueueUserAPC (KERNEL32.566)
711 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
713 struct queue_apc_request req;
714 req.handle = hthread;
715 req.func = func;
716 req.param = (void *)data;
717 CLIENT_SendRequest( REQ_QUEUE_APC, -1, 1, &req, sizeof(req) );
718 return !CLIENT_WaitReply( NULL, NULL, 0 );
722 /**********************************************************************
723 * GetThreadTimes [KERNEL32.???] Obtains timing information.
725 * NOTES
726 * What are the fields where these values are stored?
728 * RETURNS
729 * Success: TRUE
730 * Failure: FALSE
732 BOOL WINAPI GetThreadTimes(
733 HANDLE thread, /* [in] Specifies the thread of interest */
734 LPFILETIME creationtime, /* [out] When the thread was created */
735 LPFILETIME exittime, /* [out] When the thread was destroyed */
736 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
737 LPFILETIME usertime) /* [out] Time thread spent in user mode */
739 FIXME(thread,"(0x%08x): stub\n",thread);
740 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
741 return FALSE;
745 /**********************************************************************
746 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
748 * Attaches the input processing mechanism of one thread to that of
749 * another thread.
751 * RETURNS
752 * Success: TRUE
753 * Failure: FALSE
755 * TODO:
756 * 1. Reset the Key State (currenly per thread key state is not maintained)
758 BOOL WINAPI AttachThreadInput(
759 DWORD idAttach, /* [in] Thread to attach */
760 DWORD idAttachTo, /* [in] Thread to attach to */
761 BOOL fAttach) /* [in] Attach or detach */
763 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
764 BOOL16 bRet = 0;
766 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
768 /* A thread cannot attach to itself */
769 if ( idAttach == idAttachTo )
770 goto CLEANUP;
772 /* According to the docs this method should fail if a
773 * "Journal record" hook is installed. (attaches all input queues together)
775 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
776 goto CLEANUP;
778 /* Retrieve message queues corresponding to the thread id's */
779 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
780 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
782 /* Ensure we have message queues and that Src and Tgt threads
783 * are not system threads.
785 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
786 goto CLEANUP;
788 if (fAttach) /* Attach threads */
790 /* Only attach if currently detached */
791 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
793 /* First release the target threads perQData */
794 PERQDATA_Release( pTgtMsgQ->pQData );
796 /* Share a reference to the source threads perQDATA */
797 PERQDATA_Addref( pSrcMsgQ->pQData );
798 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
801 else /* Detach threads */
803 /* Only detach if currently attached */
804 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
806 /* First release the target threads perQData */
807 PERQDATA_Release( pTgtMsgQ->pQData );
809 /* Give the target thread its own private perQDATA once more */
810 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
814 /* TODO: Reset the Key State */
816 bRet = 1; // Success
818 CLEANUP:
820 /* Unlock the queues before returning */
821 if ( pSrcMsgQ )
822 QUEUE_Unlock( pSrcMsgQ );
823 if ( pTgtMsgQ )
824 QUEUE_Unlock( pTgtMsgQ );
826 return bRet;
829 /**********************************************************************
830 * VWin32_BoostThreadGroup [KERNEL.535]
832 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
834 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
837 /**********************************************************************
838 * VWin32_BoostThreadStatic [KERNEL.536]
840 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
842 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
845 /**********************************************************************
846 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
848 * RETURNS
849 * Success: TRUE
850 * Failure: FALSE
852 * NOTES
853 * Implemented in NT only (3.1 and above according to MS
855 BOOL WINAPI SetThreadLocale(
856 LCID lcid) /* [in] Locale identifier */
858 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
859 return FALSE;