Ben Clayton | 9ad0423 | 2019-08-08 08:38:37 +0100 | [diff] [blame^] | 1 | // Copyright 2019 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "Thread.hpp" |
| 16 | |
| 17 | #include <cstdarg> |
| 18 | |
| 19 | #if defined(_WIN32) |
| 20 | # ifndef WIN32_LEAN_AND_MEAN |
| 21 | # define WIN32_LEAN_AND_MEAN |
| 22 | # endif |
| 23 | # include <windows.h> |
| 24 | #elif defined(__APPLE__) |
| 25 | # include <pthread.h> |
| 26 | # include <mach/thread_act.h> |
| 27 | # include <unistd.h> |
| 28 | #else |
| 29 | # include <pthread.h> |
| 30 | # include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | namespace yarn { |
| 34 | |
| 35 | #if defined(_WIN32) |
| 36 | |
| 37 | void Thread::setName(const char* fmt, ...) |
| 38 | { |
| 39 | static auto setThreadDescription = reinterpret_cast<HRESULT(WINAPI*)(HANDLE, PCWSTR)>(GetProcAddress(GetModuleHandle("kernelbase.dll"), "SetThreadDescription")); |
| 40 | if (setThreadDescription == nullptr) |
| 41 | { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | char name[1024]; |
| 46 | va_list vararg; |
| 47 | va_start(vararg, fmt); |
| 48 | vsnprintf(name, sizeof(name), fmt, vararg); |
| 49 | va_end(vararg); |
| 50 | |
| 51 | wchar_t wname[1024]; |
| 52 | mbstowcs(wname, name, 1024); |
| 53 | setThreadDescription(GetCurrentThread(), wname); |
| 54 | } |
| 55 | |
| 56 | unsigned int Thread::numLogicalCPUs() |
| 57 | { |
| 58 | DWORD_PTR processAffinityMask = 1; |
| 59 | DWORD_PTR systemAffinityMask = 1; |
| 60 | |
| 61 | GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask); |
| 62 | |
| 63 | auto count = 0; |
| 64 | while (processAffinityMask > 0) |
| 65 | { |
| 66 | if (processAffinityMask & 1) |
| 67 | { |
| 68 | count++; |
| 69 | } |
| 70 | |
| 71 | processAffinityMask >>= 1; |
| 72 | } |
| 73 | return count; |
| 74 | } |
| 75 | |
| 76 | #else |
| 77 | |
| 78 | void Thread::setName(const char* fmt, ...) |
| 79 | { |
| 80 | char name[1024]; |
| 81 | va_list vararg; |
| 82 | va_start(vararg, fmt); |
| 83 | vsnprintf(name, sizeof(name), fmt, vararg); |
| 84 | va_end(vararg); |
| 85 | |
| 86 | #if defined(__APPLE__) |
| 87 | pthread_setname_np(name); |
| 88 | #else |
| 89 | pthread_setname_np(pthread_self(), name); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | unsigned int Thread::numLogicalCPUs() |
| 94 | { |
| 95 | return sysconf(_SC_NPROCESSORS_ONLN); |
| 96 | } |
| 97 | |
| 98 | #endif |
| 99 | |
| 100 | } // namespace yarn |