System.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <algorithm>
  2. #ifdef _WIN32
  3. # include <windows.h>
  4. #else
  5. # include <unistd.h>
  6. #endif
  7. #include "System.hpp"
  8. unsigned int System::CPUCores()
  9. {
  10. static unsigned int cores = 0;
  11. if( cores == 0 )
  12. {
  13. int tmp;
  14. #ifdef _WIN32
  15. SYSTEM_INFO info;
  16. GetSystemInfo( &info );
  17. tmp = (int)info.dwNumberOfProcessors;
  18. #else
  19. # ifndef _SC_NPROCESSORS_ONLN
  20. # ifdef _SC_NPROC_ONLN
  21. # define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
  22. # elif defined _SC_CRAY_NCPU
  23. # define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
  24. # endif
  25. # endif
  26. tmp = (int)(long)sysconf( _SC_NPROCESSORS_ONLN );
  27. #endif
  28. cores = (unsigned int)std::max( tmp, 1 );
  29. }
  30. return cores;
  31. }
  32. void System::SetThreadName( std::thread& thread, const char* name )
  33. {
  34. #ifdef _MSC_VER
  35. const DWORD MS_VC_EXCEPTION=0x406D1388;
  36. # pragma pack( push, 8 )
  37. struct THREADNAME_INFO
  38. {
  39. DWORD dwType;
  40. LPCSTR szName;
  41. DWORD dwThreadID;
  42. DWORD dwFlags;
  43. };
  44. # pragma pack(pop)
  45. DWORD ThreadId = GetThreadId( static_cast<HANDLE>( thread.native_handle() ) );
  46. THREADNAME_INFO info;
  47. info.dwType = 0x1000;
  48. info.szName = name;
  49. info.dwThreadID = ThreadId;
  50. info.dwFlags = 0;
  51. __try
  52. {
  53. RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
  54. }
  55. __except(EXCEPTION_EXECUTE_HANDLER)
  56. {
  57. }
  58. #endif
  59. }