user32: Fix a typo in the StretchDIBits stub name.
[wine/multimedia.git] / dlls / ntdll / thread.c
blob8944b6a87caf8ab507b52827d518a634cb1e54bc
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 <sys/types.h>
26 #ifdef HAVE_SYS_MMAN_H
27 #include <sys/mman.h>
28 #endif
29 #ifdef HAVE_SYS_TIMES_H
30 #include <sys/times.h>
31 #endif
33 #define NONAMELESSUNION
34 #include "ntstatus.h"
35 #define WIN32_NO_STATUS
36 #include "thread.h"
37 #include "winternl.h"
38 #include "wine/library.h"
39 #include "wine/server.h"
40 #include "wine/pthread.h"
41 #include "wine/debug.h"
42 #include "ntdll_misc.h"
43 #include "ddk/wdm.h"
44 #include "wine/exception.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(thread);
47 WINE_DECLARE_DEBUG_CHANNEL(relay);
49 struct _KUSER_SHARED_DATA *user_shared_data = NULL;
51 /* info passed to a starting thread */
52 struct startup_info
54 struct wine_pthread_thread_info pthread_info;
55 PRTL_THREAD_START_ROUTINE entry_point;
56 void *entry_arg;
59 static PEB_LDR_DATA ldr;
60 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
61 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
62 static RTL_BITMAP tls_bitmap;
63 static RTL_BITMAP tls_expansion_bitmap;
64 static LIST_ENTRY tls_links;
65 static size_t sigstack_total_size;
66 static ULONG sigstack_zero_bits;
68 struct wine_pthread_functions pthread_functions = { NULL };
71 static RTL_CRITICAL_SECTION ldt_section;
72 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
74 0, 0, &ldt_section,
75 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
76 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
78 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
79 static sigset_t ldt_sigset;
81 /***********************************************************************
82 * locking for LDT routines
84 static void ldt_lock(void)
86 sigset_t sigset;
88 pthread_functions.sigprocmask( SIG_BLOCK, &server_block_set, &sigset );
89 RtlEnterCriticalSection( &ldt_section );
90 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
93 static void ldt_unlock(void)
95 if (ldt_section.RecursionCount == 1)
97 sigset_t sigset = ldt_sigset;
98 RtlLeaveCriticalSection( &ldt_section );
99 pthread_functions.sigprocmask( SIG_SETMASK, &sigset, NULL );
101 else RtlLeaveCriticalSection( &ldt_section );
105 /***********************************************************************
106 * init_teb
108 static inline NTSTATUS init_teb( TEB *teb )
110 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
112 teb->Tib.ExceptionList = (void *)~0UL;
113 teb->Tib.StackBase = (void *)~0UL;
114 teb->Tib.Self = &teb->Tib;
115 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
116 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
118 if (!(thread_data->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
119 thread_data->request_fd = -1;
120 thread_data->reply_fd = -1;
121 thread_data->wait_fd[0] = -1;
122 thread_data->wait_fd[1] = -1;
124 return STATUS_SUCCESS;
128 /***********************************************************************
129 * fix_unicode_string
131 * Make sure the unicode string doesn't point beyond the end pointer
133 static inline void fix_unicode_string( UNICODE_STRING *str, const char *end_ptr )
135 if ((char *)str->Buffer >= end_ptr)
137 str->Length = str->MaximumLength = 0;
138 str->Buffer = NULL;
139 return;
141 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
143 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
145 if (str->Length >= str->MaximumLength)
147 if (str->MaximumLength >= sizeof(WCHAR))
148 str->Length = str->MaximumLength - sizeof(WCHAR);
149 else
150 str->Length = str->MaximumLength = 0;
155 /***********************************************************************
156 * init_user_process_params
158 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
160 static NTSTATUS init_user_process_params( SIZE_T info_size, HANDLE *exe_file )
162 void *ptr;
163 SIZE_T env_size;
164 NTSTATUS status;
165 RTL_USER_PROCESS_PARAMETERS *params = NULL;
167 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &info_size,
168 MEM_COMMIT, PAGE_READWRITE );
169 if (status != STATUS_SUCCESS) return status;
171 params->AllocationSize = info_size;
172 NtCurrentTeb()->Peb->ProcessParameters = params;
174 SERVER_START_REQ( get_startup_info )
176 wine_server_set_reply( req, params, info_size );
177 if (!(status = wine_server_call( req )))
179 info_size = wine_server_reply_size( reply );
180 *exe_file = reply->exe_file;
181 params->hStdInput = reply->hstdin;
182 params->hStdOutput = reply->hstdout;
183 params->hStdError = reply->hstderr;
186 SERVER_END_REQ;
187 if (status != STATUS_SUCCESS) return status;
189 if (params->Size > info_size) params->Size = info_size;
191 /* make sure the strings are valid */
192 fix_unicode_string( &params->CurrentDirectory.DosPath, (char *)info_size );
193 fix_unicode_string( &params->DllPath, (char *)info_size );
194 fix_unicode_string( &params->ImagePathName, (char *)info_size );
195 fix_unicode_string( &params->CommandLine, (char *)info_size );
196 fix_unicode_string( &params->WindowTitle, (char *)info_size );
197 fix_unicode_string( &params->Desktop, (char *)info_size );
198 fix_unicode_string( &params->ShellInfo, (char *)info_size );
199 fix_unicode_string( &params->RuntimeInfo, (char *)info_size );
201 /* environment needs to be a separate memory block */
202 env_size = info_size - params->Size;
203 if (!env_size) env_size = 1;
204 ptr = NULL;
205 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
206 MEM_COMMIT, PAGE_READWRITE );
207 if (status != STATUS_SUCCESS) return status;
208 memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
209 params->Environment = ptr;
211 RtlNormalizeProcessParams( params );
212 return status;
216 /***********************************************************************
217 * thread_init
219 * Setup the initial thread.
221 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
223 HANDLE thread_init(void)
225 PEB *peb;
226 TEB *teb;
227 void *addr;
228 SIZE_T size, info_size;
229 HANDLE exe_file = 0;
230 LARGE_INTEGER now;
231 struct ntdll_thread_data *thread_data;
232 struct wine_pthread_thread_info thread_info;
233 static struct debug_info debug_info; /* debug info for initial thread */
235 virtual_init();
237 /* reserve space for shared user data */
239 addr = (void *)0x7ffe0000;
240 size = 0x10000;
241 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE );
242 user_shared_data = addr;
244 /* allocate and initialize the PEB */
246 addr = NULL;
247 size = sizeof(*peb);
248 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
249 MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
250 peb = addr;
252 peb->NumberOfProcessors = 1;
253 peb->ProcessParameters = &params;
254 peb->TlsBitmap = &tls_bitmap;
255 peb->TlsExpansionBitmap = &tls_expansion_bitmap;
256 peb->LdrData = &ldr;
257 params.CurrentDirectory.DosPath.Buffer = current_dir;
258 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
259 params.wShowWindow = 1; /* SW_SHOWNORMAL */
260 RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
261 RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
262 sizeof(peb->TlsExpansionBitmapBits) * 8 );
263 InitializeListHead( &ldr.InLoadOrderModuleList );
264 InitializeListHead( &ldr.InMemoryOrderModuleList );
265 InitializeListHead( &ldr.InInitializationOrderModuleList );
266 InitializeListHead( &tls_links );
268 /* allocate and initialize the initial TEB */
270 sigstack_total_size = get_signal_stack_total_size();
271 while (1 << sigstack_zero_bits < sigstack_total_size) sigstack_zero_bits++;
272 assert( 1 << sigstack_zero_bits == sigstack_total_size ); /* must be a power of 2 */
273 assert( sigstack_total_size >= sizeof(TEB) + sizeof(struct startup_info) );
274 thread_info.teb_size = sigstack_total_size;
276 addr = NULL;
277 size = sigstack_total_size;
278 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
279 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
280 teb = addr;
281 teb->Peb = peb;
282 thread_info.teb_size = size;
283 init_teb( teb );
284 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
285 thread_data->debug_info = &debug_info;
286 InsertHeadList( &tls_links, &teb->TlsLinks );
288 thread_info.stack_base = NULL;
289 thread_info.stack_size = 0;
290 thread_info.teb_base = teb;
291 thread_info.teb_sel = thread_data->fs;
292 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
293 pthread_functions.init_current_teb( &thread_info );
294 pthread_functions.init_thread( &thread_info );
295 virtual_init_threading();
297 debug_info.str_pos = debug_info.strings;
298 debug_info.out_pos = debug_info.output;
299 debug_init();
301 /* setup the server connection */
302 server_init_process();
303 info_size = server_init_thread( thread_info.pid, thread_info.tid, NULL );
305 /* create the process heap */
306 if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
308 MESSAGE( "wine: failed to create the process heap\n" );
309 exit(1);
312 /* allocate user parameters */
313 if (info_size)
315 init_user_process_params( info_size, &exe_file );
317 else
319 /* This is wine specific: we have no parent (we're started from unix)
320 * so, create a simple console with bare handles to unix stdio
322 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, &params.hStdInput );
323 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
324 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
327 /* initialize LDT locking */
328 wine_ldt_init_locking( ldt_lock, ldt_unlock );
330 /* initialize time values in user_shared_data */
331 NtQuerySystemTime( &now );
332 user_shared_data->SystemTime.LowPart = now.u.LowPart;
333 user_shared_data->SystemTime.High1Time = user_shared_data->SystemTime.High2Time = now.u.HighPart;
334 user_shared_data->u.TickCountQuad = (now.QuadPart - server_start_time) / 10000;
335 user_shared_data->u.TickCount.High2Time = user_shared_data->u.TickCount.High1Time;
336 user_shared_data->TickCountLowDeprecated = user_shared_data->u.TickCount.LowPart;
337 user_shared_data->TickCountMultiplier = 1 << 24;
339 return exe_file;
342 typedef LONG (WINAPI *PUNHANDLED_EXCEPTION_FILTER)(PEXCEPTION_POINTERS);
343 static PUNHANDLED_EXCEPTION_FILTER get_unhandled_exception_filter(void)
345 static PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter;
346 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
347 UNICODE_STRING module_name;
348 ANSI_STRING func_name;
349 HMODULE kernel32_handle;
351 if (unhandled_exception_filter) return unhandled_exception_filter;
353 RtlInitUnicodeString(&module_name, kernel32W);
354 RtlInitAnsiString( &func_name, "UnhandledExceptionFilter" );
356 if (LdrGetDllHandle( 0, 0, &module_name, &kernel32_handle ) == STATUS_SUCCESS)
357 LdrGetProcedureAddress( kernel32_handle, &func_name, 0,
358 (void **)&unhandled_exception_filter );
360 return unhandled_exception_filter;
363 #ifdef __i386__
364 /* wrapper for apps that don't declare the thread function correctly */
365 extern DWORD call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry, void *arg );
366 __ASM_GLOBAL_FUNC(call_thread_entry_point,
367 "pushl %ebp\n\t"
368 "movl %esp,%ebp\n\t"
369 "subl $4,%esp\n\t"
370 "pushl 12(%ebp)\n\t"
371 "movl 8(%ebp),%eax\n\t"
372 "call *%eax\n\t"
373 "leave\n\t"
374 "ret" );
375 #else
376 static inline DWORD call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry, void *arg )
378 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)entry;
379 return func( arg );
381 #endif
383 /***********************************************************************
384 * call_thread_func
386 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
388 static void DECLSPEC_NORETURN call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func, void *arg )
390 DWORD exit_code;
391 BOOL last;
393 MODULE_DllThreadAttach( NULL );
395 if (TRACE_ON(relay))
396 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func, arg );
398 exit_code = call_thread_entry_point( rtl_func, arg );
400 /* send the exit code to the server */
401 SERVER_START_REQ( terminate_thread )
403 req->handle = GetCurrentThread();
404 req->exit_code = exit_code;
405 wine_server_call( req );
406 last = reply->last;
408 SERVER_END_REQ;
410 if (last)
412 LdrShutdownProcess();
413 exit( exit_code );
415 else
417 LdrShutdownThread();
418 server_exit_thread( exit_code );
423 /***********************************************************************
424 * start_thread
426 * Startup routine for a newly created thread.
428 static void start_thread( struct wine_pthread_thread_info *info )
430 TEB *teb = info->teb_base;
431 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
432 struct startup_info *startup_info = (struct startup_info *)info;
433 PRTL_THREAD_START_ROUTINE func = startup_info->entry_point;
434 void *arg = startup_info->entry_arg;
435 struct debug_info debug_info;
436 SIZE_T size, page_size = getpagesize();
438 debug_info.str_pos = debug_info.strings;
439 debug_info.out_pos = debug_info.output;
440 thread_data->debug_info = &debug_info;
442 pthread_functions.init_current_teb( info );
443 SIGNAL_Init();
444 server_init_thread( info->pid, info->tid, func );
445 pthread_functions.init_thread( info );
447 /* allocate a memory view for the stack */
448 size = info->stack_size;
449 teb->DeallocationStack = info->stack_base;
450 NtAllocateVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, 0,
451 &size, MEM_SYSTEM, PAGE_READWRITE );
452 /* limit is lower than base since the stack grows down */
453 teb->Tib.StackBase = (char *)info->stack_base + info->stack_size;
454 teb->Tib.StackLimit = (char *)info->stack_base + page_size;
456 /* setup the guard page */
457 size = page_size;
458 NtProtectVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, PAGE_NOACCESS, NULL );
460 pthread_functions.sigprocmask( SIG_UNBLOCK, &server_block_set, NULL );
462 RtlAcquirePebLock();
463 InsertHeadList( &tls_links, &teb->TlsLinks );
464 RtlReleasePebLock();
466 /* NOTE: Windows does not have an exception handler around the call to
467 * the thread attach. We do for ease of debugging */
468 if (get_unhandled_exception_filter())
470 __TRY
472 call_thread_func( func, arg );
474 __EXCEPT(get_unhandled_exception_filter())
476 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
478 __ENDTRY
480 else
481 call_thread_func( func, arg );
485 /***********************************************************************
486 * RtlCreateUserThread (NTDLL.@)
488 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
489 BOOLEAN suspended, PVOID stack_addr,
490 SIZE_T stack_reserve, SIZE_T stack_commit,
491 PRTL_THREAD_START_ROUTINE start, void *param,
492 HANDLE *handle_ptr, CLIENT_ID *id )
494 sigset_t sigset;
495 struct ntdll_thread_data *thread_data = NULL;
496 struct ntdll_thread_regs *thread_regs;
497 struct startup_info *info = NULL;
498 void *addr = NULL;
499 HANDLE handle = 0;
500 TEB *teb;
501 DWORD tid = 0;
502 int request_pipe[2];
503 NTSTATUS status;
504 SIZE_T size, page_size = getpagesize();
506 if (process != NtCurrentProcess())
508 apc_call_t call;
509 apc_result_t result;
511 memset( &call, 0, sizeof(call) );
513 call.create_thread.type = APC_CREATE_THREAD;
514 call.create_thread.func = start;
515 call.create_thread.arg = param;
516 call.create_thread.reserve = stack_reserve;
517 call.create_thread.commit = stack_commit;
518 call.create_thread.suspend = suspended;
519 status = NTDLL_queue_process_apc( process, &call, &result );
520 if (status != STATUS_SUCCESS) return status;
522 if (result.create_thread.status == STATUS_SUCCESS)
524 if (id) id->UniqueThread = ULongToHandle(result.create_thread.tid);
525 if (handle_ptr) *handle_ptr = result.create_thread.handle;
526 else NtClose( result.create_thread.handle );
528 return result.create_thread.status;
531 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
532 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
533 wine_server_send_fd( request_pipe[0] );
535 SERVER_START_REQ( new_thread )
537 req->access = THREAD_ALL_ACCESS;
538 req->attributes = 0; /* FIXME */
539 req->suspend = suspended;
540 req->request_fd = request_pipe[0];
541 if (!(status = wine_server_call( req )))
543 handle = reply->handle;
544 tid = reply->tid;
546 close( request_pipe[0] );
548 SERVER_END_REQ;
550 if (status)
552 close( request_pipe[1] );
553 return status;
556 pthread_functions.sigprocmask( SIG_BLOCK, &server_block_set, &sigset );
558 addr = NULL;
559 size = sigstack_total_size;
560 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
561 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
562 goto error;
563 teb = addr;
564 teb->Peb = NtCurrentTeb()->Peb;
565 info = (struct startup_info *)(teb + 1);
566 info->pthread_info.teb_size = size;
567 if ((status = init_teb( teb ))) goto error;
569 teb->ClientId.UniqueProcess = ULongToHandle(GetCurrentProcessId());
570 teb->ClientId.UniqueThread = ULongToHandle(tid);
572 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
573 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
574 thread_data->request_fd = request_pipe[1];
576 info->pthread_info.teb_base = teb;
577 info->pthread_info.teb_sel = thread_data->fs;
579 /* inherit debug registers from parent thread */
580 thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
581 thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
582 thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
583 thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
584 thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
585 thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
587 if (!stack_reserve || !stack_commit)
589 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
590 if (!stack_reserve) stack_reserve = nt->OptionalHeader.SizeOfStackReserve;
591 if (!stack_commit) stack_commit = nt->OptionalHeader.SizeOfStackCommit;
593 if (stack_reserve < stack_commit) stack_reserve = stack_commit;
594 stack_reserve += page_size; /* for the guard page */
595 stack_reserve = (stack_reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
596 if (stack_reserve < 1024 * 1024) stack_reserve = 1024 * 1024; /* Xlib needs a large stack */
598 info->pthread_info.stack_base = NULL;
599 info->pthread_info.stack_size = stack_reserve;
600 info->pthread_info.entry = start_thread;
601 info->entry_point = start;
602 info->entry_arg = param;
604 if (pthread_functions.create_thread( &info->pthread_info ) == -1)
606 status = STATUS_NO_MEMORY;
607 goto error;
609 pthread_functions.sigprocmask( SIG_SETMASK, &sigset, NULL );
611 if (id) id->UniqueThread = ULongToHandle(tid);
612 if (handle_ptr) *handle_ptr = handle;
613 else NtClose( handle );
615 return STATUS_SUCCESS;
617 error:
618 if (thread_data) wine_ldt_free_fs( thread_data->fs );
619 if (addr)
621 SIZE_T size = 0;
622 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
624 if (handle) NtClose( handle );
625 pthread_functions.sigprocmask( SIG_SETMASK, &sigset, NULL );
626 close( request_pipe[1] );
627 return status;
631 /***********************************************************************
632 * RtlExitUserThread (NTDLL.@)
634 void WINAPI RtlExitUserThread( ULONG status )
636 LdrShutdownThread();
637 server_exit_thread( status );
641 /***********************************************************************
642 * NtOpenThread (NTDLL.@)
643 * ZwOpenThread (NTDLL.@)
645 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
646 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
648 NTSTATUS ret;
650 SERVER_START_REQ( open_thread )
652 req->tid = HandleToULong(id->UniqueThread);
653 req->access = access;
654 req->attributes = attr ? attr->Attributes : 0;
655 ret = wine_server_call( req );
656 *handle = reply->handle;
658 SERVER_END_REQ;
659 return ret;
663 /******************************************************************************
664 * NtSuspendThread (NTDLL.@)
665 * ZwSuspendThread (NTDLL.@)
667 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
669 NTSTATUS ret;
671 SERVER_START_REQ( suspend_thread )
673 req->handle = handle;
674 if (!(ret = wine_server_call( req ))) *count = reply->count;
676 SERVER_END_REQ;
677 return ret;
681 /******************************************************************************
682 * NtResumeThread (NTDLL.@)
683 * ZwResumeThread (NTDLL.@)
685 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
687 NTSTATUS ret;
689 SERVER_START_REQ( resume_thread )
691 req->handle = handle;
692 if (!(ret = wine_server_call( req ))) *count = reply->count;
694 SERVER_END_REQ;
695 return ret;
699 /******************************************************************************
700 * NtAlertResumeThread (NTDLL.@)
701 * ZwAlertResumeThread (NTDLL.@)
703 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
705 FIXME( "stub: should alert thread %p\n", handle );
706 return NtResumeThread( handle, count );
710 /******************************************************************************
711 * NtAlertThread (NTDLL.@)
712 * ZwAlertThread (NTDLL.@)
714 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
716 FIXME( "stub: %p\n", handle );
717 return STATUS_NOT_IMPLEMENTED;
721 /******************************************************************************
722 * NtTerminateThread (NTDLL.@)
723 * ZwTerminateThread (NTDLL.@)
725 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
727 NTSTATUS ret;
728 BOOL self, last;
730 SERVER_START_REQ( terminate_thread )
732 req->handle = handle;
733 req->exit_code = exit_code;
734 ret = wine_server_call( req );
735 self = !ret && reply->self;
736 last = reply->last;
738 SERVER_END_REQ;
740 if (self)
742 if (last) exit( exit_code );
743 else server_abort_thread( exit_code );
745 return ret;
749 /******************************************************************************
750 * NtQueueApcThread (NTDLL.@)
752 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
753 ULONG_PTR arg2, ULONG_PTR arg3 )
755 NTSTATUS ret;
756 SERVER_START_REQ( queue_apc )
758 req->thread = handle;
759 if (func)
761 req->call.type = APC_USER;
762 req->call.user.func = func;
763 req->call.user.args[0] = arg1;
764 req->call.user.args[1] = arg2;
765 req->call.user.args[2] = arg3;
767 else req->call.type = APC_NONE; /* wake up only */
768 ret = wine_server_call( req );
770 SERVER_END_REQ;
771 return ret;
775 /***********************************************************************
776 * NtSetContextThread (NTDLL.@)
777 * ZwSetContextThread (NTDLL.@)
779 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
781 NTSTATUS ret;
782 DWORD dummy, i;
783 BOOL self = FALSE;
785 #ifdef __i386__
786 /* on i386 debug registers always require a server call */
787 self = (handle == GetCurrentThread());
788 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
790 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
791 self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
792 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
793 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
795 #endif
797 if (!self)
799 SERVER_START_REQ( set_thread_context )
801 req->handle = handle;
802 req->flags = context->ContextFlags;
803 req->suspend = 0;
804 wine_server_add_data( req, context, sizeof(*context) );
805 ret = wine_server_call( req );
806 self = reply->self;
808 SERVER_END_REQ;
810 if (ret == STATUS_PENDING)
812 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
814 for (i = 0; i < 100; i++)
816 SERVER_START_REQ( set_thread_context )
818 req->handle = handle;
819 req->flags = context->ContextFlags;
820 req->suspend = 0;
821 wine_server_add_data( req, context, sizeof(*context) );
822 ret = wine_server_call( req );
824 SERVER_END_REQ;
825 if (ret == STATUS_PENDING)
827 LARGE_INTEGER timeout;
828 timeout.QuadPart = -10000;
829 NtDelayExecution( FALSE, &timeout );
831 else break;
833 NtResumeThread( handle, &dummy );
835 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
838 if (ret) return ret;
841 if (self) set_cpu_context( context );
842 return STATUS_SUCCESS;
846 /* copy a context structure according to the flags */
847 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
849 #ifdef __i386__
850 flags &= ~CONTEXT_i386; /* get rid of CPU id */
851 if (flags & CONTEXT_INTEGER)
853 to->Eax = from->Eax;
854 to->Ebx = from->Ebx;
855 to->Ecx = from->Ecx;
856 to->Edx = from->Edx;
857 to->Esi = from->Esi;
858 to->Edi = from->Edi;
860 if (flags & CONTEXT_CONTROL)
862 to->Ebp = from->Ebp;
863 to->Esp = from->Esp;
864 to->Eip = from->Eip;
865 to->SegCs = from->SegCs;
866 to->SegSs = from->SegSs;
867 to->EFlags = from->EFlags;
869 if (flags & CONTEXT_SEGMENTS)
871 to->SegDs = from->SegDs;
872 to->SegEs = from->SegEs;
873 to->SegFs = from->SegFs;
874 to->SegGs = from->SegGs;
876 if (flags & CONTEXT_DEBUG_REGISTERS)
878 to->Dr0 = from->Dr0;
879 to->Dr1 = from->Dr1;
880 to->Dr2 = from->Dr2;
881 to->Dr3 = from->Dr3;
882 to->Dr6 = from->Dr6;
883 to->Dr7 = from->Dr7;
885 if (flags & CONTEXT_FLOATING_POINT)
887 to->FloatSave = from->FloatSave;
889 if (flags & CONTEXT_EXTENDED_REGISTERS)
891 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
893 #elif defined(__x86_64__)
894 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
895 if (flags & CONTEXT_CONTROL)
897 to->Rbp = from->Rbp;
898 to->Rip = from->Rip;
899 to->Rsp = from->Rsp;
900 to->SegCs = from->SegCs;
901 to->SegSs = from->SegSs;
902 to->EFlags = from->EFlags;
903 to->MxCsr = from->MxCsr;
905 if (flags & CONTEXT_INTEGER)
907 to->Rax = from->Rax;
908 to->Rcx = from->Rcx;
909 to->Rdx = from->Rdx;
910 to->Rbx = from->Rbx;
911 to->Rsi = from->Rsi;
912 to->Rdi = from->Rdi;
913 to->R8 = from->R8;
914 to->R9 = from->R9;
915 to->R10 = from->R10;
916 to->R11 = from->R11;
917 to->R12 = from->R12;
918 to->R13 = from->R13;
919 to->R14 = from->R14;
920 to->R15 = from->R15;
922 if (flags & CONTEXT_SEGMENTS)
924 to->SegDs = from->SegDs;
925 to->SegEs = from->SegEs;
926 to->SegFs = from->SegFs;
927 to->SegGs = from->SegGs;
929 if (flags & CONTEXT_FLOATING_POINT)
931 to->u.FltSave = from->u.FltSave;
933 if (flags & CONTEXT_DEBUG_REGISTERS)
935 to->Dr0 = from->Dr0;
936 to->Dr1 = from->Dr1;
937 to->Dr2 = from->Dr2;
938 to->Dr3 = from->Dr3;
939 to->Dr6 = from->Dr6;
940 to->Dr7 = from->Dr7;
942 #elif defined(__sparc__)
943 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
944 if (flags & CONTEXT_CONTROL)
946 to->psr = from->psr;
947 to->pc = from->pc;
948 to->npc = from->npc;
949 to->y = from->y;
950 to->wim = from->wim;
951 to->tbr = from->tbr;
953 if (flags & CONTEXT_INTEGER)
955 to->g0 = from->g0;
956 to->g1 = from->g1;
957 to->g2 = from->g2;
958 to->g3 = from->g3;
959 to->g4 = from->g4;
960 to->g5 = from->g5;
961 to->g6 = from->g6;
962 to->g7 = from->g7;
963 to->o0 = from->o0;
964 to->o1 = from->o1;
965 to->o2 = from->o2;
966 to->o3 = from->o3;
967 to->o4 = from->o4;
968 to->o5 = from->o5;
969 to->o6 = from->o6;
970 to->o7 = from->o7;
971 to->l0 = from->l0;
972 to->l1 = from->l1;
973 to->l2 = from->l2;
974 to->l3 = from->l3;
975 to->l4 = from->l4;
976 to->l5 = from->l5;
977 to->l6 = from->l6;
978 to->l7 = from->l7;
979 to->i0 = from->i0;
980 to->i1 = from->i1;
981 to->i2 = from->i2;
982 to->i3 = from->i3;
983 to->i4 = from->i4;
984 to->i5 = from->i5;
985 to->i6 = from->i6;
986 to->i7 = from->i7;
988 if (flags & CONTEXT_FLOATING_POINT)
990 /* FIXME */
992 #elif defined(__powerpc__)
993 /* Has no CPU id */
994 if (flags & CONTEXT_CONTROL)
996 to->Msr = from->Msr;
997 to->Ctr = from->Ctr;
998 to->Iar = from->Iar;
1000 if (flags & CONTEXT_INTEGER)
1002 to->Gpr0 = from->Gpr0;
1003 to->Gpr1 = from->Gpr1;
1004 to->Gpr2 = from->Gpr2;
1005 to->Gpr3 = from->Gpr3;
1006 to->Gpr4 = from->Gpr4;
1007 to->Gpr5 = from->Gpr5;
1008 to->Gpr6 = from->Gpr6;
1009 to->Gpr7 = from->Gpr7;
1010 to->Gpr8 = from->Gpr8;
1011 to->Gpr9 = from->Gpr9;
1012 to->Gpr10 = from->Gpr10;
1013 to->Gpr11 = from->Gpr11;
1014 to->Gpr12 = from->Gpr12;
1015 to->Gpr13 = from->Gpr13;
1016 to->Gpr14 = from->Gpr14;
1017 to->Gpr15 = from->Gpr15;
1018 to->Gpr16 = from->Gpr16;
1019 to->Gpr17 = from->Gpr17;
1020 to->Gpr18 = from->Gpr18;
1021 to->Gpr19 = from->Gpr19;
1022 to->Gpr20 = from->Gpr20;
1023 to->Gpr21 = from->Gpr21;
1024 to->Gpr22 = from->Gpr22;
1025 to->Gpr23 = from->Gpr23;
1026 to->Gpr24 = from->Gpr24;
1027 to->Gpr25 = from->Gpr25;
1028 to->Gpr26 = from->Gpr26;
1029 to->Gpr27 = from->Gpr27;
1030 to->Gpr28 = from->Gpr28;
1031 to->Gpr29 = from->Gpr29;
1032 to->Gpr30 = from->Gpr30;
1033 to->Gpr31 = from->Gpr31;
1034 to->Xer = from->Xer;
1035 to->Cr = from->Cr;
1037 if (flags & CONTEXT_FLOATING_POINT)
1039 to->Fpr0 = from->Fpr0;
1040 to->Fpr1 = from->Fpr1;
1041 to->Fpr2 = from->Fpr2;
1042 to->Fpr3 = from->Fpr3;
1043 to->Fpr4 = from->Fpr4;
1044 to->Fpr5 = from->Fpr5;
1045 to->Fpr6 = from->Fpr6;
1046 to->Fpr7 = from->Fpr7;
1047 to->Fpr8 = from->Fpr8;
1048 to->Fpr9 = from->Fpr9;
1049 to->Fpr10 = from->Fpr10;
1050 to->Fpr11 = from->Fpr11;
1051 to->Fpr12 = from->Fpr12;
1052 to->Fpr13 = from->Fpr13;
1053 to->Fpr14 = from->Fpr14;
1054 to->Fpr15 = from->Fpr15;
1055 to->Fpr16 = from->Fpr16;
1056 to->Fpr17 = from->Fpr17;
1057 to->Fpr18 = from->Fpr18;
1058 to->Fpr19 = from->Fpr19;
1059 to->Fpr20 = from->Fpr20;
1060 to->Fpr21 = from->Fpr21;
1061 to->Fpr22 = from->Fpr22;
1062 to->Fpr23 = from->Fpr23;
1063 to->Fpr24 = from->Fpr24;
1064 to->Fpr25 = from->Fpr25;
1065 to->Fpr26 = from->Fpr26;
1066 to->Fpr27 = from->Fpr27;
1067 to->Fpr28 = from->Fpr28;
1068 to->Fpr29 = from->Fpr29;
1069 to->Fpr30 = from->Fpr30;
1070 to->Fpr31 = from->Fpr31;
1071 to->Fpscr = from->Fpscr;
1073 #else
1074 #error You must implement context copying for your CPU
1075 #endif
1079 /***********************************************************************
1080 * NtGetContextThread (NTDLL.@)
1081 * ZwGetContextThread (NTDLL.@)
1083 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
1085 NTSTATUS ret;
1086 CONTEXT ctx;
1087 DWORD dummy, i;
1088 DWORD needed_flags = context->ContextFlags;
1089 BOOL self = (handle == GetCurrentThread());
1091 #ifdef __i386__
1092 /* on i386 debug registers always require a server call */
1093 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)) self = FALSE;
1094 #endif
1096 if (!self)
1098 SERVER_START_REQ( get_thread_context )
1100 req->handle = handle;
1101 req->flags = context->ContextFlags;
1102 req->suspend = 0;
1103 wine_server_set_reply( req, &ctx, sizeof(ctx) );
1104 ret = wine_server_call( req );
1105 self = reply->self;
1107 SERVER_END_REQ;
1109 if (ret == STATUS_PENDING)
1111 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
1113 for (i = 0; i < 100; i++)
1115 SERVER_START_REQ( get_thread_context )
1117 req->handle = handle;
1118 req->flags = context->ContextFlags;
1119 req->suspend = 0;
1120 wine_server_set_reply( req, &ctx, sizeof(ctx) );
1121 ret = wine_server_call( req );
1123 SERVER_END_REQ;
1124 if (ret == STATUS_PENDING)
1126 LARGE_INTEGER timeout;
1127 timeout.QuadPart = -10000;
1128 NtDelayExecution( FALSE, &timeout );
1130 else break;
1132 NtResumeThread( handle, &dummy );
1134 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
1136 if (ret) return ret;
1137 copy_context( context, &ctx, context->ContextFlags & ctx.ContextFlags );
1138 needed_flags &= ~ctx.ContextFlags;
1141 if (self)
1143 if (needed_flags)
1145 get_cpu_context( &ctx );
1146 copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
1148 #ifdef __i386__
1149 /* update the cached version of the debug registers */
1150 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1152 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1153 regs->dr0 = context->Dr0;
1154 regs->dr1 = context->Dr1;
1155 regs->dr2 = context->Dr2;
1156 regs->dr3 = context->Dr3;
1157 regs->dr6 = context->Dr6;
1158 regs->dr7 = context->Dr7;
1160 #endif
1162 return STATUS_SUCCESS;
1166 /******************************************************************************
1167 * NtQueryInformationThread (NTDLL.@)
1168 * ZwQueryInformationThread (NTDLL.@)
1170 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
1171 void *data, ULONG length, ULONG *ret_len )
1173 NTSTATUS status;
1175 switch(class)
1177 case ThreadBasicInformation:
1179 THREAD_BASIC_INFORMATION info;
1180 const unsigned int affinity_mask = (1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
1182 SERVER_START_REQ( get_thread_info )
1184 req->handle = handle;
1185 req->tid_in = 0;
1186 if (!(status = wine_server_call( req )))
1188 info.ExitStatus = reply->exit_code;
1189 info.TebBaseAddress = reply->teb;
1190 info.ClientId.UniqueProcess = ULongToHandle(reply->pid);
1191 info.ClientId.UniqueThread = ULongToHandle(reply->tid);
1192 info.AffinityMask = reply->affinity & affinity_mask;
1193 info.Priority = reply->priority;
1194 info.BasePriority = reply->priority; /* FIXME */
1197 SERVER_END_REQ;
1198 if (status == STATUS_SUCCESS)
1200 if (data) memcpy( data, &info, min( length, sizeof(info) ));
1201 if (ret_len) *ret_len = min( length, sizeof(info) );
1204 return status;
1205 case ThreadTimes:
1207 KERNEL_USER_TIMES kusrt;
1208 /* We need to do a server call to get the creation time or exit time */
1209 /* This works on any thread */
1210 SERVER_START_REQ( get_thread_info )
1212 req->handle = handle;
1213 req->tid_in = 0;
1214 status = wine_server_call( req );
1215 if (status == STATUS_SUCCESS)
1217 kusrt.CreateTime.QuadPart = reply->creation_time;
1218 kusrt.ExitTime.QuadPart = reply->exit_time;
1221 SERVER_END_REQ;
1222 if (status == STATUS_SUCCESS)
1224 /* We call times(2) for kernel time or user time */
1225 /* We can only (portably) do this for the current thread */
1226 if (handle == GetCurrentThread())
1228 struct tms time_buf;
1229 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1231 times(&time_buf);
1232 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1233 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1235 else
1237 static BOOL reported = FALSE;
1239 kusrt.KernelTime.QuadPart = 0;
1240 kusrt.UserTime.QuadPart = 0;
1241 if (reported)
1242 TRACE("Cannot get kerneltime or usertime of other threads\n");
1243 else
1245 FIXME("Cannot get kerneltime or usertime of other threads\n");
1246 reported = TRUE;
1249 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1250 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1253 return status;
1254 case ThreadDescriptorTableEntry:
1256 #ifdef __i386__
1257 THREAD_DESCRIPTOR_INFORMATION* tdi = data;
1258 if (length < sizeof(*tdi))
1259 status = STATUS_INFO_LENGTH_MISMATCH;
1260 else if (!(tdi->Selector & 4)) /* GDT selector */
1262 unsigned sel = tdi->Selector & ~3; /* ignore RPL */
1263 status = STATUS_SUCCESS;
1264 if (!sel) /* null selector */
1265 memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1266 else
1268 tdi->Entry.BaseLow = 0;
1269 tdi->Entry.HighWord.Bits.BaseMid = 0;
1270 tdi->Entry.HighWord.Bits.BaseHi = 0;
1271 tdi->Entry.LimitLow = 0xffff;
1272 tdi->Entry.HighWord.Bits.LimitHi = 0xf;
1273 tdi->Entry.HighWord.Bits.Dpl = 3;
1274 tdi->Entry.HighWord.Bits.Sys = 0;
1275 tdi->Entry.HighWord.Bits.Pres = 1;
1276 tdi->Entry.HighWord.Bits.Granularity = 1;
1277 tdi->Entry.HighWord.Bits.Default_Big = 1;
1278 tdi->Entry.HighWord.Bits.Type = 0x12;
1279 /* it has to be one of the system GDT selectors */
1280 if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1282 if (sel == (wine_get_cs() & ~3))
1283 tdi->Entry.HighWord.Bits.Type |= 8; /* code segment */
1284 else status = STATUS_ACCESS_DENIED;
1288 else
1290 SERVER_START_REQ( get_selector_entry )
1292 req->handle = handle;
1293 req->entry = tdi->Selector >> 3;
1294 status = wine_server_call( req );
1295 if (!status)
1297 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1298 status = STATUS_ACCESS_VIOLATION;
1299 else
1301 wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1302 wine_ldt_set_limit( &tdi->Entry, reply->limit );
1303 wine_ldt_set_flags( &tdi->Entry, reply->flags );
1307 SERVER_END_REQ;
1309 if (status == STATUS_SUCCESS && ret_len)
1310 /* yes, that's a bit strange, but it's the way it is */
1311 *ret_len = sizeof(LDT_ENTRY);
1312 #else
1313 status = STATUS_NOT_IMPLEMENTED;
1314 #endif
1315 return status;
1317 case ThreadAmILastThread:
1319 SERVER_START_REQ(get_thread_info)
1321 req->handle = handle;
1322 req->tid_in = 0;
1323 status = wine_server_call( req );
1324 if (status == STATUS_SUCCESS)
1326 BOOLEAN last = reply->last;
1327 if (data) memcpy( data, &last, min( length, sizeof(last) ));
1328 if (ret_len) *ret_len = min( length, sizeof(last) );
1331 SERVER_END_REQ;
1332 return status;
1334 case ThreadPriority:
1335 case ThreadBasePriority:
1336 case ThreadAffinityMask:
1337 case ThreadImpersonationToken:
1338 case ThreadEnableAlignmentFaultFixup:
1339 case ThreadEventPair_Reusable:
1340 case ThreadQuerySetWin32StartAddress:
1341 case ThreadZeroTlsCell:
1342 case ThreadPerformanceCount:
1343 case ThreadIdealProcessor:
1344 case ThreadPriorityBoost:
1345 case ThreadSetTlsArrayAddress:
1346 case ThreadIsIoPending:
1347 default:
1348 FIXME( "info class %d not supported yet\n", class );
1349 return STATUS_NOT_IMPLEMENTED;
1354 /******************************************************************************
1355 * NtSetInformationThread (NTDLL.@)
1356 * ZwSetInformationThread (NTDLL.@)
1358 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1359 LPCVOID data, ULONG length )
1361 NTSTATUS status;
1362 switch(class)
1364 case ThreadZeroTlsCell:
1365 if (handle == GetCurrentThread())
1367 LIST_ENTRY *entry;
1368 DWORD index;
1370 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1371 index = *(const DWORD *)data;
1372 if (index < TLS_MINIMUM_AVAILABLE)
1374 RtlAcquirePebLock();
1375 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1377 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1378 teb->TlsSlots[index] = 0;
1380 RtlReleasePebLock();
1382 else
1384 index -= TLS_MINIMUM_AVAILABLE;
1385 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1386 return STATUS_INVALID_PARAMETER;
1387 RtlAcquirePebLock();
1388 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1390 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1391 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1393 RtlReleasePebLock();
1395 return STATUS_SUCCESS;
1397 FIXME( "ZeroTlsCell not supported on other threads\n" );
1398 return STATUS_NOT_IMPLEMENTED;
1400 case ThreadImpersonationToken:
1402 const HANDLE *phToken = data;
1403 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1404 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1405 SERVER_START_REQ( set_thread_info )
1407 req->handle = handle;
1408 req->token = *phToken;
1409 req->mask = SET_THREAD_INFO_TOKEN;
1410 status = wine_server_call( req );
1412 SERVER_END_REQ;
1414 return status;
1415 case ThreadBasePriority:
1417 const DWORD *pprio = data;
1418 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1419 SERVER_START_REQ( set_thread_info )
1421 req->handle = handle;
1422 req->priority = *pprio;
1423 req->mask = SET_THREAD_INFO_PRIORITY;
1424 status = wine_server_call( req );
1426 SERVER_END_REQ;
1428 return status;
1429 case ThreadAffinityMask:
1431 const DWORD affinity_mask = (1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
1432 const DWORD *paff = data;
1433 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1434 if (*paff & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1435 SERVER_START_REQ( set_thread_info )
1437 req->handle = handle;
1438 req->affinity = *paff;
1439 req->mask = SET_THREAD_INFO_AFFINITY;
1440 status = wine_server_call( req );
1442 SERVER_END_REQ;
1444 return status;
1445 case ThreadBasicInformation:
1446 case ThreadTimes:
1447 case ThreadPriority:
1448 case ThreadDescriptorTableEntry:
1449 case ThreadEnableAlignmentFaultFixup:
1450 case ThreadEventPair_Reusable:
1451 case ThreadQuerySetWin32StartAddress:
1452 case ThreadPerformanceCount:
1453 case ThreadAmILastThread:
1454 case ThreadIdealProcessor:
1455 case ThreadPriorityBoost:
1456 case ThreadSetTlsArrayAddress:
1457 case ThreadIsIoPending:
1458 default:
1459 FIXME( "info class %d not supported yet\n", class );
1460 return STATUS_NOT_IMPLEMENTED;
1465 /**********************************************************************
1466 * NtCurrentTeb (NTDLL.@)
1468 #if defined(__i386__) && defined(__GNUC__)
1470 __ASM_GLOBAL_FUNC( NtCurrentTeb, ".byte 0x64\n\tmovl 0x18,%eax\n\tret" )
1472 #elif defined(__i386__) && defined(_MSC_VER)
1474 /* Nothing needs to be done. MS C "magically" exports the inline version from winnt.h */
1476 #else
1478 /**********************************************************************/
1480 TEB * WINAPI NtCurrentTeb(void)
1482 return pthread_functions.get_current_teb();
1485 #endif /* __i386__ */