Implemented FAT32 function 7302 (Get Extended DPB).
[wine/multimedia.git] / scheduler / thread.c
blob00ec37bf7ba8024316264fdcf311fbd9d15c97d6
1 /*
2 * Win32 threads
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include "config.h"
9 #include <assert.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #ifdef HAVE_SYS_MMAN_H
13 #include <sys/mman.h>
14 #endif
15 #include <unistd.h>
16 #include "wine/winbase16.h"
17 #include "thread.h"
18 #include "process.h"
19 #include "task.h"
20 #include "module.h"
21 #include "user.h"
22 #include "winerror.h"
23 #include "heap.h"
24 #include "selectors.h"
25 #include "winnt.h"
26 #include "server.h"
27 #include "services.h"
28 #include "stackframe.h"
29 #include "builtin16.h"
30 #include "debugtools.h"
31 #include "queue.h"
32 #include "hook.h"
34 DEFAULT_DEBUG_CHANNEL(thread);
36 /* TEB of the initial thread */
37 static TEB initial_teb;
39 /***********************************************************************
40 * THREAD_IsWin16
42 BOOL THREAD_IsWin16( TEB *teb )
44 return !teb || !(teb->tibflags & TEBF_WIN32);
47 /***********************************************************************
48 * THREAD_IdToTEB
50 * Convert a thread id to a TEB, making sure it is valid.
52 TEB *THREAD_IdToTEB( DWORD id )
54 struct get_thread_info_request *req = get_req_buffer();
56 if (!id || id == GetCurrentThreadId()) return NtCurrentTeb();
57 req->handle = -1;
58 req->tid_in = (void *)id;
59 if (!server_call_noerr( REQ_GET_THREAD_INFO )) return req->teb;
61 /* Allow task handles to be used; convert to main thread */
62 if ( IsTask16( id ) )
64 TDB *pTask = (TDB *)GlobalLock16( id );
65 if (pTask) return pTask->teb;
67 SetLastError( ERROR_INVALID_PARAMETER );
68 return NULL;
72 /***********************************************************************
73 * THREAD_InitTEB
75 * Initialization of a newly created TEB.
77 static BOOL THREAD_InitTEB( TEB *teb, DWORD stack_size, BOOL alloc_stack16 )
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 teb->stack_base = VirtualAlloc(NULL, stack_size + SIGNAL_STACK_SIZE +
96 (alloc_stack16 ? 0x10000 : 0),
97 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
98 if (!teb->stack_base) goto error;
99 /* Set a guard page at the bottom of the stack */
100 VirtualProtect( teb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD, &old_prot );
101 teb->stack_top = (char *)teb->stack_base + stack_size;
102 teb->stack_low = teb->stack_base;
103 teb->signal_stack = teb->stack_top; /* start of signal stack */
105 /* Allocate the 16-bit stack selector */
107 if (alloc_stack16)
109 teb->stack_sel = SELECTOR_AllocBlock( teb->stack_top, 0x10000, SEGMENT_DATA,
110 FALSE, FALSE );
111 if (!teb->stack_sel) goto error;
112 teb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( teb->stack_sel,
113 0x10000 - sizeof(STACK16FRAME) );
114 teb->signal_stack = (char *)teb->signal_stack + 0x10000;
117 /* StaticUnicodeString */
119 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
120 teb->StaticUnicodeString.Buffer = (PWSTR)teb->StaticUnicodeBuffer;
122 return TRUE;
124 error:
125 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
126 if (teb->stack_base) VirtualFree( teb->stack_base, 0, MEM_RELEASE );
127 return FALSE;
131 /***********************************************************************
132 * THREAD_FreeTEB
134 * Free data structures associated with a thread.
135 * Must be called from the context of another thread.
137 void CALLBACK THREAD_FreeTEB( ULONG_PTR arg )
139 TEB *teb = (TEB *)arg;
141 TRACE("(%p) called\n", teb );
142 SERVICE_Delete( teb->cleanup );
144 /* Free the associated memory */
146 if (teb->socket != -1) close( teb->socket );
147 if (teb->stack_sel) SELECTOR_FreeBlock( teb->stack_sel, 1 );
148 SELECTOR_FreeBlock( teb->teb_sel, 1 );
149 if (teb->buffer) munmap( teb->buffer, teb->buffer_size );
150 if (teb->debug_info) HeapFree( GetProcessHeap(), 0, teb->debug_info );
151 VirtualFree( teb->stack_base, 0, MEM_RELEASE );
152 VirtualFree( teb, 0, MEM_RELEASE );
156 /***********************************************************************
157 * THREAD_CreateInitialThread
159 * Create the initial thread.
161 TEB *THREAD_CreateInitialThread( PDB *pdb, int server_fd )
163 initial_teb.except = (void *)-1;
164 initial_teb.self = &initial_teb;
165 initial_teb.tibflags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
166 initial_teb.tls_ptr = initial_teb.tls_array;
167 initial_teb.process = pdb;
168 initial_teb.exit_code = STILL_ACTIVE;
169 initial_teb.socket = server_fd;
171 /* Allocate the TEB selector (%fs register) */
173 if (!(initial_teb.teb_sel = SELECTOR_AllocBlock( &initial_teb, 0x1000,
174 SEGMENT_DATA, TRUE, FALSE )))
176 MESSAGE("Could not allocate fs register for initial thread\n" );
177 return NULL;
179 SYSDEPS_SetCurThread( &initial_teb );
181 /* Now proceed with normal initialization */
183 if (CLIENT_InitThread()) return NULL;
184 if (!THREAD_InitTEB( &initial_teb, 0, TRUE )) return NULL;
185 return &initial_teb;
189 /***********************************************************************
190 * THREAD_Create
192 * NOTES:
193 * Native NT dlls are using the space left on the allocated page
194 * the first allocated TEB on NT is at 0x7ffde000, since we can't
195 * allocate in this area and don't support a granularity of 4kb
196 * yet we leave it to VirtualAlloc to choose an address.
198 TEB *THREAD_Create( PDB *pdb, void *tid, int fd, DWORD flags,
199 DWORD stack_size, BOOL alloc_stack16 )
201 TEB *teb = VirtualAlloc(0, 0x1000, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
202 if (!teb) return NULL;
203 teb->except = (void *)-1;
204 teb->htask16 = pdb->task;
205 teb->self = teb;
206 teb->tibflags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
207 teb->tls_ptr = teb->tls_array;
208 teb->process = pdb;
209 teb->exit_code = STILL_ACTIVE;
210 teb->socket = fd;
211 teb->tid = tid;
212 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
214 /* Allocate the TEB selector (%fs register) */
216 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
217 if (!teb->teb_sel) goto error;
219 /* Do the rest of the initialization */
221 if (!THREAD_InitTEB( teb, stack_size, alloc_stack16 )) goto error;
223 TRACE("(%p) succeeded\n", teb);
224 return teb;
226 error:
227 if (teb->teb_sel) SELECTOR_FreeBlock( teb->teb_sel, 1 );
228 VirtualFree( teb, 0, MEM_RELEASE );
229 return NULL;
233 /***********************************************************************
234 * THREAD_Start
236 * Start execution of a newly created thread. Does not return.
238 static void THREAD_Start(void)
240 HANDLE cleanup_object;
241 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
243 /* install cleanup handler */
244 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
245 GetCurrentProcess(), &cleanup_object,
246 0, FALSE, DUPLICATE_SAME_ACCESS ))
247 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTEB,
248 (ULONG_PTR)NtCurrentTeb() );
250 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
251 PE_InitTls();
252 MODULE_DllThreadAttach( NULL );
253 ExitThread( func( NtCurrentTeb()->entry_arg ) );
257 /***********************************************************************
258 * CreateThread (KERNEL32.63)
260 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
261 LPTHREAD_START_ROUTINE start, LPVOID param,
262 DWORD flags, LPDWORD id )
264 struct new_thread_request *req = get_req_buffer();
265 int socket, handle = -1;
266 TEB *teb;
268 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
269 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
270 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
271 handle = req->handle;
273 if (!(teb = THREAD_Create( PROCESS_Current(), req->tid, socket, flags, stack, TRUE )))
275 close( socket );
276 return 0;
278 teb->tibflags |= TEBF_WIN32;
279 teb->entry_point = start;
280 teb->entry_arg = param;
281 teb->startup = THREAD_Start;
282 if (SYSDEPS_SpawnThread( teb ) == -1)
284 CloseHandle( handle );
285 return 0;
287 if (id) *id = (DWORD)teb->tid;
288 return handle;
291 /***********************************************************************
292 * CreateThread16 (KERNEL.441)
294 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
296 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
297 DWORD param = ((DWORD *)threadArgs)[1];
298 HeapFree( GetProcessHeap(), 0, threadArgs );
300 ((LPDWORD)CURRENT_STACK16)[-1] = param;
301 return CallTo16Long( start, sizeof(DWORD) );
303 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
304 FARPROC16 start, SEGPTR param,
305 DWORD flags, LPDWORD id )
307 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
308 if (!threadArgs) return INVALID_HANDLE_VALUE;
309 threadArgs[0] = (DWORD)start;
310 threadArgs[1] = (DWORD)param;
312 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
316 /***********************************************************************
317 * ExitThread [KERNEL32.215] Ends a thread
319 * RETURNS
320 * None
322 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
324 struct terminate_thread_request *req = get_req_buffer();
326 /* send the exit code to the server */
327 req->handle = GetCurrentThread();
328 req->exit_code = code;
329 server_call( REQ_TERMINATE_THREAD );
330 if (req->last)
332 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
333 TASK_KillTask( 0 );
334 exit( code );
336 else
338 MODULE_DllThreadDetach( NULL );
339 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
340 SYSDEPS_ExitThread( code );
345 /***********************************************************************
346 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
348 * RETURNS
349 * Pseudohandle for the current thread
351 HANDLE WINAPI GetCurrentThread(void)
353 return CURRENT_THREAD_PSEUDOHANDLE;
357 /**********************************************************************
358 * SetLastErrorEx [USER32.485] Sets the last-error code.
360 * RETURNS
361 * None.
363 void WINAPI SetLastErrorEx(
364 DWORD error, /* [in] Per-thread error code */
365 DWORD type) /* [in] Error type */
367 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
368 switch(type) {
369 case 0:
370 break;
371 case SLE_ERROR:
372 case SLE_MINORERROR:
373 case SLE_WARNING:
374 /* Fall through for now */
375 default:
376 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
377 break;
379 SetLastError( error );
383 /**********************************************************************
384 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
386 * Allocates a thread local storage index
388 * RETURNS
389 * Success: TLS Index
390 * Failure: 0xFFFFFFFF
392 DWORD WINAPI TlsAlloc( void )
394 PDB *process = PROCESS_Current();
395 DWORD i, mask, ret = 0;
396 DWORD *bits = process->tls_bits;
397 EnterCriticalSection( &process->crit_section );
398 if (*bits == 0xffffffff)
400 bits++;
401 ret = 32;
402 if (*bits == 0xffffffff)
404 LeaveCriticalSection( &process->crit_section );
405 SetLastError( ERROR_NO_MORE_ITEMS );
406 return 0xffffffff;
409 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
410 *bits |= mask;
411 LeaveCriticalSection( &process->crit_section );
412 return ret + i;
416 /**********************************************************************
417 * TlsFree [KERNEL32.531] Releases a TLS index.
419 * Releases a thread local storage index, making it available for reuse
421 * RETURNS
422 * Success: TRUE
423 * Failure: FALSE
425 BOOL WINAPI TlsFree(
426 DWORD index) /* [in] TLS Index to free */
428 PDB *process = PROCESS_Current();
429 DWORD mask;
430 DWORD *bits = process->tls_bits;
431 if (index >= 64)
433 SetLastError( ERROR_INVALID_PARAMETER );
434 return FALSE;
436 EnterCriticalSection( &process->crit_section );
437 if (index >= 32) bits++;
438 mask = (1 << (index & 31));
439 if (!(*bits & mask)) /* already free? */
441 LeaveCriticalSection( &process->crit_section );
442 SetLastError( ERROR_INVALID_PARAMETER );
443 return FALSE;
445 *bits &= ~mask;
446 NtCurrentTeb()->tls_array[index] = 0;
447 /* FIXME: should zero all other thread values */
448 LeaveCriticalSection( &process->crit_section );
449 return TRUE;
453 /**********************************************************************
454 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
456 * RETURNS
457 * Success: Value stored in calling thread's TLS slot for index
458 * Failure: 0 and GetLastError returns NO_ERROR
460 LPVOID WINAPI TlsGetValue(
461 DWORD index) /* [in] TLS index to retrieve value for */
463 if (index >= 64)
465 SetLastError( ERROR_INVALID_PARAMETER );
466 return NULL;
468 SetLastError( ERROR_SUCCESS );
469 return NtCurrentTeb()->tls_array[index];
473 /**********************************************************************
474 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
476 * RETURNS
477 * Success: TRUE
478 * Failure: FALSE
480 BOOL WINAPI TlsSetValue(
481 DWORD index, /* [in] TLS index to set value for */
482 LPVOID value) /* [in] Value to be stored */
484 if (index >= 64)
486 SetLastError( ERROR_INVALID_PARAMETER );
487 return FALSE;
489 NtCurrentTeb()->tls_array[index] = value;
490 return TRUE;
494 /***********************************************************************
495 * SetThreadContext [KERNEL32.670] Sets context of thread.
497 * RETURNS
498 * Success: TRUE
499 * Failure: FALSE
501 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
502 const CONTEXT *context ) /* [in] Address of context structure */
504 struct set_thread_context_request *req = get_req_buffer();
505 req->handle = handle;
506 req->flags = context->ContextFlags;
507 memcpy( &req->context, context, sizeof(*context) );
508 return !server_call( REQ_SET_THREAD_CONTEXT );
512 /***********************************************************************
513 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
515 * RETURNS
516 * Success: TRUE
517 * Failure: FALSE
519 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
520 CONTEXT *context ) /* [out] Address of context structure */
522 struct get_thread_context_request *req = get_req_buffer();
523 req->handle = handle;
524 req->flags = context->ContextFlags;
525 memcpy( &req->context, context, sizeof(*context) );
526 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
527 memcpy( context, &req->context, sizeof(*context) );
528 return TRUE;
532 /**********************************************************************
533 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
535 * RETURNS
536 * Success: Thread's priority level.
537 * Failure: THREAD_PRIORITY_ERROR_RETURN
539 INT WINAPI GetThreadPriority(
540 HANDLE hthread) /* [in] Handle to thread */
542 INT ret = THREAD_PRIORITY_ERROR_RETURN;
543 struct get_thread_info_request *req = get_req_buffer();
544 req->handle = hthread;
545 req->tid_in = 0;
546 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
547 return ret;
551 /**********************************************************************
552 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
554 * RETURNS
555 * Success: TRUE
556 * Failure: FALSE
558 BOOL WINAPI SetThreadPriority(
559 HANDLE hthread, /* [in] Handle to thread */
560 INT priority) /* [in] Thread priority level */
562 struct set_thread_info_request *req = get_req_buffer();
563 req->handle = hthread;
564 req->priority = priority;
565 req->mask = SET_THREAD_INFO_PRIORITY;
566 return !server_call( REQ_SET_THREAD_INFO );
570 /**********************************************************************
571 * SetThreadAffinityMask (KERNEL32.669)
573 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
575 struct set_thread_info_request *req = get_req_buffer();
576 req->handle = hThread;
577 req->affinity = dwThreadAffinityMask;
578 req->mask = SET_THREAD_INFO_AFFINITY;
579 if (server_call( REQ_SET_THREAD_INFO )) return 0;
580 return 1; /* FIXME: should return previous value */
584 /**********************************************************************
585 * TerminateThread [KERNEL32.685] Terminates a thread
587 * RETURNS
588 * Success: TRUE
589 * Failure: FALSE
591 BOOL WINAPI TerminateThread(
592 HANDLE handle, /* [in] Handle to thread */
593 DWORD exitcode) /* [in] Exit code for thread */
595 BOOL ret;
596 struct terminate_thread_request *req = get_req_buffer();
597 req->handle = handle;
598 req->exit_code = exitcode;
599 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
601 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
602 if (req->last) exit( exitcode );
603 else SYSDEPS_ExitThread( exitcode );
605 return ret;
609 /**********************************************************************
610 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
612 * RETURNS
613 * Success: TRUE
614 * Failure: FALSE
616 BOOL WINAPI GetExitCodeThread(
617 HANDLE hthread, /* [in] Handle to thread */
618 LPDWORD exitcode) /* [out] Address to receive termination status */
620 BOOL ret = FALSE;
621 struct get_thread_info_request *req = get_req_buffer();
622 req->handle = hthread;
623 req->tid_in = 0;
624 if (!server_call( REQ_GET_THREAD_INFO ))
626 if (exitcode) *exitcode = req->exit_code;
627 ret = TRUE;
629 return ret;
633 /**********************************************************************
634 * ResumeThread [KERNEL32.587] Resumes a thread.
636 * Decrements a thread's suspend count. When count is zero, the
637 * execution of the thread is resumed.
639 * RETURNS
640 * Success: Previous suspend count
641 * Failure: 0xFFFFFFFF
642 * Already running: 0
644 DWORD WINAPI ResumeThread(
645 HANDLE hthread) /* [in] Identifies thread to restart */
647 DWORD ret = 0xffffffff;
648 struct resume_thread_request *req = get_req_buffer();
649 req->handle = hthread;
650 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
651 return ret;
655 /**********************************************************************
656 * SuspendThread [KERNEL32.681] Suspends a thread.
658 * RETURNS
659 * Success: Previous suspend count
660 * Failure: 0xFFFFFFFF
662 DWORD WINAPI SuspendThread(
663 HANDLE hthread) /* [in] Handle to the thread */
665 DWORD ret = 0xffffffff;
666 struct suspend_thread_request *req = get_req_buffer();
667 req->handle = hthread;
668 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
669 return ret;
673 /***********************************************************************
674 * QueueUserAPC (KERNEL32.566)
676 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
678 struct queue_apc_request *req = get_req_buffer();
679 req->handle = hthread;
680 req->func = func;
681 req->param = (void *)data;
682 return !server_call( REQ_QUEUE_APC );
686 /**********************************************************************
687 * GetThreadTimes [KERNEL32.???] Obtains timing information.
689 * NOTES
690 * What are the fields where these values are stored?
692 * RETURNS
693 * Success: TRUE
694 * Failure: FALSE
696 BOOL WINAPI GetThreadTimes(
697 HANDLE thread, /* [in] Specifies the thread of interest */
698 LPFILETIME creationtime, /* [out] When the thread was created */
699 LPFILETIME exittime, /* [out] When the thread was destroyed */
700 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
701 LPFILETIME usertime) /* [out] Time thread spent in user mode */
703 FIXME("(0x%08x): stub\n",thread);
704 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
705 return FALSE;
709 /**********************************************************************
710 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
712 * Attaches the input processing mechanism of one thread to that of
713 * another thread.
715 * RETURNS
716 * Success: TRUE
717 * Failure: FALSE
719 * TODO:
720 * 1. Reset the Key State (currenly per thread key state is not maintained)
722 BOOL WINAPI AttachThreadInput(
723 DWORD idAttach, /* [in] Thread to attach */
724 DWORD idAttachTo, /* [in] Thread to attach to */
725 BOOL fAttach) /* [in] Attach or detach */
727 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
728 BOOL16 bRet = 0;
730 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
732 /* A thread cannot attach to itself */
733 if ( idAttach == idAttachTo )
734 goto CLEANUP;
736 /* According to the docs this method should fail if a
737 * "Journal record" hook is installed. (attaches all input queues together)
739 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
740 goto CLEANUP;
742 /* Retrieve message queues corresponding to the thread id's */
743 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
744 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
746 /* Ensure we have message queues and that Src and Tgt threads
747 * are not system threads.
749 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
750 goto CLEANUP;
752 if (fAttach) /* Attach threads */
754 /* Only attach if currently detached */
755 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
757 /* First release the target threads perQData */
758 PERQDATA_Release( pTgtMsgQ->pQData );
760 /* Share a reference to the source threads perQDATA */
761 PERQDATA_Addref( pSrcMsgQ->pQData );
762 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
765 else /* Detach threads */
767 /* Only detach if currently attached */
768 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
770 /* First release the target threads perQData */
771 PERQDATA_Release( pTgtMsgQ->pQData );
773 /* Give the target thread its own private perQDATA once more */
774 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
778 /* TODO: Reset the Key State */
780 bRet = 1; /* Success */
782 CLEANUP:
784 /* Unlock the queues before returning */
785 if ( pSrcMsgQ )
786 QUEUE_Unlock( pSrcMsgQ );
787 if ( pTgtMsgQ )
788 QUEUE_Unlock( pTgtMsgQ );
790 return bRet;
793 /**********************************************************************
794 * VWin32_BoostThreadGroup [KERNEL.535]
796 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
798 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
801 /**********************************************************************
802 * VWin32_BoostThreadStatic [KERNEL.536]
804 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
806 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
809 /**********************************************************************
810 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
812 * RETURNS
813 * Success: TRUE
814 * Failure: FALSE
816 * NOTES
817 * Implemented in NT only (3.1 and above according to MS
819 BOOL WINAPI SetThreadLocale(
820 LCID lcid) /* [in] Locale identifier */
822 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
823 return FALSE;
827 #ifdef __i386__
829 /* void WINAPI SetLastError( DWORD error ); */
830 __ASM_GLOBAL_FUNC( SetLastError,
831 "movl 4(%esp),%eax\n\t"
832 ".byte 0x64\n\t"
833 "movl %eax,0x60\n\t"
834 "ret $4" );
836 /* DWORD WINAPI GetLastError(void); */
837 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
839 /* DWORD WINAPI GetCurrentThreadId(void) */
840 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
842 #else /* __i386__ */
844 /**********************************************************************
845 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
847 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
849 NtCurrentTeb()->last_error = error;
852 /**********************************************************************
853 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
855 DWORD WINAPI GetLastError(void)
857 return NtCurrentTeb()->last_error;
860 /***********************************************************************
861 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
863 DWORD WINAPI GetCurrentThreadId(void)
865 return (DWORD)NtCurrentTeb()->tid;
868 #endif /* __i386__ */