using System;
using System.Runtime.InteropServices;
namespace Vanara.PInvoke
{
public static partial class Kernel32
{
/// Indicates that FlsAlloc cannout allocate an index.
public const uint FLS_OUT_OF_INDEXES = 0xFFFFFFFF;
///
/// An application-defined function. If the FLS slot is in use, FlsCallback is called on fiber deletion, thread exit, and when an FLS
/// index is freed. Specify this function when calling the FlsAlloc function. The PFLS_CALLBACK_FUNCTION type defines a pointer to
/// this callback function. FlsCallback is a placeholder for the application-defined function name.
///
/// The value stored in the FLS slot for the calling fiber.
public delegate void FlsCallback(IntPtr lpFlsData);
///
/// Allocates a fiber local storage (FLS) index. Any fiber in the process can subsequently use this index to store and retrieve
/// values that are local to the fiber.
///
///
/// A pointer to the application-defined callback function of type PFLS_CALLBACK_FUNCTION. This parameter is optional. For
/// more information, see FlsCallback.
///
///
/// If the function succeeds, the return value is an FLS index initialized to zero.
/// If the function fails, the return value is FLS_OUT_OF_INDEXES. To get extended error information, call GetLastError.
///
// DWORD WINAPI FlsAlloc( _In_ PFLS_CALLBACK_FUNCTION lpCallback); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682664(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("WinBase.h", MSDNShortId = "ms682664")]
public static extern uint FlsAlloc(FlsCallback lpCallback);
/// Releases a fiber local storage (FLS) index, making it available for reuse.
/// The FLS index that was allocated by the FlsAlloc function.
///
/// If the function succeeds, the return value is nonzero.
/// If the function fails, the return value is zero. To get extended error information, call GetLastError.
///
// BOOL WINAPI FlsFree( _In_ DWORD dwFlsIndex); https://msdn.microsoft.com/en-us/library/windows/desktop/ms682667(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("WinBase.h", MSDNShortId = "ms682667")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FlsFree(uint dwFlsIndex);
///
/// Retrieves the value in the calling fiber's fiber local storage (FLS) slot for the specified FLS index. Each fiber has its own
/// slot for each FLS index.
///
/// The FLS index that was allocated by the FlsAlloc function.
///
///
/// If the function succeeds, the return value is the value stored in the calling fiber's FLS slot associated with the specified index.
///
/// If the function fails, the return value is NULL. To get extended error information, call GetLastError.
///
// PVOID WINAPI FlsGetValue( _In_ DWORD dwFlsIndex); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683141(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("WinBase.h", MSDNShortId = "ms683141")]
public static extern IntPtr FlsGetValue(uint dwFlsIndex);
///
/// Stores a value in the calling fiber's fiber local storage (FLS) slot for the specified FLS index. Each fiber has its own slot for
/// each FLS index.
///
/// The FLS index that was allocated by the FlsAlloc function.
/// The value to be stored in the FLS slot for the calling fiber.
///
/// If the function succeeds, the return value is nonzero.
///
/// If the function fails, the return value is zero. To get extended error information, call GetLastError. The following
/// errors can be returned.
///
///
///
///
/// Return code
/// Description
///
/// -
/// ERROR_INVALID_PARAMETER
/// The index is not in range.
///
/// -
/// ERROR_NO_MEMORY
/// The FLS array has not been allocated.
///
///
///
///
// BOOL WINAPI FlsSetValue( _In_ DWORD dwFlsIndex, _In_opt_ PVOID lpFlsData); https://msdn.microsoft.com/en-us/library/windows/desktop/ms683146(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
[PInvokeData("WinBase.h", MSDNShortId = "ms683146")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FlsSetValue(uint dwFlsIndex, IntPtr lpFlsData);
/// Determines whether the current thread is a fiber.
/// The function returns TRUE if the thread is a fiber and FALSE otherwise.
// BOOL WINAPI IsThreadAFiber(void); https://msdn.microsoft.com/en-us/library/windows/desktop/ms684131(v=vs.85).aspx
[DllImport(Lib.Kernel32, SetLastError = false, ExactSpelling = true)]
[PInvokeData("WinBase.h", MSDNShortId = "ms684131")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsThreadAFiber();
}
}