summaryrefslogtreecommitdiff
path: root/thirdparty/etcpak/System.cpp
blob: a09b289cb2824ca50fcb7df6a91d98972e02c783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <algorithm>
#ifdef _WIN32
#  include <windows.h>
#else
#  include <pthread.h>
#  include <unistd.h>
#endif

#include "System.hpp"

unsigned int System::CPUCores()
{
    static unsigned int cores = 0;
    if( cores == 0 )
    {
        int tmp;
#ifdef _WIN32
        SYSTEM_INFO info;
        GetSystemInfo( &info );
        tmp = (int)info.dwNumberOfProcessors;
#else
#  ifndef _SC_NPROCESSORS_ONLN
#    ifdef _SC_NPROC_ONLN
#      define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
#    elif defined _SC_CRAY_NCPU
#      define _SC_NPROCESSORS_ONLN _SC_CRAY_NCPU
#    endif
#  endif
        tmp = (int)(long)sysconf( _SC_NPROCESSORS_ONLN );
#endif
        cores = (unsigned int)std::max( tmp, 1 );
    }
    return cores;
}

void System::SetThreadName( std::thread& thread, const char* name )
{
#ifdef _MSC_VER
    const DWORD MS_VC_EXCEPTION=0x406D1388;

#  pragma pack( push, 8 )
    struct THREADNAME_INFO
    {
       DWORD dwType;
       LPCSTR szName;
       DWORD dwThreadID;
       DWORD dwFlags;
    };
#  pragma pack(pop)

    DWORD ThreadId = GetThreadId( static_cast<HANDLE>( thread.native_handle() ) );
    THREADNAME_INFO info;
    info.dwType = 0x1000;
    info.szName = name;
    info.dwThreadID = ThreadId;
    info.dwFlags = 0;

    __try
    {
       RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
    }
    __except(EXCEPTION_EXECUTE_HANDLER)
    {
    }
#elif !defined(__APPLE__)
    pthread_setname_np( thread.native_handle(), name );
#endif
}