ThreadEmulation.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifdef __cplusplus
  33. //namespace ThreadEmulation
  34. //{
  35. extern "C"
  36. {
  37. #endif
  38. #ifndef CREATE_SUSPENDED
  39. #define CREATE_SUSPENDED 0x00000004
  40. #endif
  41. HANDLE WINAPI CreateThreadRT(_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);
  42. DWORD WINAPI ResumeThreadRT(_In_ HANDLE hThread);
  43. BOOL WINAPI SetThreadPriorityRT(_In_ HANDLE hThread, _In_ int nPriority);
  44. VOID WINAPI SleepRT(_In_ DWORD dwMilliseconds);
  45. DWORD WINAPI TlsAllocRT();
  46. BOOL WINAPI TlsFreeRT(_In_ DWORD dwTlsIndex);
  47. LPVOID WINAPI TlsGetValueRT(_In_ DWORD dwTlsIndex);
  48. BOOL WINAPI TlsSetValueRT(_In_ DWORD dwTlsIndex, _In_opt_ LPVOID lpTlsValue);
  49. void WINAPI TlsShutdown();
  50. #ifdef __cplusplus
  51. }
  52. #endif