ThreadEmulation.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //
  8. //
  9. // Emulates a subset of the Win32 threading API as a layer on top of WinRT threadpools.
  10. //
  11. // Supported features:
  12. //
  13. // - CreateThread (returns a standard Win32 handle which can be waited on, then closed)
  14. // - CREATE_SUSPENDED and ResumeThread
  15. // - Partial support for SetThreadPriority (see below)
  16. // - Sleep
  17. // - Thread local storage (TlsAlloc, TlsFree, TlsGetValue, TlsSetValue)
  18. //
  19. // Differences from Win32:
  20. //
  21. // - If using TLS other than from this CreateThread emulation, call TlsShutdown before thread/task exit
  22. // - No ExitThread or TerminateThread (just return from the thread function to exit)
  23. // - No SuspendThread, so ResumeThread is only useful in combination with CREATE_SUSPENDED
  24. // - SetThreadPriority is only available while a thread is in CREATE_SUSPENDED state
  25. // - SetThreadPriority only supports three priority levels (negative, zero, or positive)
  26. // - No thread identifier APIs (GetThreadId, GetCurrentThreadId, OpenThread)
  27. // - No affinity APIs
  28. // - No GetExitCodeThread
  29. // - Failure cases return error codes but do not always call SetLastError
  30. #pragma once
  31. #include <windows.h>
  32. namespace ThreadEmulation
  33. {
  34. #ifndef CREATE_SUSPENDED
  35. #define CREATE_SUSPENDED 0x00000004
  36. #endif
  37. HANDLE WINAPI CreateThread(_In_opt_ LPSECURITY_ATTRIBUTES unusedThreadAttributes, _In_ SIZE_T unusedStackSize, _In_ LPTHREAD_START_ROUTINE lpStartAddress, _In_opt_ LPVOID lpParameter, _In_ DWORD dwCreationFlags, _Out_opt_ LPDWORD unusedThreadId);
  38. DWORD WINAPI ResumeThread(_In_ HANDLE hThread);
  39. BOOL WINAPI SetThreadPriority(_In_ HANDLE hThread, _In_ int nPriority);
  40. VOID WINAPI Sleep(_In_ DWORD dwMilliseconds);
  41. DWORD WINAPI TlsAlloc();
  42. BOOL WINAPI TlsFree(_In_ DWORD dwTlsIndex);
  43. LPVOID WINAPI TlsGetValue(_In_ DWORD dwTlsIndex);
  44. BOOL WINAPI TlsSetValue(_In_ DWORD dwTlsIndex, _In_opt_ LPVOID lpTlsValue);
  45. void WINAPI TlsShutdown();
  46. }