1#ifndef NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_THIS_THREAD_HPP__
12#ifdef NEFORCE_PLATFORM_WINDOWS
14# include <processthreadsapi.h>
23#ifdef NEFORCE_PLATFORM_LINUX
27#ifdef NEFORCE_ARCH_RISCV
28# include <riscv_pause.h>
30NEFORCE_BEGIN_NAMESPACE__
31NEFORCE_BEGIN_THIS_THREAD__
52#ifdef NEFORCE_PLATFORM_WINDOWS
53 return static_cast<int64_t>(::GetCurrentProcessorNumber());
55 return static_cast<int64_t>(::sched_getcpu());
64NEFORCE_ALWAYS_INLINE_INLINE
void yield() noexcept {
65#ifdef NEFORCE_PLATFORM_WINDOWS
78NEFORCE_ALWAYS_INLINE_INLINE
void relax() noexcept {
79#ifdef NEFORCE_PLATFORM_WINDOWS
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)
88# elif defined(NEFORCE_ARCH_LOONGARCH)
89 asm volatile(
"dbar 0" :::
"memory");
105NEFORCE_ALWAYS_INLINE_INLINE
void relax(
const int count)
noexcept {
107 for (
int i = 0; i < (1 <<
count); ++i) {
108 this_thread::relax();
110 }
else if (
count < 8) {
111 this_thread::relax();
113 this_thread::yield();
126#ifdef NEFORCE_PLATFORM_WINDOWS
172bool NEFORCE_API
affinity(
size_t cpu_mask)
noexcept;
187NEFORCE_END_THIS_THREAD__
188NEFORCE_END_NAMESPACE__
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
线程放松