windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / ntdll / thread.c
blob01733585a756bd8a7cc5f6daabc1f48840f7f88c
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 <assert.h>
22 #include <stdarg.h>
23 #include <limits.h>
24 #include <sys/types.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "winternl.h"
29 #include "wine/debug.h"
30 #include "ntdll_misc.h"
31 #include "ddk/wdm.h"
32 #include "wine/exception.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(thread);
35 WINE_DECLARE_DEBUG_CHANNEL(relay);
36 WINE_DECLARE_DEBUG_CHANNEL(pid);
37 WINE_DECLARE_DEBUG_CHANNEL(timestamp);
39 struct _KUSER_SHARED_DATA *user_shared_data = (void *)0x7ffe0000;
41 struct debug_info
43 unsigned int str_pos; /* current position in strings buffer */
44 unsigned int out_pos; /* current position in output buffer */
45 char strings[1020]; /* buffer for temporary strings */
46 char output[1020]; /* current output line */
49 C_ASSERT( sizeof(struct debug_info) == 0x800 );
51 static int nb_debug_options;
52 static struct __wine_debug_channel *debug_options;
54 static inline struct debug_info *get_info(void)
56 #ifdef _WIN64
57 return (struct debug_info *)((TEB32 *)((char *)NtCurrentTeb() + 0x2000) + 1);
58 #else
59 return (struct debug_info *)(NtCurrentTeb() + 1);
60 #endif
63 static void init_options(void)
65 unsigned int offset = page_size * (sizeof(void *) / 4);
67 debug_options = (struct __wine_debug_channel *)((char *)NtCurrentTeb()->Peb + offset);
68 while (debug_options[nb_debug_options].name[0]) nb_debug_options++;
71 /* add a string to the output buffer */
72 static int append_output( struct debug_info *info, const char *str, size_t len )
74 if (len >= sizeof(info->output) - info->out_pos)
76 __wine_dbg_write( info->output, info->out_pos );
77 info->out_pos = 0;
78 ERR_(thread)( "debug buffer overflow:\n" );
79 __wine_dbg_write( str, len );
80 RtlRaiseStatus( STATUS_BUFFER_OVERFLOW );
82 memcpy( info->output + info->out_pos, str, len );
83 info->out_pos += len;
84 return len;
87 /***********************************************************************
88 * __wine_dbg_get_channel_flags (NTDLL.@)
90 * Get the flags to use for a given channel, possibly setting them too in case of lazy init
92 unsigned char __cdecl __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
94 int min, max, pos, res;
95 unsigned char default_flags;
97 if (!debug_options) init_options();
99 min = 0;
100 max = nb_debug_options - 1;
101 while (min <= max)
103 pos = (min + max) / 2;
104 res = strcmp( channel->name, debug_options[pos].name );
105 if (!res) return debug_options[pos].flags;
106 if (res < 0) max = pos - 1;
107 else min = pos + 1;
109 /* no option for this channel */
110 default_flags = debug_options[nb_debug_options].flags;
111 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
112 return default_flags;
115 /***********************************************************************
116 * __wine_dbg_strdup (NTDLL.@)
118 const char * __cdecl __wine_dbg_strdup( const char *str )
120 struct debug_info *info = get_info();
121 unsigned int pos = info->str_pos;
122 size_t n = strlen( str ) + 1;
124 assert( n <= sizeof(info->strings) );
125 if (pos + n > sizeof(info->strings)) pos = 0;
126 info->str_pos = pos + n;
127 return memcpy( info->strings + pos, str, n );
130 /***********************************************************************
131 * __wine_dbg_header (NTDLL.@)
133 int __cdecl __wine_dbg_header( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
134 const char *function )
136 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
137 struct debug_info *info = get_info();
138 char *pos = info->output;
140 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
142 /* only print header if we are at the beginning of the line */
143 if (info->out_pos) return 0;
145 if (TRACE_ON(timestamp))
147 ULONG ticks = NtGetTickCount();
148 pos += sprintf( pos, "%3lu.%03lu:", ticks / 1000, ticks % 1000 );
150 if (TRACE_ON(pid)) pos += sprintf( pos, "%04lx:", GetCurrentProcessId() );
151 pos += sprintf( pos, "%04lx:", GetCurrentThreadId() );
152 if (function && cls < ARRAY_SIZE( classes ))
153 pos += snprintf( pos, sizeof(info->output) - (pos - info->output), "%s:%s:%s ",
154 classes[cls], channel->name, function );
155 info->out_pos = pos - info->output;
156 return info->out_pos;
159 /***********************************************************************
160 * __wine_dbg_write (NTDLL.@)
162 int WINAPI __wine_dbg_write( const char *str, unsigned int len )
164 struct wine_dbg_write_params params = { str, len };
166 return WINE_UNIX_CALL( unix_wine_dbg_write, &params );
169 /***********************************************************************
170 * __wine_dbg_output (NTDLL.@)
172 int __cdecl __wine_dbg_output( const char *str )
174 struct debug_info *info = get_info();
175 const char *end = strrchr( str, '\n' );
176 int ret = 0;
178 if (end)
180 ret += append_output( info, str, end + 1 - str );
181 __wine_dbg_write( info->output, info->out_pos );
182 info->out_pos = 0;
183 str = end + 1;
185 if (*str) ret += append_output( info, str, strlen( str ));
186 return ret;
190 /***********************************************************************
191 * set_native_thread_name
193 void set_native_thread_name( DWORD tid, const char *name )
195 THREAD_NAME_INFORMATION info;
196 HANDLE h = GetCurrentThread();
197 WCHAR nameW[64];
199 if (tid != -1)
201 OBJECT_ATTRIBUTES attr;
202 CLIENT_ID cid;
204 attr.Length = sizeof(attr);
205 attr.RootDirectory = 0;
206 attr.Attributes = 0;
207 attr.ObjectName = NULL;
208 attr.SecurityDescriptor = NULL;
209 attr.SecurityQualityOfService = NULL;
211 cid.UniqueProcess = 0;
212 cid.UniqueThread = ULongToHandle( tid );
214 if (NtOpenThread( &h, THREAD_QUERY_LIMITED_INFORMATION, &attr, &cid )) return;
217 if (name)
219 mbstowcs( nameW, name, ARRAY_SIZE(nameW) );
220 nameW[ARRAY_SIZE(nameW) - 1] = '\0';
222 else
224 nameW[0] = '\0';
227 RtlInitUnicodeString( &info.ThreadName, nameW );
228 NtSetInformationThread( h, ThreadWineNativeThreadName, &info, sizeof(info) );
230 if (h != GetCurrentThread())
231 NtClose(h);
235 /***********************************************************************
236 * RtlExitUserThread (NTDLL.@)
238 void WINAPI RtlExitUserThread( ULONG status )
240 ULONG last;
242 NtQueryInformationThread( GetCurrentThread(), ThreadAmILastThread, &last, sizeof(last), NULL );
243 if (last) RtlExitUserProcess( status );
244 LdrShutdownThread();
245 for (;;) NtTerminateThread( GetCurrentThread(), status );
249 /***********************************************************************
250 * RtlUserThreadStart (NTDLL.@)
252 #ifdef __i386__
253 __ASM_STDCALL_FUNC( RtlUserThreadStart, 8,
254 "movl %ebx,8(%esp)\n\t" /* arg */
255 "movl %eax,4(%esp)\n\t" /* entry */
256 "jmp " __ASM_NAME("call_thread_func") )
258 /* wrapper to call BaseThreadInitThunk */
259 extern void DECLSPEC_NORETURN call_thread_func_wrapper( void *thunk, PRTL_THREAD_START_ROUTINE entry, void *arg );
260 __ASM_GLOBAL_FUNC( call_thread_func_wrapper,
261 "pushl %ebp\n\t"
262 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
263 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
264 "movl %esp,%ebp\n\t"
265 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
266 "subl $4,%esp\n\t"
267 "andl $~0xf,%esp\n\t"
268 "xorl %ecx,%ecx\n\t"
269 "movl 12(%ebp),%edx\n\t"
270 "movl 16(%ebp),%eax\n\t"
271 "movl %eax,(%esp)\n\t"
272 "call *8(%ebp)" )
274 void DECLSPEC_HIDDEN call_thread_func( PRTL_THREAD_START_ROUTINE entry, void *arg )
276 __TRY
278 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
279 call_thread_func_wrapper( pBaseThreadInitThunk, entry, arg );
281 __EXCEPT(call_unhandled_exception_filter)
283 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
285 __ENDTRY
288 #else /* __i386__ */
290 void WINAPI RtlUserThreadStart( PRTL_THREAD_START_ROUTINE entry, void *arg )
292 __TRY
294 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
295 pBaseThreadInitThunk( 0, (LPTHREAD_START_ROUTINE)entry, arg );
297 __EXCEPT(call_unhandled_exception_filter)
299 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
301 __ENDTRY
304 #endif /* __i386__ */
307 /***********************************************************************
308 * RtlCreateUserThread (NTDLL.@)
310 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, SECURITY_DESCRIPTOR *descr,
311 BOOLEAN suspended, ULONG zero_bits,
312 SIZE_T stack_reserve, SIZE_T stack_commit,
313 PRTL_THREAD_START_ROUTINE start, void *param,
314 HANDLE *handle_ptr, CLIENT_ID *id )
316 ULONG flags = suspended ? THREAD_CREATE_FLAGS_CREATE_SUSPENDED : 0;
317 ULONG_PTR buffer[offsetof( PS_ATTRIBUTE_LIST, Attributes[2] ) / sizeof(ULONG_PTR)];
318 PS_ATTRIBUTE_LIST *attr_list = (PS_ATTRIBUTE_LIST *)buffer;
319 HANDLE handle, actctx;
320 TEB *teb;
321 ULONG ret;
322 NTSTATUS status;
323 CLIENT_ID client_id;
324 OBJECT_ATTRIBUTES attr;
326 attr_list->TotalLength = sizeof(buffer);
327 attr_list->Attributes[0].Attribute = PS_ATTRIBUTE_CLIENT_ID;
328 attr_list->Attributes[0].Size = sizeof(client_id);
329 attr_list->Attributes[0].ValuePtr = &client_id;
330 attr_list->Attributes[0].ReturnLength = NULL;
331 attr_list->Attributes[1].Attribute = PS_ATTRIBUTE_TEB_ADDRESS;
332 attr_list->Attributes[1].Size = sizeof(teb);
333 attr_list->Attributes[1].ValuePtr = &teb;
334 attr_list->Attributes[1].ReturnLength = NULL;
336 InitializeObjectAttributes( &attr, NULL, 0, NULL, descr );
338 RtlGetActiveActivationContext( &actctx );
339 if (actctx) flags |= THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
341 status = NtCreateThreadEx( &handle, THREAD_ALL_ACCESS, &attr, process, start, param,
342 flags, zero_bits, stack_commit, stack_reserve, attr_list );
343 if (!status)
345 if (actctx)
347 ULONG_PTR cookie;
348 RtlActivateActivationContextEx( 0, teb, actctx, &cookie );
349 if (!suspended) NtResumeThread( handle, &ret );
351 if (id) *id = client_id;
352 if (handle_ptr) *handle_ptr = handle;
353 else NtClose( handle );
355 if (actctx) RtlReleaseActivationContext( actctx );
356 return status;
360 /**********************************************************************
361 * RtlCreateUserStack (NTDLL.@)
363 NTSTATUS WINAPI RtlCreateUserStack( SIZE_T commit, SIZE_T reserve, ULONG zero_bits,
364 SIZE_T commit_align, SIZE_T reserve_align, INITIAL_TEB *stack )
366 PROCESS_STACK_ALLOCATION_INFORMATION alloc;
367 NTSTATUS status;
369 TRACE("commit %#Ix, reserve %#Ix, zero_bits %lu, commit_align %#Ix, reserve_align %#Ix, stack %p\n",
370 commit, reserve, zero_bits, commit_align, reserve_align, stack);
372 if (!commit_align || !reserve_align)
373 return STATUS_INVALID_PARAMETER;
375 if (!commit || !reserve)
377 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
378 if (!reserve) reserve = nt->OptionalHeader.SizeOfStackReserve;
379 if (!commit) commit = nt->OptionalHeader.SizeOfStackCommit;
382 reserve = (reserve + reserve_align - 1) & ~(reserve_align - 1);
383 commit = (commit + commit_align - 1) & ~(commit_align - 1);
385 if (reserve < commit) reserve = commit;
386 if (reserve < 0x100000) reserve = 0x100000;
387 reserve = (reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
389 alloc.ReserveSize = reserve;
390 alloc.ZeroBits = zero_bits;
391 status = NtSetInformationProcess( GetCurrentProcess(), ProcessThreadStackAllocation,
392 &alloc, sizeof(alloc) );
393 if (!status)
395 void *addr = alloc.StackBase;
396 SIZE_T size = page_size;
398 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_NOACCESS );
399 addr = (char *)alloc.StackBase + page_size;
400 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
401 addr = (char *)alloc.StackBase + 2 * page_size;
402 size = reserve - 2 * page_size;
403 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE );
405 /* note: limit is lower than base since the stack grows down */
406 stack->OldStackBase = 0;
407 stack->OldStackLimit = 0;
408 stack->DeallocationStack = alloc.StackBase;
409 stack->StackBase = (char *)alloc.StackBase + reserve;
410 stack->StackLimit = (char *)alloc.StackBase + 2 * page_size;
412 return status;
416 /**********************************************************************
417 * RtlFreeUserStack (NTDLL.@)
419 void WINAPI RtlFreeUserStack( void *stack )
421 SIZE_T size = 0;
423 TRACE("stack %p\n", stack);
425 NtFreeVirtualMemory( NtCurrentProcess(), &stack, &size, MEM_RELEASE );
429 /******************************************************************************
430 * RtlGetNtGlobalFlags (NTDLL.@)
432 ULONG WINAPI RtlGetNtGlobalFlags(void)
434 return NtCurrentTeb()->Peb->NtGlobalFlag;
438 /******************************************************************************
439 * RtlPushFrame (NTDLL.@)
441 void WINAPI RtlPushFrame( TEB_ACTIVE_FRAME *frame )
443 frame->Previous = NtCurrentTeb()->ActiveFrame;
444 NtCurrentTeb()->ActiveFrame = frame;
448 /******************************************************************************
449 * RtlPopFrame (NTDLL.@)
451 void WINAPI RtlPopFrame( TEB_ACTIVE_FRAME *frame )
453 NtCurrentTeb()->ActiveFrame = frame->Previous;
457 /******************************************************************************
458 * RtlGetFrame (NTDLL.@)
460 TEB_ACTIVE_FRAME * WINAPI RtlGetFrame(void)
462 return NtCurrentTeb()->ActiveFrame;
466 /******************************************************************************
467 * RtlIsCurrentThread (NTDLL.@)
469 BOOLEAN WINAPI RtlIsCurrentThread( HANDLE handle )
471 return handle == NtCurrentThread() || !NtCompareObjects( handle, NtCurrentThread() );
475 /***********************************************************************
476 * _errno (NTDLL.@)
478 int * CDECL _errno(void)
480 return (int *)&NtCurrentTeb()->TlsSlots[NTDLL_TLS_ERRNO];
484 /***********************************************************************
485 * Fibers
486 ***********************************************************************/
489 static GLOBAL_FLS_DATA fls_data = { { NULL }, { &fls_data.fls_list_head, &fls_data.fls_list_head } };
491 static RTL_CRITICAL_SECTION fls_section;
492 static RTL_CRITICAL_SECTION_DEBUG fls_critsect_debug =
494 0, 0, &fls_section,
495 { &fls_critsect_debug.ProcessLocksList, &fls_critsect_debug.ProcessLocksList },
496 0, 0, { (DWORD_PTR)(__FILE__ ": fls_section") }
498 static RTL_CRITICAL_SECTION fls_section = { &fls_critsect_debug, -1, 0, 0, 0, 0 };
500 #define MAX_FLS_DATA_COUNT 0xff0
502 static void lock_fls_data(void)
504 RtlEnterCriticalSection( &fls_section );
507 static void unlock_fls_data(void)
509 RtlLeaveCriticalSection( &fls_section );
512 static unsigned int fls_chunk_size( unsigned int chunk_index )
514 return 0x10 << chunk_index;
517 static unsigned int fls_index_from_chunk_index( unsigned int chunk_index, unsigned int index )
519 return 0x10 * ((1 << chunk_index) - 1) + index;
522 static unsigned int fls_chunk_index_from_index( unsigned int index, unsigned int *index_in_chunk )
524 unsigned int chunk_index = 0;
526 while (index >= fls_chunk_size( chunk_index ))
527 index -= fls_chunk_size( chunk_index++ );
529 *index_in_chunk = index;
530 return chunk_index;
533 TEB_FLS_DATA *fls_alloc_data(void)
535 TEB_FLS_DATA *fls;
537 if (!(fls = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*fls) )))
538 return NULL;
540 lock_fls_data();
541 InsertTailList( &fls_data.fls_list_head, &fls->fls_list_entry );
542 unlock_fls_data();
544 return fls;
548 /***********************************************************************
549 * RtlFlsAlloc (NTDLL.@)
551 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsAlloc( PFLS_CALLBACK_FUNCTION callback, ULONG *ret_index )
553 unsigned int chunk_index, index, i;
554 FLS_INFO_CHUNK *chunk;
555 TEB_FLS_DATA *fls;
557 if (!(fls = NtCurrentTeb()->FlsSlots)
558 && !(NtCurrentTeb()->FlsSlots = fls = fls_alloc_data()))
559 return STATUS_NO_MEMORY;
561 lock_fls_data();
562 for (i = 0; i < ARRAY_SIZE(fls_data.fls_callback_chunks); ++i)
564 if (!fls_data.fls_callback_chunks[i] || fls_data.fls_callback_chunks[i]->count < fls_chunk_size( i ))
565 break;
568 if ((chunk_index = i) == ARRAY_SIZE(fls_data.fls_callback_chunks))
570 unlock_fls_data();
571 return STATUS_NO_MEMORY;
574 if ((chunk = fls_data.fls_callback_chunks[chunk_index]))
576 for (index = 0; index < fls_chunk_size( chunk_index ); ++index)
577 if (!chunk->callbacks[index].callback)
578 break;
579 assert( index < fls_chunk_size( chunk_index ));
581 else
583 fls_data.fls_callback_chunks[chunk_index] = chunk = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
584 offsetof(FLS_INFO_CHUNK, callbacks) + sizeof(*chunk->callbacks) * fls_chunk_size( chunk_index ));
585 if (!chunk)
587 unlock_fls_data();
588 return STATUS_NO_MEMORY;
591 if (chunk_index)
593 index = 0;
595 else
597 chunk->count = 1; /* FLS index 0 is prohibited. */
598 chunk->callbacks[0].callback = (void *)~(ULONG_PTR)0;
599 index = 1;
603 ++chunk->count;
604 chunk->callbacks[index].callback = callback ? callback : (PFLS_CALLBACK_FUNCTION)~(ULONG_PTR)0;
606 if ((*ret_index = fls_index_from_chunk_index( chunk_index, index )) > fls_data.fls_high_index)
607 fls_data.fls_high_index = *ret_index;
609 unlock_fls_data();
611 return STATUS_SUCCESS;
615 /***********************************************************************
616 * RtlFlsFree (NTDLL.@)
618 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsFree( ULONG index )
620 PFLS_CALLBACK_FUNCTION callback;
621 unsigned int chunk_index, idx;
622 FLS_INFO_CHUNK *chunk;
623 LIST_ENTRY *entry;
625 lock_fls_data();
627 if (!index || index > fls_data.fls_high_index)
629 unlock_fls_data();
630 return STATUS_INVALID_PARAMETER;
633 chunk_index = fls_chunk_index_from_index( index, &idx );
634 if (!(chunk = fls_data.fls_callback_chunks[chunk_index])
635 || !(callback = chunk->callbacks[idx].callback))
637 unlock_fls_data();
638 return STATUS_INVALID_PARAMETER;
641 for (entry = fls_data.fls_list_head.Flink; entry != &fls_data.fls_list_head; entry = entry->Flink)
643 TEB_FLS_DATA *fls = CONTAINING_RECORD(entry, TEB_FLS_DATA, fls_list_entry);
645 if (fls->fls_data_chunks[chunk_index] && fls->fls_data_chunks[chunk_index][idx + 1])
647 if (callback != (void *)~(ULONG_PTR)0)
649 TRACE_(relay)("Calling FLS callback %p, arg %p.\n", callback,
650 fls->fls_data_chunks[chunk_index][idx + 1]);
652 callback( fls->fls_data_chunks[chunk_index][idx + 1] );
654 fls->fls_data_chunks[chunk_index][idx + 1] = NULL;
658 --chunk->count;
659 chunk->callbacks[idx].callback = NULL;
661 unlock_fls_data();
662 return STATUS_SUCCESS;
666 /***********************************************************************
667 * RtlFlsSetValue (NTDLL.@)
669 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsSetValue( ULONG index, void *data )
671 unsigned int chunk_index, idx;
672 TEB_FLS_DATA *fls;
674 if (!index || index >= MAX_FLS_DATA_COUNT)
675 return STATUS_INVALID_PARAMETER;
677 if (!(fls = NtCurrentTeb()->FlsSlots)
678 && !(NtCurrentTeb()->FlsSlots = fls = fls_alloc_data()))
679 return STATUS_NO_MEMORY;
681 chunk_index = fls_chunk_index_from_index( index, &idx );
683 if (!fls->fls_data_chunks[chunk_index] &&
684 !(fls->fls_data_chunks[chunk_index] = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
685 (fls_chunk_size( chunk_index ) + 1) * sizeof(*fls->fls_data_chunks[chunk_index]) )))
686 return STATUS_NO_MEMORY;
688 fls->fls_data_chunks[chunk_index][idx + 1] = data;
690 return STATUS_SUCCESS;
694 /***********************************************************************
695 * RtlFlsGetValue (NTDLL.@)
697 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsGetValue( ULONG index, void **data )
699 unsigned int chunk_index, idx;
700 TEB_FLS_DATA *fls;
702 if (!index || index >= MAX_FLS_DATA_COUNT || !(fls = NtCurrentTeb()->FlsSlots))
703 return STATUS_INVALID_PARAMETER;
705 chunk_index = fls_chunk_index_from_index( index, &idx );
707 *data = fls->fls_data_chunks[chunk_index] ? fls->fls_data_chunks[chunk_index][idx + 1] : NULL;
708 return STATUS_SUCCESS;
712 /***********************************************************************
713 * RtlProcessFlsData (NTDLL.@)
715 void WINAPI DECLSPEC_HOTPATCH RtlProcessFlsData( void *teb_fls_data, ULONG flags )
717 TEB_FLS_DATA *fls = teb_fls_data;
718 unsigned int i, index;
720 TRACE_(thread)( "teb_fls_data %p, flags %#lx.\n", teb_fls_data, flags );
722 if (flags & ~3)
723 FIXME_(thread)( "Unknown flags %#lx.\n", flags );
725 if (!fls)
726 return;
728 if (flags & 1)
730 lock_fls_data();
731 for (i = 0; i < ARRAY_SIZE(fls->fls_data_chunks); ++i)
733 if (!fls->fls_data_chunks[i] || !fls_data.fls_callback_chunks[i]
734 || !fls_data.fls_callback_chunks[i]->count)
735 continue;
737 for (index = 0; index < fls_chunk_size( i ); ++index)
739 PFLS_CALLBACK_FUNCTION callback = fls_data.fls_callback_chunks[i]->callbacks[index].callback;
741 if (!fls->fls_data_chunks[i][index + 1])
742 continue;
744 if (callback && callback != (void *)~(ULONG_PTR)0)
746 TRACE_(relay)("Calling FLS callback %p, arg %p.\n", callback,
747 fls->fls_data_chunks[i][index + 1]);
749 callback( fls->fls_data_chunks[i][index + 1] );
751 fls->fls_data_chunks[i][index + 1] = NULL;
754 /* Not using RemoveEntryList() as Windows does not zero list entry here. */
755 fls->fls_list_entry.Flink->Blink = fls->fls_list_entry.Blink;
756 fls->fls_list_entry.Blink->Flink = fls->fls_list_entry.Flink;
757 unlock_fls_data();
760 if (flags & 2)
762 for (i = 0; i < ARRAY_SIZE(fls->fls_data_chunks); ++i)
763 RtlFreeHeap( GetProcessHeap(), 0, fls->fls_data_chunks[i] );
765 RtlFreeHeap( GetProcessHeap(), 0, fls );