NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
this_thread.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
3
10
12#ifdef NEFORCE_PLATFORM_WINDOWS
14# include <processthreadsapi.h>
15# include <synchapi.h>
16# ifdef max
17# undef max
18# endif
19# ifdef min
20# undef min
21# endif
22#endif
23#ifdef NEFORCE_PLATFORM_LINUX
24# include <sched.h>
25# include <ctime>
26#endif
27NEFORCE_BEGIN_NAMESPACE__
28
33struct cpu_times {
37 NEFORCE_NODISCARD uint64_t total() const noexcept { return user + kernel + idle; }
38};
39
40NEFORCE_BEGIN_THIS_THREAD__
41
47
53
60NEFORCE_ALWAYS_INLINE_INLINE int64_t current_cpu() noexcept {
61#ifdef NEFORCE_PLATFORM_WINDOWS
62 return static_cast<int64_t>(::GetCurrentProcessorNumber());
63#else
64 return static_cast<int64_t>(::sched_getcpu());
65#endif
66}
67
73NEFORCE_ALWAYS_INLINE_INLINE void yield() noexcept {
74#ifdef NEFORCE_PLATFORM_WINDOWS
75 ::SwitchToThread();
76#else
77 ::sched_yield();
78#endif
79}
80
87NEFORCE_ALWAYS_INLINE_INLINE void relax() noexcept {
88#ifdef NEFORCE_COMPILER_MSVC
89 ::_mm_pause();
90#else
91# if defined(NEFORCE_ARCH_X86)
92 __builtin_ia32_pause();
93# elif defined(NEFORCE_ARCH_ARM)
94 asm volatile("yield");
95# elif defined(NEFORCE_ARCH_RISCV)
96 asm volatile("pause" ::: "memory");
97# elif defined(NEFORCE_ARCH_LOONGARCH)
98 asm volatile("dbar 0" ::: "memory");
99# else
100 this_thread::yield();
101# endif
102#endif
103}
104
113NEFORCE_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept {
114#ifdef NEFORCE_PLATFORM_WINDOWS
115 ::Sleep(milliseconds);
116#else
117 ::timespec ts;
118 ts.tv_sec = milliseconds / 1000;
119 ts.tv_nsec = static_cast<decay_t<decltype(ts.tv_nsec)>>(milliseconds % 1000) * 1000000;
120 ::nanosleep(&ts, nullptr);
121#endif
122}
123
134void NEFORCE_API sleep_for_ms(uint32_t ms, bool busy_wait) noexcept;
135
144void NEFORCE_API sleep_for_us(uint64_t us) noexcept;
145
154void NEFORCE_API sleep_for_ns(uint64_t ns) noexcept;
155
163bool NEFORCE_API set_affinity(size_t cpu_mask) noexcept;
164
170bool NEFORCE_API affinity(uint64_t& affi) noexcept;
171
177bool NEFORCE_API cpu_time(cpu_times& times) noexcept;
178
186bool NEFORCE_API set_priority(int priority) noexcept;
187
195int NEFORCE_API priority() noexcept;
196 // Thread
198 // AsyncComponents
200
201NEFORCE_END_THIS_THREAD__
202NEFORCE_END_NAMESPACE__
203#endif // NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
unsigned long long uint64_t
64位无符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
bool cpu_time(cpu_times &times) noexcept
获取当前线程的 CPU 时间
void sleep_for_ns(uint64_t ns) noexcept
精确睡眠指定纳秒数
void relax() noexcept
线程放松
int priority() noexcept
获取线程优先级
void sleep_for_us(uint64_t us) noexcept
精确睡眠指定微秒数
bool affinity(uint64_t &affi) noexcept
获取线程的 CPU 亲和性
void yield() noexcept
让出当前线程的时间片
bool set_priority(int priority) noexcept
设置线程优先级
void sleep_for_ms(uint32_t milliseconds) noexcept
睡眠指定毫秒数
bool set_affinity(size_t cpu_mask) noexcept
设置线程的 CPU 亲和性
int64_t current_cpu() noexcept
获取当前CPU核心编号
typename decay< T >::type decay_t
decay的便捷别名
CPU时间信息类
uint64_t user
用户态时间
uint64_t kernel
内核态时间
uint64_t idle
空闲时间
类型萃取
WindowsAPI 平台宏