oleaut32: Move the debug logging of the variable type in SLTG_DoVars.
[wine/gsoc_dplay.git] / dlls / kernel32 / fiber.c
blobfda5f92483317adc305ccb1fe17970525e68f6a6
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"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(fiber);
41 struct fiber_data
43 LPVOID param; /* 00 fiber param */
44 void *except; /* 04 saved exception handlers list */
45 void *stack_base; /* 08 top of fiber stack */
46 void *stack_limit; /* 0c fiber stack low-water mark */
47 void *stack_allocation; /* 10 base of the fiber stack allocation */
48 sigjmp_buf jmpbuf; /* 14 setjmp buffer (on Windows: CONTEXT) */
49 DWORD flags; /* fiber flags */
50 LPFIBER_START_ROUTINE start; /* start routine */
51 void **fls_slots; /* fiber storage slots */
55 /* call the fiber initial function once we have switched stack */
56 static void start_fiber( void *arg )
58 struct fiber_data *fiber = arg;
59 LPFIBER_START_ROUTINE start = fiber->start;
61 __TRY
63 fiber->start = NULL;
64 start( fiber->param );
65 ExitThread( 1 );
67 __EXCEPT(UnhandledExceptionFilter)
69 TerminateThread( GetCurrentThread(), GetExceptionCode() );
71 __ENDTRY
75 /***********************************************************************
76 * CreateFiber (KERNEL32.@)
78 LPVOID WINAPI CreateFiber( SIZE_T stack, LPFIBER_START_ROUTINE start, LPVOID param )
80 return CreateFiberEx( stack, 0, 0, start, param );
84 /***********************************************************************
85 * CreateFiberEx (KERNEL32.@)
87 LPVOID WINAPI CreateFiberEx( SIZE_T stack_commit, SIZE_T stack_reserve, DWORD flags,
88 LPFIBER_START_ROUTINE start, LPVOID param )
90 struct fiber_data *fiber;
92 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
94 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
95 return NULL;
98 /* FIXME: should use the thread stack allocation routines here */
99 if (!stack_reserve) stack_reserve = 1024*1024;
100 if(!(fiber->stack_allocation = VirtualAlloc( 0, stack_reserve, MEM_COMMIT, PAGE_READWRITE )))
102 HeapFree( GetProcessHeap(), 0, fiber );
103 return NULL;
105 fiber->stack_base = (char *)fiber->stack_allocation + stack_reserve;
106 fiber->stack_limit = fiber->stack_allocation;
107 fiber->param = param;
108 fiber->except = (void *)-1;
109 fiber->start = start;
110 fiber->flags = flags;
111 fiber->fls_slots = NULL;
112 return fiber;
116 /***********************************************************************
117 * DeleteFiber (KERNEL32.@)
119 void WINAPI DeleteFiber( LPVOID fiber_ptr )
121 struct fiber_data *fiber = fiber_ptr;
123 if (!fiber) return;
124 if (fiber == NtCurrentTeb()->Tib.u.FiberData)
126 HeapFree( GetProcessHeap(), 0, fiber );
127 ExitThread(1);
129 VirtualFree( fiber->stack_allocation, 0, MEM_RELEASE );
130 HeapFree( GetProcessHeap(), 0, fiber->fls_slots );
131 HeapFree( GetProcessHeap(), 0, fiber );
135 /***********************************************************************
136 * ConvertThreadToFiber (KERNEL32.@)
138 LPVOID WINAPI ConvertThreadToFiber( LPVOID param )
140 return ConvertThreadToFiberEx( param, 0 );
144 /***********************************************************************
145 * ConvertThreadToFiberEx (KERNEL32.@)
147 LPVOID WINAPI ConvertThreadToFiberEx( LPVOID param, DWORD flags )
149 struct fiber_data *fiber;
151 if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
153 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
154 return NULL;
156 fiber->param = param;
157 fiber->except = NtCurrentTeb()->Tib.ExceptionList;
158 fiber->stack_base = NtCurrentTeb()->Tib.StackBase;
159 fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
160 fiber->stack_allocation = NtCurrentTeb()->DeallocationStack;
161 fiber->start = NULL;
162 fiber->flags = flags;
163 fiber->fls_slots = NtCurrentTeb()->FlsSlots;
164 NtCurrentTeb()->Tib.u.FiberData = fiber;
165 return fiber;
169 /***********************************************************************
170 * ConvertFiberToThread (KERNEL32.@)
172 BOOL WINAPI ConvertFiberToThread(void)
174 struct fiber_data *fiber = NtCurrentTeb()->Tib.u.FiberData;
176 if (fiber)
178 NtCurrentTeb()->Tib.u.FiberData = NULL;
179 HeapFree( GetProcessHeap(), 0, fiber );
181 return TRUE;
185 /***********************************************************************
186 * SwitchToFiber (KERNEL32.@)
188 void WINAPI SwitchToFiber( LPVOID fiber )
190 struct fiber_data *new_fiber = fiber;
191 struct fiber_data *current_fiber = NtCurrentTeb()->Tib.u.FiberData;
193 current_fiber->except = NtCurrentTeb()->Tib.ExceptionList;
194 current_fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
195 current_fiber->fls_slots = NtCurrentTeb()->FlsSlots;
196 /* stack_allocation and stack_base never change */
198 /* FIXME: should save floating point context if requested in fiber->flags */
199 if (!sigsetjmp( current_fiber->jmpbuf, 0 ))
201 NtCurrentTeb()->Tib.u.FiberData = new_fiber;
202 NtCurrentTeb()->Tib.ExceptionList = new_fiber->except;
203 NtCurrentTeb()->Tib.StackBase = new_fiber->stack_base;
204 NtCurrentTeb()->Tib.StackLimit = new_fiber->stack_limit;
205 NtCurrentTeb()->DeallocationStack = new_fiber->stack_allocation;
206 NtCurrentTeb()->FlsSlots = new_fiber->fls_slots;
207 if (new_fiber->start) /* first time */
208 wine_switch_to_stack( start_fiber, new_fiber, new_fiber->stack_base );
209 else
210 siglongjmp( new_fiber->jmpbuf, 1 );
214 /***********************************************************************
215 * FlsAlloc (KERNEL32.@)
217 DWORD WINAPI FlsAlloc( PFLS_CALLBACK_FUNCTION callback )
219 DWORD index;
220 PEB * const peb = NtCurrentTeb()->Peb;
222 RtlAcquirePebLock();
223 if (!peb->FlsCallback &&
224 !(peb->FlsCallback = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
225 8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
227 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
228 index = FLS_OUT_OF_INDEXES;
230 else
232 index = RtlFindClearBitsAndSet( peb->FlsBitmap, 1, 0 );
233 if (index != ~0U)
235 if (!NtCurrentTeb()->FlsSlots &&
236 !(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
237 8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
239 RtlClearBits( peb->FlsBitmap, index, 1 );
240 index = FLS_OUT_OF_INDEXES;
241 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
243 else
245 NtCurrentTeb()->FlsSlots[index] = 0; /* clear the value */
246 peb->FlsCallback[index] = callback;
249 else SetLastError( ERROR_NO_MORE_ITEMS );
251 RtlReleasePebLock();
252 return index;
255 /***********************************************************************
256 * FlsFree (KERNEL32.@)
258 BOOL WINAPI FlsFree( DWORD index )
260 BOOL ret;
262 RtlAcquirePebLock();
263 ret = RtlAreBitsSet( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
264 if (ret) RtlClearBits( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
265 if (ret)
267 /* FIXME: call Fls callback */
268 /* FIXME: add equivalent of ThreadZeroTlsCell here */
269 if (NtCurrentTeb()->FlsSlots) NtCurrentTeb()->FlsSlots[index] = 0;
271 else SetLastError( ERROR_INVALID_PARAMETER );
272 RtlReleasePebLock();
273 return TRUE;
276 /***********************************************************************
277 * FlsGetValue (KERNEL32.@)
279 PVOID WINAPI FlsGetValue( DWORD index )
281 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) || !NtCurrentTeb()->FlsSlots)
283 SetLastError( ERROR_INVALID_PARAMETER );
284 return NULL;
286 SetLastError( ERROR_SUCCESS );
287 return NtCurrentTeb()->FlsSlots[index];
290 /***********************************************************************
291 * FlsSetValue (KERNEL32.@)
293 BOOL WINAPI FlsSetValue( DWORD index, PVOID data )
295 if (index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits))
297 SetLastError( ERROR_INVALID_PARAMETER );
298 return FALSE;
300 if (!NtCurrentTeb()->FlsSlots &&
301 !(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
302 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) * sizeof(void*) )))
304 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
305 return FALSE;
307 NtCurrentTeb()->FlsSlots[index] = data;
308 return TRUE;