2002-12-17 21:06:25 +00:00
|
|
|
/*
|
|
|
|
* Fiber support
|
|
|
|
*
|
|
|
|
* Copyright 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-12-17 21:06:25 +00:00
|
|
|
*
|
|
|
|
* FIXME:
|
|
|
|
* - proper handling of 16-bit stack and signal stack
|
|
|
|
*/
|
|
|
|
|
2010-01-26 12:05:28 +00:00
|
|
|
/* Fortify source chokes on siglongjmp stack switching, so disable it */
|
|
|
|
#undef _FORTIFY_SOURCE
|
|
|
|
#define _FORTIFY_SOURCE 0
|
|
|
|
|
2003-11-27 00:59:36 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2002-12-17 21:06:25 +00:00
|
|
|
#include <setjmp.h>
|
2003-09-05 23:08:26 +00:00
|
|
|
#include <stdarg.h>
|
2002-12-17 21:06:25 +00:00
|
|
|
|
2003-08-28 03:44:41 +00:00
|
|
|
#define NONAMELESSUNION
|
2003-09-05 23:08:26 +00:00
|
|
|
#include "windef.h"
|
2002-12-17 21:06:25 +00:00
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
2005-06-06 20:13:08 +00:00
|
|
|
#include "winternl.h"
|
2003-06-27 04:08:04 +00:00
|
|
|
#include "wine/exception.h"
|
2003-08-29 22:23:42 +00:00
|
|
|
#include "wine/library.h"
|
2002-12-17 21:06:25 +00:00
|
|
|
|
|
|
|
struct fiber_data
|
|
|
|
{
|
2003-08-28 03:44:41 +00:00
|
|
|
LPVOID param; /* 00 fiber param */
|
|
|
|
void *except; /* 04 saved exception handlers list */
|
|
|
|
void *stack_base; /* 08 top of fiber stack */
|
|
|
|
void *stack_limit; /* 0c fiber stack low-water mark */
|
|
|
|
void *stack_allocation; /* 10 base of the fiber stack allocation */
|
2003-10-24 04:30:13 +00:00
|
|
|
sigjmp_buf jmpbuf; /* 14 setjmp buffer (on Windows: CONTEXT) */
|
2003-08-28 03:44:41 +00:00
|
|
|
DWORD flags; /* fiber flags */
|
|
|
|
LPFIBER_START_ROUTINE start; /* start routine */
|
2008-02-29 18:56:04 +00:00
|
|
|
void **fls_slots; /* fiber storage slots */
|
2002-12-17 21:06:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* call the fiber initial function once we have switched stack */
|
2003-06-27 04:08:04 +00:00
|
|
|
static void start_fiber( void *arg )
|
2002-12-17 21:06:25 +00:00
|
|
|
{
|
2003-06-27 04:08:04 +00:00
|
|
|
struct fiber_data *fiber = arg;
|
2002-12-17 21:06:25 +00:00
|
|
|
LPFIBER_START_ROUTINE start = fiber->start;
|
|
|
|
|
2003-06-27 04:08:04 +00:00
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
fiber->start = NULL;
|
|
|
|
start( fiber->param );
|
|
|
|
ExitThread( 1 );
|
|
|
|
}
|
|
|
|
__EXCEPT(UnhandledExceptionFilter)
|
|
|
|
{
|
|
|
|
TerminateThread( GetCurrentThread(), GetExceptionCode() );
|
|
|
|
}
|
|
|
|
__ENDTRY
|
2002-12-17 21:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CreateFiber (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI CreateFiber( SIZE_T stack, LPFIBER_START_ROUTINE start, LPVOID param )
|
|
|
|
{
|
|
|
|
return CreateFiberEx( stack, 0, 0, start, param );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CreateFiberEx (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI CreateFiberEx( SIZE_T stack_commit, SIZE_T stack_reserve, DWORD flags,
|
|
|
|
LPFIBER_START_ROUTINE start, LPVOID param )
|
|
|
|
{
|
|
|
|
struct fiber_data *fiber;
|
2019-06-14 15:32:44 +00:00
|
|
|
INITIAL_TEB stack;
|
|
|
|
NTSTATUS status;
|
2002-12-17 21:06:25 +00:00
|
|
|
|
|
|
|
if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:32:44 +00:00
|
|
|
if ((status = RtlCreateUserStack( stack_commit, stack_reserve, 0, 1, 1, &stack )))
|
2002-12-17 21:06:25 +00:00
|
|
|
{
|
2019-06-14 15:32:44 +00:00
|
|
|
SetLastError( RtlNtStatusToDosError(status) );
|
2002-12-17 21:06:25 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-14 15:32:44 +00:00
|
|
|
|
|
|
|
fiber->stack_allocation = stack.DeallocationStack;
|
|
|
|
fiber->stack_base = stack.StackBase;
|
|
|
|
fiber->stack_limit = stack.StackLimit;
|
2003-08-28 03:44:41 +00:00
|
|
|
fiber->param = param;
|
|
|
|
fiber->except = (void *)-1;
|
|
|
|
fiber->start = start;
|
|
|
|
fiber->flags = flags;
|
2008-02-29 18:56:04 +00:00
|
|
|
fiber->fls_slots = NULL;
|
2002-12-17 21:06:25 +00:00
|
|
|
return fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* DeleteFiber (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
void WINAPI DeleteFiber( LPVOID fiber_ptr )
|
|
|
|
{
|
|
|
|
struct fiber_data *fiber = fiber_ptr;
|
|
|
|
|
|
|
|
if (!fiber) return;
|
2003-08-28 03:44:41 +00:00
|
|
|
if (fiber == NtCurrentTeb()->Tib.u.FiberData)
|
2002-12-17 21:06:25 +00:00
|
|
|
{
|
|
|
|
HeapFree( GetProcessHeap(), 0, fiber );
|
|
|
|
ExitThread(1);
|
|
|
|
}
|
2019-06-14 15:32:44 +00:00
|
|
|
RtlFreeUserStack( fiber->stack_allocation );
|
2008-02-29 18:56:04 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, fiber->fls_slots );
|
2002-12-17 21:06:25 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, fiber );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ConvertThreadToFiber (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI ConvertThreadToFiber( LPVOID param )
|
|
|
|
{
|
|
|
|
return ConvertThreadToFiberEx( param, 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ConvertThreadToFiberEx (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
LPVOID WINAPI ConvertThreadToFiberEx( LPVOID param, DWORD flags )
|
|
|
|
{
|
|
|
|
struct fiber_data *fiber;
|
|
|
|
|
|
|
|
if (!(fiber = HeapAlloc( GetProcessHeap(), 0, sizeof(*fiber) )))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-08-28 03:44:41 +00:00
|
|
|
fiber->param = param;
|
|
|
|
fiber->except = NtCurrentTeb()->Tib.ExceptionList;
|
|
|
|
fiber->stack_base = NtCurrentTeb()->Tib.StackBase;
|
|
|
|
fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
|
|
|
|
fiber->stack_allocation = NtCurrentTeb()->DeallocationStack;
|
|
|
|
fiber->start = NULL;
|
|
|
|
fiber->flags = flags;
|
2008-02-29 18:56:04 +00:00
|
|
|
fiber->fls_slots = NtCurrentTeb()->FlsSlots;
|
2003-08-28 03:44:41 +00:00
|
|
|
NtCurrentTeb()->Tib.u.FiberData = fiber;
|
2002-12-17 21:06:25 +00:00
|
|
|
return fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* ConvertFiberToThread (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI ConvertFiberToThread(void)
|
|
|
|
{
|
2003-08-28 03:44:41 +00:00
|
|
|
struct fiber_data *fiber = NtCurrentTeb()->Tib.u.FiberData;
|
2002-12-17 21:06:25 +00:00
|
|
|
|
|
|
|
if (fiber)
|
|
|
|
{
|
2003-08-28 03:44:41 +00:00
|
|
|
NtCurrentTeb()->Tib.u.FiberData = NULL;
|
2002-12-17 21:06:25 +00:00
|
|
|
HeapFree( GetProcessHeap(), 0, fiber );
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* SwitchToFiber (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
void WINAPI SwitchToFiber( LPVOID fiber )
|
|
|
|
{
|
|
|
|
struct fiber_data *new_fiber = fiber;
|
2003-08-28 03:44:41 +00:00
|
|
|
struct fiber_data *current_fiber = NtCurrentTeb()->Tib.u.FiberData;
|
2002-12-17 21:06:25 +00:00
|
|
|
|
2003-08-28 03:44:41 +00:00
|
|
|
current_fiber->except = NtCurrentTeb()->Tib.ExceptionList;
|
|
|
|
current_fiber->stack_limit = NtCurrentTeb()->Tib.StackLimit;
|
2008-02-29 18:56:04 +00:00
|
|
|
current_fiber->fls_slots = NtCurrentTeb()->FlsSlots;
|
2003-08-28 03:44:41 +00:00
|
|
|
/* stack_allocation and stack_base never change */
|
2002-12-17 21:06:25 +00:00
|
|
|
|
|
|
|
/* FIXME: should save floating point context if requested in fiber->flags */
|
2008-02-12 21:39:54 +00:00
|
|
|
if (!sigsetjmp( current_fiber->jmpbuf, 0 ))
|
2002-12-17 21:06:25 +00:00
|
|
|
{
|
2003-08-28 03:44:41 +00:00
|
|
|
NtCurrentTeb()->Tib.u.FiberData = new_fiber;
|
|
|
|
NtCurrentTeb()->Tib.ExceptionList = new_fiber->except;
|
|
|
|
NtCurrentTeb()->Tib.StackBase = new_fiber->stack_base;
|
|
|
|
NtCurrentTeb()->Tib.StackLimit = new_fiber->stack_limit;
|
|
|
|
NtCurrentTeb()->DeallocationStack = new_fiber->stack_allocation;
|
2008-02-29 18:56:04 +00:00
|
|
|
NtCurrentTeb()->FlsSlots = new_fiber->fls_slots;
|
2002-12-17 21:06:25 +00:00
|
|
|
if (new_fiber->start) /* first time */
|
2003-08-29 22:23:42 +00:00
|
|
|
wine_switch_to_stack( start_fiber, new_fiber, new_fiber->stack_base );
|
2002-12-17 21:06:25 +00:00
|
|
|
else
|
2003-10-24 04:30:13 +00:00
|
|
|
siglongjmp( new_fiber->jmpbuf, 1 );
|
2002-12-17 21:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-29 09:19:43 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FlsAlloc (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
DWORD WINAPI FlsAlloc( PFLS_CALLBACK_FUNCTION callback )
|
|
|
|
{
|
2008-02-29 18:56:04 +00:00
|
|
|
DWORD index;
|
|
|
|
PEB * const peb = NtCurrentTeb()->Peb;
|
2008-02-29 09:19:43 +00:00
|
|
|
|
2008-02-29 18:56:04 +00:00
|
|
|
RtlAcquirePebLock();
|
|
|
|
if (!peb->FlsCallback &&
|
|
|
|
!(peb->FlsCallback = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
index = FLS_OUT_OF_INDEXES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-28 15:20:21 +00:00
|
|
|
index = RtlFindClearBitsAndSet( peb->FlsBitmap, 1, 1 );
|
2008-02-29 18:56:04 +00:00
|
|
|
if (index != ~0U)
|
|
|
|
{
|
|
|
|
if (!NtCurrentTeb()->FlsSlots &&
|
|
|
|
!(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
8 * sizeof(peb->FlsBitmapBits) * sizeof(void*) )))
|
|
|
|
{
|
|
|
|
RtlClearBits( peb->FlsBitmap, index, 1 );
|
|
|
|
index = FLS_OUT_OF_INDEXES;
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NtCurrentTeb()->FlsSlots[index] = 0; /* clear the value */
|
|
|
|
peb->FlsCallback[index] = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else SetLastError( ERROR_NO_MORE_ITEMS );
|
|
|
|
}
|
|
|
|
RtlReleasePebLock();
|
|
|
|
return index;
|
2008-02-29 09:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FlsFree (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI FlsFree( DWORD index )
|
|
|
|
{
|
2008-02-29 18:56:04 +00:00
|
|
|
BOOL ret;
|
2008-02-29 09:19:43 +00:00
|
|
|
|
2008-02-29 18:56:04 +00:00
|
|
|
RtlAcquirePebLock();
|
|
|
|
ret = RtlAreBitsSet( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
|
|
|
|
if (ret) RtlClearBits( NtCurrentTeb()->Peb->FlsBitmap, index, 1 );
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
/* FIXME: call Fls callback */
|
|
|
|
/* FIXME: add equivalent of ThreadZeroTlsCell here */
|
|
|
|
if (NtCurrentTeb()->FlsSlots) NtCurrentTeb()->FlsSlots[index] = 0;
|
|
|
|
}
|
|
|
|
else SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
RtlReleasePebLock();
|
2010-03-22 14:43:20 +00:00
|
|
|
return ret;
|
2008-02-29 09:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FlsGetValue (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
PVOID WINAPI FlsGetValue( DWORD index )
|
|
|
|
{
|
2014-03-28 15:20:21 +00:00
|
|
|
if (!index || index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) || !NtCurrentTeb()->FlsSlots)
|
2008-02-29 18:56:04 +00:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
SetLastError( ERROR_SUCCESS );
|
|
|
|
return NtCurrentTeb()->FlsSlots[index];
|
2008-02-29 09:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* FlsSetValue (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI FlsSetValue( DWORD index, PVOID data )
|
|
|
|
{
|
2014-03-28 15:20:21 +00:00
|
|
|
if (!index || index >= 8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits))
|
2008-02-29 18:56:04 +00:00
|
|
|
{
|
|
|
|
SetLastError( ERROR_INVALID_PARAMETER );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!NtCurrentTeb()->FlsSlots &&
|
|
|
|
!(NtCurrentTeb()->FlsSlots = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
8 * sizeof(NtCurrentTeb()->Peb->FlsBitmapBits) * sizeof(void*) )))
|
|
|
|
{
|
|
|
|
SetLastError( ERROR_NOT_ENOUGH_MEMORY );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
NtCurrentTeb()->FlsSlots[index] = data;
|
|
|
|
return TRUE;
|
2008-02-29 09:19:43 +00:00
|
|
|
}
|
2010-01-24 15:31:19 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* IsThreadAFiber (KERNEL32.@)
|
|
|
|
*/
|
|
|
|
BOOL WINAPI IsThreadAFiber(void)
|
|
|
|
{
|
|
|
|
return NtCurrentTeb()->Tib.u.FiberData != NULL;
|
|
|
|
}
|