NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
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# ifdef NEFORCE_ARCH_X86
90 ::_mm_pause();
91# else
92 ::__yield();
93# endif
94#else
95# if defined(NEFORCE_ARCH_X86)
96 asm volatile("pause" ::: "memory");
97# elif defined(NEFORCE_ARCH_ARM)
98 asm volatile("yield" ::: "memory");
99# elif defined(NEFORCE_ARCH_RISCV)
100# ifdef __riscv_zihintpause
101 asm volatile("pause" ::: "memory");
102# else
103 asm volatile("fence" ::: "memory");
104# endif
105# elif defined(NEFORCE_ARCH_LOONGARCH)
106 asm volatile("dbar 0" ::: "memory");
107# else
109# endif
110#endif
111}
112
121NEFORCE_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept {
122#ifdef NEFORCE_PLATFORM_WINDOWS
123 ::Sleep(milliseconds);
124#else
125 ::timespec ts;
126 ts.tv_sec = milliseconds / 1000;
127 ts.tv_nsec = static_cast<decay_t<decltype(ts.tv_nsec)>>(milliseconds % 1000) * 1000000;
128 ::nanosleep(&ts, nullptr);
129#endif
130}
131
142void NEFORCE_API sleep_for_ms(uint32_t ms, bool busy_wait) noexcept;
143
152void NEFORCE_API sleep_for_us(uint64_t us) noexcept;
153
162void NEFORCE_API sleep_for_ns(uint64_t ns) noexcept;
163
171bool NEFORCE_API set_affinity(size_t cpu_mask) noexcept;
172
178bool NEFORCE_API affinity(uint64_t& affi) noexcept;
179
185bool NEFORCE_API cpu_time(cpu_times& times) noexcept;
186
194bool NEFORCE_API set_priority(int priority) noexcept;
195
203int NEFORCE_API priority() noexcept;
204 // Thread
206 // AsyncComponents
208
209NEFORCE_END_THIS_THREAD__
210NEFORCE_END_NAMESPACE__
211#endif // NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
long int64_t
64位有符号整数类型
unsigned int uint32_t
32位无符号整数类型
unsigned long uint64_t
64位无符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
void sleep_for_ms(uint32_t milliseconds) noexcept
睡眠指定毫秒数
bool cpu_time(cpu_times &times) noexcept
获取当前线程的 CPU 时间
bool set_priority(int priority) noexcept
设置线程优先级
bool affinity(uint64_t &affi) noexcept
获取线程的 CPU 亲和性
void sleep_for_us(uint64_t us) noexcept
精确睡眠指定微秒数
int64_t current_cpu() noexcept
获取当前CPU核心编号
void yield() noexcept
让出当前线程的时间片
void sleep_for_ns(uint64_t ns) noexcept
精确睡眠指定纳秒数
void relax() noexcept
线程放松
int priority() noexcept
获取线程优先级
bool set_affinity(size_t cpu_mask) noexcept
设置线程的 CPU 亲和性
typename decay< T >::type decay_t
decay的便捷别名
CPU时间信息类
uint64_t idle
空闲时间
uint64_t kernel
内核态时间
uint64_t user
用户态时间
类型萃取
WindowsAPI 平台宏