shell32: Update Lithuanian translation.
[wine/multimedia.git] / dlls / ntdll / thread.c
blob87b9d57797890cc7f3d237936192e529fa0c053d
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 <sys/types.h>
27 #ifdef HAVE_SYS_MMAN_H
28 #include <sys/mman.h>
29 #endif
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
32 #endif
34 #define NONAMELESSUNION
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
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 PUNHANDLED_EXCEPTION_FILTER unhandled_exception_filter = NULL;
53 /* info passed to a starting thread */
54 struct startup_info
56 TEB *teb;
57 PRTL_THREAD_START_ROUTINE entry_point;
58 void *entry_arg;
61 static PEB_LDR_DATA ldr;
62 static RTL_USER_PROCESS_PARAMETERS params; /* default parameters if no parent */
63 static WCHAR current_dir[MAX_NT_PATH_LENGTH];
64 static RTL_BITMAP tls_bitmap;
65 static RTL_BITMAP tls_expansion_bitmap;
66 static RTL_BITMAP fls_bitmap;
67 static LIST_ENTRY tls_links;
68 static size_t sigstack_total_size;
69 static ULONG sigstack_zero_bits;
70 static int nb_threads = 1;
72 static struct wine_pthread_functions pthread_functions;
75 static RTL_CRITICAL_SECTION ldt_section;
76 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
78 0, 0, &ldt_section,
79 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
80 0, 0, { (DWORD_PTR)(__FILE__ ": ldt_section") }
82 static RTL_CRITICAL_SECTION ldt_section = { &critsect_debug, -1, 0, 0, 0, 0 };
83 static sigset_t ldt_sigset;
85 /***********************************************************************
86 * locking for LDT routines
88 static void ldt_lock(void)
90 sigset_t sigset;
92 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
93 RtlEnterCriticalSection( &ldt_section );
94 if (ldt_section.RecursionCount == 1) ldt_sigset = sigset;
97 static void ldt_unlock(void)
99 if (ldt_section.RecursionCount == 1)
101 sigset_t sigset = ldt_sigset;
102 RtlLeaveCriticalSection( &ldt_section );
103 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
105 else RtlLeaveCriticalSection( &ldt_section );
109 /***********************************************************************
110 * init_teb
112 static inline NTSTATUS init_teb( TEB *teb )
114 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
116 teb->Tib.ExceptionList = (void *)~0UL;
117 teb->Tib.StackBase = (void *)~0UL;
118 teb->Tib.Self = &teb->Tib;
119 teb->StaticUnicodeString.Buffer = teb->StaticUnicodeBuffer;
120 teb->StaticUnicodeString.MaximumLength = sizeof(teb->StaticUnicodeBuffer);
122 if (!(thread_data->fs = wine_ldt_alloc_fs())) return STATUS_TOO_MANY_THREADS;
123 thread_data->request_fd = -1;
124 thread_data->reply_fd = -1;
125 thread_data->wait_fd[0] = -1;
126 thread_data->wait_fd[1] = -1;
128 return STATUS_SUCCESS;
132 /***********************************************************************
133 * fix_unicode_string
135 * Make sure the unicode string doesn't point beyond the end pointer
137 static inline void fix_unicode_string( UNICODE_STRING *str, const char *end_ptr )
139 if ((char *)str->Buffer >= end_ptr)
141 str->Length = str->MaximumLength = 0;
142 str->Buffer = NULL;
143 return;
145 if ((char *)str->Buffer + str->MaximumLength > end_ptr)
147 str->MaximumLength = (end_ptr - (char *)str->Buffer) & ~(sizeof(WCHAR) - 1);
149 if (str->Length >= str->MaximumLength)
151 if (str->MaximumLength >= sizeof(WCHAR))
152 str->Length = str->MaximumLength - sizeof(WCHAR);
153 else
154 str->Length = str->MaximumLength = 0;
159 /***********************************************************************
160 * init_user_process_params
162 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
164 static NTSTATUS init_user_process_params( SIZE_T info_size, HANDLE *exe_file )
166 void *ptr;
167 SIZE_T env_size;
168 NTSTATUS status;
169 RTL_USER_PROCESS_PARAMETERS *params = NULL;
171 status = NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&params, 0, &info_size,
172 MEM_COMMIT, PAGE_READWRITE );
173 if (status != STATUS_SUCCESS) return status;
175 params->AllocationSize = info_size;
176 NtCurrentTeb()->Peb->ProcessParameters = params;
178 SERVER_START_REQ( get_startup_info )
180 wine_server_set_reply( req, params, info_size );
181 if (!(status = wine_server_call( req )))
183 info_size = wine_server_reply_size( reply );
184 *exe_file = wine_server_ptr_handle( reply->exe_file );
185 params->hStdInput = wine_server_ptr_handle( reply->hstdin );
186 params->hStdOutput = wine_server_ptr_handle( reply->hstdout );
187 params->hStdError = wine_server_ptr_handle( reply->hstderr );
190 SERVER_END_REQ;
191 if (status != STATUS_SUCCESS) return status;
193 if (params->Size > info_size) params->Size = info_size;
195 /* make sure the strings are valid */
196 fix_unicode_string( &params->CurrentDirectory.DosPath, (char *)info_size );
197 fix_unicode_string( &params->DllPath, (char *)info_size );
198 fix_unicode_string( &params->ImagePathName, (char *)info_size );
199 fix_unicode_string( &params->CommandLine, (char *)info_size );
200 fix_unicode_string( &params->WindowTitle, (char *)info_size );
201 fix_unicode_string( &params->Desktop, (char *)info_size );
202 fix_unicode_string( &params->ShellInfo, (char *)info_size );
203 fix_unicode_string( &params->RuntimeInfo, (char *)info_size );
205 /* environment needs to be a separate memory block */
206 env_size = info_size - params->Size;
207 if (!env_size) env_size = 1;
208 ptr = NULL;
209 status = NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0, &env_size,
210 MEM_COMMIT, PAGE_READWRITE );
211 if (status != STATUS_SUCCESS) return status;
212 memcpy( ptr, (char *)params + params->Size, info_size - params->Size );
213 params->Environment = ptr;
215 RtlNormalizeProcessParams( params );
216 return status;
220 /***********************************************************************
221 * thread_init
223 * Setup the initial thread.
225 * NOTES: The first allocated TEB on NT is at 0x7ffde000.
227 HANDLE thread_init(void)
229 PEB *peb;
230 TEB *teb;
231 void *addr;
232 SIZE_T size, info_size;
233 HANDLE exe_file = 0;
234 LARGE_INTEGER now;
235 struct ntdll_thread_data *thread_data;
236 static struct debug_info debug_info; /* debug info for initial thread */
238 virtual_init();
240 /* reserve space for shared user data */
242 addr = (void *)0x7ffe0000;
243 size = 0x10000;
244 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE );
245 user_shared_data = addr;
247 /* allocate and initialize the PEB */
249 addr = NULL;
250 size = sizeof(*peb);
251 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 1, &size,
252 MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
253 peb = addr;
255 peb->NumberOfProcessors = 1;
256 peb->ProcessParameters = &params;
257 peb->TlsBitmap = &tls_bitmap;
258 peb->TlsExpansionBitmap = &tls_expansion_bitmap;
259 peb->FlsBitmap = &fls_bitmap;
260 peb->LdrData = &ldr;
261 params.CurrentDirectory.DosPath.Buffer = current_dir;
262 params.CurrentDirectory.DosPath.MaximumLength = sizeof(current_dir);
263 params.wShowWindow = 1; /* SW_SHOWNORMAL */
264 RtlInitializeBitMap( &tls_bitmap, peb->TlsBitmapBits, sizeof(peb->TlsBitmapBits) * 8 );
265 RtlInitializeBitMap( &tls_expansion_bitmap, peb->TlsExpansionBitmapBits,
266 sizeof(peb->TlsExpansionBitmapBits) * 8 );
267 RtlInitializeBitMap( &fls_bitmap, peb->FlsBitmapBits, sizeof(peb->FlsBitmapBits) * 8 );
268 InitializeListHead( &peb->FlsListHead );
269 InitializeListHead( &ldr.InLoadOrderModuleList );
270 InitializeListHead( &ldr.InMemoryOrderModuleList );
271 InitializeListHead( &ldr.InInitializationOrderModuleList );
272 InitializeListHead( &tls_links );
274 /* allocate and initialize the initial TEB */
276 sigstack_total_size = get_signal_stack_total_size();
277 while (1U << sigstack_zero_bits < sigstack_total_size) sigstack_zero_bits++;
278 assert( 1U << sigstack_zero_bits == sigstack_total_size ); /* must be a power of 2 */
279 assert( sigstack_total_size >= sizeof(TEB) + sizeof(struct startup_info) );
281 addr = NULL;
282 size = sigstack_total_size;
283 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
284 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
285 teb = addr;
286 teb->Peb = peb;
287 init_teb( teb );
288 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
289 thread_data->debug_info = &debug_info;
290 InsertHeadList( &tls_links, &teb->TlsLinks );
292 wine_pthread_get_functions( &pthread_functions, sizeof(pthread_functions) );
293 signal_init_thread( teb );
294 virtual_init_threading();
296 debug_info.str_pos = debug_info.strings;
297 debug_info.out_pos = debug_info.output;
298 debug_init();
300 /* setup the server connection */
301 server_init_process();
302 info_size = server_init_thread( NULL );
304 /* create the process heap */
305 if (!(peb->ProcessHeap = RtlCreateHeap( HEAP_GROWABLE, NULL, 0, 0, NULL, NULL )))
307 MESSAGE( "wine: failed to create the process heap\n" );
308 exit(1);
311 /* allocate user parameters */
312 if (info_size)
314 init_user_process_params( info_size, &exe_file );
316 else
318 /* This is wine specific: we have no parent (we're started from unix)
319 * so, create a simple console with bare handles to unix stdio
321 wine_server_fd_to_handle( 0, GENERIC_READ|SYNCHRONIZE, OBJ_INHERIT, &params.hStdInput );
322 wine_server_fd_to_handle( 1, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdOutput );
323 wine_server_fd_to_handle( 2, GENERIC_WRITE|SYNCHRONIZE, OBJ_INHERIT, &params.hStdError );
326 /* initialize LDT locking */
327 wine_ldt_init_locking( ldt_lock, ldt_unlock );
329 /* initialize time values in user_shared_data */
330 NtQuerySystemTime( &now );
331 user_shared_data->SystemTime.LowPart = now.u.LowPart;
332 user_shared_data->SystemTime.High1Time = user_shared_data->SystemTime.High2Time = now.u.HighPart;
333 user_shared_data->u.TickCountQuad = (now.QuadPart - server_start_time) / 10000;
334 user_shared_data->u.TickCount.High2Time = user_shared_data->u.TickCount.High1Time;
335 user_shared_data->TickCountLowDeprecated = user_shared_data->u.TickCount.LowPart;
336 user_shared_data->TickCountMultiplier = 1 << 24;
338 pthread_init();
339 return exe_file;
343 /***********************************************************************
344 * abort_thread
346 void abort_thread( int status )
348 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
349 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) _exit( status );
351 close( ntdll_get_thread_data()->wait_fd[0] );
352 close( ntdll_get_thread_data()->wait_fd[1] );
353 close( ntdll_get_thread_data()->reply_fd );
354 close( ntdll_get_thread_data()->request_fd );
355 pthread_exit( UIntToPtr(status) );
359 /***********************************************************************
360 * exit_thread
362 static void DECLSPEC_NORETURN exit_thread( int status )
364 static void *prev_teb;
365 TEB *teb;
367 RtlAcquirePebLock();
368 RemoveEntryList( &NtCurrentTeb()->TlsLinks );
369 RtlReleasePebLock();
370 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots );
371 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots );
373 pthread_sigmask( SIG_BLOCK, &server_block_set, NULL );
374 if (interlocked_xchg_add( &nb_threads, -1 ) <= 1) exit( status );
376 if ((teb = interlocked_xchg_ptr( &prev_teb, NtCurrentTeb() )))
378 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
379 SIZE_T size;
381 pthread_join( thread_data->pthread_id, NULL );
382 wine_ldt_free_fs( thread_data->fs );
383 size = 0;
384 NtFreeVirtualMemory( GetCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
385 size = 0;
386 NtFreeVirtualMemory( GetCurrentProcess(), (void **)&teb, &size, MEM_RELEASE );
389 close( ntdll_get_thread_data()->wait_fd[0] );
390 close( ntdll_get_thread_data()->wait_fd[1] );
391 close( ntdll_get_thread_data()->reply_fd );
392 close( ntdll_get_thread_data()->request_fd );
393 pthread_exit( UIntToPtr(status) );
397 #ifdef __i386__
398 /* wrapper for apps that don't declare the thread function correctly */
399 extern DWORD call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry, void *arg );
400 __ASM_GLOBAL_FUNC(call_thread_entry_point,
401 "pushl %ebp\n\t"
402 "movl %esp,%ebp\n\t"
403 "subl $4,%esp\n\t"
404 "pushl 12(%ebp)\n\t"
405 "movl 8(%ebp),%eax\n\t"
406 "call *%eax\n\t"
407 "leave\n\t"
408 "ret" )
409 #else
410 static inline DWORD call_thread_entry_point( PRTL_THREAD_START_ROUTINE entry, void *arg )
412 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)entry;
413 return func( arg );
415 #endif
417 /***********************************************************************
418 * call_thread_func
420 * Hack to make things compatible with the thread procedures used by kernel32.CreateThread.
422 static void DECLSPEC_NORETURN call_thread_func( PRTL_THREAD_START_ROUTINE rtl_func, void *arg )
424 DWORD exit_code;
425 BOOL last;
427 MODULE_DllThreadAttach( NULL );
429 if (TRACE_ON(relay))
430 DPRINTF( "%04x:Starting thread proc %p (arg=%p)\n", GetCurrentThreadId(), rtl_func, arg );
432 exit_code = call_thread_entry_point( rtl_func, arg );
434 /* send the exit code to the server */
435 SERVER_START_REQ( terminate_thread )
437 req->handle = wine_server_obj_handle( GetCurrentThread() );
438 req->exit_code = exit_code;
439 wine_server_call( req );
440 last = reply->last;
442 SERVER_END_REQ;
444 if (last)
446 LdrShutdownProcess();
447 exit( exit_code );
449 else
451 LdrShutdownThread();
452 exit_thread( exit_code );
457 /***********************************************************************
458 * start_thread
460 * Startup routine for a newly created thread.
462 static void start_thread( struct startup_info *info )
464 TEB *teb = info->teb;
465 struct ntdll_thread_data *thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
466 PRTL_THREAD_START_ROUTINE func = info->entry_point;
467 void *arg = info->entry_arg;
468 struct debug_info debug_info;
470 debug_info.str_pos = debug_info.strings;
471 debug_info.out_pos = debug_info.output;
472 thread_data->debug_info = &debug_info;
473 thread_data->pthread_id = pthread_self();
475 signal_init_thread( teb );
476 server_init_thread( func );
477 pthread_sigmask( SIG_UNBLOCK, &server_block_set, NULL );
479 RtlAcquirePebLock();
480 InsertHeadList( &tls_links, &teb->TlsLinks );
481 RtlReleasePebLock();
483 /* NOTE: Windows does not have an exception handler around the call to
484 * the thread attach. We do for ease of debugging */
485 if (unhandled_exception_filter)
487 __TRY
489 call_thread_func( func, arg );
491 __EXCEPT(unhandled_exception_filter)
493 NtTerminateThread( GetCurrentThread(), GetExceptionCode() );
495 __ENDTRY
497 else
498 call_thread_func( func, arg );
502 /***********************************************************************
503 * RtlCreateUserThread (NTDLL.@)
505 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, const SECURITY_DESCRIPTOR *descr,
506 BOOLEAN suspended, PVOID stack_addr,
507 SIZE_T stack_reserve, SIZE_T stack_commit,
508 PRTL_THREAD_START_ROUTINE start, void *param,
509 HANDLE *handle_ptr, CLIENT_ID *id )
511 sigset_t sigset;
512 pthread_t pthread_id;
513 pthread_attr_t attr;
514 struct ntdll_thread_data *thread_data = NULL;
515 struct ntdll_thread_regs *thread_regs;
516 struct startup_info *info = NULL;
517 void *addr = NULL;
518 HANDLE handle = 0;
519 TEB *teb;
520 DWORD tid = 0;
521 int request_pipe[2];
522 NTSTATUS status;
523 SIZE_T size;
525 if (process != NtCurrentProcess())
527 apc_call_t call;
528 apc_result_t result;
530 memset( &call, 0, sizeof(call) );
532 call.create_thread.type = APC_CREATE_THREAD;
533 call.create_thread.func = wine_server_client_ptr( start );
534 call.create_thread.arg = wine_server_client_ptr( param );
535 call.create_thread.reserve = stack_reserve;
536 call.create_thread.commit = stack_commit;
537 call.create_thread.suspend = suspended;
538 status = NTDLL_queue_process_apc( process, &call, &result );
539 if (status != STATUS_SUCCESS) return status;
541 if (result.create_thread.status == STATUS_SUCCESS)
543 if (id) id->UniqueThread = ULongToHandle(result.create_thread.tid);
544 if (handle_ptr) *handle_ptr = wine_server_ptr_handle( result.create_thread.handle );
545 else NtClose( wine_server_ptr_handle( result.create_thread.handle ));
547 return result.create_thread.status;
550 if (pipe( request_pipe ) == -1) return STATUS_TOO_MANY_OPENED_FILES;
551 fcntl( request_pipe[1], F_SETFD, 1 ); /* set close on exec flag */
552 wine_server_send_fd( request_pipe[0] );
554 SERVER_START_REQ( new_thread )
556 req->access = THREAD_ALL_ACCESS;
557 req->attributes = 0; /* FIXME */
558 req->suspend = suspended;
559 req->request_fd = request_pipe[0];
560 if (!(status = wine_server_call( req )))
562 handle = wine_server_ptr_handle( reply->handle );
563 tid = reply->tid;
565 close( request_pipe[0] );
567 SERVER_END_REQ;
569 if (status)
571 close( request_pipe[1] );
572 return status;
575 pthread_sigmask( SIG_BLOCK, &server_block_set, &sigset );
577 addr = NULL;
578 size = sigstack_total_size;
579 if ((status = NtAllocateVirtualMemory( NtCurrentProcess(), &addr, sigstack_zero_bits,
580 &size, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE )))
581 goto error;
582 teb = addr;
583 teb->Peb = NtCurrentTeb()->Peb;
584 info = (struct startup_info *)(teb + 1);
585 info->teb = teb;
586 info->entry_point = start;
587 info->entry_arg = param;
589 if ((status = init_teb( teb ))) goto error;
591 teb->ClientId.UniqueProcess = ULongToHandle(GetCurrentProcessId());
592 teb->ClientId.UniqueThread = ULongToHandle(tid);
594 thread_data = (struct ntdll_thread_data *)teb->SystemReserved2;
595 thread_regs = (struct ntdll_thread_regs *)teb->SpareBytes1;
596 thread_data->request_fd = request_pipe[1];
598 /* inherit debug registers from parent thread */
599 thread_regs->dr0 = ntdll_get_thread_regs()->dr0;
600 thread_regs->dr1 = ntdll_get_thread_regs()->dr1;
601 thread_regs->dr2 = ntdll_get_thread_regs()->dr2;
602 thread_regs->dr3 = ntdll_get_thread_regs()->dr3;
603 thread_regs->dr6 = ntdll_get_thread_regs()->dr6;
604 thread_regs->dr7 = ntdll_get_thread_regs()->dr7;
606 if ((status = virtual_alloc_thread_stack( teb, stack_reserve, stack_commit ))) goto error;
608 pthread_attr_init( &attr );
609 pthread_attr_setstack( &attr, teb->DeallocationStack,
610 (char *)teb->Tib.StackBase - (char *)teb->DeallocationStack );
611 pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM ); /* force creating a kernel thread */
612 interlocked_xchg_add( &nb_threads, 1 );
613 if (pthread_create( &pthread_id, &attr, (void * (*)(void *))start_thread, info ))
615 interlocked_xchg_add( &nb_threads, -1 );
616 pthread_attr_destroy( &attr );
617 size = 0;
618 NtFreeVirtualMemory( NtCurrentProcess(), &teb->DeallocationStack, &size, MEM_RELEASE );
619 status = STATUS_NO_MEMORY;
620 goto error;
622 pthread_attr_destroy( &attr );
623 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
625 if (id) id->UniqueThread = ULongToHandle(tid);
626 if (handle_ptr) *handle_ptr = handle;
627 else NtClose( handle );
629 return STATUS_SUCCESS;
631 error:
632 if (thread_data) wine_ldt_free_fs( thread_data->fs );
633 if (addr)
635 size = 0;
636 NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
638 if (handle) NtClose( handle );
639 pthread_sigmask( SIG_SETMASK, &sigset, NULL );
640 close( request_pipe[1] );
641 return status;
645 /***********************************************************************
646 * RtlExitUserThread (NTDLL.@)
648 void WINAPI RtlExitUserThread( ULONG status )
650 BOOL last;
652 SERVER_START_REQ( terminate_thread )
654 /* send the exit code to the server */
655 req->handle = wine_server_obj_handle( GetCurrentThread() );
656 req->exit_code = status;
657 wine_server_call( req );
658 last = reply->last;
660 SERVER_END_REQ;
662 if (last)
664 LdrShutdownProcess();
665 exit( status );
667 else
669 LdrShutdownThread();
670 exit_thread( status );
675 /***********************************************************************
676 * NtOpenThread (NTDLL.@)
677 * ZwOpenThread (NTDLL.@)
679 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
680 const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
682 NTSTATUS ret;
684 SERVER_START_REQ( open_thread )
686 req->tid = HandleToULong(id->UniqueThread);
687 req->access = access;
688 req->attributes = attr ? attr->Attributes : 0;
689 ret = wine_server_call( req );
690 *handle = wine_server_ptr_handle( reply->handle );
692 SERVER_END_REQ;
693 return ret;
697 /******************************************************************************
698 * NtSuspendThread (NTDLL.@)
699 * ZwSuspendThread (NTDLL.@)
701 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
703 NTSTATUS ret;
705 SERVER_START_REQ( suspend_thread )
707 req->handle = wine_server_obj_handle( handle );
708 if (!(ret = wine_server_call( req ))) *count = reply->count;
710 SERVER_END_REQ;
711 return ret;
715 /******************************************************************************
716 * NtResumeThread (NTDLL.@)
717 * ZwResumeThread (NTDLL.@)
719 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
721 NTSTATUS ret;
723 SERVER_START_REQ( resume_thread )
725 req->handle = wine_server_obj_handle( handle );
726 if (!(ret = wine_server_call( req ))) *count = reply->count;
728 SERVER_END_REQ;
729 return ret;
733 /******************************************************************************
734 * NtAlertResumeThread (NTDLL.@)
735 * ZwAlertResumeThread (NTDLL.@)
737 NTSTATUS WINAPI NtAlertResumeThread( HANDLE handle, PULONG count )
739 FIXME( "stub: should alert thread %p\n", handle );
740 return NtResumeThread( handle, count );
744 /******************************************************************************
745 * NtAlertThread (NTDLL.@)
746 * ZwAlertThread (NTDLL.@)
748 NTSTATUS WINAPI NtAlertThread( HANDLE handle )
750 FIXME( "stub: %p\n", handle );
751 return STATUS_NOT_IMPLEMENTED;
755 /******************************************************************************
756 * NtTerminateThread (NTDLL.@)
757 * ZwTerminateThread (NTDLL.@)
759 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
761 NTSTATUS ret;
762 BOOL self, last;
764 SERVER_START_REQ( terminate_thread )
766 req->handle = wine_server_obj_handle( handle );
767 req->exit_code = exit_code;
768 ret = wine_server_call( req );
769 self = !ret && reply->self;
770 last = reply->last;
772 SERVER_END_REQ;
774 if (self)
776 if (last) _exit( exit_code );
777 else abort_thread( exit_code );
779 return ret;
783 /******************************************************************************
784 * NtQueueApcThread (NTDLL.@)
786 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
787 ULONG_PTR arg2, ULONG_PTR arg3 )
789 NTSTATUS ret;
790 SERVER_START_REQ( queue_apc )
792 req->handle = wine_server_obj_handle( handle );
793 if (func)
795 req->call.type = APC_USER;
796 req->call.user.func = wine_server_client_ptr( func );
797 req->call.user.args[0] = arg1;
798 req->call.user.args[1] = arg2;
799 req->call.user.args[2] = arg3;
801 else req->call.type = APC_NONE; /* wake up only */
802 ret = wine_server_call( req );
804 SERVER_END_REQ;
805 return ret;
809 /***********************************************************************
810 * NtSetContextThread (NTDLL.@)
811 * ZwSetContextThread (NTDLL.@)
813 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
815 NTSTATUS ret;
816 DWORD dummy, i;
817 BOOL self = FALSE;
819 #ifdef __i386__
820 /* on i386 debug registers always require a server call */
821 self = (handle == GetCurrentThread());
822 if (self && (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)))
824 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
825 self = (regs->dr0 == context->Dr0 && regs->dr1 == context->Dr1 &&
826 regs->dr2 == context->Dr2 && regs->dr3 == context->Dr3 &&
827 regs->dr6 == context->Dr6 && regs->dr7 == context->Dr7);
829 #endif
831 if (!self)
833 SERVER_START_REQ( set_thread_context )
835 req->handle = wine_server_obj_handle( handle );
836 req->flags = context->ContextFlags;
837 req->suspend = 0;
838 wine_server_add_data( req, context, sizeof(*context) );
839 ret = wine_server_call( req );
840 self = reply->self;
842 SERVER_END_REQ;
844 if (ret == STATUS_PENDING)
846 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
848 for (i = 0; i < 100; i++)
850 SERVER_START_REQ( set_thread_context )
852 req->handle = wine_server_obj_handle( handle );
853 req->flags = context->ContextFlags;
854 req->suspend = 0;
855 wine_server_add_data( req, context, sizeof(*context) );
856 ret = wine_server_call( req );
858 SERVER_END_REQ;
859 if (ret == STATUS_PENDING)
861 LARGE_INTEGER timeout;
862 timeout.QuadPart = -10000;
863 NtDelayExecution( FALSE, &timeout );
865 else break;
867 NtResumeThread( handle, &dummy );
869 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
872 if (ret) return ret;
875 if (self) set_cpu_context( context );
876 return STATUS_SUCCESS;
880 /* copy a context structure according to the flags */
881 static inline void copy_context( CONTEXT *to, const CONTEXT *from, DWORD flags )
883 #ifdef __i386__
884 flags &= ~CONTEXT_i386; /* get rid of CPU id */
885 if (flags & CONTEXT_INTEGER)
887 to->Eax = from->Eax;
888 to->Ebx = from->Ebx;
889 to->Ecx = from->Ecx;
890 to->Edx = from->Edx;
891 to->Esi = from->Esi;
892 to->Edi = from->Edi;
894 if (flags & CONTEXT_CONTROL)
896 to->Ebp = from->Ebp;
897 to->Esp = from->Esp;
898 to->Eip = from->Eip;
899 to->SegCs = from->SegCs;
900 to->SegSs = from->SegSs;
901 to->EFlags = from->EFlags;
903 if (flags & CONTEXT_SEGMENTS)
905 to->SegDs = from->SegDs;
906 to->SegEs = from->SegEs;
907 to->SegFs = from->SegFs;
908 to->SegGs = from->SegGs;
910 if (flags & CONTEXT_DEBUG_REGISTERS)
912 to->Dr0 = from->Dr0;
913 to->Dr1 = from->Dr1;
914 to->Dr2 = from->Dr2;
915 to->Dr3 = from->Dr3;
916 to->Dr6 = from->Dr6;
917 to->Dr7 = from->Dr7;
919 if (flags & CONTEXT_FLOATING_POINT)
921 to->FloatSave = from->FloatSave;
923 if (flags & CONTEXT_EXTENDED_REGISTERS)
925 memcpy( to->ExtendedRegisters, from->ExtendedRegisters, sizeof(to->ExtendedRegisters) );
927 #elif defined(__x86_64__)
928 flags &= ~CONTEXT_AMD64; /* get rid of CPU id */
929 if (flags & CONTEXT_CONTROL)
931 to->Rbp = from->Rbp;
932 to->Rip = from->Rip;
933 to->Rsp = from->Rsp;
934 to->SegCs = from->SegCs;
935 to->SegSs = from->SegSs;
936 to->EFlags = from->EFlags;
937 to->MxCsr = from->MxCsr;
939 if (flags & CONTEXT_INTEGER)
941 to->Rax = from->Rax;
942 to->Rcx = from->Rcx;
943 to->Rdx = from->Rdx;
944 to->Rbx = from->Rbx;
945 to->Rsi = from->Rsi;
946 to->Rdi = from->Rdi;
947 to->R8 = from->R8;
948 to->R9 = from->R9;
949 to->R10 = from->R10;
950 to->R11 = from->R11;
951 to->R12 = from->R12;
952 to->R13 = from->R13;
953 to->R14 = from->R14;
954 to->R15 = from->R15;
956 if (flags & CONTEXT_SEGMENTS)
958 to->SegDs = from->SegDs;
959 to->SegEs = from->SegEs;
960 to->SegFs = from->SegFs;
961 to->SegGs = from->SegGs;
963 if (flags & CONTEXT_FLOATING_POINT)
965 to->u.FltSave = from->u.FltSave;
967 if (flags & CONTEXT_DEBUG_REGISTERS)
969 to->Dr0 = from->Dr0;
970 to->Dr1 = from->Dr1;
971 to->Dr2 = from->Dr2;
972 to->Dr3 = from->Dr3;
973 to->Dr6 = from->Dr6;
974 to->Dr7 = from->Dr7;
976 #elif defined(__sparc__)
977 flags &= ~CONTEXT_SPARC; /* get rid of CPU id */
978 if (flags & CONTEXT_CONTROL)
980 to->psr = from->psr;
981 to->pc = from->pc;
982 to->npc = from->npc;
983 to->y = from->y;
984 to->wim = from->wim;
985 to->tbr = from->tbr;
987 if (flags & CONTEXT_INTEGER)
989 to->g0 = from->g0;
990 to->g1 = from->g1;
991 to->g2 = from->g2;
992 to->g3 = from->g3;
993 to->g4 = from->g4;
994 to->g5 = from->g5;
995 to->g6 = from->g6;
996 to->g7 = from->g7;
997 to->o0 = from->o0;
998 to->o1 = from->o1;
999 to->o2 = from->o2;
1000 to->o3 = from->o3;
1001 to->o4 = from->o4;
1002 to->o5 = from->o5;
1003 to->o6 = from->o6;
1004 to->o7 = from->o7;
1005 to->l0 = from->l0;
1006 to->l1 = from->l1;
1007 to->l2 = from->l2;
1008 to->l3 = from->l3;
1009 to->l4 = from->l4;
1010 to->l5 = from->l5;
1011 to->l6 = from->l6;
1012 to->l7 = from->l7;
1013 to->i0 = from->i0;
1014 to->i1 = from->i1;
1015 to->i2 = from->i2;
1016 to->i3 = from->i3;
1017 to->i4 = from->i4;
1018 to->i5 = from->i5;
1019 to->i6 = from->i6;
1020 to->i7 = from->i7;
1022 if (flags & CONTEXT_FLOATING_POINT)
1024 /* FIXME */
1026 #elif defined(__powerpc__)
1027 /* Has no CPU id */
1028 if (flags & CONTEXT_CONTROL)
1030 to->Msr = from->Msr;
1031 to->Ctr = from->Ctr;
1032 to->Iar = from->Iar;
1034 if (flags & CONTEXT_INTEGER)
1036 to->Gpr0 = from->Gpr0;
1037 to->Gpr1 = from->Gpr1;
1038 to->Gpr2 = from->Gpr2;
1039 to->Gpr3 = from->Gpr3;
1040 to->Gpr4 = from->Gpr4;
1041 to->Gpr5 = from->Gpr5;
1042 to->Gpr6 = from->Gpr6;
1043 to->Gpr7 = from->Gpr7;
1044 to->Gpr8 = from->Gpr8;
1045 to->Gpr9 = from->Gpr9;
1046 to->Gpr10 = from->Gpr10;
1047 to->Gpr11 = from->Gpr11;
1048 to->Gpr12 = from->Gpr12;
1049 to->Gpr13 = from->Gpr13;
1050 to->Gpr14 = from->Gpr14;
1051 to->Gpr15 = from->Gpr15;
1052 to->Gpr16 = from->Gpr16;
1053 to->Gpr17 = from->Gpr17;
1054 to->Gpr18 = from->Gpr18;
1055 to->Gpr19 = from->Gpr19;
1056 to->Gpr20 = from->Gpr20;
1057 to->Gpr21 = from->Gpr21;
1058 to->Gpr22 = from->Gpr22;
1059 to->Gpr23 = from->Gpr23;
1060 to->Gpr24 = from->Gpr24;
1061 to->Gpr25 = from->Gpr25;
1062 to->Gpr26 = from->Gpr26;
1063 to->Gpr27 = from->Gpr27;
1064 to->Gpr28 = from->Gpr28;
1065 to->Gpr29 = from->Gpr29;
1066 to->Gpr30 = from->Gpr30;
1067 to->Gpr31 = from->Gpr31;
1068 to->Xer = from->Xer;
1069 to->Cr = from->Cr;
1071 if (flags & CONTEXT_FLOATING_POINT)
1073 to->Fpr0 = from->Fpr0;
1074 to->Fpr1 = from->Fpr1;
1075 to->Fpr2 = from->Fpr2;
1076 to->Fpr3 = from->Fpr3;
1077 to->Fpr4 = from->Fpr4;
1078 to->Fpr5 = from->Fpr5;
1079 to->Fpr6 = from->Fpr6;
1080 to->Fpr7 = from->Fpr7;
1081 to->Fpr8 = from->Fpr8;
1082 to->Fpr9 = from->Fpr9;
1083 to->Fpr10 = from->Fpr10;
1084 to->Fpr11 = from->Fpr11;
1085 to->Fpr12 = from->Fpr12;
1086 to->Fpr13 = from->Fpr13;
1087 to->Fpr14 = from->Fpr14;
1088 to->Fpr15 = from->Fpr15;
1089 to->Fpr16 = from->Fpr16;
1090 to->Fpr17 = from->Fpr17;
1091 to->Fpr18 = from->Fpr18;
1092 to->Fpr19 = from->Fpr19;
1093 to->Fpr20 = from->Fpr20;
1094 to->Fpr21 = from->Fpr21;
1095 to->Fpr22 = from->Fpr22;
1096 to->Fpr23 = from->Fpr23;
1097 to->Fpr24 = from->Fpr24;
1098 to->Fpr25 = from->Fpr25;
1099 to->Fpr26 = from->Fpr26;
1100 to->Fpr27 = from->Fpr27;
1101 to->Fpr28 = from->Fpr28;
1102 to->Fpr29 = from->Fpr29;
1103 to->Fpr30 = from->Fpr30;
1104 to->Fpr31 = from->Fpr31;
1105 to->Fpscr = from->Fpscr;
1107 #else
1108 #error You must implement context copying for your CPU
1109 #endif
1113 /***********************************************************************
1114 * NtGetContextThread (NTDLL.@)
1115 * ZwGetContextThread (NTDLL.@)
1117 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
1119 NTSTATUS ret;
1120 CONTEXT ctx;
1121 DWORD dummy, i;
1122 DWORD needed_flags = context->ContextFlags;
1123 BOOL self = (handle == GetCurrentThread());
1125 #ifdef __i386__
1126 /* on i386 debug registers always require a server call */
1127 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386)) self = FALSE;
1128 #endif
1130 if (!self)
1132 SERVER_START_REQ( get_thread_context )
1134 req->handle = wine_server_obj_handle( handle );
1135 req->flags = context->ContextFlags;
1136 req->suspend = 0;
1137 wine_server_set_reply( req, &ctx, sizeof(ctx) );
1138 ret = wine_server_call( req );
1139 self = reply->self;
1141 SERVER_END_REQ;
1143 if (ret == STATUS_PENDING)
1145 if (NtSuspendThread( handle, &dummy ) == STATUS_SUCCESS)
1147 for (i = 0; i < 100; i++)
1149 SERVER_START_REQ( get_thread_context )
1151 req->handle = wine_server_obj_handle( handle );
1152 req->flags = context->ContextFlags;
1153 req->suspend = 0;
1154 wine_server_set_reply( req, &ctx, sizeof(ctx) );
1155 ret = wine_server_call( req );
1157 SERVER_END_REQ;
1158 if (ret == STATUS_PENDING)
1160 LARGE_INTEGER timeout;
1161 timeout.QuadPart = -10000;
1162 NtDelayExecution( FALSE, &timeout );
1164 else break;
1166 NtResumeThread( handle, &dummy );
1168 if (ret == STATUS_PENDING) ret = STATUS_ACCESS_DENIED;
1170 if (ret) return ret;
1171 copy_context( context, &ctx, context->ContextFlags & ctx.ContextFlags );
1172 needed_flags &= ~ctx.ContextFlags;
1175 if (self)
1177 if (needed_flags)
1179 RtlCaptureContext( &ctx );
1180 copy_context( context, &ctx, ctx.ContextFlags & needed_flags );
1182 #ifdef __i386__
1183 /* update the cached version of the debug registers */
1184 if (context->ContextFlags & (CONTEXT_DEBUG_REGISTERS & ~CONTEXT_i386))
1186 struct ntdll_thread_regs * const regs = ntdll_get_thread_regs();
1187 regs->dr0 = context->Dr0;
1188 regs->dr1 = context->Dr1;
1189 regs->dr2 = context->Dr2;
1190 regs->dr3 = context->Dr3;
1191 regs->dr6 = context->Dr6;
1192 regs->dr7 = context->Dr7;
1194 #endif
1196 return STATUS_SUCCESS;
1200 /******************************************************************************
1201 * NtQueryInformationThread (NTDLL.@)
1202 * ZwQueryInformationThread (NTDLL.@)
1204 NTSTATUS WINAPI NtQueryInformationThread( HANDLE handle, THREADINFOCLASS class,
1205 void *data, ULONG length, ULONG *ret_len )
1207 NTSTATUS status;
1209 switch(class)
1211 case ThreadBasicInformation:
1213 THREAD_BASIC_INFORMATION info;
1214 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
1216 SERVER_START_REQ( get_thread_info )
1218 req->handle = wine_server_obj_handle( handle );
1219 req->tid_in = 0;
1220 if (!(status = wine_server_call( req )))
1222 info.ExitStatus = reply->exit_code;
1223 info.TebBaseAddress = wine_server_get_ptr( reply->teb );
1224 info.ClientId.UniqueProcess = ULongToHandle(reply->pid);
1225 info.ClientId.UniqueThread = ULongToHandle(reply->tid);
1226 info.AffinityMask = reply->affinity & affinity_mask;
1227 info.Priority = reply->priority;
1228 info.BasePriority = reply->priority; /* FIXME */
1231 SERVER_END_REQ;
1232 if (status == STATUS_SUCCESS)
1234 if (data) memcpy( data, &info, min( length, sizeof(info) ));
1235 if (ret_len) *ret_len = min( length, sizeof(info) );
1238 return status;
1239 case ThreadAffinityMask:
1241 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
1242 ULONG_PTR affinity = 0;
1244 SERVER_START_REQ( get_thread_info )
1246 req->handle = wine_server_obj_handle( handle );
1247 req->tid_in = 0;
1248 if (!(status = wine_server_call( req )))
1249 affinity = reply->affinity & affinity_mask;
1251 SERVER_END_REQ;
1252 if (status == STATUS_SUCCESS)
1254 if (data) memcpy( data, &affinity, min( length, sizeof(affinity) ));
1255 if (ret_len) *ret_len = min( length, sizeof(affinity) );
1258 return status;
1259 case ThreadTimes:
1261 KERNEL_USER_TIMES kusrt;
1262 /* We need to do a server call to get the creation time or exit time */
1263 /* This works on any thread */
1264 SERVER_START_REQ( get_thread_info )
1266 req->handle = wine_server_obj_handle( handle );
1267 req->tid_in = 0;
1268 status = wine_server_call( req );
1269 if (status == STATUS_SUCCESS)
1271 kusrt.CreateTime.QuadPart = reply->creation_time;
1272 kusrt.ExitTime.QuadPart = reply->exit_time;
1275 SERVER_END_REQ;
1276 if (status == STATUS_SUCCESS)
1278 /* We call times(2) for kernel time or user time */
1279 /* We can only (portably) do this for the current thread */
1280 if (handle == GetCurrentThread())
1282 struct tms time_buf;
1283 long clocks_per_sec = sysconf(_SC_CLK_TCK);
1285 times(&time_buf);
1286 kusrt.KernelTime.QuadPart = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
1287 kusrt.UserTime.QuadPart = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
1289 else
1291 static BOOL reported = FALSE;
1293 kusrt.KernelTime.QuadPart = 0;
1294 kusrt.UserTime.QuadPart = 0;
1295 if (reported)
1296 TRACE("Cannot get kerneltime or usertime of other threads\n");
1297 else
1299 FIXME("Cannot get kerneltime or usertime of other threads\n");
1300 reported = TRUE;
1303 if (data) memcpy( data, &kusrt, min( length, sizeof(kusrt) ));
1304 if (ret_len) *ret_len = min( length, sizeof(kusrt) );
1307 return status;
1308 case ThreadDescriptorTableEntry:
1310 #ifdef __i386__
1311 THREAD_DESCRIPTOR_INFORMATION* tdi = data;
1312 if (length < sizeof(*tdi))
1313 status = STATUS_INFO_LENGTH_MISMATCH;
1314 else if (!(tdi->Selector & 4)) /* GDT selector */
1316 unsigned sel = tdi->Selector & ~3; /* ignore RPL */
1317 status = STATUS_SUCCESS;
1318 if (!sel) /* null selector */
1319 memset( &tdi->Entry, 0, sizeof(tdi->Entry) );
1320 else
1322 tdi->Entry.BaseLow = 0;
1323 tdi->Entry.HighWord.Bits.BaseMid = 0;
1324 tdi->Entry.HighWord.Bits.BaseHi = 0;
1325 tdi->Entry.LimitLow = 0xffff;
1326 tdi->Entry.HighWord.Bits.LimitHi = 0xf;
1327 tdi->Entry.HighWord.Bits.Dpl = 3;
1328 tdi->Entry.HighWord.Bits.Sys = 0;
1329 tdi->Entry.HighWord.Bits.Pres = 1;
1330 tdi->Entry.HighWord.Bits.Granularity = 1;
1331 tdi->Entry.HighWord.Bits.Default_Big = 1;
1332 tdi->Entry.HighWord.Bits.Type = 0x12;
1333 /* it has to be one of the system GDT selectors */
1334 if (sel != (wine_get_ds() & ~3) && sel != (wine_get_ss() & ~3))
1336 if (sel == (wine_get_cs() & ~3))
1337 tdi->Entry.HighWord.Bits.Type |= 8; /* code segment */
1338 else status = STATUS_ACCESS_DENIED;
1342 else
1344 SERVER_START_REQ( get_selector_entry )
1346 req->handle = wine_server_obj_handle( handle );
1347 req->entry = tdi->Selector >> 3;
1348 status = wine_server_call( req );
1349 if (!status)
1351 if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED))
1352 status = STATUS_ACCESS_VIOLATION;
1353 else
1355 wine_ldt_set_base ( &tdi->Entry, (void *)reply->base );
1356 wine_ldt_set_limit( &tdi->Entry, reply->limit );
1357 wine_ldt_set_flags( &tdi->Entry, reply->flags );
1361 SERVER_END_REQ;
1363 if (status == STATUS_SUCCESS && ret_len)
1364 /* yes, that's a bit strange, but it's the way it is */
1365 *ret_len = sizeof(LDT_ENTRY);
1366 #else
1367 status = STATUS_NOT_IMPLEMENTED;
1368 #endif
1369 return status;
1371 case ThreadAmILastThread:
1373 SERVER_START_REQ(get_thread_info)
1375 req->handle = wine_server_obj_handle( handle );
1376 req->tid_in = 0;
1377 status = wine_server_call( req );
1378 if (status == STATUS_SUCCESS)
1380 BOOLEAN last = reply->last;
1381 if (data) memcpy( data, &last, min( length, sizeof(last) ));
1382 if (ret_len) *ret_len = min( length, sizeof(last) );
1385 SERVER_END_REQ;
1386 return status;
1388 case ThreadPriority:
1389 case ThreadBasePriority:
1390 case ThreadImpersonationToken:
1391 case ThreadEnableAlignmentFaultFixup:
1392 case ThreadEventPair_Reusable:
1393 case ThreadQuerySetWin32StartAddress:
1394 case ThreadZeroTlsCell:
1395 case ThreadPerformanceCount:
1396 case ThreadIdealProcessor:
1397 case ThreadPriorityBoost:
1398 case ThreadSetTlsArrayAddress:
1399 case ThreadIsIoPending:
1400 default:
1401 FIXME( "info class %d not supported yet\n", class );
1402 return STATUS_NOT_IMPLEMENTED;
1407 /******************************************************************************
1408 * NtSetInformationThread (NTDLL.@)
1409 * ZwSetInformationThread (NTDLL.@)
1411 NTSTATUS WINAPI NtSetInformationThread( HANDLE handle, THREADINFOCLASS class,
1412 LPCVOID data, ULONG length )
1414 NTSTATUS status;
1415 switch(class)
1417 case ThreadZeroTlsCell:
1418 if (handle == GetCurrentThread())
1420 LIST_ENTRY *entry;
1421 DWORD index;
1423 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1424 index = *(const DWORD *)data;
1425 if (index < TLS_MINIMUM_AVAILABLE)
1427 RtlAcquirePebLock();
1428 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1430 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1431 teb->TlsSlots[index] = 0;
1433 RtlReleasePebLock();
1435 else
1437 index -= TLS_MINIMUM_AVAILABLE;
1438 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->TlsExpansionBitmapBits))
1439 return STATUS_INVALID_PARAMETER;
1440 RtlAcquirePebLock();
1441 for (entry = tls_links.Flink; entry != &tls_links; entry = entry->Flink)
1443 TEB *teb = CONTAINING_RECORD(entry, TEB, TlsLinks);
1444 if (teb->TlsExpansionSlots) teb->TlsExpansionSlots[index] = 0;
1446 RtlReleasePebLock();
1448 return STATUS_SUCCESS;
1450 FIXME( "ZeroTlsCell not supported on other threads\n" );
1451 return STATUS_NOT_IMPLEMENTED;
1453 case ThreadImpersonationToken:
1455 const HANDLE *phToken = data;
1456 if (length != sizeof(HANDLE)) return STATUS_INVALID_PARAMETER;
1457 TRACE("Setting ThreadImpersonationToken handle to %p\n", *phToken );
1458 SERVER_START_REQ( set_thread_info )
1460 req->handle = wine_server_obj_handle( handle );
1461 req->token = wine_server_obj_handle( *phToken );
1462 req->mask = SET_THREAD_INFO_TOKEN;
1463 status = wine_server_call( req );
1465 SERVER_END_REQ;
1467 return status;
1468 case ThreadBasePriority:
1470 const DWORD *pprio = data;
1471 if (length != sizeof(DWORD)) return STATUS_INVALID_PARAMETER;
1472 SERVER_START_REQ( set_thread_info )
1474 req->handle = wine_server_obj_handle( handle );
1475 req->priority = *pprio;
1476 req->mask = SET_THREAD_INFO_PRIORITY;
1477 status = wine_server_call( req );
1479 SERVER_END_REQ;
1481 return status;
1482 case ThreadAffinityMask:
1484 const ULONG_PTR affinity_mask = ((ULONG_PTR)1 << NtCurrentTeb()->Peb->NumberOfProcessors) - 1;
1485 const ULONG_PTR *paff = data;
1486 if (length != sizeof(ULONG_PTR)) return STATUS_INVALID_PARAMETER;
1487 if (*paff & ~affinity_mask) return STATUS_INVALID_PARAMETER;
1488 SERVER_START_REQ( set_thread_info )
1490 req->handle = wine_server_obj_handle( handle );
1491 req->affinity = *paff;
1492 req->mask = SET_THREAD_INFO_AFFINITY;
1493 status = wine_server_call( req );
1495 SERVER_END_REQ;
1497 return status;
1498 case ThreadBasicInformation:
1499 case ThreadTimes:
1500 case ThreadPriority:
1501 case ThreadDescriptorTableEntry:
1502 case ThreadEnableAlignmentFaultFixup:
1503 case ThreadEventPair_Reusable:
1504 case ThreadQuerySetWin32StartAddress:
1505 case ThreadPerformanceCount:
1506 case ThreadAmILastThread:
1507 case ThreadIdealProcessor:
1508 case ThreadPriorityBoost:
1509 case ThreadSetTlsArrayAddress:
1510 case ThreadIsIoPending:
1511 default:
1512 FIXME( "info class %d not supported yet\n", class );
1513 return STATUS_NOT_IMPLEMENTED;