mstask: Improve stubs for ITask::GetTaskFlags and ITask::GetFlags.
[wine.git] / dlls / ntdll / thread.c
blob35e23d21ed5beaaaceae63eb67f30989536f8729
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;
56 static const WCHAR default_windirW[] = {'C',':','\\','w','i','n','d','o','w','s',0};
58 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter = NULL;
59 void (WINAPI *kernel32_start_process)(LPTHREAD_START_ROUTINE,void*) = NULL;
61 /* info passed to a starting thread */
62 struct startup_info
64 TEB *teb;
65 PRTL_THREAD_START_ROUTINE entry_point;
66 void *entry_arg;
69 static PEB *peb;
70 static PEB_LDR_DATA ldr;
71 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
72 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
73 static RTL_BITMAP tls_bitmap;
74 static RTL_BITMAP tls_expansion_bitmap;
75 static RTL_BITMAP fls_bitmap;
76 static int nb_threads = 1;
78 static RTL_CRITICAL_SECTION peb_lock;
79 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
81 0, 0, &peb_lock,
82 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
83 0, 0, { (DWORD_PTR)(__FILE__ ": peb_lock") }
85 static RTL_CRITICAL_SECTION peb_lock = { &critsect_debug, -1, 0, 0, 0, 0 };
87 /***********************************************************************
88 * get_unicode_string
90 * Copy a unicode string from the startup info.
92 static inline void get_unicode_string( UNICODE_STRING *str, WCHAR **src, WCHAR **dst, UINT len )
94 str->Buffer = *dst;
95 str->Length = len;
96 str->MaximumLength = len + sizeof(WCHAR);
97 memcpy( str->Buffer, *src, len );
98 str->Buffer[len / sizeof(WCHAR)] = 0;
99 *src += len / sizeof(WCHAR);
100 *dst += len / sizeof(WCHAR) + 1;
103 /***********************************************************************
104 * init_user_process_params
106 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
108 static NTSTATUS init_user_process_params( SIZE_T data_size, HANDLE *exe_file )
110 void *ptr;
111 WCHAR *src, *dst;
112 SIZE_T info_size, env_size, size, alloc_size;
113 NTSTATUS status;
114 startup_info_t *info;
115 RTL_USER_PROCESS_PARAMETERS *params = NULL;
117 if (!(info = RtlAllocateHeap( GetProcessHeap(), 0, data_size )))
118 return STATUS_NO_MEMORY;
120 SERVER_START_REQ( get_startup_info )
122 wine_server_set_reply( req, info, data_size );
123 if (!(status = wine_server_call( req )))
125 data_size = wine_server_reply_size( reply );
126 info_size = reply->info_size;
127 env_size = data_size - info_size;
128 *exe_file = wine_server_ptr_handle( reply->exe_file );
131 SERVER_END_REQ;
132 if (status != STATUS_SUCCESS) goto done;
134 size = sizeof(*params);
135 size += MAX_NT_PATH_LENGTH * sizeof(WCHAR);
136 size += info->dllpath_len + sizeof(WCHAR);
137 size += info->imagepath_len + sizeof(WCHAR);
138 size += info->cmdline_len + sizeof(WCHAR);
139 size += info->title_len + sizeof(WCHAR);
140 size += info->desktop_len + sizeof(WCHAR);
141 size += info->shellinfo_len + sizeof(WCHAR);
142 size += info->runtime_len + sizeof(WCHAR);
144 alloc_size = size;
145 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &alloc_size,
146 MEM_COMMIT, PAGE_READWRITE );
147 if (status != STATUS_SUCCESS) goto done;
149 NtCurrentTeb()->Peb->ProcessParameters = params;
150 params->AllocationSize = alloc_size;
151 params->Size = size;
152 params->Flags = PROCESS_PARAMS_FLAG_NORMALIZED;
153 params->DebugFlags = info->debug_flags;
154 params->ConsoleHandle = wine_server_ptr_handle( info->console );
155 params->ConsoleFlags = info->console_flags;
156 params->hStdInput = wine_server_ptr_handle( info->hstdin );
157 params->hStdOutput = wine_server_ptr_handle( info->hstdout );
158 params->hStdError = wine_server_ptr_handle( info->hstderr );
159 params->dwX = info->x;
160 params->dwY = info->y;
161 params->dwXSize = info->xsize;
162 params->dwYSize = info->ysize;
163 params->dwXCountChars = info->xchars;
164 params->dwYCountChars = info->ychars;
165 params->dwFillAttribute = info->attribute;
166 params->dwFlags = info->flags;
167 params->wShowWindow = info->show;
169 src = (WCHAR *)(info + 1);
170 dst = (WCHAR *)(params + 1);
172 /* current directory needs more space */
173 get_unicode_string( &params->CurrentDirectory.DosPath, &src, &dst, info->curdir_len );
174 params->CurrentDirectory.DosPath.MaximumLength = MAX_NT_PATH_LENGTH * sizeof(WCHAR);
175 dst = (WCHAR *)(params + 1) + MAX_NT_PATH_LENGTH;
177 get_unicode_string( &params->DllPath, &src, &dst, info->dllpath_len );
178 get_unicode_string( &params->ImagePathName, &src, &dst, info->imagepath_len );
179 get_unicode_string( &params->CommandLine, &src, &dst, info->cmdline_len );
180 get_unicode_string( &params->WindowTitle, &src, &dst, info->title_len );
181 get_unicode_string( &params->Desktop, &src, &dst, info->desktop_len );
182 get_unicode_string( &params->ShellInfo, &src, &dst, info->shellinfo_len );
184 /* runtime info isn't a real string */
185 params->RuntimeInfo.Buffer = dst;
186 params->RuntimeInfo.Length = params->RuntimeInfo.MaximumLength = info->runtime_len;
187 memcpy( dst, src, info->runtime_len );
189 /* environment needs to be a separate memory block */
190 ptr = NULL;
191 alloc_size = max( 1, env_size );
192 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &alloc_size,
193 MEM_COMMIT, PAGE_READWRITE );
194 if (status != STATUS_SUCCESS) goto done;
195 memcpy( ptr, (char *)info + info_size, env_size );
196 params->Environment = ptr;
198 done:
199 RtlFreeHeap( GetProcessHeap(), 0, info );
200 return status;
203 #ifdef __linux__
205 #ifdef HAVE_ELF_H
206 # include <elf.h>
207 #endif
208 #ifdef HAVE_LINK_H
209 # include <link.h>
210 #endif
211 #ifdef HAVE_SYS_AUXV_H
212 # include <sys/auxv.h>
213 #endif
214 #ifndef HAVE_GETAUXVAL
215 static unsigned long getauxval( unsigned long id )
217 extern char **__wine_main_environ;
218 char **ptr = __wine_main_environ;
219 ElfW(auxv_t) *auxv;
221 while (*ptr) ptr++;
222 while (!*ptr) ptr++;
223 for (auxv = (ElfW(auxv_t) *)ptr; auxv->a_type; auxv++)
224 if (auxv->a_type == id) return auxv->a_un.a_val;
225 return 0;
227 #endif
229 static ULONG_PTR get_image_addr(void)
231 ULONG_PTR size, num, phdr_addr = getauxval( AT_PHDR );
232 ElfW(Phdr) *phdr;
234 if (!phdr_addr) return 0;
235 phdr = (ElfW(Phdr) *)phdr_addr;
236 size = getauxval( AT_PHENT );
237 num = getauxval( AT_PHNUM );
238 while (num--)
240 if (phdr->p_type == PT_PHDR) return phdr_addr - phdr->p_offset;
241 phdr = (ElfW(Phdr) *)((char *)phdr + size);
243 return 0;
246 #elif defined(__APPLE__)
247 #include <mach/mach.h>
248 #include <mach/mach_error.h>
250 static ULONG_PTR get_image_addr(void)
252 ULONG_PTR ret = 0;
253 #ifdef TASK_DYLD_INFO
254 struct task_dyld_info dyld_info;
255 mach_msg_type_number_t size = TASK_DYLD_INFO_COUNT;
256 if (task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&dyld_info, &size) == KERN_SUCCESS)
257 ret = dyld_info.all_image_info_addr;
258 #endif
259 return ret;
262 #else
263 static ULONG_PTR get_image_addr(void)
265 return 0;
267 #endif
269 /***********************************************************************
270 * thread_init
272 * Setup the initial thread.
274 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
276 HANDLE thread_init(void)
278 TEB *teb;
279 void *addr;
280 BOOL suspend;
281 SIZE_T size, info_size;
282 HANDLE exe_file = 0;
283 LARGE_INTEGER now;
284 NTSTATUS status;
285 struct ntdll_thread_data *thread_data;
286 static struct debug_info debug_info; /* debug info for initial thread */
288 virtual_init();
290 /* reserve space for shared user data */
292 addr = (void *)0x7ffe0000;
293 size = 0x10000;
294 status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size,
295 MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE );
296 if (status)
298 MESSAGE( "wine: failed to map the shared user data: %08x\n", status );
299 exit(1);
301 user_shared_data = addr;
302 memcpy( user_shared_data->NtSystemRoot, default_windirW, sizeof(default_windirW) );
304 /* allocate and initialize the PEB */
306 addr = NULL;
307 size = sizeof(*peb);
308 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
309 MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
310 peb = addr;
312 peb->FastPebLock = &peb_lock;
313 peb->ProcessParameters = &params;
314 peb->TlsBitmap = &tls_bitmap;
315 peb->TlsExpansionBitmap = &tls_expansion_bitmap;
316 peb->FlsBitmap = &fls_bitmap;
317 peb->LdrData = &ldr;
318 peb->OSMajorVersion = 5;
319 peb->OSMinorVersion = 1;
320 peb->OSBuildNumber = 0xA28;
321 peb->OSPlatformId = VER_PLATFORM_WIN32_NT;
322 params.CurrentDirectory.DosPath.Buffer = current_dir;
323 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
324 params.wShowWindow = 1; /* SW_SHOWNORMAL */
325 ldr.Length = sizeof(ldr);
326 ldr.Initialized = TRUE;
327 RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
328 RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
329 sizeof(peb->TlsExpansionBitmapBits) * 8 );
330 RtlInitializeBitMap( &fls_bitmap, peb->FlsBitmapBits, sizeof(peb->FlsBitmapBits) * 8 );
331 RtlSetBits( peb->TlsBitmap, 0, 1 ); /* TLS index 0 is reserved and should be initialized to NULL. */
332 RtlSetBits( peb->FlsBitmap, 0, 1 );
333 InitializeListHead( &peb->FlsListHead );
334 InitializeListHead( &ldr.InLoadOrderModuleList );
335 InitializeListHead( &ldr.InMemoryOrderModuleList );
336 InitializeListHead( &ldr.InInitializationOrderModuleList );
337 *(ULONG_PTR *)peb->Reserved = get_image_addr();
340 * Starting with Vista, the first user to log on has session id 1.
341 * Session id 0 is for processes that don't interact with the user (like services).
343 peb->SessionId = 1;
345 /* allocate and initialize the initial TEB */
347 signal_alloc_thread( &teb );
348 teb->Peb = peb;
349 teb->Tib.StackBase = (void *)~0UL;
350 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
351 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
353 thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
354 thread_data->request_fd = -1;
355 thread_data->reply_fd = -1;
356 thread_data->wait_fd[0] = -1;
357 thread_data->wait_fd[1] = -1;
358 thread_data->debug_info = &debug_info;
360 signal_init_thread( teb );
361 virtual_init_threading();
363 debug_info.str_pos = debug_info.strings;
364 debug_info.out_pos = debug_info.output;
365 debug_init();
367 /* setup the server connection */
368 server_init_process();
369 info_size = server_init_thread( peb, &suspend );
371 /* create the process heap */
372 if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
374 MESSAGE( "wine: failed to create the process heap\n" );
375 exit(1);
378 /* allocate user parameters */
379 if (info_size)
381 init_user_process_params( info_size, &exe_file );
383 else
385 if (isatty(0) || isatty(1) || isatty(2))
386 params.ConsoleHandle = (HANDLE)2; /* see kernel32/kernel_private.h */
387 if (!isatty(0))
388 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, &params.hStdInput );
389 if (!isatty(1))
390 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
391 if (!isatty(2))
392 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
395 /* initialize time values in user_shared_data */
396 NtQuerySystemTime( &now );
397 user_shared_data->SystemTime.LowPart = now.u.LowPart;
398 user_shared_data->SystemTime.High1Time = user_shared_data->SystemTime.High2Time = now.u.HighPart;
399 user_shared_data->u.TickCountQuad = (now.QuadPart - server_start_time) / 10000;
400 user_shared_data->u.TickCount.High2Time = user_shared_data->u.TickCount.High1Time;
401 user_shared_data->TickCountLowDeprecated = user_shared_data->u.TickCount.LowPart;
402 user_shared_data->TickCountMultiplier = 1 << 24;
404 fill_cpu_info();
406 NtCreateKeyedEvent( &keyed_event, GENERIC_READ | GENERIC_WRITE, NULL, 0 );
408 return exe_file;
412 /***********************************************************************
413 * free_thread_data
415 static void free_thread_data( TEB *teb )
417 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
418 SIZE_T size;
420 if (teb->DeallocationStack)
422 size = 0;
423 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
425 if (thread_data->start_stack)
427 size = 0;
428 NtFreeVirtualMemory( GetCurrentProcess(), &thread_data->start_stack, &size, MEM_RELEASE );
430 signal_free_thread( teb );
434 /***********************************************************************
435 * abort_thread
437 void abort_thread( int status )
439 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
440 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
441 signal_exit_thread( status );
445 /***********************************************************************
446 * exit_thread
448 void exit_thread( int status )
450 close( ntdll_get_thread_data()->wait_fd[0] );
451 close( ntdll_get_thread_data()->wait_fd[1] );
452 close( ntdll_get_thread_data()->reply_fd );
453 close( ntdll_get_thread_data()->request_fd );
454 pthread_exit( UIntToPtr(status) );
458 /***********************************************************************
459 * RtlExitUserThread (NTDLL.@)
461 void WINAPI RtlExitUserThread( ULONG status )
463 static void *prev_teb;
464 TEB *teb;
466 if (status) /* send the exit code to the server (0 is already the default) */
468 SERVER_START_REQ( terminate_thread )
470 req->handle = wine_server_obj_handle( GetCurrentThread() );
471 req->exit_code = status;
472 wine_server_call( req );
474 SERVER_END_REQ;
477 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1)
479 LdrShutdownProcess();
480 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
481 signal_exit_process( status );
484 LdrShutdownThread();
485 RtlFreeThreadActivationContextStack();
487 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
489 if ((teb = interlocked_xchg_ptr( &prev_teb, NtCurrentTeb() )))
491 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
493 if (thread_data->pthread_id)
495 pthread_join( thread_data->pthread_id, NULL );
496 free_thread_data( teb );
500 signal_exit_thread( status );
504 /***********************************************************************
505 * start_thread
507 * Startup routine for a newly created thread.
509 static void start_thread( struct startup_info *info )
511 BOOL suspend;
512 TEB *teb = info->teb;
513 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
514 struct debug_info debug_info;
516 debug_info.str_pos = debug_info.strings;
517 debug_info.out_pos = debug_info.output;
518 thread_data->debug_info = &debug_info;
519 thread_data->pthread_id = pthread_self();
521 signal_init_thread( teb );
522 server_init_thread( info->entry_point, &suspend );
523 signal_start_thread( (LPTHREAD_START_ROUTINE)info->entry_point, info->entry_arg, suspend );
527 /***********************************************************************
528 * NtCreateThreadEx (NTDLL.@)
530 NTSTATUS WINAPI NtCreateThreadEx( HANDLE *handle_ptr, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
531 HANDLE process, LPTHREAD_START_ROUTINE start, void *param,
532 ULONG flags, ULONG zero_bits, ULONG stack_commit,
533 ULONG stack_reserve, void *attribute_list )
535 FIXME( "%p, %x, %p, %p, %p, %p, %x, %x, %x, %x, %p semi-stub!\n", handle_ptr, access, attr,
536 process, start, param, flags, zero_bits, stack_commit, stack_reserve, attribute_list );
538 return RtlCreateUserThread( process, NULL, flags & THREAD_CREATE_FLAGS_CREATE_SUSPENDED,
539 NULL, stack_reserve, stack_commit, (PRTL_THREAD_START_ROUTINE)start,
540 param, handle_ptr, NULL );
544 /***********************************************************************
545 * RtlCreateUserThread (NTDLL.@)
547 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
548 BOOLEAN suspended, PVOID stack_addr,
549 SIZE_T stack_reserve, SIZE_T stack_commit,
550 PRTL_THREAD_START_ROUTINE start, void *param,
551 HANDLE *handle_ptr, CLIENT_ID *id )
553 sigset_t sigset;
554 pthread_t pthread_id;
555 pthread_attr_t attr;
556 struct ntdll_thread_data *thread_data;
557 struct startup_info *info;
558 HANDLE handle = 0, actctx = 0;
559 TEB *teb = NULL;
560 DWORD tid = 0;
561 int request_pipe[2];
562 NTSTATUS status;
563 SIZE_T extra_stack = PTHREAD_STACK_MIN;
565 if (process != NtCurrentProcess())
567 apc_call_t call;
568 apc_result_t result;
570 memset( &call, 0, sizeof(call) );
572 call.create_thread.type = APC_CREATE_THREAD;
573 call.create_thread.func = wine_server_client_ptr( start );
574 call.create_thread.arg = wine_server_client_ptr( param );
575 call.create_thread.reserve = stack_reserve;
576 call.create_thread.commit = stack_commit;
577 call.create_thread.suspend = suspended;
578 status = server_queue_process_apc( process, &call, &result );
579 if (status != STATUS_SUCCESS) return status;
581 if (result.create_thread.status == STATUS_SUCCESS)
583 if (id) id->UniqueThread = ULongToHandle(result.create_thread.tid);
584 if (handle_ptr) *handle_ptr = wine_server_ptr_handle( result.create_thread.handle );
585 else NtClose( wine_server_ptr_handle( result.create_thread.handle ));
587 return result.create_thread.status;
590 if (server_pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
591 wine_server_send_fd( request_pipe[0] );
593 SERVER_START_REQ( new_thread )
595 req->access = THREAD_ALL_ACCESS;
596 req->attributes = 0; /* FIXME */
597 req->suspend = suspended;
598 req->request_fd = request_pipe[0];
599 if (!(status = wine_server_call( req )))
601 handle = wine_server_ptr_handle( reply->handle );
602 tid = reply->tid;
604 close( request_pipe[0] );
606 SERVER_END_REQ;
608 if (status)
610 close( request_pipe[1] );
611 return status;
614 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
616 if ((status = signal_alloc_thread( &teb ))) goto error;
618 teb->Peb = NtCurrentTeb()->Peb;
619 teb->ClientId.UniqueProcess = ULongToHandle(GetCurrentProcessId());
620 teb->ClientId.UniqueThread = ULongToHandle(tid);
621 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
622 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
624 /* create default activation context frame for new thread */
625 RtlGetActiveActivationContext(&actctx);
626 if (actctx)
628 RTL_ACTIVATION_CONTEXT_STACK_FRAME *frame;
630 frame = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(*frame));
631 frame->Previous = NULL;
632 frame->ActivationContext = actctx;
633 frame->Flags = 0;
634 teb->ActivationContextStack.ActiveFrame = frame;
637 info = (struct startup_info *)(teb + 1);
638 info->teb = teb;
639 info->entry_point = start;
640 info->entry_arg = param;
642 if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit, &extra_stack )))
643 goto error;
645 thread_data = (struct ntdll_thread_data *)&teb->GdiTebBatch;
646 thread_data->request_fd = request_pipe[1];
647 thread_data->reply_fd = -1;
648 thread_data->wait_fd[0] = -1;
649 thread_data->wait_fd[1] = -1;
650 thread_data->start_stack = (char *)teb->Tib.StackBase;
652 pthread_attr_init( &attr );
653 pthread_attr_setstack( &attr, teb->DeallocationStack,
654 (char *)teb->Tib.StackBase + extra_stack - (char *)teb->DeallocationStack );
655 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */
656 interlocked_xchg_add( &nb_threads, 1 );
657 if (pthread_create( &pthread_id, &attr, (void * (*)(void *))start_thread, info ))
659 interlocked_xchg_add( &nb_threads, -1 );
660 pthread_attr_destroy( &attr );
661 status = STATUS_NO_MEMORY;
662 goto error;
664 pthread_attr_destroy( &attr );
665 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
667 if (id) id->UniqueThread = ULongToHandle(tid);
668 if (handle_ptr) *handle_ptr = handle;
669 else NtClose( handle );
671 return STATUS_SUCCESS;
673 error:
674 if (teb) free_thread_data( teb );
675 if (handle) NtClose( handle );
676 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
677 close( request_pipe[1] );
678 return status;
682 /******************************************************************************
683 * RtlGetNtGlobalFlags (NTDLL.@)
685 ULONG WINAPI RtlGetNtGlobalFlags(void)
687 if (!peb) return 0; /* init not done yet */
688 return peb->NtGlobalFlag;
692 /***********************************************************************
693 * NtOpenThread (NTDLL.@)
694 * ZwOpenThread (NTDLL.@)
696 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
697 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
699 NTSTATUS ret;
701 SERVER_START_REQ( open_thread )
703 req->tid = HandleToULong(id->UniqueThread);
704 req->access = access;
705 req->attributes = attr ? attr->Attributes : 0;
706 ret = wine_server_call( req );
707 *handle = wine_server_ptr_handle( reply->handle );
709 SERVER_END_REQ;
710 return ret;
714 /******************************************************************************
715 * NtSuspendThread (NTDLL.@)
716 * ZwSuspendThread (NTDLL.@)
718 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
720 NTSTATUS ret;
722 SERVER_START_REQ( suspend_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 * NtResumeThread (NTDLL.@)
737 * ZwResumeThread (NTDLL.@)
739 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
741 NTSTATUS ret;
743 SERVER_START_REQ( resume_thread )
745 req->handle = wine_server_obj_handle( handle );
746 if (!(ret = wine_server_call( req )))
748 if (count) *count = reply->count;
751 SERVER_END_REQ;
752 return ret;
756 /******************************************************************************
757 * NtAlertResumeThread (NTDLL.@)
758 * ZwAlertResumeThread (NTDLL.@)
760 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
762 FIXME( "stub: should alert thread %p\n", handle );
763 return NtResumeThread( handle, count );
767 /******************************************************************************
768 * NtAlertThread (NTDLL.@)
769 * ZwAlertThread (NTDLL.@)
771 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
773 FIXME( "stub: %p\n", handle );
774 return STATUS_NOT_IMPLEMENTED;
778 /******************************************************************************
779 * NtTerminateThread (NTDLL.@)
780 * ZwTerminateThread (NTDLL.@)
782 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
784 NTSTATUS ret;
785 BOOL self;
787 SERVER_START_REQ( terminate_thread )
789 req->handle = wine_server_obj_handle( handle );
790 req->exit_code = exit_code;
791 ret = wine_server_call( req );
792 self = !ret && reply->self;
794 SERVER_END_REQ;
796 if (self) abort_thread( exit_code );
797 return ret;
801 /******************************************************************************
802 * NtQueueApcThread (NTDLL.@)
804 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
805 ULONG_PTR arg2, ULONG_PTR arg3 )
807 NTSTATUS ret;
808 SERVER_START_REQ( queue_apc )
810 req->handle = wine_server_obj_handle( handle );
811 if (func)
813 req->call.type = APC_USER;
814 req->call.user.func = wine_server_client_ptr( func );
815 req->call.user.args[0] = arg1;
816 req->call.user.args[1] = arg2;
817 req->call.user.args[2] = arg3;
819 else req->call.type = APC_NONE; /* wake up only */
820 ret = wine_server_call( req );
822 SERVER_END_REQ;
823 return ret;
827 /******************************************************************************
828 * RtlPushFrame (NTDLL.@)
830 void WINAPI RtlPushFrame( TEB_ACTIVE_FRAME *frame )
832 frame->Previous = NtCurrentTeb()->ActiveFrame;
833 NtCurrentTeb()->ActiveFrame = frame;
837 /******************************************************************************
838 * RtlPopFrame (NTDLL.@)
840 void WINAPI RtlPopFrame( TEB_ACTIVE_FRAME *frame )
842 NtCurrentTeb()->ActiveFrame = frame->Previous;
846 /******************************************************************************
847 * RtlGetFrame (NTDLL.@)
849 TEB_ACTIVE_FRAME * WINAPI RtlGetFrame(void)
851 return NtCurrentTeb()->ActiveFrame;
855 /***********************************************************************
856 * set_thread_context
858 NTSTATUS set_thread_context( HANDLE handle, const CONTEXT *context, BOOL *self )
860 NTSTATUS ret;
861 DWORD dummy, i;
862 context_t server_context;
864 context_to_server( &server_context, context );
866 SERVER_START_REQ( set_thread_context )
868 req->handle = wine_server_obj_handle( handle );
869 req->suspend = 1;
870 wine_server_add_data( req, &server_context, sizeof(server_context) );
871 ret = wine_server_call( req );
872 *self = reply->self;
874 SERVER_END_REQ;
876 if (ret == STATUS_PENDING)
878 for (i = 0; i < 100; i++)
880 SERVER_START_REQ( set_thread_context )
882 req->handle = wine_server_obj_handle( handle );
883 req->suspend = 0;
884 wine_server_add_data( req, &server_context, sizeof(server_context) );
885 ret = wine_server_call( req );
887 SERVER_END_REQ;
888 if (ret == STATUS_PENDING)
890 LARGE_INTEGER timeout;
891 timeout.QuadPart = -10000;
892 NtDelayExecution( FALSE, &timeout );
894 else break;
896 NtResumeThread( handle, &dummy );
897 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
900 return ret;
904 /* convert CPU-specific flags to generic server flags */
905 static inline unsigned int get_server_context_flags( DWORD flags )
907 unsigned int ret = 0;
909 flags &= 0x3f; /* mask CPU id flags */
910 if (flags & CONTEXT_CONTROL) ret |= SERVER_CTX_CONTROL;
911 if (flags & CONTEXT_INTEGER) ret |= SERVER_CTX_INTEGER;
912 #ifdef CONTEXT_SEGMENTS
913 if (flags & CONTEXT_SEGMENTS) ret |= SERVER_CTX_SEGMENTS;
914 #endif
915 #ifdef CONTEXT_FLOATING_POINT
916 if (flags & CONTEXT_FLOATING_POINT) ret |= SERVER_CTX_FLOATING_POINT;
917 #endif
918 #ifdef CONTEXT_DEBUG_REGISTERS
919 if (flags & CONTEXT_DEBUG_REGISTERS) ret |= SERVER_CTX_DEBUG_REGISTERS;
920 #endif
921 #ifdef CONTEXT_EXTENDED_REGISTERS
922 if (flags & CONTEXT_EXTENDED_REGISTERS) ret |= SERVER_CTX_EXTENDED_REGISTERS;
923 #endif
924 return ret;
927 /***********************************************************************
928 * get_thread_context
930 NTSTATUS get_thread_context( HANDLE handle, CONTEXT *context, BOOL *self )
932 NTSTATUS ret;
933 DWORD dummy, i;
934 unsigned int server_flags = get_server_context_flags( context->ContextFlags );
935 context_t server_context;
937 SERVER_START_REQ( get_thread_context )
939 req->handle = wine_server_obj_handle( handle );
940 req->flags = server_flags;
941 req->suspend = 1;
942 wine_server_set_reply( req, &server_context, sizeof(server_context) );
943 ret = wine_server_call( req );
944 *self = reply->self;
946 SERVER_END_REQ;
948 if (ret == STATUS_PENDING)
950 for (i = 0; i < 100; i++)
952 SERVER_START_REQ( get_thread_context )
954 req->handle = wine_server_obj_handle( handle );
955 req->flags = server_flags;
956 req->suspend = 0;
957 wine_server_set_reply( req, &server_context, sizeof(server_context) );
958 ret = wine_server_call( req );
960 SERVER_END_REQ;
961 if (ret == STATUS_PENDING)
963 LARGE_INTEGER timeout;
964 timeout.QuadPart = -10000;
965 NtDelayExecution( FALSE, &timeout );
967 else break;
969 NtResumeThread( handle, &dummy );
970 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
972 if (!ret) ret = context_from_server( context, &server_context );
973 return ret;
977 /******************************************************************************
978 * NtQueryInformationThread (NTDLL.@)
979 * ZwQueryInformationThread (NTDLL.@)
981 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
982 void *data, ULONG length, ULONG *ret_len )
984 NTSTATUS status;
986 switch(class)
988 case ThreadBasicInformation:
990 THREAD_BASIC_INFORMATION info;
991 const ULONG_PTR affinity_mask = get_system_affinity_mask();
993 SERVER_START_REQ( get_thread_info )
995 req->handle = wine_server_obj_handle( handle );
996 req->tid_in = 0;
997 if (!(status = wine_server_call( req )))
999 info.ExitStatus = reply->exit_code;
1000 info.TebBaseAddress = wine_server_get_ptr( reply->teb );
1001 info.ClientId.UniqueProcess = ULongToHandle(reply->pid);
1002 info.ClientId.UniqueThread = ULongToHandle(reply->tid);
1003 info.AffinityMask = reply->affinity & affinity_mask;
1004 info.Priority = reply->priority;
1005 info.BasePriority = reply->priority; /* FIXME */
1008 SERVER_END_REQ;
1009 if (status == STATUS_SUCCESS)
1011 if (data) memcpy( data, &info, min( length, sizeof(info) ));
1012 if (ret_len) *ret_len = min( length, sizeof(info) );
1015 return status;
1016 case ThreadAffinityMask:
1018 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1019 ULONG_PTR affinity = 0;
1021 SERVER_START_REQ( get_thread_info )
1023 req->handle = wine_server_obj_handle( handle );
1024 req->tid_in = 0;
1025 if (!(status = wine_server_call( req )))
1026 affinity = reply->affinity & affinity_mask;
1028 SERVER_END_REQ;
1029 if (status == STATUS_SUCCESS)
1031 if (data) memcpy( data, &affinity, min( length, sizeof(affinity) ));
1032 if (ret_len) *ret_len = min( length, sizeof(affinity) );
1035 return status;
1036 case ThreadTimes:
1038 KERNEL_USER_TIMES kusrt;
1040 SERVER_START_REQ( get_thread_times )
1042 req->handle = wine_server_obj_handle( handle );
1043 status = wine_server_call( req );
1044 if (status == STATUS_SUCCESS)
1046 kusrt.CreateTime.QuadPart = reply->creation_time;
1047 kusrt.ExitTime.QuadPart = reply->exit_time;
1050 SERVER_END_REQ;
1051 if (status == STATUS_SUCCESS)
1053 /* We call times(2) for kernel time or user time */
1054 /* We can only (portably) do this for the current thread */
1055 if (handle == GetCurrentThread())
1057 struct tms time_buf;
1058 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1060 times(&time_buf);
1061 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1062 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1064 else
1066 static BOOL reported = FALSE;
1068 kusrt.KernelTime.QuadPart = 0;
1069 kusrt.UserTime.QuadPart = 0;
1070 if (reported)
1071 TRACE("Cannot get kerneltime or usertime of other threads\n");
1072 else
1074 FIXME("Cannot get kerneltime or usertime of other threads\n");
1075 reported = TRUE;
1078 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1079 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1082 return status;
1083 case ThreadDescriptorTableEntry:
1085 #ifdef __i386__
1086 THREAD_DESCRIPTOR_INFORMATION* tdi = data;
1087 if (length < sizeof(*tdi))
1088 status = STATUS_INFO_LENGTH_MISMATCH;
1089 else if (!(tdi->Selector & 4)) /* GDT selector */
1091 unsigned sel = LOWORD(tdi->Selector) & ~3; /* ignore RPL */
1092 status = STATUS_SUCCESS;
1093 if (!sel) /* null selector */
1094 memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1095 else
1097 tdi->Entry.BaseLow = 0;
1098 tdi->Entry.HighWord.Bits.BaseMid = 0;
1099 tdi->Entry.HighWord.Bits.BaseHi = 0;
1100 tdi->Entry.LimitLow = 0xffff;
1101 tdi->Entry.HighWord.Bits.LimitHi = 0xf;
1102 tdi->Entry.HighWord.Bits.Dpl = 3;
1103 tdi->Entry.HighWord.Bits.Sys = 0;
1104 tdi->Entry.HighWord.Bits.Pres = 1;
1105 tdi->Entry.HighWord.Bits.Granularity = 1;
1106 tdi->Entry.HighWord.Bits.Default_Big = 1;
1107 tdi->Entry.HighWord.Bits.Type = 0x12;
1108 tdi->Entry.HighWord.Bits.Reserved_0 = 0;
1109 /* it has to be one of the system GDT selectors */
1110 if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1112 if (sel == (wine_get_cs() & ~3))
1113 tdi->Entry.HighWord.Bits.Type |= 8; /* code segment */
1114 else if (sel == (wine_get_fs() & ~3))
1116 ULONG_PTR fs_base = (ULONG_PTR)NtCurrentTeb();
1117 tdi->Entry.BaseLow = fs_base & 0xffff;
1118 tdi->Entry.HighWord.Bits.BaseMid = (fs_base >> 16) & 0xff;
1119 tdi->Entry.HighWord.Bits.BaseHi = (fs_base >> 24) & 0xff;
1120 tdi->Entry.LimitLow = 0x0fff;
1121 tdi->Entry.HighWord.Bits.LimitHi = 0;
1122 tdi->Entry.HighWord.Bits.Granularity = 0;
1124 else status = STATUS_ACCESS_DENIED;
1128 else
1130 SERVER_START_REQ( get_selector_entry )
1132 req->handle = wine_server_obj_handle( handle );
1133 req->entry = LOWORD(tdi->Selector) >> 3;
1134 status = wine_server_call( req );
1135 if (!status)
1137 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1138 status = STATUS_ACCESS_VIOLATION;
1139 else
1141 wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1142 wine_ldt_set_limit( &tdi->Entry, reply->limit );
1143 wine_ldt_set_flags( &tdi->Entry, reply->flags );
1147 SERVER_END_REQ;
1149 if (status == STATUS_SUCCESS && ret_len)
1150 /* yes, that's a bit strange, but it's the way it is */
1151 *ret_len = sizeof(LDT_ENTRY);
1152 #else
1153 status = STATUS_NOT_IMPLEMENTED;
1154 #endif
1155 return status;
1157 case ThreadAmILastThread:
1159 SERVER_START_REQ(get_thread_info)
1161 req->handle = wine_server_obj_handle( handle );
1162 req->tid_in = 0;
1163 status = wine_server_call( req );
1164 if (status == STATUS_SUCCESS)
1166 BOOLEAN last = reply->last;
1167 if (data) memcpy( data, &last, min( length, sizeof(last) ));
1168 if (ret_len) *ret_len = min( length, sizeof(last) );
1171 SERVER_END_REQ;
1172 return status;
1174 case ThreadQuerySetWin32StartAddress:
1176 SERVER_START_REQ( get_thread_info )
1178 req->handle = wine_server_obj_handle( handle );
1179 req->tid_in = 0;
1180 status = wine_server_call( req );
1181 if (status == STATUS_SUCCESS)
1183 PRTL_THREAD_START_ROUTINE entry = wine_server_get_ptr( reply->entry_point );
1184 if (data) memcpy( data, &entry, min( length, sizeof(entry) ) );
1185 if (ret_len) *ret_len = min( length, sizeof(entry) );
1188 SERVER_END_REQ;
1189 return status;
1191 case ThreadGroupInformation:
1193 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1194 GROUP_AFFINITY affinity;
1196 memset(&affinity, 0, sizeof(affinity));
1197 affinity.Group = 0; /* Wine only supports max 64 processors */
1199 SERVER_START_REQ( get_thread_info )
1201 req->handle = wine_server_obj_handle( handle );
1202 req->tid_in = 0;
1203 if (!(status = wine_server_call( req )))
1204 affinity.Mask = reply->affinity & affinity_mask;
1206 SERVER_END_REQ;
1207 if (status == STATUS_SUCCESS)
1209 if (data) memcpy( data, &affinity, min( length, sizeof(affinity) ));
1210 if (ret_len) *ret_len = min( length, sizeof(affinity) );
1213 return status;
1214 case ThreadIsIoPending:
1215 FIXME( "ThreadIsIoPending info class not supported yet\n" );
1216 if (length != sizeof(BOOL)) return STATUS_INFO_LENGTH_MISMATCH;
1217 if (!data) return STATUS_ACCESS_DENIED;
1219 *(BOOL*)data = FALSE;
1220 if (ret_len) *ret_len = sizeof(BOOL);
1221 return STATUS_SUCCESS;
1222 case ThreadPriority:
1223 case ThreadBasePriority:
1224 case ThreadImpersonationToken:
1225 case ThreadEnableAlignmentFaultFixup:
1226 case ThreadEventPair_Reusable:
1227 case ThreadZeroTlsCell:
1228 case ThreadPerformanceCount:
1229 case ThreadIdealProcessor:
1230 case ThreadPriorityBoost:
1231 case ThreadSetTlsArrayAddress:
1232 default:
1233 FIXME( "info class %d not supported yet\n", class );
1234 return STATUS_NOT_IMPLEMENTED;
1239 /******************************************************************************
1240 * NtSetInformationThread (NTDLL.@)
1241 * ZwSetInformationThread (NTDLL.@)
1243 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1244 LPCVOID data, ULONG length )
1246 NTSTATUS status;
1247 switch(class)
1249 case ThreadZeroTlsCell:
1250 if (handle == GetCurrentThread())
1252 LIST_ENTRY *entry;
1253 DWORD index;
1255 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1256 index = *(const DWORD *)data;
1257 if (index < TLS_MINIMUM_AVAILABLE)
1259 RtlAcquirePebLock();
1260 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1262 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1263 teb->TlsSlots[index] = 0;
1265 RtlReleasePebLock();
1267 else
1269 index -= TLS_MINIMUM_AVAILABLE;
1270 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1271 return STATUS_INVALID_PARAMETER;
1272 RtlAcquirePebLock();
1273 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1275 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1276 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1278 RtlReleasePebLock();
1280 return STATUS_SUCCESS;
1282 FIXME( "ZeroTlsCell not supported on other threads\n" );
1283 return STATUS_NOT_IMPLEMENTED;
1285 case ThreadImpersonationToken:
1287 const HANDLE *phToken = data;
1288 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1289 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1290 SERVER_START_REQ( set_thread_info )
1292 req->handle = wine_server_obj_handle( handle );
1293 req->token = wine_server_obj_handle( *phToken );
1294 req->mask = SET_THREAD_INFO_TOKEN;
1295 status = wine_server_call( req );
1297 SERVER_END_REQ;
1299 return status;
1300 case ThreadBasePriority:
1302 const DWORD *pprio = data;
1303 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1304 SERVER_START_REQ( set_thread_info )
1306 req->handle = wine_server_obj_handle( handle );
1307 req->priority = *pprio;
1308 req->mask = SET_THREAD_INFO_PRIORITY;
1309 status = wine_server_call( req );
1311 SERVER_END_REQ;
1313 return status;
1314 case ThreadAffinityMask:
1316 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1317 ULONG_PTR req_aff;
1319 if (length != sizeof(ULONG_PTR)) return STATUS_INVALID_PARAMETER;
1320 req_aff = *(const ULONG_PTR *)data;
1321 if ((ULONG)req_aff == ~0u) req_aff = affinity_mask;
1322 else if (req_aff & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1323 else if (!req_aff) return STATUS_INVALID_PARAMETER;
1324 SERVER_START_REQ( set_thread_info )
1326 req->handle = wine_server_obj_handle( handle );
1327 req->affinity = req_aff;
1328 req->mask = SET_THREAD_INFO_AFFINITY;
1329 status = wine_server_call( req );
1331 SERVER_END_REQ;
1333 return status;
1334 case ThreadHideFromDebugger:
1335 /* pretend the call succeeded to satisfy some code protectors */
1336 return STATUS_SUCCESS;
1337 case ThreadQuerySetWin32StartAddress:
1339 const PRTL_THREAD_START_ROUTINE *entry = data;
1340 if (length != sizeof(PRTL_THREAD_START_ROUTINE)) return STATUS_INVALID_PARAMETER;
1341 SERVER_START_REQ( set_thread_info )
1343 req->handle = wine_server_obj_handle( handle );
1344 req->mask = SET_THREAD_INFO_ENTRYPOINT;
1345 req->entry_point = wine_server_client_ptr( *entry );
1346 status = wine_server_call( req );
1348 SERVER_END_REQ;
1350 return status;
1351 case ThreadGroupInformation:
1353 const ULONG_PTR affinity_mask = get_system_affinity_mask();
1354 const GROUP_AFFINITY *req_aff;
1356 if (length != sizeof(*req_aff)) return STATUS_INVALID_PARAMETER;
1357 if (!data) return STATUS_ACCESS_VIOLATION;
1358 req_aff = data;
1360 /* On Windows the request fails if the reserved fields are set */
1361 if (req_aff->Reserved[0] || req_aff->Reserved[1] || req_aff->Reserved[2])
1362 return STATUS_INVALID_PARAMETER;
1364 /* Wine only supports max 64 processors */
1365 if (req_aff->Group) return STATUS_INVALID_PARAMETER;
1366 if (req_aff->Mask & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1367 if (!req_aff->Mask) return STATUS_INVALID_PARAMETER;
1368 SERVER_START_REQ( set_thread_info )
1370 req->handle = wine_server_obj_handle( handle );
1371 req->affinity = req_aff->Mask;
1372 req->mask = SET_THREAD_INFO_AFFINITY;
1373 status = wine_server_call( req );
1375 SERVER_END_REQ;
1377 return status;
1378 case ThreadBasicInformation:
1379 case ThreadTimes:
1380 case ThreadPriority:
1381 case ThreadDescriptorTableEntry:
1382 case ThreadEnableAlignmentFaultFixup:
1383 case ThreadEventPair_Reusable:
1384 case ThreadPerformanceCount:
1385 case ThreadAmILastThread:
1386 case ThreadIdealProcessor:
1387 case ThreadPriorityBoost:
1388 case ThreadSetTlsArrayAddress:
1389 case ThreadIsIoPending:
1390 default:
1391 FIXME( "info class %d not supported yet\n", class );
1392 return STATUS_NOT_IMPLEMENTED;
1396 /******************************************************************************
1397 * NtGetCurrentProcessorNumber (NTDLL.@)
1399 * Return the processor, on which the thread is running
1402 ULONG WINAPI NtGetCurrentProcessorNumber(void)
1404 ULONG processor;
1406 #if defined(__linux__) && defined(__NR_getcpu)
1407 int res = syscall(__NR_getcpu, &processor, NULL, NULL);
1408 if (res != -1) return processor;
1409 #endif
1411 if (NtCurrentTeb()->Peb->NumberOfProcessors > 1)
1413 ULONG_PTR thread_mask, processor_mask;
1414 NTSTATUS status;
1416 status = NtQueryInformationThread(GetCurrentThread(), ThreadAffinityMask,
1417 &thread_mask, sizeof(thread_mask), NULL);
1418 if (status == STATUS_SUCCESS)
1420 for (processor = 0; processor < NtCurrentTeb()->Peb->NumberOfProcessors; processor++)
1422 processor_mask = (1 << processor);
1423 if (thread_mask & processor_mask)
1425 if (thread_mask != processor_mask)
1426 FIXME("need multicore support (%d processors)\n",
1427 NtCurrentTeb()->Peb->NumberOfProcessors);
1428 return processor;
1434 /* fallback to the first processor */
1435 return 0;