comctl32/tooltips: Reset window subclass data when removing tools.
[wine.git] / dlls / ntdll / thread.c
blob479b8d67c750963f145e496a02b9980307da651b
1 /*
2 * NT threads support
4 * Copyright 1996, 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <limits.h>
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_MMAN_H
29 #include <sys/mman.h>
30 #endif
31 #ifdef HAVE_SYS_TIMES_H
32 #include <sys/times.h>
33 #endif
34 #ifdef HAVE_SYS_SYSCALL_H
35 #include <sys/syscall.h>
36 #endif
38 #define NONAMELESSUNION
39 #include "ntstatus.h"
40 #define WIN32_NO_STATUS
41 #include "winternl.h"
42 #include "wine/library.h"
43 #include "wine/server.h"
44 #include "wine/debug.h"
45 #include "ntdll_misc.h"
46 #include "ddk/wdm.h"
47 #include "wine/exception.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(thread);
51 #ifndef PTHREAD_STACK_MIN
52 #define PTHREAD_STACK_MIN 16384
53 #endif
55 struct _KUSER_SHARED_DATA *user_shared_data = NULL;
57 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter = NULL;
58 void (WINAPI *kernel32_start_process)(LPTHREAD_START_ROUTINE,void*) = NULL;
60 /* info passed to a starting thread */
61 struct startup_info
63 TEB *teb;
64 PRTL_THREAD_START_ROUTINE entry_point;
65 void *entry_arg;
68 static PEB *peb;
69 static PEB_LDR_DATA ldr;
70 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
71 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
72 static RTL_BITMAP tls_bitmap;
73 static RTL_BITMAP tls_expansion_bitmap;
74 static RTL_BITMAP fls_bitmap;
75 static int nb_threads = 1;
77 static RTL_CRITICAL_SECTION peb_lock;
78 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
80 0, 0, &peb_lock,
81 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
82 0, 0, { (DWORD_PTR)(__FILE__ ": peb_lock") }
84 static RTL_CRITICAL_SECTION peb_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
86 /***********************************************************************
87 * get_unicode_string
89 * Copy a unicode string from the startup info.
91 static inline void get_unicode_string( UNICODE_STRING *str, WCHAR **src, WCHAR **dst, UINT len )
93 str->Buffer = *dst;
94 str->Length = len;
95 str->MaximumLength = len + sizeof(WCHAR);
96 memcpy( str->Buffer, *src, len );
97 str->Buffer[len / sizeof(WCHAR)] = 0;
98 *src += len / sizeof(WCHAR);
99 *dst += len / sizeof(WCHAR) + 1;
102 /***********************************************************************
103 * init_user_process_params
105 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
107 static NTSTATUS init_user_process_params( SIZE_T data_size, HANDLE *exe_file )
109 void *ptr;
110 WCHAR *src, *dst;
111 SIZE_T info_size, env_size, size, alloc_size;
112 NTSTATUS status;
113 startup_info_t *info;
114 RTL_USER_PROCESS_PARAMETERS *params = NULL;
116 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, data_size )))
117 return STATUS_NO_MEMORY;
119 SERVER_START_REQ( get_startup_info )
121 wine_server_set_reply( req, info, data_size );
122 if (!(status = wine_server_call( req )))
124 data_size = wine_server_reply_size( reply );
125 info_size = reply->info_size;
126 env_size = data_size - info_size;
127 *exe_file = wine_server_ptr_handle( reply->exe_file );
130 SERVER_END_REQ;
131 if (status != STATUS_SUCCESS) goto done;
133 size = sizeof(*params);
134 size += MAX_NT_PATH_LENGTH * sizeof(WCHAR);
135 size += info->dllpath_len + sizeof(WCHAR);
136 size += info->imagepath_len + sizeof(WCHAR);
137 size += info->cmdline_len + sizeof(WCHAR);
138 size += info->title_len + sizeof(WCHAR);
139 size += info->desktop_len + sizeof(WCHAR);
140 size += info->shellinfo_len + sizeof(WCHAR);
141 size += info->runtime_len + sizeof(WCHAR);
143 alloc_size = size;
144 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &alloc_size,
145 MEM_COMMIT, PAGE_READWRITE );
146 if (status != STATUS_SUCCESS) goto done;
148 NtCurrentTeb()->Peb->ProcessParameters = params;
149 params->AllocationSize = alloc_size;
150 params->Size = size;
151 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
152 params->DebugFlags = info->debug_flags;
153 params->ConsoleHandle = wine_server_ptr_handle( info->console );
154 params->ConsoleFlags = info->console_flags;
155 params->hStdInput = wine_server_ptr_handle( info->hstdin );
156 params->hStdOutput = wine_server_ptr_handle( info->hstdout );
157 params->hStdError = wine_server_ptr_handle( info->hstderr );
158 params->dwX = info->x;
159 params->dwY = info->y;
160 params->dwXSize = info->xsize;
161 params->dwYSize = info->ysize;
162 params->dwXCountChars = info->xchars;
163 params->dwYCountChars = info->ychars;
164 params->dwFillAttribute = info->attribute;
165 params->dwFlags = info->flags;
166 params->wShowWindow = info->show;
168 src = (WCHAR *)(info + 1);
169 dst = (WCHAR *)(params + 1);
171 /* current directory needs more space */
172 get_unicode_string( &params->CurrentDirectory.DosPath, &src, &dst, info->curdir_len );
173 params->CurrentDirectory.DosPath.MaximumLength = MAX_NT_PATH_LENGTH * sizeof(WCHAR);
174 dst = (WCHAR *)(params + 1) + MAX_NT_PATH_LENGTH;
176 get_unicode_string( &params->DllPath, &src, &dst, info->dllpath_len );
177 get_unicode_string( &params->ImagePathName, &src, &dst, info->imagepath_len );
178 get_unicode_string( &params->CommandLine, &src, &dst, info->cmdline_len );
179 get_unicode_string( &params->WindowTitle, &src, &dst, info->title_len );
180 get_unicode_string( &params->Desktop, &src, &dst, info->desktop_len );
181 get_unicode_string( &params->ShellInfo, &src, &dst, info->shellinfo_len );
183 /* runtime info isn't a real string */
184 params->RuntimeInfo.Buffer = dst;
185 params->RuntimeInfo.Length = params->RuntimeInfo.MaximumLength = info->runtime_len;
186 memcpy( dst, src, info->runtime_len );
188 /* environment needs to be a separate memory block */
189 ptr = NULL;
190 alloc_size = max( 1, env_size );
191 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &alloc_size,
192 MEM_COMMIT, PAGE_READWRITE );
193 if (status != STATUS_SUCCESS) goto done;
194 memcpy( ptr, (char *)info + info_size, env_size );
195 params->Environment = ptr;
197 done:
198 RtlFreeHeap( GetProcessHeap(), 0, info );
199 return status;
202 #ifdef __linux__
204 #ifdef HAVE_ELF_H
205 # include <elf.h>
206 #endif
207 #ifdef HAVE_LINK_H
208 # include <link.h>
209 #endif
210 #ifdef HAVE_SYS_AUXV_H
211 # include <sys/auxv.h>
212 #endif
213 #ifndef HAVE_GETAUXVAL
214 static unsigned long getauxval( unsigned long id )
216 extern char **__wine_main_environ;
217 char **ptr = __wine_main_environ;
218 ElfW(auxv_t) *auxv;
220 while (*ptr) ptr++;
221 while (!*ptr) ptr++;
222 for (auxv = (ElfW(auxv_t) *)ptr; auxv->a_type; auxv++)
223 if (auxv->a_type == id) return auxv->a_un.a_val;
224 return 0;
226 #endif
228 static ULONG_PTR get_image_addr(void)
230 ULONG_PTR size, num, phdr_addr = getauxval( AT_PHDR );
231 ElfW(Phdr) *phdr;
233 if (!phdr_addr) return 0;
234 phdr = (ElfW(Phdr) *)phdr_addr;
235 size = getauxval( AT_PHENT );
236 num = getauxval( AT_PHNUM );
237 while (num--)
239 if (phdr->p_type == PT_PHDR) return phdr_addr - phdr->p_offset;
240 phdr = (ElfW(Phdr) *)((char *)phdr + size);
242 return 0;
245 #elif defined(__APPLE__)
246 #include <mach/mach.h>
247 #include <mach/mach_error.h>
249 static ULONG_PTR get_image_addr(void)
251 ULONG_PTR ret = 0;
252 #ifdef TASK_DYLD_INFO
253 struct task_dyld_info dyld_info;
254 mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT;
255 if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&dyld_info, &size) == KERN_SUCCESS)
256 ret = dyld_info.all_image_info_addr;
257 #endif
258 return ret;
261 #else
262 static ULONG_PTR get_image_addr(void)
264 return 0;
266 #endif
268 /***********************************************************************
269 * thread_init
271 * Setup the initial thread.
273 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
275 HANDLE thread_init(void)
277 TEB *teb;
278 void *addr;
279 BOOL suspend;
280 SIZE_T size, info_size;
281 HANDLE exe_file = 0;
282 LARGE_INTEGER now;
283 NTSTATUS status;
284 struct ntdll_thread_data *thread_data;
285 static struct debug_info debug_info; /* debug info for initial thread */
287 virtual_init();
289 /* reserve space for shared user data */
291 addr = (void *)0x7ffe0000;
292 size = 0x10000;
293 status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size,
294 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE );
295 if (status)
297 MESSAGE( "wine: failed to map the shared user data: %08x\n", status );
298 exit(1);
300 user_shared_data = addr;
302 /* allocate and initialize the PEB */
304 addr = NULL;
305 size = sizeof(*peb);
306 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
307 MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
308 peb = addr;
310 peb->FastPebLock = &peb_lock;
311 peb->ProcessParameters = &params;
312 peb->TlsBitmap = &tls_bitmap;
313 peb->TlsExpansionBitmap = &tls_expansion_bitmap;
314 peb->FlsBitmap = &fls_bitmap;
315 peb->LdrData = &ldr;
316 peb->OSMajorVersion = 5;
317 peb->OSMinorVersion = 1;
318 peb->OSBuildNumber = 0xA28;
319 peb->OSPlatformId = VER_PLATFORM_WIN32_NT;
320 params.CurrentDirectory.DosPath.Buffer = current_dir;
321 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
322 params.wShowWindow = 1; /* SW_SHOWNORMAL */
323 ldr.Length = sizeof(ldr);
324 ldr.Initialized = TRUE;
325 RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
326 RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
327 sizeof(peb->TlsExpansionBitmapBits) * 8 );
328 RtlInitializeBitMap( &fls_bitmap, peb->FlsBitmapBits, sizeof(peb->FlsBitmapBits) * 8 );
329 RtlSetBits( peb->TlsBitmap, 0, 1 ); /* TLS index 0 is reserved and should be initialized to NULL. */
330 RtlSetBits( peb->FlsBitmap, 0, 1 );
331 InitializeListHead( &peb->FlsListHead );
332 InitializeListHead( &ldr.InLoadOrderModuleList );
333 InitializeListHead( &ldr.InMemoryOrderModuleList );
334 InitializeListHead( &ldr.InInitializationOrderModuleList );
335 *(ULONG_PTR *)peb->Reserved = get_image_addr();
338 * Starting with Vista, the first user to log on has session id 1.
339 * Session id 0 is for processes that don't interact with the user (like services).
341 peb->SessionId = 1;
343 /* allocate and initialize the initial TEB */
345 signal_alloc_thread( &teb );
346 teb->Peb = peb;
347 teb->Tib.StackBase = (void *)~0UL;
348 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
349 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
351 thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
352 thread_data->request_fd = -1;
353 thread_data->reply_fd = -1;
354 thread_data->wait_fd[0] = -1;
355 thread_data->wait_fd[1] = -1;
356 thread_data->debug_info = &debug_info;
358 signal_init_thread( teb );
359 virtual_init_threading();
361 debug_info.str_pos = debug_info.strings;
362 debug_info.out_pos = debug_info.output;
363 debug_init();
365 /* setup the server connection */
366 server_init_process();
367 info_size = server_init_thread( peb, &suspend );
369 /* create the process heap */
370 if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
372 MESSAGE( "wine: failed to create the process heap\n" );
373 exit(1);
376 /* allocate user parameters */
377 if (info_size)
379 init_user_process_params( info_size, &exe_file );
381 else
383 if (isatty(0) || isatty(1) || isatty(2))
384 params.ConsoleHandle = (HANDLE)2; /* see kernel32/kernel_private.h */
385 if (!isatty(0))
386 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, &params.hStdInput );
387 if (!isatty(1))
388 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
389 if (!isatty(2))
390 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
393 /* initialize time values in user_shared_data */
394 NtQuerySystemTime( &now );
395 user_shared_data->SystemTime.LowPart = now.u.LowPart;
396 user_shared_data->SystemTime.High1Time = user_shared_data->SystemTime.High2Time = now.u.HighPart;
397 user_shared_data->u.TickCountQuad = (now.QuadPart - server_start_time) / 10000;
398 user_shared_data->u.TickCount.High2Time = user_shared_data->u.TickCount.High1Time;
399 user_shared_data->TickCountLowDeprecated = user_shared_data->u.TickCount.LowPart;
400 user_shared_data->TickCountMultiplier = 1 << 24;
402 fill_cpu_info();
404 NtCreateKeyedEvent( &keyed_event, GENERIC_READ | GENERIC_WRITE, NULL, 0 );
406 return exe_file;
410 /***********************************************************************
411 * free_thread_data
413 static void free_thread_data( TEB *teb )
415 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
416 SIZE_T size;
418 if (teb->DeallocationStack)
420 size = 0;
421 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
423 if (thread_data->start_stack)
425 size = 0;
426 NtFreeVirtualMemory( GetCurrentProcess(), &thread_data->start_stack, &size, MEM_RELEASE );
428 signal_free_thread( teb );
432 /***********************************************************************
433 * abort_thread
435 void abort_thread( int status )
437 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
438 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
439 signal_exit_thread( status );
443 /***********************************************************************
444 * exit_thread
446 void exit_thread( int status )
448 close( ntdll_get_thread_data()->wait_fd[0] );
449 close( ntdll_get_thread_data()->wait_fd[1] );
450 close( ntdll_get_thread_data()->reply_fd );
451 close( ntdll_get_thread_data()->request_fd );
452 pthread_exit( UIntToPtr(status) );
456 /***********************************************************************
457 * RtlExitUserThread (NTDLL.@)
459 void WINAPI RtlExitUserThread( ULONG status )
461 static void *prev_teb;
462 TEB *teb;
464 if (status) /* send the exit code to the server (0 is already the default) */
466 SERVER_START_REQ( terminate_thread )
468 req->handle = wine_server_obj_handle( GetCurrentThread() );
469 req->exit_code = status;
470 wine_server_call( req );
472 SERVER_END_REQ;
475 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1)
477 LdrShutdownProcess();
478 signal_exit_process( status );
481 LdrShutdownThread();
482 RtlFreeThreadActivationContextStack();
484 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
486 if ((teb = interlocked_xchg_ptr( &prev_teb, NtCurrentTeb() )))
488 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
490 if (thread_data->pthread_id)
492 pthread_join( thread_data->pthread_id, NULL );
493 free_thread_data( teb );
497 signal_exit_thread( status );
501 /***********************************************************************
502 * start_thread
504 * Startup routine for a newly created thread.
506 static void start_thread( struct startup_info *info )
508 BOOL suspend;
509 TEB *teb = info->teb;
510 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
511 struct debug_info debug_info;
513 debug_info.str_pos = debug_info.strings;
514 debug_info.out_pos = debug_info.output;
515 thread_data->debug_info = &debug_info;
516 thread_data->pthread_id = pthread_self();
518 signal_init_thread( teb );
519 server_init_thread( info->entry_point, &suspend );
520 signal_start_thread( (LPTHREAD_START_ROUTINE)info->entry_point, info->entry_arg, suspend );
524 /***********************************************************************
525 * RtlCreateUserThread (NTDLL.@)
527 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
528 BOOLEAN suspended, PVOID stack_addr,
529 SIZE_T stack_reserve, SIZE_T stack_commit,
530 PRTL_THREAD_START_ROUTINE start, void *param,
531 HANDLE *handle_ptr, CLIENT_ID *id )
533 sigset_t sigset;
534 pthread_t pthread_id;
535 pthread_attr_t attr;
536 struct ntdll_thread_data *thread_data;
537 struct startup_info *info;
538 HANDLE handle = 0, actctx = 0;
539 TEB *teb = NULL;
540 DWORD tid = 0;
541 int request_pipe[2];
542 NTSTATUS status;
544 if (process != NtCurrentProcess())
546 apc_call_t call;
547 apc_result_t result;
549 memset( &call, 0, sizeof(call) );
551 call.create_thread.type = APC_CREATE_THREAD;
552 call.create_thread.func = wine_server_client_ptr( start );
553 call.create_thread.arg = wine_server_client_ptr( param );
554 call.create_thread.reserve = stack_reserve;
555 call.create_thread.commit = stack_commit;
556 call.create_thread.suspend = suspended;
557 status = server_queue_process_apc( process, &call, &result );
558 if (status != STATUS_SUCCESS) return status;
560 if (result.create_thread.status == STATUS_SUCCESS)
562 if (id) id->UniqueThread = ULongToHandle(result.create_thread.tid);
563 if (handle_ptr) *handle_ptr = wine_server_ptr_handle( result.create_thread.handle );
564 else NtClose( wine_server_ptr_handle( result.create_thread.handle ));
566 return result.create_thread.status;
569 if (server_pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
570 wine_server_send_fd( request_pipe[0] );
572 SERVER_START_REQ( new_thread )
574 req->access = THREAD_ALL_ACCESS;
575 req->attributes = 0; /* FIXME */
576 req->suspend = suspended;
577 req->request_fd = request_pipe[0];
578 if (!(status = wine_server_call( req )))
580 handle = wine_server_ptr_handle( reply->handle );
581 tid = reply->tid;
583 close( request_pipe[0] );
585 SERVER_END_REQ;
587 if (status)
589 close( request_pipe[1] );
590 return status;
593 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
595 if ((status = signal_alloc_thread( &teb ))) goto error;
597 teb->Peb = NtCurrentTeb()->Peb;
598 teb->ClientId.UniqueProcess = ULongToHandle(GetCurrentProcessId());
599 teb->ClientId.UniqueThread = ULongToHandle(tid);
600 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
601 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
603 /* create default activation context frame for new thread */
604 RtlGetActiveActivationContext(&actctx);
605 if (actctx)
607 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
609 frame = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*frame));
610 frame->Previous = NULL;
611 frame->ActivationContext = actctx;
612 frame->Flags = 0;
613 teb->ActivationContextStack.ActiveFrame = frame;
616 info = (struct startup_info *)(teb + 1);
617 info->teb = teb;
618 info->entry_point = start;
619 info->entry_arg = param;
621 if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit, PTHREAD_STACK_MIN )))
622 goto error;
624 thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
625 thread_data->request_fd = request_pipe[1];
626 thread_data->reply_fd = -1;
627 thread_data->wait_fd[0] = -1;
628 thread_data->wait_fd[1] = -1;
629 thread_data->start_stack = (char *)teb->Tib.StackBase;
631 pthread_attr_init( &attr );
632 pthread_attr_setstack( &attr, teb->DeallocationStack,
633 (char *)teb->Tib.StackBase + PTHREAD_STACK_MIN - (char *)teb->DeallocationStack );
634 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */
635 interlocked_xchg_add( &nb_threads, 1 );
636 if (pthread_create( &pthread_id, &attr, (void * (*)(void *))start_thread, info ))
638 interlocked_xchg_add( &nb_threads, -1 );
639 pthread_attr_destroy( &attr );
640 status = STATUS_NO_MEMORY;
641 goto error;
643 pthread_attr_destroy( &attr );
644 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
646 if (id) id->UniqueThread = ULongToHandle(tid);
647 if (handle_ptr) *handle_ptr = handle;
648 else NtClose( handle );
650 return STATUS_SUCCESS;
652 error:
653 if (teb) free_thread_data( teb );
654 if (handle) NtClose( handle );
655 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
656 close( request_pipe[1] );
657 return status;
661 /******************************************************************************
662 * RtlGetNtGlobalFlags (NTDLL.@)
664 ULONG WINAPI RtlGetNtGlobalFlags(void)
666 if (!peb) return 0; /* init not done yet */
667 return peb->NtGlobalFlag;
671 /***********************************************************************
672 * NtOpenThread (NTDLL.@)
673 * ZwOpenThread (NTDLL.@)
675 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
676 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
678 NTSTATUS ret;
680 SERVER_START_REQ( open_thread )
682 req->tid = HandleToULong(id->UniqueThread);
683 req->access = access;
684 req->attributes = attr ? attr->Attributes : 0;
685 ret = wine_server_call( req );
686 *handle = wine_server_ptr_handle( reply->handle );
688 SERVER_END_REQ;
689 return ret;
693 /******************************************************************************
694 * NtSuspendThread (NTDLL.@)
695 * ZwSuspendThread (NTDLL.@)
697 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
699 NTSTATUS ret;
701 SERVER_START_REQ( suspend_thread )
703 req->handle = wine_server_obj_handle( handle );
704 if (!(ret = wine_server_call( req )))
706 if (count) *count = reply->count;
709 SERVER_END_REQ;
710 return ret;
714 /******************************************************************************
715 * NtResumeThread (NTDLL.@)
716 * ZwResumeThread (NTDLL.@)
718 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
720 NTSTATUS ret;
722 SERVER_START_REQ( resume_thread )
724 req->handle = wine_server_obj_handle( handle );
725 if (!(ret = wine_server_call( req )))
727 if (count) *count = reply->count;
730 SERVER_END_REQ;
731 return ret;
735 /******************************************************************************
736 * NtAlertResumeThread (NTDLL.@)
737 * ZwAlertResumeThread (NTDLL.@)
739 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
741 FIXME( "stub: should alert thread %p\n", handle );
742 return NtResumeThread( handle, count );
746 /******************************************************************************
747 * NtAlertThread (NTDLL.@)
748 * ZwAlertThread (NTDLL.@)
750 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
752 FIXME( "stub: %p\n", handle );
753 return STATUS_NOT_IMPLEMENTED;
757 /******************************************************************************
758 * NtTerminateThread (NTDLL.@)
759 * ZwTerminateThread (NTDLL.@)
761 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
763 NTSTATUS ret;
764 BOOL self;
766 SERVER_START_REQ( terminate_thread )
768 req->handle = wine_server_obj_handle( handle );
769 req->exit_code = exit_code;
770 ret = wine_server_call( req );
771 self = !ret && reply->self;
773 SERVER_END_REQ;
775 if (self) abort_thread( exit_code );
776 return ret;
780 /******************************************************************************
781 * NtQueueApcThread (NTDLL.@)
783 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
784 ULONG_PTR arg2, ULONG_PTR arg3 )
786 NTSTATUS ret;
787 SERVER_START_REQ( queue_apc )
789 req->handle = wine_server_obj_handle( handle );
790 if (func)
792 req->call.type = APC_USER;
793 req->call.user.func = wine_server_client_ptr( func );
794 req->call.user.args[0] = arg1;
795 req->call.user.args[1] = arg2;
796 req->call.user.args[2] = arg3;
798 else req->call.type = APC_NONE; /* wake up only */
799 ret = wine_server_call( req );
801 SERVER_END_REQ;
802 return ret;
806 /***********************************************************************
807 * set_thread_context
809 NTSTATUS set_thread_context( HANDLE handle, const CONTEXT *context, BOOL *self )
811 NTSTATUS ret;
812 DWORD dummy, i;
813 context_t server_context;
815 context_to_server( &server_context, context );
817 SERVER_START_REQ( set_thread_context )
819 req->handle = wine_server_obj_handle( handle );
820 req->suspend = 1;
821 wine_server_add_data( req, &server_context, sizeof(server_context) );
822 ret = wine_server_call( req );
823 *self = reply->self;
825 SERVER_END_REQ;
827 if (ret == STATUS_PENDING)
829 for (i = 0; i < 100; i++)
831 SERVER_START_REQ( set_thread_context )
833 req->handle = wine_server_obj_handle( handle );
834 req->suspend = 0;
835 wine_server_add_data( req, &server_context, sizeof(server_context) );
836 ret = wine_server_call( req );
838 SERVER_END_REQ;
839 if (ret == STATUS_PENDING)
841 LARGE_INTEGER timeout;
842 timeout.QuadPart = -10000;
843 NtDelayExecution( FALSE, &timeout );
845 else break;
847 NtResumeThread( handle, &dummy );
848 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
851 return ret;
855 /* convert CPU-specific flags to generic server flags */
856 static inline unsigned int get_server_context_flags( DWORD flags )
858 unsigned int ret = 0;
860 flags &= 0x3f; /* mask CPU id flags */
861 if (flags & CONTEXT_CONTROL) ret |= SERVER_CTX_CONTROL;
862 if (flags & CONTEXT_INTEGER) ret |= SERVER_CTX_INTEGER;
863 #ifdef CONTEXT_SEGMENTS
864 if (flags & CONTEXT_SEGMENTS) ret |= SERVER_CTX_SEGMENTS;
865 #endif
866 #ifdef CONTEXT_FLOATING_POINT
867 if (flags & CONTEXT_FLOATING_POINT) ret |= SERVER_CTX_FLOATING_POINT;
868 #endif
869 #ifdef CONTEXT_DEBUG_REGISTERS
870 if (flags & CONTEXT_DEBUG_REGISTERS) ret |= SERVER_CTX_DEBUG_REGISTERS;
871 #endif
872 #ifdef CONTEXT_EXTENDED_REGISTERS
873 if (flags & CONTEXT_EXTENDED_REGISTERS) ret |= SERVER_CTX_EXTENDED_REGISTERS;
874 #endif
875 return ret;
878 /***********************************************************************
879 * get_thread_context
881 NTSTATUS get_thread_context( HANDLE handle, CONTEXT *context, BOOL *self )
883 NTSTATUS ret;
884 DWORD dummy, i;
885 unsigned int server_flags = get_server_context_flags( context->ContextFlags );
886 context_t server_context;
888 SERVER_START_REQ( get_thread_context )
890 req->handle = wine_server_obj_handle( handle );
891 req->flags = server_flags;
892 req->suspend = 1;
893 wine_server_set_reply( req, &server_context, sizeof(server_context) );
894 ret = wine_server_call( req );
895 *self = reply->self;
897 SERVER_END_REQ;
899 if (ret == STATUS_PENDING)
901 for (i = 0; i < 100; i++)
903 SERVER_START_REQ( get_thread_context )
905 req->handle = wine_server_obj_handle( handle );
906 req->flags = server_flags;
907 req->suspend = 0;
908 wine_server_set_reply( req, &server_context, sizeof(server_context) );
909 ret = wine_server_call( req );
911 SERVER_END_REQ;
912 if (ret == STATUS_PENDING)
914 LARGE_INTEGER timeout;
915 timeout.QuadPart = -10000;
916 NtDelayExecution( FALSE, &timeout );
918 else break;
920 NtResumeThread( handle, &dummy );
921 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
923 if (!ret) ret = context_from_server( context, &server_context );
924 return ret;
928 /******************************************************************************
929 * NtQueryInformationThread (NTDLL.@)
930 * ZwQueryInformationThread (NTDLL.@)
932 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
933 void *data, ULONG length, ULONG *ret_len )
935 NTSTATUS status;
937 switch(class)
939 case ThreadBasicInformation:
941 THREAD_BASIC_INFORMATION info;
942 const ULONG_PTR affinity_mask = get_system_affinity_mask();
944 SERVER_START_REQ( get_thread_info )
946 req->handle = wine_server_obj_handle( handle );
947 req->tid_in = 0;
948 if (!(status = wine_server_call( req )))
950 info.ExitStatus = reply->exit_code;
951 info.TebBaseAddress = wine_server_get_ptr( reply->teb );
952 info.ClientId.UniqueProcess = ULongToHandle(reply->pid);
953 info.ClientId.UniqueThread = ULongToHandle(reply->tid);
954 info.AffinityMask = reply->affinity & affinity_mask;
955 info.Priority = reply->priority;
956 info.BasePriority = reply->priority; /* FIXME */
959 SERVER_END_REQ;
960 if (status == STATUS_SUCCESS)
962 if (data) memcpy( data, &info, min( length, sizeof(info) ));
963 if (ret_len) *ret_len = min( length, sizeof(info) );
966 return status;
967 case ThreadAffinityMask:
969 const ULONG_PTR affinity_mask = get_system_affinity_mask();
970 ULONG_PTR affinity = 0;
972 SERVER_START_REQ( get_thread_info )
974 req->handle = wine_server_obj_handle( handle );
975 req->tid_in = 0;
976 if (!(status = wine_server_call( req )))
977 affinity = reply->affinity & affinity_mask;
979 SERVER_END_REQ;
980 if (status == STATUS_SUCCESS)
982 if (data) memcpy( data, &affinity, min( length, sizeof(affinity) ));
983 if (ret_len) *ret_len = min( length, sizeof(affinity) );
986 return status;
987 case ThreadTimes:
989 KERNEL_USER_TIMES kusrt;
991 SERVER_START_REQ( get_thread_times )
993 req->handle = wine_server_obj_handle( handle );
994 status = wine_server_call( req );
995 if (status == STATUS_SUCCESS)
997 kusrt.CreateTime.QuadPart = reply->creation_time;
998 kusrt.ExitTime.QuadPart = reply->exit_time;
1001 SERVER_END_REQ;
1002 if (status == STATUS_SUCCESS)
1004 /* We call times(2) for kernel time or user time */
1005 /* We can only (portably) do this for the current thread */
1006 if (handle == GetCurrentThread())
1008 struct tms time_buf;
1009 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1011 times(&time_buf);
1012 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1013 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1015 else
1017 static BOOL reported = FALSE;
1019 kusrt.KernelTime.QuadPart = 0;
1020 kusrt.UserTime.QuadPart = 0;
1021 if (reported)
1022 TRACE("Cannot get kerneltime or usertime of other threads\n");
1023 else
1025 FIXME("Cannot get kerneltime or usertime of other threads\n");
1026 reported = TRUE;
1029 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1030 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1033 return status;
1034 case ThreadDescriptorTableEntry:
1036 #ifdef __i386__
1037 THREAD_DESCRIPTOR_INFORMATION* tdi = data;
1038 if (length < sizeof(*tdi))
1039 status = STATUS_INFO_LENGTH_MISMATCH;
1040 else if (!(tdi->Selector & 4)) /* GDT selector */
1042 unsigned sel = LOWORD(tdi->Selector) & ~3; /* ignore RPL */
1043 status = STATUS_SUCCESS;
1044 if (!sel) /* null selector */
1045 memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1046 else
1048 tdi->Entry.BaseLow = 0;
1049 tdi->Entry.HighWord.Bits.BaseMid = 0;
1050 tdi->Entry.HighWord.Bits.BaseHi = 0;
1051 tdi->Entry.LimitLow = 0xffff;
1052 tdi->Entry.HighWord.Bits.LimitHi = 0xf;
1053 tdi->Entry.HighWord.Bits.Dpl = 3;
1054 tdi->Entry.HighWord.Bits.Sys = 0;
1055 tdi->Entry.HighWord.Bits.Pres = 1;
1056 tdi->Entry.HighWord.Bits.Granularity = 1;
1057 tdi->Entry.HighWord.Bits.Default_Big = 1;
1058 tdi->Entry.HighWord.Bits.Type = 0x12;
1059 tdi->Entry.HighWord.Bits.Reserved_0 = 0;
1060 /* it has to be one of the system GDT selectors */
1061 if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1063 if (sel == (wine_get_cs() & ~3))
1064 tdi->Entry.HighWord.Bits.Type |= 8; /* code segment */
1065 else if (sel == (wine_get_fs() & ~3))
1067 ULONG_PTR fs_base = (ULONG_PTR)NtCurrentTeb();
1068 tdi->Entry.BaseLow = fs_base & 0xffff;
1069 tdi->Entry.HighWord.Bits.BaseMid = (fs_base >> 16) & 0xff;
1070 tdi->Entry.HighWord.Bits.BaseHi = (fs_base >> 24) & 0xff;
1071 tdi->Entry.LimitLow = 0x0fff;
1072 tdi->Entry.HighWord.Bits.LimitHi = 0;
1073 tdi->Entry.HighWord.Bits.Granularity = 0;
1075 else status = STATUS_ACCESS_DENIED;
1079 else
1081 SERVER_START_REQ( get_selector_entry )
1083 req->handle = wine_server_obj_handle( handle );
1084 req->entry = LOWORD(tdi->Selector) >> 3;
1085 status = wine_server_call( req );
1086 if (!status)
1088 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1089 status = STATUS_ACCESS_VIOLATION;
1090 else
1092 wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1093 wine_ldt_set_limit( &tdi->Entry, reply->limit );
1094 wine_ldt_set_flags( &tdi->Entry, reply->flags );
1098 SERVER_END_REQ;
1100 if (status == STATUS_SUCCESS && ret_len)
1101 /* yes, that's a bit strange, but it's the way it is */
1102 *ret_len = sizeof(LDT_ENTRY);
1103 #else
1104 status = STATUS_NOT_IMPLEMENTED;
1105 #endif
1106 return status;
1108 case ThreadAmILastThread:
1110 SERVER_START_REQ(get_thread_info)
1112 req->handle = wine_server_obj_handle( handle );
1113 req->tid_in = 0;
1114 status = wine_server_call( req );
1115 if (status == STATUS_SUCCESS)
1117 BOOLEAN last = reply->last;
1118 if (data) memcpy( data, &last, min( length, sizeof(last) ));
1119 if (ret_len) *ret_len = min( length, sizeof(last) );
1122 SERVER_END_REQ;
1123 return status;
1125 case ThreadQuerySetWin32StartAddress:
1127 SERVER_START_REQ( get_thread_info )
1129 req->handle = wine_server_obj_handle( handle );
1130 req->tid_in = 0;
1131 status = wine_server_call( req );
1132 if (status == STATUS_SUCCESS)
1134 PRTL_THREAD_START_ROUTINE entry = wine_server_get_ptr( reply->entry_point );
1135 if (data) memcpy( data, &entry, min( length, sizeof(entry) ) );
1136 if (ret_len) *ret_len = min( length, sizeof(entry) );
1139 SERVER_END_REQ;
1140 return status;
1142 case ThreadGroupInformation:
1144 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1145 GROUP_AFFINITY affinity;
1147 memset(&affinity, 0, sizeof(affinity));
1148 affinity.Group = 0; /* Wine only supports max 64 processors */
1150 SERVER_START_REQ( get_thread_info )
1152 req->handle = wine_server_obj_handle( handle );
1153 req->tid_in = 0;
1154 if (!(status = wine_server_call( req )))
1155 affinity.Mask = reply->affinity & affinity_mask;
1157 SERVER_END_REQ;
1158 if (status == STATUS_SUCCESS)
1160 if (data) memcpy( data, &affinity, min( length, sizeof(affinity) ));
1161 if (ret_len) *ret_len = min( length, sizeof(affinity) );
1164 return status;
1165 case ThreadIsIoPending:
1166 FIXME( "ThreadIsIoPending info class not supported yet\n" );
1167 if (length != sizeof(BOOL)) return STATUS_INFO_LENGTH_MISMATCH;
1168 if (!data) return STATUS_ACCESS_DENIED;
1170 *(BOOL*)data = FALSE;
1171 if (ret_len) *ret_len = sizeof(BOOL);
1172 return STATUS_SUCCESS;
1173 case ThreadPriority:
1174 case ThreadBasePriority:
1175 case ThreadImpersonationToken:
1176 case ThreadEnableAlignmentFaultFixup:
1177 case ThreadEventPair_Reusable:
1178 case ThreadZeroTlsCell:
1179 case ThreadPerformanceCount:
1180 case ThreadIdealProcessor:
1181 case ThreadPriorityBoost:
1182 case ThreadSetTlsArrayAddress:
1183 default:
1184 FIXME( "info class %d not supported yet\n", class );
1185 return STATUS_NOT_IMPLEMENTED;
1190 /******************************************************************************
1191 * NtSetInformationThread (NTDLL.@)
1192 * ZwSetInformationThread (NTDLL.@)
1194 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1195 LPCVOID data, ULONG length )
1197 NTSTATUS status;
1198 switch(class)
1200 case ThreadZeroTlsCell:
1201 if (handle == GetCurrentThread())
1203 LIST_ENTRY *entry;
1204 DWORD index;
1206 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1207 index = *(const DWORD *)data;
1208 if (index < TLS_MINIMUM_AVAILABLE)
1210 RtlAcquirePebLock();
1211 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1213 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1214 teb->TlsSlots[index] = 0;
1216 RtlReleasePebLock();
1218 else
1220 index -= TLS_MINIMUM_AVAILABLE;
1221 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1222 return STATUS_INVALID_PARAMETER;
1223 RtlAcquirePebLock();
1224 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1226 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1227 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1229 RtlReleasePebLock();
1231 return STATUS_SUCCESS;
1233 FIXME( "ZeroTlsCell not supported on other threads\n" );
1234 return STATUS_NOT_IMPLEMENTED;
1236 case ThreadImpersonationToken:
1238 const HANDLE *phToken = data;
1239 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1240 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1241 SERVER_START_REQ( set_thread_info )
1243 req->handle = wine_server_obj_handle( handle );
1244 req->token = wine_server_obj_handle( *phToken );
1245 req->mask = SET_THREAD_INFO_TOKEN;
1246 status = wine_server_call( req );
1248 SERVER_END_REQ;
1250 return status;
1251 case ThreadBasePriority:
1253 const DWORD *pprio = data;
1254 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1255 SERVER_START_REQ( set_thread_info )
1257 req->handle = wine_server_obj_handle( handle );
1258 req->priority = *pprio;
1259 req->mask = SET_THREAD_INFO_PRIORITY;
1260 status = wine_server_call( req );
1262 SERVER_END_REQ;
1264 return status;
1265 case ThreadAffinityMask:
1267 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1268 ULONG_PTR req_aff;
1270 if (length != sizeof(ULONG_PTR)) return STATUS_INVALID_PARAMETER;
1271 req_aff = *(const ULONG_PTR *)data;
1272 if ((ULONG)req_aff == ~0u) req_aff = affinity_mask;
1273 else if (req_aff & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1274 else if (!req_aff) return STATUS_INVALID_PARAMETER;
1275 SERVER_START_REQ( set_thread_info )
1277 req->handle = wine_server_obj_handle( handle );
1278 req->affinity = req_aff;
1279 req->mask = SET_THREAD_INFO_AFFINITY;
1280 status = wine_server_call( req );
1282 SERVER_END_REQ;
1284 return status;
1285 case ThreadHideFromDebugger:
1286 /* pretend the call succeeded to satisfy some code protectors */
1287 return STATUS_SUCCESS;
1288 case ThreadQuerySetWin32StartAddress:
1290 const PRTL_THREAD_START_ROUTINE *entry = data;
1291 if (length != sizeof(PRTL_THREAD_START_ROUTINE)) return STATUS_INVALID_PARAMETER;
1292 SERVER_START_REQ( set_thread_info )
1294 req->handle = wine_server_obj_handle( handle );
1295 req->mask = SET_THREAD_INFO_ENTRYPOINT;
1296 req->entry_point = wine_server_client_ptr( *entry );
1297 status = wine_server_call( req );
1299 SERVER_END_REQ;
1301 return status;
1302 case ThreadGroupInformation:
1304 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1305 const GROUP_AFFINITY *req_aff;
1307 if (length != sizeof(*req_aff)) return STATUS_INVALID_PARAMETER;
1308 if (!data) return STATUS_ACCESS_VIOLATION;
1309 req_aff = data;
1311 /* On Windows the request fails if the reserved fields are set */
1312 if (req_aff->Reserved[0] || req_aff->Reserved[1] || req_aff->Reserved[2])
1313 return STATUS_INVALID_PARAMETER;
1315 /* Wine only supports max 64 processors */
1316 if (req_aff->Group) return STATUS_INVALID_PARAMETER;
1317 if (req_aff->Mask & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1318 if (!req_aff->Mask) return STATUS_INVALID_PARAMETER;
1319 SERVER_START_REQ( set_thread_info )
1321 req->handle = wine_server_obj_handle( handle );
1322 req->affinity = req_aff->Mask;
1323 req->mask = SET_THREAD_INFO_AFFINITY;
1324 status = wine_server_call( req );
1326 SERVER_END_REQ;
1328 return status;
1329 case ThreadBasicInformation:
1330 case ThreadTimes:
1331 case ThreadPriority:
1332 case ThreadDescriptorTableEntry:
1333 case ThreadEnableAlignmentFaultFixup:
1334 case ThreadEventPair_Reusable:
1335 case ThreadPerformanceCount:
1336 case ThreadAmILastThread:
1337 case ThreadIdealProcessor:
1338 case ThreadPriorityBoost:
1339 case ThreadSetTlsArrayAddress:
1340 case ThreadIsIoPending:
1341 default:
1342 FIXME( "info class %d not supported yet\n", class );
1343 return STATUS_NOT_IMPLEMENTED;
1347 /******************************************************************************
1348 * NtGetCurrentProcessorNumber (NTDLL.@)
1350 * Return the processor, on which the thread is running
1353 ULONG WINAPI NtGetCurrentProcessorNumber(void)
1355 ULONG processor;
1357 #if defined(__linux__) && defined(__NR_getcpu)
1358 int res = syscall(__NR_getcpu, &processor, NULL, NULL);
1359 if (res != -1) return processor;
1360 #endif
1362 if (NtCurrentTeb()->Peb->NumberOfProcessors > 1)
1364 ULONG_PTR thread_mask, processor_mask;
1365 NTSTATUS status;
1367 status = NtQueryInformationThread(GetCurrentThread(), ThreadAffinityMask,
1368 &thread_mask, sizeof(thread_mask), NULL);
1369 if (status == STATUS_SUCCESS)
1371 for (processor = 0; processor < NtCurrentTeb()->Peb->NumberOfProcessors; processor++)
1373 processor_mask = (1 << processor);
1374 if (thread_mask & processor_mask)
1376 if (thread_mask != processor_mask)
1377 FIXME("need multicore support (%d processors)\n",
1378 NtCurrentTeb()->Peb->NumberOfProcessors);
1379 return processor;
1385 /* fallback to the first processor */
1386 return 0;