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
21 * - proper handling of 16-bit stack and signal stack
25 #include "wine/port.h"
30 #define NONAMELESSUNION
35 #include "wine/exception.h"
36 #include "wine/library.h"
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
;
61 start( fiber
->param
);
64 __EXCEPT(UnhandledExceptionFilter
)
66 TerminateThread( GetCurrentThread(), GetExceptionCode() );
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
);
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
);
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
;
113 /***********************************************************************
114 * DeleteFiber (KERNEL32.@)
116 void WINAPI
DeleteFiber( LPVOID fiber_ptr
)
118 struct fiber_data
*fiber
= fiber_ptr
;
121 if (fiber
== NtCurrentTeb()->Tib
.u
.FiberData
)
123 HeapFree( GetProcessHeap(), 0, fiber
);
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
);
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
;
159 fiber
->flags
= flags
;
160 fiber
->fls_slots
= NtCurrentTeb()->FlsSlots
;
161 NtCurrentTeb()->Tib
.u
.FiberData
= fiber
;
166 /***********************************************************************
167 * ConvertFiberToThread (KERNEL32.@)
169 BOOL WINAPI
ConvertFiberToThread(void)
171 struct fiber_data
*fiber
= NtCurrentTeb()->Tib
.u
.FiberData
;
175 NtCurrentTeb()->Tib
.u
.FiberData
= NULL
;
176 HeapFree( GetProcessHeap(), 0, fiber
);
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
);
207 siglongjmp( new_fiber
->jmpbuf
, 1 );
211 /***********************************************************************
212 * FlsAlloc (KERNEL32.@)
214 DWORD WINAPI
FlsAlloc( PFLS_CALLBACK_FUNCTION callback
)
217 PEB
* const peb
= NtCurrentTeb()->Peb
;
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
;
229 index
= RtlFindClearBitsAndSet( peb
->FlsBitmap
, 1, 0 );
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
);
242 NtCurrentTeb()->FlsSlots
[index
] = 0; /* clear the value */
243 peb
->FlsCallback
[index
] = callback
;
246 else SetLastError( ERROR_NO_MORE_ITEMS
);
252 /***********************************************************************
253 * FlsFree (KERNEL32.@)
255 BOOL WINAPI
FlsFree( DWORD index
)
260 ret
= RtlAreBitsSet( NtCurrentTeb()->Peb
->FlsBitmap
, index
, 1 );
261 if (ret
) RtlClearBits( NtCurrentTeb()->Peb
->FlsBitmap
, index
, 1 );
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
);
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
);
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
);
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
);
304 NtCurrentTeb()->FlsSlots
[index
] = data
;
308 /***********************************************************************
309 * IsThreadAFiber (KERNEL32.@)
311 BOOL WINAPI
IsThreadAFiber(void)
313 return NtCurrentTeb()->Tib
.u
.FiberData
!= NULL
;