Yarn: Add Thread helper class

Contains static methods that abstract OS-specific thread / cpu queries and control.

Bug: b/139010488
Change-Id: Idab47624598c3585d7741058ba62974faadc191d
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/34810
Tested-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Yarn/Thread.cpp b/src/Yarn/Thread.cpp
new file mode 100644
index 0000000..83c56b2
--- /dev/null
+++ b/src/Yarn/Thread.cpp
@@ -0,0 +1,100 @@
+// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "Thread.hpp"
+
+#include <cstdarg>
+
+#if defined(_WIN32)
+#   ifndef WIN32_LEAN_AND_MEAN
+#       define WIN32_LEAN_AND_MEAN
+#   endif
+#   include <windows.h>
+#elif defined(__APPLE__)
+#   include <pthread.h>
+#   include <mach/thread_act.h>
+#   include <unistd.h>
+#else
+#   include <pthread.h>
+#   include <unistd.h>
+#endif
+
+namespace yarn {
+
+#if defined(_WIN32)
+
+void Thread::setName(const char* fmt, ...)
+{
+    static auto setThreadDescription = reinterpret_cast<HRESULT(WINAPI*)(HANDLE, PCWSTR)>(GetProcAddress(GetModuleHandle("kernelbase.dll"), "SetThreadDescription"));
+    if (setThreadDescription == nullptr)
+    {
+        return;
+    }
+
+    char name[1024];
+    va_list vararg;
+    va_start(vararg, fmt);
+    vsnprintf(name, sizeof(name), fmt, vararg);
+    va_end(vararg);
+
+    wchar_t wname[1024];
+    mbstowcs(wname, name, 1024);
+    setThreadDescription(GetCurrentThread(), wname);
+}
+
+unsigned int Thread::numLogicalCPUs()
+{
+    DWORD_PTR processAffinityMask = 1;
+    DWORD_PTR systemAffinityMask = 1;
+
+    GetProcessAffinityMask(GetCurrentProcess(), &processAffinityMask, &systemAffinityMask);
+
+    auto count = 0;
+    while (processAffinityMask > 0)
+    {
+        if (processAffinityMask & 1)
+        {
+            count++;
+        }
+
+        processAffinityMask >>= 1;
+    }
+    return count;
+}
+
+#else
+
+void Thread::setName(const char* fmt, ...)
+{
+    char name[1024];
+    va_list vararg;
+    va_start(vararg, fmt);
+    vsnprintf(name, sizeof(name), fmt, vararg);
+    va_end(vararg);
+
+#if defined(__APPLE__)
+    pthread_setname_np(name);
+#else
+    pthread_setname_np(pthread_self(), name);
+#endif
+}
+
+unsigned int Thread::numLogicalCPUs()
+{
+    return sysconf(_SC_NPROCESSORS_ONLN);
+}
+
+#endif
+
+} // namespace yarn
diff --git a/src/Yarn/Thread.hpp b/src/Yarn/Thread.hpp
new file mode 100644
index 0000000..878cb8e
--- /dev/null
+++ b/src/Yarn/Thread.hpp
@@ -0,0 +1,38 @@
+// Copyright 2019 The SwiftShader Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef yarn_thread_hpp
+#define yarn_thread_hpp
+
+#include <bitset>
+
+namespace yarn {
+
+// Thread contains static methods that abstract OS-specific thread / cpu
+// queries and control.
+class Thread
+{
+public:
+    // setName() sets the name of the currently executing thread for displaying
+    // in a debugger.
+    static void setName(const char* fmt, ...);
+
+    // numLogicalCPUs() returns the number of available logical CPU cores for
+    // the system.
+    static unsigned int numLogicalCPUs();
+};
+
+} // namespace yarn
+
+#endif  // yarn_thread_hpp