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 <unistd.h>
26#endif
27#ifdef NEFORCE_ARCH_RISCV
28# include <riscv_pause.h>
29#endif
30NEFORCE_BEGIN_NAMESPACE__
31NEFORCE_BEGIN_THIS_THREAD__
32
38
44
51NEFORCE_ALWAYS_INLINE_INLINE int64_t current_cpu() noexcept {
52#ifdef NEFORCE_PLATFORM_WINDOWS
53 return static_cast<int64_t>(::GetCurrentProcessorNumber());
54#else
55 return static_cast<int64_t>(::sched_getcpu());
56#endif
57}
58
64NEFORCE_ALWAYS_INLINE_INLINE void yield() noexcept {
65#ifdef NEFORCE_PLATFORM_WINDOWS
66 ::SwitchToThread();
67#else
68 ::sched_yield();
69#endif
70}
71
78NEFORCE_ALWAYS_INLINE_INLINE void relax() noexcept {
79#ifdef NEFORCE_PLATFORM_WINDOWS
80 ::_mm_pause();
81#else
82# if defined(NEFORCE_ARCH_X86)
83 __builtin_ia32_pause();
84# elif defined(NEFORCE_ARCH_ARM)
85 asm volatile("yield" ::: "memory");
86# elif defined(NEFORCE_ARCH_RISCV)
87 ::riscv_pause();
88# elif defined(NEFORCE_ARCH_LOONGARCH)
89 asm volatile("dbar 0" ::: "memory");
90# else
91 this_thread::yield();
92# endif
93#endif
94}
95
105NEFORCE_ALWAYS_INLINE_INLINE void relax(const int count) noexcept {
106 if (count < 4) {
107 for (int i = 0; i < (1 << count); ++i) {
108 this_thread::relax();
109 }
110 } else if (count < 8) {
111 this_thread::relax();
112 } else {
113 this_thread::yield();
114 }
115}
116
125NEFORCE_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept {
126#ifdef NEFORCE_PLATFORM_WINDOWS
127 ::Sleep(milliseconds);
128#else
129 ::usleep(milliseconds * 1000);
130#endif
131}
132
143void NEFORCE_API sleep_for_ms(uint32_t ms, bool busy_wait) noexcept;
144
153void NEFORCE_API sleep_for_us(uint64_t us) noexcept;
154
163void NEFORCE_API sleep_for_ns(uint64_t ns) noexcept;
164
172bool NEFORCE_API affinity(size_t cpu_mask) noexcept;
173
181bool NEFORCE_API priority(int priority) noexcept;
182 // Thread
184 // AsyncComponents
186
187NEFORCE_END_THIS_THREAD__
188NEFORCE_END_NAMESPACE__
189#endif // NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
unsigned long long uint64_t
64位无符号整数类型
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
duration< int64_t, milli > milliseconds
毫秒持续时间
bool NEFORCE_API priority(int priority) noexcept
设置线程优先级
NEFORCE_ALWAYS_INLINE_INLINE void sleep_for_ms(uint32_t milliseconds) noexcept
睡眠指定毫秒数
void NEFORCE_API sleep_for_ns(uint64_t ns) noexcept
精确睡眠指定纳秒数
void NEFORCE_API sleep_for_us(uint64_t us) noexcept
精确睡眠指定微秒数
NEFORCE_ALWAYS_INLINE_INLINE int64_t current_cpu() noexcept
获取当前CPU核心编号
NEFORCE_ALWAYS_INLINE_INLINE void yield() noexcept
让出当前线程的时间片
bool NEFORCE_API affinity(size_t cpu_mask) noexcept
设置线程的CPU亲和性
NEFORCE_ALWAYS_INLINE_INLINE void relax() noexcept
线程放松
基本类型别名
NeForce WindowsAPI 平台宏