dinput: Clear DIA_APPNOMAP BuildActionMap flag with specific device semantic.
[wine.git] / dlls / ntdll / thread.c
blobb0b61419e376e9513b0c6d5f3fe312d2593b3307
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 #define NONAMELESSUNION
27 #include "ntstatus.h"
28 #define WIN32_NO_STATUS
29 #include "winternl.h"
30 #include "wine/debug.h"
31 #include "ntdll_misc.h"
32 #include "ddk/wdm.h"
33 #include "wine/exception.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(thread);
36 WINE_DECLARE_DEBUG_CHANNEL(relay);
37 WINE_DECLARE_DEBUG_CHANNEL(pid);
38 WINE_DECLARE_DEBUG_CHANNEL(timestamp);
40 struct _KUSER_SHARED_DATA *user_shared_data = (void *)0x7ffe0000;
42 struct debug_info
44 unsigned int str_pos; /* current position in strings buffer */
45 unsigned int out_pos; /* current position in output buffer */
46 char strings[1020]; /* buffer for temporary strings */
47 char output[1020]; /* current output line */
50 C_ASSERT( sizeof(struct debug_info) == 0x800 );
52 static int nb_debug_options;
53 static struct __wine_debug_channel *debug_options;
55 static inline struct debug_info *get_info(void)
57 #ifdef _WIN64
58 return (struct debug_info *)((TEB32 *)((char *)NtCurrentTeb() + 0x2000) + 1);
59 #else
60 return (struct debug_info *)(NtCurrentTeb() + 1);
61 #endif
64 static void init_options(void)
66 unsigned int offset = page_size * (sizeof(void *) / 4);
68 debug_options = (struct __wine_debug_channel *)((char *)NtCurrentTeb()->Peb + offset);
69 while (debug_options[nb_debug_options].name[0]) nb_debug_options++;
72 /* add a string to the output buffer */
73 static int append_output( struct debug_info *info, const char *str, size_t len )
75 if (len >= sizeof(info->output) - info->out_pos)
77 __wine_dbg_write( info->output, info->out_pos );
78 info->out_pos = 0;
79 ERR_(thread)( "debug buffer overflow:\n" );
80 __wine_dbg_write( str, len );
81 RtlRaiseStatus( STATUS_BUFFER_OVERFLOW );
83 memcpy( info->output + info->out_pos, str, len );
84 info->out_pos += len;
85 return len;
88 /***********************************************************************
89 * __wine_dbg_get_channel_flags (NTDLL.@)
91 * Get the flags to use for a given channel, possibly setting them too in case of lazy init
93 unsigned char __cdecl __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
95 int min, max, pos, res;
96 unsigned char default_flags;
98 if (!debug_options) init_options();
100 min = 0;
101 max = nb_debug_options - 1;
102 while (min <= max)
104 pos = (min + max) / 2;
105 res = strcmp( channel->name, debug_options[pos].name );
106 if (!res) return debug_options[pos].flags;
107 if (res < 0) max = pos - 1;
108 else min = pos + 1;
110 /* no option for this channel */
111 default_flags = debug_options[nb_debug_options].flags;
112 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
113 return default_flags;
116 /***********************************************************************
117 * __wine_dbg_strdup (NTDLL.@)
119 const char * __cdecl __wine_dbg_strdup( const char *str )
121 struct debug_info *info = get_info();
122 unsigned int pos = info->str_pos;
123 size_t n = strlen( str ) + 1;
125 assert( n <= sizeof(info->strings) );
126 if (pos + n > sizeof(info->strings)) pos = 0;
127 info->str_pos = pos + n;
128 return memcpy( info->strings + pos, str, n );
131 /***********************************************************************
132 * __wine_dbg_header (NTDLL.@)
134 int __cdecl __wine_dbg_header( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
135 const char *function )
137 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
138 struct debug_info *info = get_info();
139 char *pos = info->output;
141 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
143 /* only print header if we are at the beginning of the line */
144 if (info->out_pos) return 0;
146 if (TRACE_ON(timestamp))
148 ULONG ticks = NtGetTickCount();
149 pos += sprintf( pos, "%3lu.%03lu:", ticks / 1000, ticks % 1000 );
151 if (TRACE_ON(pid)) pos += sprintf( pos, "%04lx:", GetCurrentProcessId() );
152 pos += sprintf( pos, "%04lx:", GetCurrentThreadId() );
153 if (function && cls < ARRAY_SIZE( classes ))
154 pos += snprintf( pos, sizeof(info->output) - (pos - info->output), "%s:%s:%s ",
155 classes[cls], channel->name, function );
156 info->out_pos = pos - info->output;
157 return info->out_pos;
160 /***********************************************************************
161 * __wine_dbg_write (NTDLL.@)
163 int WINAPI __wine_dbg_write( const char *str, unsigned int len )
165 struct wine_dbg_write_params params = { str, len };
167 return WINE_UNIX_CALL( unix_wine_dbg_write, &params );
170 /***********************************************************************
171 * __wine_dbg_output (NTDLL.@)
173 int __cdecl __wine_dbg_output( const char *str )
175 struct debug_info *info = get_info();
176 const char *end = strrchr( str, '\n' );
177 int ret = 0;
179 if (end)
181 ret += append_output( info, str, end + 1 - str );
182 __wine_dbg_write( info->output, info->out_pos );
183 info->out_pos = 0;
184 str = end + 1;
186 if (*str) ret += append_output( info, str, strlen( str ));
187 return ret;
191 /***********************************************************************
192 * set_native_thread_name
194 void set_native_thread_name( DWORD tid, const char *name )
196 THREAD_NAME_INFORMATION info;
197 HANDLE h = GetCurrentThread();
198 WCHAR nameW[64];
200 if (tid != -1)
202 OBJECT_ATTRIBUTES attr;
203 CLIENT_ID cid;
205 attr.Length = sizeof(attr);
206 attr.RootDirectory = 0;
207 attr.Attributes = 0;
208 attr.ObjectName = NULL;
209 attr.SecurityDescriptor = NULL;
210 attr.SecurityQualityOfService = NULL;
212 cid.UniqueProcess = 0;
213 cid.UniqueThread = ULongToHandle( tid );
215 if (NtOpenThread( &h, THREAD_QUERY_LIMITED_INFORMATION, &attr, &cid )) return;
218 if (name)
220 mbstowcs( nameW, name, ARRAY_SIZE(nameW) );
221 nameW[ARRAY_SIZE(nameW) - 1] = '\0';
223 else
225 nameW[0] = '\0';
228 RtlInitUnicodeString( &info.ThreadName, nameW );
229 NtSetInformationThread( h, ThreadWineNativeThreadName, &info, sizeof(info) );
231 if (h != GetCurrentThread())
232 NtClose(h);
236 /***********************************************************************
237 * RtlExitUserThread (NTDLL.@)
239 void WINAPI RtlExitUserThread( ULONG status )
241 ULONG last;
243 NtQueryInformationThread( GetCurrentThread(), ThreadAmILastThread, &last, sizeof(last), NULL );
244 if (last) RtlExitUserProcess( status );
245 LdrShutdownThread();
246 for (;;) NtTerminateThread( GetCurrentThread(), status );
250 /***********************************************************************
251 * RtlUserThreadStart (NTDLL.@)
253 #ifdef __i386__
254 __ASM_STDCALL_FUNC( RtlUserThreadStart, 8,
255 "movl %ebx,8(%esp)\n\t" /* arg */
256 "movl %eax,4(%esp)\n\t" /* entry */
257 "jmp " __ASM_NAME("call_thread_func") )
259 /* wrapper to call BaseThreadInitThunk */
260 extern void DECLSPEC_NORETURN call_thread_func_wrapper( void *thunk, PRTL_THREAD_START_ROUTINE entry, void *arg );
261 __ASM_GLOBAL_FUNC( call_thread_func_wrapper,
262 "pushl %ebp\n\t"
263 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
264 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
265 "movl %esp,%ebp\n\t"
266 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
267 "subl $4,%esp\n\t"
268 "andl $~0xf,%esp\n\t"
269 "xorl %ecx,%ecx\n\t"
270 "movl 12(%ebp),%edx\n\t"
271 "movl 16(%ebp),%eax\n\t"
272 "movl %eax,(%esp)\n\t"
273 "call *8(%ebp)" )
275 void DECLSPEC_HIDDEN call_thread_func( PRTL_THREAD_START_ROUTINE entry, void *arg )
277 __TRY
279 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
280 call_thread_func_wrapper( pBaseThreadInitThunk, entry, arg );
282 __EXCEPT(call_unhandled_exception_filter)
284 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
286 __ENDTRY
289 #else /* __i386__ */
291 void WINAPI RtlUserThreadStart( PRTL_THREAD_START_ROUTINE entry, void *arg )
293 __TRY
295 TRACE_(relay)( "\1Starting thread proc %p (arg=%p)\n", entry, arg );
296 pBaseThreadInitThunk( 0, (LPTHREAD_START_ROUTINE)entry, arg );
298 __EXCEPT(call_unhandled_exception_filter)
300 NtTerminateProcess( GetCurrentProcess(), GetExceptionCode() );
302 __ENDTRY
305 #endif /* __i386__ */
308 /***********************************************************************
309 * RtlCreateUserThread (NTDLL.@)
311 NTSTATUS WINAPI RtlCreateUserThread( HANDLE process, SECURITY_DESCRIPTOR *descr,
312 BOOLEAN suspended, ULONG zero_bits,
313 SIZE_T stack_reserve, SIZE_T stack_commit,
314 PRTL_THREAD_START_ROUTINE start, void *param,
315 HANDLE *handle_ptr, CLIENT_ID *id )
317 ULONG flags = suspended ? THREAD_CREATE_FLAGS_CREATE_SUSPENDED : 0;
318 ULONG_PTR buffer[offsetof( PS_ATTRIBUTE_LIST, Attributes[2] ) / sizeof(ULONG_PTR)];
319 PS_ATTRIBUTE_LIST *attr_list = (PS_ATTRIBUTE_LIST *)buffer;
320 HANDLE handle, actctx;
321 TEB *teb;
322 ULONG ret;
323 NTSTATUS status;
324 CLIENT_ID client_id;
325 OBJECT_ATTRIBUTES attr;
327 attr_list->TotalLength = sizeof(buffer);
328 attr_list->Attributes[0].Attribute = PS_ATTRIBUTE_CLIENT_ID;
329 attr_list->Attributes[0].Size = sizeof(client_id);
330 attr_list->Attributes[0].ValuePtr = &client_id;
331 attr_list->Attributes[0].ReturnLength = NULL;
332 attr_list->Attributes[1].Attribute = PS_ATTRIBUTE_TEB_ADDRESS;
333 attr_list->Attributes[1].Size = sizeof(teb);
334 attr_list->Attributes[1].ValuePtr = &teb;
335 attr_list->Attributes[1].ReturnLength = NULL;
337 InitializeObjectAttributes( &attr, NULL, 0, NULL, descr );
339 RtlGetActiveActivationContext( &actctx );
340 if (actctx) flags |= THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
342 status = NtCreateThreadEx( &handle, THREAD_ALL_ACCESS, &attr, process, start, param,
343 flags, zero_bits, stack_commit, stack_reserve, attr_list );
344 if (!status)
346 if (actctx)
348 ULONG_PTR cookie;
349 RtlActivateActivationContextEx( 0, teb, actctx, &cookie );
350 if (!suspended) NtResumeThread( handle, &ret );
352 if (id) *id = client_id;
353 if (handle_ptr) *handle_ptr = handle;
354 else NtClose( handle );
356 if (actctx) RtlReleaseActivationContext( actctx );
357 return status;
361 /**********************************************************************
362 * RtlCreateUserStack (NTDLL.@)
364 NTSTATUS WINAPI RtlCreateUserStack( SIZE_T commit, SIZE_T reserve, ULONG zero_bits,
365 SIZE_T commit_align, SIZE_T reserve_align, INITIAL_TEB *stack )
367 PROCESS_STACK_ALLOCATION_INFORMATION alloc;
368 NTSTATUS status;
370 TRACE("commit %#Ix, reserve %#Ix, zero_bits %lu, commit_align %#Ix, reserve_align %#Ix, stack %p\n",
371 commit, reserve, zero_bits, commit_align, reserve_align, stack);
373 if (!commit_align || !reserve_align)
374 return STATUS_INVALID_PARAMETER;
376 if (!commit || !reserve)
378 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( NtCurrentTeb()->Peb->ImageBaseAddress );
379 if (!reserve) reserve = nt->OptionalHeader.SizeOfStackReserve;
380 if (!commit) commit = nt->OptionalHeader.SizeOfStackCommit;
383 reserve = (reserve + reserve_align - 1) & ~(reserve_align - 1);
384 commit = (commit + commit_align - 1) & ~(commit_align - 1);
386 if (reserve < commit) reserve = commit;
387 if (reserve < 0x100000) reserve = 0x100000;
388 reserve = (reserve + 0xffff) & ~0xffff; /* round to 64K boundary */
390 alloc.ReserveSize = reserve;
391 alloc.ZeroBits = zero_bits;
392 status = NtSetInformationProcess( GetCurrentProcess(), ProcessThreadStackAllocation,
393 &alloc, sizeof(alloc) );
394 if (!status)
396 void *addr = alloc.StackBase;
397 SIZE_T size = page_size;
399 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_NOACCESS );
400 addr = (char *)alloc.StackBase + page_size;
401 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE | PAGE_GUARD );
402 addr = (char *)alloc.StackBase + 2 * page_size;
403 size = reserve - 2 * page_size;
404 NtAllocateVirtualMemory( GetCurrentProcess(), &addr, 0, &size, MEM_COMMIT, PAGE_READWRITE );
406 /* note: limit is lower than base since the stack grows down */
407 stack->OldStackBase = 0;
408 stack->OldStackLimit = 0;
409 stack->DeallocationStack = alloc.StackBase;
410 stack->StackBase = (char *)alloc.StackBase + reserve;
411 stack->StackLimit = (char *)alloc.StackBase + 2 * page_size;
413 return status;
417 /**********************************************************************
418 * RtlFreeUserStack (NTDLL.@)
420 void WINAPI RtlFreeUserStack( void *stack )
422 SIZE_T size = 0;
424 TRACE("stack %p\n", stack);
426 NtFreeVirtualMemory( NtCurrentProcess(), &stack, &size, MEM_RELEASE );
430 /******************************************************************************
431 * RtlGetNtGlobalFlags (NTDLL.@)
433 ULONG WINAPI RtlGetNtGlobalFlags(void)
435 return NtCurrentTeb()->Peb->NtGlobalFlag;
439 /******************************************************************************
440 * RtlPushFrame (NTDLL.@)
442 void WINAPI RtlPushFrame( TEB_ACTIVE_FRAME *frame )
444 frame->Previous = NtCurrentTeb()->ActiveFrame;
445 NtCurrentTeb()->ActiveFrame = frame;
449 /******************************************************************************
450 * RtlPopFrame (NTDLL.@)
452 void WINAPI RtlPopFrame( TEB_ACTIVE_FRAME *frame )
454 NtCurrentTeb()->ActiveFrame = frame->Previous;
458 /******************************************************************************
459 * RtlGetFrame (NTDLL.@)
461 TEB_ACTIVE_FRAME * WINAPI RtlGetFrame(void)
463 return NtCurrentTeb()->ActiveFrame;
467 /***********************************************************************
468 * Fibers
469 ***********************************************************************/
472 static GLOBAL_FLS_DATA fls_data = { { NULL }, { &fls_data.fls_list_head, &fls_data.fls_list_head } };
474 static RTL_CRITICAL_SECTION fls_section;
475 static RTL_CRITICAL_SECTION_DEBUG fls_critsect_debug =
477 0, 0, &fls_section,
478 { &fls_critsect_debug.ProcessLocksList, &fls_critsect_debug.ProcessLocksList },
479 0, 0, { (DWORD_PTR)(__FILE__ ": fls_section") }
481 static RTL_CRITICAL_SECTION fls_section = { &fls_critsect_debug, -1, 0, 0, 0, 0 };
483 #define MAX_FLS_DATA_COUNT 0xff0
485 static void lock_fls_data(void)
487 RtlEnterCriticalSection( &fls_section );
490 static void unlock_fls_data(void)
492 RtlLeaveCriticalSection( &fls_section );
495 static unsigned int fls_chunk_size( unsigned int chunk_index )
497 return 0x10 << chunk_index;
500 static unsigned int fls_index_from_chunk_index( unsigned int chunk_index, unsigned int index )
502 return 0x10 * ((1 << chunk_index) - 1) + index;
505 static unsigned int fls_chunk_index_from_index( unsigned int index, unsigned int *index_in_chunk )
507 unsigned int chunk_index = 0;
509 while (index >= fls_chunk_size( chunk_index ))
510 index -= fls_chunk_size( chunk_index++ );
512 *index_in_chunk = index;
513 return chunk_index;
516 TEB_FLS_DATA *fls_alloc_data(void)
518 TEB_FLS_DATA *fls;
520 if (!(fls = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*fls) )))
521 return NULL;
523 lock_fls_data();
524 InsertTailList( &fls_data.fls_list_head, &fls->fls_list_entry );
525 unlock_fls_data();
527 return fls;
531 /***********************************************************************
532 * RtlFlsAlloc (NTDLL.@)
534 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsAlloc( PFLS_CALLBACK_FUNCTION callback, ULONG *ret_index )
536 unsigned int chunk_index, index, i;
537 FLS_INFO_CHUNK *chunk;
538 TEB_FLS_DATA *fls;
540 if (!(fls = NtCurrentTeb()->FlsSlots)
541 && !(NtCurrentTeb()->FlsSlots = fls = fls_alloc_data()))
542 return STATUS_NO_MEMORY;
544 lock_fls_data();
545 for (i = 0; i < ARRAY_SIZE(fls_data.fls_callback_chunks); ++i)
547 if (!fls_data.fls_callback_chunks[i] || fls_data.fls_callback_chunks[i]->count < fls_chunk_size( i ))
548 break;
551 if ((chunk_index = i) == ARRAY_SIZE(fls_data.fls_callback_chunks))
553 unlock_fls_data();
554 return STATUS_NO_MEMORY;
557 if ((chunk = fls_data.fls_callback_chunks[chunk_index]))
559 for (index = 0; index < fls_chunk_size( chunk_index ); ++index)
560 if (!chunk->callbacks[index].callback)
561 break;
562 assert( index < fls_chunk_size( chunk_index ));
564 else
566 fls_data.fls_callback_chunks[chunk_index] = chunk = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
567 offsetof(FLS_INFO_CHUNK, callbacks) + sizeof(*chunk->callbacks) * fls_chunk_size( chunk_index ));
568 if (!chunk)
570 unlock_fls_data();
571 return STATUS_NO_MEMORY;
574 if (chunk_index)
576 index = 0;
578 else
580 chunk->count = 1; /* FLS index 0 is prohibited. */
581 chunk->callbacks[0].callback = (void *)~(ULONG_PTR)0;
582 index = 1;
586 ++chunk->count;
587 chunk->callbacks[index].callback = callback ? callback : (PFLS_CALLBACK_FUNCTION)~(ULONG_PTR)0;
589 if ((*ret_index = fls_index_from_chunk_index( chunk_index, index )) > fls_data.fls_high_index)
590 fls_data.fls_high_index = *ret_index;
592 unlock_fls_data();
594 return STATUS_SUCCESS;
598 /***********************************************************************
599 * RtlFlsFree (NTDLL.@)
601 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsFree( ULONG index )
603 PFLS_CALLBACK_FUNCTION callback;
604 unsigned int chunk_index, idx;
605 FLS_INFO_CHUNK *chunk;
606 LIST_ENTRY *entry;
608 lock_fls_data();
610 if (!index || index > fls_data.fls_high_index)
612 unlock_fls_data();
613 return STATUS_INVALID_PARAMETER;
616 chunk_index = fls_chunk_index_from_index( index, &idx );
617 if (!(chunk = fls_data.fls_callback_chunks[chunk_index])
618 || !(callback = chunk->callbacks[idx].callback))
620 unlock_fls_data();
621 return STATUS_INVALID_PARAMETER;
624 for (entry = fls_data.fls_list_head.Flink; entry != &fls_data.fls_list_head; entry = entry->Flink)
626 TEB_FLS_DATA *fls = CONTAINING_RECORD(entry, TEB_FLS_DATA, fls_list_entry);
628 if (fls->fls_data_chunks[chunk_index] && fls->fls_data_chunks[chunk_index][idx + 1])
630 if (callback != (void *)~(ULONG_PTR)0)
632 TRACE_(relay)("Calling FLS callback %p, arg %p.\n", callback,
633 fls->fls_data_chunks[chunk_index][idx + 1]);
635 callback( fls->fls_data_chunks[chunk_index][idx + 1] );
637 fls->fls_data_chunks[chunk_index][idx + 1] = NULL;
641 --chunk->count;
642 chunk->callbacks[idx].callback = NULL;
644 unlock_fls_data();
645 return STATUS_SUCCESS;
649 /***********************************************************************
650 * RtlFlsSetValue (NTDLL.@)
652 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsSetValue( ULONG index, void *data )
654 unsigned int chunk_index, idx;
655 TEB_FLS_DATA *fls;
657 if (!index || index >= MAX_FLS_DATA_COUNT)
658 return STATUS_INVALID_PARAMETER;
660 if (!(fls = NtCurrentTeb()->FlsSlots)
661 && !(NtCurrentTeb()->FlsSlots = fls = fls_alloc_data()))
662 return STATUS_NO_MEMORY;
664 chunk_index = fls_chunk_index_from_index( index, &idx );
666 if (!fls->fls_data_chunks[chunk_index] &&
667 !(fls->fls_data_chunks[chunk_index] = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
668 (fls_chunk_size( chunk_index ) + 1) * sizeof(*fls->fls_data_chunks[chunk_index]) )))
669 return STATUS_NO_MEMORY;
671 fls->fls_data_chunks[chunk_index][idx + 1] = data;
673 return STATUS_SUCCESS;
677 /***********************************************************************
678 * RtlFlsGetValue (NTDLL.@)
680 NTSTATUS WINAPI DECLSPEC_HOTPATCH RtlFlsGetValue( ULONG index, void **data )
682 unsigned int chunk_index, idx;
683 TEB_FLS_DATA *fls;
685 if (!index || index >= MAX_FLS_DATA_COUNT || !(fls = NtCurrentTeb()->FlsSlots))
686 return STATUS_INVALID_PARAMETER;
688 chunk_index = fls_chunk_index_from_index( index, &idx );
690 *data = fls->fls_data_chunks[chunk_index] ? fls->fls_data_chunks[chunk_index][idx + 1] : NULL;
691 return STATUS_SUCCESS;
695 /***********************************************************************
696 * RtlProcessFlsData (NTDLL.@)
698 void WINAPI DECLSPEC_HOTPATCH RtlProcessFlsData( void *teb_fls_data, ULONG flags )
700 TEB_FLS_DATA *fls = teb_fls_data;
701 unsigned int i, index;
703 TRACE_(thread)( "teb_fls_data %p, flags %#lx.\n", teb_fls_data, flags );
705 if (flags & ~3)
706 FIXME_(thread)( "Unknown flags %#lx.\n", flags );
708 if (!fls)
709 return;
711 if (flags & 1)
713 lock_fls_data();
714 for (i = 0; i < ARRAY_SIZE(fls->fls_data_chunks); ++i)
716 if (!fls->fls_data_chunks[i] || !fls_data.fls_callback_chunks[i]
717 || !fls_data.fls_callback_chunks[i]->count)
718 continue;
720 for (index = 0; index < fls_chunk_size( i ); ++index)
722 PFLS_CALLBACK_FUNCTION callback = fls_data.fls_callback_chunks[i]->callbacks[index].callback;
724 if (!fls->fls_data_chunks[i][index + 1])
725 continue;
727 if (callback && callback != (void *)~(ULONG_PTR)0)
729 TRACE_(relay)("Calling FLS callback %p, arg %p.\n", callback,
730 fls->fls_data_chunks[i][index + 1]);
732 callback( fls->fls_data_chunks[i][index + 1] );
734 fls->fls_data_chunks[i][index + 1] = NULL;
737 /* Not using RemoveEntryList() as Windows does not zero list entry here. */
738 fls->fls_list_entry.Flink->Blink = fls->fls_list_entry.Blink;
739 fls->fls_list_entry.Blink->Flink = fls->fls_list_entry.Flink;
740 unlock_fls_data();
743 if (flags & 2)
745 for (i = 0; i < ARRAY_SIZE(fls->fls_data_chunks); ++i)
746 RtlFreeHeap( GetProcessHeap(), 0, fls->fls_data_chunks[i] );
748 RtlFreeHeap( GetProcessHeap(), 0, fls );