thread.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/thread.h"
  5. #ifdef __APPLE__
  6. #include <mach/mach.h>
  7. #elif defined(_WIN32)
  8. #include <windows.h>
  9. #else
  10. #if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  11. #include <pthread_np.h>
  12. #else
  13. #include <pthread.h>
  14. #endif
  15. #include <sched.h>
  16. #endif
  17. #ifndef _WIN32
  18. #include <unistd.h>
  19. #endif
  20. #ifdef __FreeBSD__
  21. #define cpu_set_t cpuset_t
  22. #endif
  23. namespace Common {
  24. #ifdef _MSC_VER
  25. // Sets the debugger-visible name of the current thread.
  26. // Uses trick documented in:
  27. // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code
  28. void SetCurrentThreadName(const char* name) {
  29. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  30. #pragma pack(push, 8)
  31. struct THREADNAME_INFO {
  32. DWORD dwType; // must be 0x1000
  33. LPCSTR szName; // pointer to name (in user addr space)
  34. DWORD dwThreadID; // thread ID (-1=caller thread)
  35. DWORD dwFlags; // reserved for future use, must be zero
  36. } info;
  37. #pragma pack(pop)
  38. info.dwType = 0x1000;
  39. info.szName = name;
  40. info.dwThreadID = std::numeric_limits<DWORD>::max();
  41. info.dwFlags = 0;
  42. __try {
  43. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  44. } __except (EXCEPTION_CONTINUE_EXECUTION) {
  45. }
  46. }
  47. #else // !MSVC_VER, so must be POSIX threads
  48. // MinGW with the POSIX threading model does not support pthread_setname_np
  49. #if !defined(_WIN32) || defined(_MSC_VER)
  50. void SetCurrentThreadName(const char* name) {
  51. #ifdef __APPLE__
  52. pthread_setname_np(name);
  53. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  54. pthread_set_name_np(pthread_self(), name);
  55. #elif defined(__NetBSD__)
  56. pthread_setname_np(pthread_self(), "%s", (void*)name);
  57. #else
  58. pthread_setname_np(pthread_self(), name);
  59. #endif
  60. }
  61. #endif
  62. #endif
  63. } // namespace Common