Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
platform_detection.h
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, kcenon
5All rights reserved.
6*****************************************************************************/
7
14#pragma once
15
16#include <cstdint>
17#include <string>
18#include <thread>
19
20// Platform detection macros
21#if defined(_WIN32) || defined(_WIN64)
22 #define THREAD_SYSTEM_WINDOWS 1
23 #define THREAD_SYSTEM_PLATFORM_NAME "windows"
24#elif defined(__APPLE__)
25 #define THREAD_SYSTEM_MACOS 1
26 #define THREAD_SYSTEM_PLATFORM_NAME "macos"
27 #include <TargetConditionals.h>
28#elif defined(__linux__)
29 #define THREAD_SYSTEM_LINUX 1
30 #define THREAD_SYSTEM_PLATFORM_NAME "linux"
31#else
32 #define THREAD_SYSTEM_UNKNOWN_PLATFORM 1
33 #define THREAD_SYSTEM_PLATFORM_NAME "unknown"
34#endif
35
36// Architecture detection
37#if defined(__x86_64__) || defined(_M_X64)
38 #define THREAD_SYSTEM_ARCH_X64 1
39 #define THREAD_SYSTEM_ARCH_NAME "x86_64"
40#elif defined(__aarch64__) || defined(_M_ARM64)
41 #define THREAD_SYSTEM_ARCH_ARM64 1
42 #define THREAD_SYSTEM_ARCH_NAME "arm64"
43#elif defined(__i386__) || defined(_M_IX86)
44 #define THREAD_SYSTEM_ARCH_X86 1
45 #define THREAD_SYSTEM_ARCH_NAME "x86"
46#else
47 #define THREAD_SYSTEM_ARCH_UNKNOWN 1
48 #define THREAD_SYSTEM_ARCH_NAME "unknown"
49#endif
50
51// Feature detection
52#if defined(THREAD_SYSTEM_MACOS) && defined(THREAD_SYSTEM_ARCH_ARM64)
53 #define THREAD_SYSTEM_HAS_EFFICIENCY_CORES 1
54#endif
55
57
61enum class cpu_architecture {
62 x86,
63 x86_64,
64 arm64,
66};
67
71enum class os_type {
72 windows,
73 macos,
76};
77
85 uint32_t logical_cores;
86 uint32_t efficiency_cores; // 0 if not applicable (Apple Silicon only)
90};
91
96#if defined(THREAD_SYSTEM_ARCH_X64)
98#elif defined(THREAD_SYSTEM_ARCH_ARM64)
100#elif defined(THREAD_SYSTEM_ARCH_X86)
102#else
104#endif
105}
106
110inline os_type get_os() noexcept {
111#if defined(THREAD_SYSTEM_WINDOWS)
112 return os_type::windows;
113#elif defined(THREAD_SYSTEM_MACOS)
114 return os_type::macos;
115#elif defined(THREAD_SYSTEM_LINUX)
116 return os_type::linux_os;
117#else
118 return os_type::unknown;
119#endif
120}
121
125inline bool is_arm64() noexcept {
126#if defined(THREAD_SYSTEM_ARCH_ARM64)
127 return true;
128#else
129 return false;
130#endif
131}
132
136inline uint32_t get_physical_core_count() noexcept {
137 // std::thread::hardware_concurrency returns logical cores
138 // For physical cores, platform-specific implementation needed
139 return static_cast<uint32_t>(std::thread::hardware_concurrency());
140}
141
145inline uint32_t get_logical_core_count() noexcept {
146 return static_cast<uint32_t>(std::thread::hardware_concurrency());
147}
148
152inline bool is_container_environment() noexcept {
153#if defined(THREAD_SYSTEM_LINUX)
154 // Check for common container indicators
155 // This is a simplified check; production code should be more thorough
156 return false; // Would check /proc/1/cgroup in real implementation
157#else
158 return false;
159#endif
160}
161
165inline bool has_efficiency_cores() noexcept {
166#if defined(THREAD_SYSTEM_HAS_EFFICIENCY_CORES)
167 return true;
168#else
169 return false;
170#endif
171}
172
176inline system_info get_system_info() noexcept {
178 info.os = get_os();
179 info.arch = get_architecture();
180 info.physical_cores = get_physical_core_count();
181 info.logical_cores = get_logical_core_count();
182 info.efficiency_cores = 0; // Platform-specific
183 info.is_container = is_container_environment();
184 info.is_arm64 = is_arm64();
185 info.has_efficiency_cores = has_efficiency_cores();
186 return info;
187}
188
192inline const char* get_platform_name() noexcept {
194}
195
199inline const char* get_arch_name() noexcept {
201}
202
203} // namespace kcenon::thread::platform
bool has_efficiency_cores() noexcept
Check if system has efficiency cores (Apple Silicon)
os_type
Operating system enumeration.
cpu_architecture
CPU architecture enumeration.
uint32_t get_logical_core_count() noexcept
Get logical core count.
cpu_architecture get_architecture() noexcept
Get current CPU architecture.
uint32_t get_physical_core_count() noexcept
Get physical core count.
const char * get_arch_name() noexcept
Get architecture name string.
os_type get_os() noexcept
Get current operating system.
bool is_container_environment() noexcept
Check if running in container environment.
bool is_arm64() noexcept
Check if running on ARM64 architecture.
system_info get_system_info() noexcept
Get comprehensive system information.
const char * get_platform_name() noexcept
Get platform name string.
@ info
Informational messages highlighting progress.
#define THREAD_SYSTEM_PLATFORM_NAME
#define THREAD_SYSTEM_ARCH_NAME
System information structure.