Stub for NtAllocateUuids.
[wine/testsucceed.git] / scheduler / thread.c
blobd1f47bbb59e193295c3281aaa612e235aca53bb4
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 *pid, 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->pid = pid;
212 teb->tid = tid;
213 fcntl( fd, F_SETFD, 1 ); /* set close on exec flag */
215 /* Allocate the TEB selector (%fs register) */
217 teb->teb_sel = SELECTOR_AllocBlock( teb, 0x1000, SEGMENT_DATA, TRUE, FALSE );
218 if (!teb->teb_sel) goto error;
220 /* Do the rest of the initialization */
222 if (!THREAD_InitTEB( teb, stack_size, alloc_stack16 )) goto error;
224 TRACE("(%p) succeeded\n", teb);
225 return teb;
227 error:
228 if (teb->teb_sel) SELECTOR_FreeBlock( teb->teb_sel, 1 );
229 VirtualFree( teb, 0, MEM_RELEASE );
230 return NULL;
234 /***********************************************************************
235 * THREAD_Start
237 * Start execution of a newly created thread. Does not return.
239 static void THREAD_Start(void)
241 HANDLE cleanup_object;
242 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)NtCurrentTeb()->entry_point;
244 /* install cleanup handler */
245 if (DuplicateHandle( GetCurrentProcess(), GetCurrentThread(),
246 GetCurrentProcess(), &cleanup_object,
247 0, FALSE, DUPLICATE_SAME_ACCESS ))
248 NtCurrentTeb()->cleanup = SERVICE_AddObject( cleanup_object, THREAD_FreeTEB,
249 (ULONG_PTR)NtCurrentTeb() );
251 PROCESS_CallUserSignalProc( USIG_THREAD_INIT, 0 );
252 PE_InitTls();
253 MODULE_DllThreadAttach( NULL );
254 ExitThread( func( NtCurrentTeb()->entry_arg ) );
258 /***********************************************************************
259 * CreateThread (KERNEL32.63)
261 HANDLE WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
262 LPTHREAD_START_ROUTINE start, LPVOID param,
263 DWORD flags, LPDWORD id )
265 struct new_thread_request *req = get_req_buffer();
266 int socket, handle = -1;
267 TEB *teb;
269 req->suspend = ((flags & CREATE_SUSPENDED) != 0);
270 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
271 if (server_call_fd( REQ_NEW_THREAD, -1, &socket )) return 0;
272 handle = req->handle;
274 if (!(teb = THREAD_Create( PROCESS_Current(), (void *)GetCurrentProcessId(),
275 req->tid, socket, flags, stack, TRUE )))
277 close( socket );
278 return 0;
280 teb->tibflags |= TEBF_WIN32;
281 teb->entry_point = start;
282 teb->entry_arg = param;
283 teb->startup = THREAD_Start;
284 if (SYSDEPS_SpawnThread( teb ) == -1)
286 CloseHandle( handle );
287 return 0;
289 if (id) *id = (DWORD)teb->tid;
290 return handle;
293 /***********************************************************************
294 * CreateThread16 (KERNEL.441)
296 static DWORD CALLBACK THREAD_StartThread16( LPVOID threadArgs )
298 FARPROC16 start = ((FARPROC16 *)threadArgs)[0];
299 DWORD param = ((DWORD *)threadArgs)[1];
300 HeapFree( GetProcessHeap(), 0, threadArgs );
302 ((LPDWORD)CURRENT_STACK16)[-1] = param;
303 return CallTo16Long( start, sizeof(DWORD) );
305 HANDLE WINAPI CreateThread16( SECURITY_ATTRIBUTES *sa, DWORD stack,
306 FARPROC16 start, SEGPTR param,
307 DWORD flags, LPDWORD id )
309 DWORD *threadArgs = HeapAlloc( GetProcessHeap(), 0, 2*sizeof(DWORD) );
310 if (!threadArgs) return INVALID_HANDLE_VALUE;
311 threadArgs[0] = (DWORD)start;
312 threadArgs[1] = (DWORD)param;
314 return CreateThread( sa, stack, THREAD_StartThread16, threadArgs, flags, id );
318 /***********************************************************************
319 * ExitThread [KERNEL32.215] Ends a thread
321 * RETURNS
322 * None
324 void WINAPI ExitThread( DWORD code ) /* [in] Exit code for this thread */
326 struct terminate_thread_request *req = get_req_buffer();
328 /* send the exit code to the server */
329 req->handle = GetCurrentThread();
330 req->exit_code = code;
331 server_call( REQ_TERMINATE_THREAD );
332 if (req->last)
334 MODULE_DllProcessDetach( TRUE, (LPVOID)1 );
335 TASK_KillTask( 0 );
336 exit( code );
338 else
340 MODULE_DllThreadDetach( NULL );
341 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
342 SYSDEPS_ExitThread( code );
347 /**********************************************************************
348 * SetLastErrorEx [USER32.485] Sets the last-error code.
350 * RETURNS
351 * None.
353 void WINAPI SetLastErrorEx(
354 DWORD error, /* [in] Per-thread error code */
355 DWORD type) /* [in] Error type */
357 TRACE("(0x%08lx, 0x%08lx)\n", error,type);
358 switch(type) {
359 case 0:
360 break;
361 case SLE_ERROR:
362 case SLE_MINORERROR:
363 case SLE_WARNING:
364 /* Fall through for now */
365 default:
366 FIXME("(error=%08lx, type=%08lx): Unhandled type\n", error,type);
367 break;
369 SetLastError( error );
373 /**********************************************************************
374 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
376 * Allocates a thread local storage index
378 * RETURNS
379 * Success: TLS Index
380 * Failure: 0xFFFFFFFF
382 DWORD WINAPI TlsAlloc( void )
384 PDB *process = PROCESS_Current();
385 DWORD i, mask, ret = 0;
386 DWORD *bits = process->tls_bits;
387 EnterCriticalSection( &process->crit_section );
388 if (*bits == 0xffffffff)
390 bits++;
391 ret = 32;
392 if (*bits == 0xffffffff)
394 LeaveCriticalSection( &process->crit_section );
395 SetLastError( ERROR_NO_MORE_ITEMS );
396 return 0xffffffff;
399 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
400 *bits |= mask;
401 LeaveCriticalSection( &process->crit_section );
402 return ret + i;
406 /**********************************************************************
407 * TlsFree [KERNEL32.531] Releases a TLS index.
409 * Releases a thread local storage index, making it available for reuse
411 * RETURNS
412 * Success: TRUE
413 * Failure: FALSE
415 BOOL WINAPI TlsFree(
416 DWORD index) /* [in] TLS Index to free */
418 PDB *process = PROCESS_Current();
419 DWORD mask;
420 DWORD *bits = process->tls_bits;
421 if (index >= 64)
423 SetLastError( ERROR_INVALID_PARAMETER );
424 return FALSE;
426 EnterCriticalSection( &process->crit_section );
427 if (index >= 32) bits++;
428 mask = (1 << (index & 31));
429 if (!(*bits & mask)) /* already free? */
431 LeaveCriticalSection( &process->crit_section );
432 SetLastError( ERROR_INVALID_PARAMETER );
433 return FALSE;
435 *bits &= ~mask;
436 NtCurrentTeb()->tls_array[index] = 0;
437 /* FIXME: should zero all other thread values */
438 LeaveCriticalSection( &process->crit_section );
439 return TRUE;
443 /**********************************************************************
444 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
446 * RETURNS
447 * Success: Value stored in calling thread's TLS slot for index
448 * Failure: 0 and GetLastError returns NO_ERROR
450 LPVOID WINAPI TlsGetValue(
451 DWORD index) /* [in] TLS index to retrieve value for */
453 if (index >= 64)
455 SetLastError( ERROR_INVALID_PARAMETER );
456 return NULL;
458 SetLastError( ERROR_SUCCESS );
459 return NtCurrentTeb()->tls_array[index];
463 /**********************************************************************
464 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
466 * RETURNS
467 * Success: TRUE
468 * Failure: FALSE
470 BOOL WINAPI TlsSetValue(
471 DWORD index, /* [in] TLS index to set value for */
472 LPVOID value) /* [in] Value to be stored */
474 if (index >= 64)
476 SetLastError( ERROR_INVALID_PARAMETER );
477 return FALSE;
479 NtCurrentTeb()->tls_array[index] = value;
480 return TRUE;
484 /***********************************************************************
485 * SetThreadContext [KERNEL32.670] Sets context of thread.
487 * RETURNS
488 * Success: TRUE
489 * Failure: FALSE
491 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
492 const CONTEXT *context ) /* [in] Address of context structure */
494 struct set_thread_context_request *req = get_req_buffer();
495 req->handle = handle;
496 req->flags = context->ContextFlags;
497 memcpy( &req->context, context, sizeof(*context) );
498 return !server_call( REQ_SET_THREAD_CONTEXT );
502 /***********************************************************************
503 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
505 * RETURNS
506 * Success: TRUE
507 * Failure: FALSE
509 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
510 CONTEXT *context ) /* [out] Address of context structure */
512 struct get_thread_context_request *req = get_req_buffer();
513 req->handle = handle;
514 req->flags = context->ContextFlags;
515 memcpy( &req->context, context, sizeof(*context) );
516 if (server_call( REQ_GET_THREAD_CONTEXT )) return FALSE;
517 memcpy( context, &req->context, sizeof(*context) );
518 return TRUE;
522 /**********************************************************************
523 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
525 * RETURNS
526 * Success: Thread's priority level.
527 * Failure: THREAD_PRIORITY_ERROR_RETURN
529 INT WINAPI GetThreadPriority(
530 HANDLE hthread) /* [in] Handle to thread */
532 INT ret = THREAD_PRIORITY_ERROR_RETURN;
533 struct get_thread_info_request *req = get_req_buffer();
534 req->handle = hthread;
535 req->tid_in = 0;
536 if (!server_call( REQ_GET_THREAD_INFO )) ret = req->priority;
537 return ret;
541 /**********************************************************************
542 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
544 * RETURNS
545 * Success: TRUE
546 * Failure: FALSE
548 BOOL WINAPI SetThreadPriority(
549 HANDLE hthread, /* [in] Handle to thread */
550 INT priority) /* [in] Thread priority level */
552 struct set_thread_info_request *req = get_req_buffer();
553 req->handle = hthread;
554 req->priority = priority;
555 req->mask = SET_THREAD_INFO_PRIORITY;
556 return !server_call( REQ_SET_THREAD_INFO );
560 /**********************************************************************
561 * SetThreadAffinityMask (KERNEL32.669)
563 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
565 struct set_thread_info_request *req = get_req_buffer();
566 req->handle = hThread;
567 req->affinity = dwThreadAffinityMask;
568 req->mask = SET_THREAD_INFO_AFFINITY;
569 if (server_call( REQ_SET_THREAD_INFO )) return 0;
570 return 1; /* FIXME: should return previous value */
574 /**********************************************************************
575 * TerminateThread [KERNEL32.685] Terminates a thread
577 * RETURNS
578 * Success: TRUE
579 * Failure: FALSE
581 BOOL WINAPI TerminateThread(
582 HANDLE handle, /* [in] Handle to thread */
583 DWORD exitcode) /* [in] Exit code for thread */
585 BOOL ret;
586 struct terminate_thread_request *req = get_req_buffer();
587 req->handle = handle;
588 req->exit_code = exitcode;
589 if ((ret = !server_call( REQ_TERMINATE_THREAD )) && req->self)
591 PROCESS_CallUserSignalProc( USIG_THREAD_EXIT, 0 );
592 if (req->last) exit( exitcode );
593 else SYSDEPS_ExitThread( exitcode );
595 return ret;
599 /**********************************************************************
600 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
602 * RETURNS
603 * Success: TRUE
604 * Failure: FALSE
606 BOOL WINAPI GetExitCodeThread(
607 HANDLE hthread, /* [in] Handle to thread */
608 LPDWORD exitcode) /* [out] Address to receive termination status */
610 BOOL ret = FALSE;
611 struct get_thread_info_request *req = get_req_buffer();
612 req->handle = hthread;
613 req->tid_in = 0;
614 if (!server_call( REQ_GET_THREAD_INFO ))
616 if (exitcode) *exitcode = req->exit_code;
617 ret = TRUE;
619 return ret;
623 /**********************************************************************
624 * ResumeThread [KERNEL32.587] Resumes a thread.
626 * Decrements a thread's suspend count. When count is zero, the
627 * execution of the thread is resumed.
629 * RETURNS
630 * Success: Previous suspend count
631 * Failure: 0xFFFFFFFF
632 * Already running: 0
634 DWORD WINAPI ResumeThread(
635 HANDLE hthread) /* [in] Identifies thread to restart */
637 DWORD ret = 0xffffffff;
638 struct resume_thread_request *req = get_req_buffer();
639 req->handle = hthread;
640 if (!server_call( REQ_RESUME_THREAD )) ret = req->count;
641 return ret;
645 /**********************************************************************
646 * SuspendThread [KERNEL32.681] Suspends a thread.
648 * RETURNS
649 * Success: Previous suspend count
650 * Failure: 0xFFFFFFFF
652 DWORD WINAPI SuspendThread(
653 HANDLE hthread) /* [in] Handle to the thread */
655 DWORD ret = 0xffffffff;
656 struct suspend_thread_request *req = get_req_buffer();
657 req->handle = hthread;
658 if (!server_call( REQ_SUSPEND_THREAD )) ret = req->count;
659 return ret;
663 /***********************************************************************
664 * QueueUserAPC (KERNEL32.566)
666 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
668 struct queue_apc_request *req = get_req_buffer();
669 req->handle = hthread;
670 req->func = func;
671 req->param = (void *)data;
672 return !server_call( REQ_QUEUE_APC );
676 /**********************************************************************
677 * GetThreadTimes [KERNEL32.???] Obtains timing information.
679 * NOTES
680 * What are the fields where these values are stored?
682 * RETURNS
683 * Success: TRUE
684 * Failure: FALSE
686 BOOL WINAPI GetThreadTimes(
687 HANDLE thread, /* [in] Specifies the thread of interest */
688 LPFILETIME creationtime, /* [out] When the thread was created */
689 LPFILETIME exittime, /* [out] When the thread was destroyed */
690 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
691 LPFILETIME usertime) /* [out] Time thread spent in user mode */
693 FIXME("(0x%08x): stub\n",thread);
694 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
695 return FALSE;
699 /**********************************************************************
700 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
702 * Attaches the input processing mechanism of one thread to that of
703 * another thread.
705 * RETURNS
706 * Success: TRUE
707 * Failure: FALSE
709 * TODO:
710 * 1. Reset the Key State (currenly per thread key state is not maintained)
712 BOOL WINAPI AttachThreadInput(
713 DWORD idAttach, /* [in] Thread to attach */
714 DWORD idAttachTo, /* [in] Thread to attach to */
715 BOOL fAttach) /* [in] Attach or detach */
717 MESSAGEQUEUE *pSrcMsgQ = 0, *pTgtMsgQ = 0;
718 BOOL16 bRet = 0;
720 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
722 /* A thread cannot attach to itself */
723 if ( idAttach == idAttachTo )
724 goto CLEANUP;
726 /* According to the docs this method should fail if a
727 * "Journal record" hook is installed. (attaches all input queues together)
729 if ( HOOK_IsHooked( WH_JOURNALRECORD ) )
730 goto CLEANUP;
732 /* Retrieve message queues corresponding to the thread id's */
733 pTgtMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttach ) );
734 pSrcMsgQ = (MESSAGEQUEUE *)QUEUE_Lock( GetThreadQueue16( idAttachTo ) );
736 /* Ensure we have message queues and that Src and Tgt threads
737 * are not system threads.
739 if ( !pSrcMsgQ || !pTgtMsgQ || !pSrcMsgQ->pQData || !pTgtMsgQ->pQData )
740 goto CLEANUP;
742 if (fAttach) /* Attach threads */
744 /* Only attach if currently detached */
745 if ( pTgtMsgQ->pQData != pSrcMsgQ->pQData )
747 /* First release the target threads perQData */
748 PERQDATA_Release( pTgtMsgQ->pQData );
750 /* Share a reference to the source threads perQDATA */
751 PERQDATA_Addref( pSrcMsgQ->pQData );
752 pTgtMsgQ->pQData = pSrcMsgQ->pQData;
755 else /* Detach threads */
757 /* Only detach if currently attached */
758 if ( pTgtMsgQ->pQData == pSrcMsgQ->pQData )
760 /* First release the target threads perQData */
761 PERQDATA_Release( pTgtMsgQ->pQData );
763 /* Give the target thread its own private perQDATA once more */
764 pTgtMsgQ->pQData = PERQDATA_CreateInstance();
768 /* TODO: Reset the Key State */
770 bRet = 1; /* Success */
772 CLEANUP:
774 /* Unlock the queues before returning */
775 if ( pSrcMsgQ )
776 QUEUE_Unlock( pSrcMsgQ );
777 if ( pTgtMsgQ )
778 QUEUE_Unlock( pTgtMsgQ );
780 return bRet;
783 /**********************************************************************
784 * VWin32_BoostThreadGroup [KERNEL.535]
786 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
788 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
791 /**********************************************************************
792 * VWin32_BoostThreadStatic [KERNEL.536]
794 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
796 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
799 /**********************************************************************
800 * SetThreadLocale [KERNEL32.671] Sets the calling threads current locale.
802 * RETURNS
803 * Success: TRUE
804 * Failure: FALSE
806 * NOTES
807 * Implemented in NT only (3.1 and above according to MS
809 BOOL WINAPI SetThreadLocale(
810 LCID lcid) /* [in] Locale identifier */
812 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
813 return FALSE;
817 /***********************************************************************
818 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
820 * RETURNS
821 * Pseudohandle for the current thread
823 #undef GetCurrentThread
824 HANDLE WINAPI GetCurrentThread(void)
826 return 0xfffffffe;
830 #ifdef __i386__
832 /* void WINAPI SetLastError( DWORD error ); */
833 __ASM_GLOBAL_FUNC( SetLastError,
834 "movl 4(%esp),%eax\n\t"
835 ".byte 0x64\n\t"
836 "movl %eax,0x60\n\t"
837 "ret $4" );
839 /* DWORD WINAPI GetLastError(void); */
840 __ASM_GLOBAL_FUNC( GetLastError, ".byte 0x64\n\tmovl 0x60,%eax\n\tret" );
842 /* DWORD WINAPI GetCurrentProcessId(void) */
843 __ASM_GLOBAL_FUNC( GetCurrentProcessId, ".byte 0x64\n\tmovl 0x20,%eax\n\tret" );
845 /* DWORD WINAPI GetCurrentThreadId(void) */
846 __ASM_GLOBAL_FUNC( GetCurrentThreadId, ".byte 0x64\n\tmovl 0x24,%eax\n\tret" );
848 #else /* __i386__ */
850 /**********************************************************************
851 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
853 void WINAPI SetLastError( DWORD error ) /* [in] Per-thread error code */
855 NtCurrentTeb()->last_error = error;
858 /**********************************************************************
859 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
861 DWORD WINAPI GetLastError(void)
863 return NtCurrentTeb()->last_error;
866 /***********************************************************************
867 * GetCurrentProcessId [KERNEL32.199] Returns process identifier.
869 DWORD WINAPI GetCurrentProcessId(void)
871 return (DWORD)NtCurrentTeb()->pid;
874 /***********************************************************************
875 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
877 DWORD WINAPI GetCurrentThreadId(void)
879 return (DWORD)NtCurrentTeb()->tid;
882 #endif /* __i386__ */