kernel32: Implement IsThreadAFiber function.
[wine/multimedia.git] / dlls / kernel32 / fiber.c
blobc8666617c9b1ac4a867150daf71ba5a6c698214c
1 /*
2 * Fiber support
4 * Copyright 2002 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
20 * FIXME:
21 * - proper handling of 16-bit stack and signal stack
24 #include "config.h"
25 #include "wine/port.h"
27 #include <setjmp.h>
28 #include <stdarg.h>
30 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winternl.h"
35 #include "wine/exception.h"
36 #include "wine/library.h"
38 struct fiber_data
40 LPVOID param; /* 00 fiber param */
41 void *except; /* 04 saved exception handlers list */
42 void *stack_base; /* 08 top of fiber stack */
43 void *stack_limit; /* 0c fiber stack low-water mark */
44 void *stack_allocation; /* 10 base of the fiber stack allocation */
45 sigjmp_buf jmpbuf; /* 14 setjmp buffer (on Windows: CONTEXT) */
46 DWORD flags; /* fiber flags */
47 LPFIBER_START_ROUTINE start; /* start routine */
48 void **fls_slots; /* fiber storage slots */
52 /* call the fiber initial function once we have switched stack */
53 static void start_fiber( void *arg )
55 struct fiber_data *fiber = arg;
56 LPFIBER_START_ROUTINE start = fiber->start;
58 __TRY
60 fiber->start = NULL;
61 start( fiber->param );
62 ExitThread( 1 );
64 __EXCEPT(UnhandledExceptionFilter)
66 TerminateThread( GetCurrentThread(), GetExceptionCode() );
68 __ENDTRY
72 /***********************************************************************
73 * CreateFiber (KERNEL32.@)
75 LPVOID WINAPI CreateFiber( SIZE_T stack, LPFIBER_START_ROUTINE start, LPVOID param )
77 return CreateFiberEx( stack, 0, 0, start, param );
81 /***********************************************************************
82 * CreateFiberEx (KERNEL32.@)
84 LPVOID WINAPI CreateFiberEx( SIZE_T stack_commit, SIZE_T stack_reserve, DWORD flags,
85 LPFIBER_START_ROUTINE start, LPVOID param )
87 struct fiber_data *fiber;
89 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
91 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
92 return NULL;
95 /* FIXME: should use the thread stack allocation routines here */
96 if (!stack_reserve) stack_reserve = 1024*1024;
97 if(!(fiber->stack_allocation = VirtualAlloc( 0, stack_reserve, MEM_COMMIT, PAGE_READWRITE )))
99 HeapFree( GetProcessHeap(), 0, fiber );
100 return NULL;
102 fiber->stack_base = (char *)fiber->stack_allocation + stack_reserve;
103 fiber->stack_limit = fiber->stack_allocation;
104 fiber->param = param;
105 fiber->except = (void *)-1;
106 fiber->start = start;
107 fiber->flags = flags;
108 fiber->fls_slots = NULL;
109 return fiber;
113 /***********************************************************************
114 * DeleteFiber (KERNEL32.@)
116 void WINAPI DeleteFiber( LPVOID fiber_ptr )
118 struct fiber_data *fiber = fiber_ptr;
120 if (!fiber) return;
121 if (fiber == NtCurrentTeb()->Tib.u.FiberData)
123 HeapFree( GetProcessHeap(), 0, fiber );
124 ExitThread(1);
126 VirtualFree( fiber->stack_allocation, 0, MEM_RELEASE );
127 HeapFree( GetProcessHeap(), 0, fiber->fls_slots );
128 HeapFree( GetProcessHeap(), 0, fiber );
132 /***********************************************************************
133 * ConvertThreadToFiber (KERNEL32.@)
135 LPVOID WINAPI ConvertThreadToFiber( LPVOID param )
137 return ConvertThreadToFiberEx( param, 0 );
141 /***********************************************************************
142 * ConvertThreadToFiberEx (KERNEL32.@)
144 LPVOID WINAPI ConvertThreadToFiberEx( LPVOID param, DWORD flags )
146 struct fiber_data *fiber;
148 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
150 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
151 return NULL;
153 fiber->param = param;
154 fiber->except = NtCurrentTeb()->Tib.ExceptionList;
155 fiber->stack_base = NtCurrentTeb()->Tib.StackBase;
156 fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
157 fiber->stack_allocation = NtCurrentTeb()->DeallocationStack;
158 fiber->start = NULL;
159 fiber->flags = flags;
160 fiber->fls_slots = NtCurrentTeb()->FlsSlots;
161 NtCurrentTeb()->Tib.u.FiberData = fiber;
162 return fiber;
166 /***********************************************************************
167 * ConvertFiberToThread (KERNEL32.@)
169 BOOL WINAPI ConvertFiberToThread(void)
171 struct fiber_data *fiber = NtCurrentTeb()->Tib.u.FiberData;
173 if (fiber)
175 NtCurrentTeb()->Tib.u.FiberData = NULL;
176 HeapFree( GetProcessHeap(), 0, fiber );
178 return TRUE;
182 /***********************************************************************
183 * SwitchToFiber (KERNEL32.@)
185 void WINAPI SwitchToFiber( LPVOID fiber )
187 struct fiber_data *new_fiber = fiber;
188 struct fiber_data *current_fiber = NtCurrentTeb()->Tib.u.FiberData;
190 current_fiber->except = NtCurrentTeb()->Tib.ExceptionList;
191 current_fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
192 current_fiber->fls_slots = NtCurrentTeb()->FlsSlots;
193 /* stack_allocation and stack_base never change */
195 /* FIXME: should save floating point context if requested in fiber->flags */
196 if (!sigsetjmp( current_fiber->jmpbuf, 0 ))
198 NtCurrentTeb()->Tib.u.FiberData = new_fiber;
199 NtCurrentTeb()->Tib.ExceptionList = new_fiber->except;
200 NtCurrentTeb()->Tib.StackBase = new_fiber->stack_base;
201 NtCurrentTeb()->Tib.StackLimit = new_fiber->stack_limit;
202 NtCurrentTeb()->DeallocationStack = new_fiber->stack_allocation;
203 NtCurrentTeb()->FlsSlots = new_fiber->fls_slots;
204 if (new_fiber->start) /* first time */
205 wine_switch_to_stack( start_fiber, new_fiber, new_fiber->stack_base );
206 else
207 siglongjmp( new_fiber->jmpbuf, 1 );
211 /***********************************************************************
212 * FlsAlloc (KERNEL32.@)
214 DWORD WINAPI FlsAlloc( PFLS_CALLBACK_FUNCTION callback )
216 DWORD index;
217 PEB * const peb = NtCurrentTeb()->Peb;
219 RtlAcquirePebLock();
220 if (!peb->FlsCallback &&
221 !(peb->FlsCallback = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
222 8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
224 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
225 index = FLS_OUT_OF_INDEXES;
227 else
229 index = RtlFindClearBitsAndSet( peb->FlsBitmap, 1, 0 );
230 if (index != ~0U)
232 if (!NtCurrentTeb()->FlsSlots &&
233 !(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
234 8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
236 RtlClearBits( peb->FlsBitmap, index, 1 );
237 index = FLS_OUT_OF_INDEXES;
238 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
240 else
242 NtCurrentTeb()->FlsSlots[index] = 0; /* clear the value */
243 peb->FlsCallback[index] = callback;
246 else SetLastError( ERROR_NO_MORE_ITEMS );
248 RtlReleasePebLock();
249 return index;
252 /***********************************************************************
253 * FlsFree (KERNEL32.@)
255 BOOL WINAPI FlsFree( DWORD index )
257 BOOL ret;
259 RtlAcquirePebLock();
260 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
261 if (ret) RtlClearBits( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
262 if (ret)
264 /* FIXME: call Fls callback */
265 /* FIXME: add equivalent of ThreadZeroTlsCell here */
266 if (NtCurrentTeb()->FlsSlots) NtCurrentTeb()->FlsSlots[index] = 0;
268 else SetLastError( ERROR_INVALID_PARAMETER );
269 RtlReleasePebLock();
270 return TRUE;
273 /***********************************************************************
274 * FlsGetValue (KERNEL32.@)
276 PVOID WINAPI FlsGetValue( DWORD index )
278 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) || !NtCurrentTeb()->FlsSlots)
280 SetLastError( ERROR_INVALID_PARAMETER );
281 return NULL;
283 SetLastError( ERROR_SUCCESS );
284 return NtCurrentTeb()->FlsSlots[index];
287 /***********************************************************************
288 * FlsSetValue (KERNEL32.@)
290 BOOL WINAPI FlsSetValue( DWORD index, PVOID data )
292 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits))
294 SetLastError( ERROR_INVALID_PARAMETER );
295 return FALSE;
297 if (!NtCurrentTeb()->FlsSlots &&
298 !(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
299 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) * sizeof(void*) )))
301 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
302 return FALSE;
304 NtCurrentTeb()->FlsSlots[index] = data;
305 return TRUE;
308 /***********************************************************************
309 * IsThreadAFiber (KERNEL32.@)
311 BOOL WINAPI IsThreadAFiber(void)
313 return NtCurrentTeb()->Tib.u.FiberData != NULL;