thread.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 undocumented (actually, it is now documented) trick.
  27. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxtsksettingthreadname.asp
  28. // This is implemented much nicer in upcoming msvc++, see:
  29. // http://msdn.microsoft.com/en-us/library/xcb2z8hs(VS.100).aspx
  30. void SetCurrentThreadName(const char* name) {
  31. static const DWORD MS_VC_EXCEPTION = 0x406D1388;
  32. #pragma pack(push, 8)
  33. struct THREADNAME_INFO {
  34. DWORD dwType; // must be 0x1000
  35. LPCSTR szName; // pointer to name (in user addr space)
  36. DWORD dwThreadID; // thread ID (-1=caller thread)
  37. DWORD dwFlags; // reserved for future use, must be zero
  38. } info;
  39. #pragma pack(pop)
  40. info.dwType = 0x1000;
  41. info.szName = name;
  42. info.dwThreadID = -1; // dwThreadID;
  43. info.dwFlags = 0;
  44. __try {
  45. RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info);
  46. } __except (EXCEPTION_CONTINUE_EXECUTION) {
  47. }
  48. }
  49. #else // !MSVC_VER, so must be POSIX threads
  50. // MinGW with the POSIX threading model does not support pthread_setname_np
  51. #if !defined(_WIN32) || defined(_MSC_VER)
  52. void SetCurrentThreadName(const char* name) {
  53. #ifdef __APPLE__
  54. pthread_setname_np(name);
  55. #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
  56. pthread_set_name_np(pthread_self(), name);
  57. #elif defined(__NetBSD__)
  58. pthread_setname_np(pthread_self(), "%s", (void*)name);
  59. #else
  60. pthread_setname_np(pthread_self(), name);
  61. #endif
  62. }
  63. #endif
  64. #endif
  65. } // namespace Common