1#ifndef NEFORCE_CORE_ASYNC_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_THREAD_HPP__
16#ifdef NEFORCE_PLATFORM_LINUX
19NEFORCE_BEGIN_NAMESPACE__
31struct thread_exception final : system_exception {
32 explicit thread_exception(
const char* info =
"Thread Operation Failed.",
const char*
type = static_type,
33 const int code = 0) noexcept :
34 system_exception(info,
type,
code) {}
36 explicit thread_exception(
const exception& e) :
37 system_exception(e) {}
39 ~thread_exception()
override =
default;
41 static constexpr auto static_type =
"thread_exception";
78 using native_id_type =
79#ifdef NEFORCE_PLATFORM_WINDOWS
93 id() noexcept = default;
99 explicit
id(const native_id_type
id) noexcept :
106 NEFORCE_NODISCARD native_id_type
native_handle() const noexcept {
return id_; }
112 NEFORCE_NODISCARD
size_t to_hash() const noexcept {
113 return _NEFORCE
FNV_hash(
reinterpret_cast<const byte_t*
>(&id_),
sizeof(
id));
121 NEFORCE_NODISCARD
bool operator==(
const id& rhs)
const noexcept {
122#ifdef NEFORCE_PLATFORM_WINDOWS
123 return id_ == rhs.id_;
125 return ::pthread_equal(id_, rhs.id_) != 0;
134 NEFORCE_NODISCARD
bool operator!=(
const id& rhs)
const noexcept {
return !(*
this == rhs); }
197 virtual ~data_base() =
default;
198 virtual void run() = 0;
206 template <
typename Callable>
207 struct thread_data final : data_base {
210 template <
typename F>
211 explicit thread_data(F&& f) :
212 func_(_NEFORCE
forward<F>(f)) {}
214 void run()
override { func_(); }
223 struct thread_startup_args {
224 unique_ptr<data_base>
data;
234 struct NEFORCE_API thread_monitor {
243 explicit thread_monitor(
id thread_id);
248 ~thread_monitor() noexcept;
256#ifdef NEFORCE_PLATFORM_WINDOWS
265 state state_ = NOT_A_THREAD;
267#ifdef NEFORCE_PLATFORM_WINDOWS
268 static unsigned int __stdcall thread_entry(
void* arg);
270 static void* thread_entry(
void* arg);
273 void start_thread_impl(thread_startup_args* args);
275 template <
typename F>
276 void start_thread(F&& f) {
278 this->start_thread_impl(
new thread_startup_args{_NEFORCE
move(
data), id_});
304 return _NEFORCE
apply(_NEFORCE
move(func), _NEFORCE
move(args));
306 thread::start_thread(_NEFORCE
move(func));
335 NEFORCE_NODISCARD
id get_id() const noexcept {
return id_; }
349 NEFORCE_NODISCARD
bool joinable() const noexcept {
return state_ == CREATED; }
411NEFORCE_BEGIN_THIS_THREAD__
430#ifdef NEFORCE_PLATFORM_WINDOWS
442#ifdef NEFORCE_PLATFORM_WINDOWS
443 return ::GetCurrentThread();
445 return ::pthread_self();
455NEFORCE_ALWAYS_INLINE_INLINE
bool name(
char* buffer,
size_t size) {
470NEFORCE_END_THIS_THREAD__
472NEFORCE_END_NAMESPACE__
bool set_name(const char *name)
设置线程名称
thread() noexcept=default
默认构造函数
NEFORCE_NODISCARD id get_id() const noexcept
获取线程标识符
static bool name(native_handle_type handle, char *buffer, size_t size)
获取指定线程的名称
void swap(thread &other) noexcept
交换两个线程对象
thread(thread &&other) noexcept
移动构造函数
::pthread_t native_handle_type
系统线程句柄类型
NEFORCE_NODISCARD native_handle_type native_handle() const noexcept
获取原生句柄
thread & operator=(thread &&other) noexcept
移动赋值运算符
NEFORCE_NODISCARD bool joinable() const noexcept
检查线程是否可被等待
bool name(char *buffer, size_t size) const
获取线程名称
static bool set_name(native_handle_type handle, const char *name)
设置指定线程的名称
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
unsigned char byte_t
字节类型,定义为无符号字符
NEFORCE_CONSTEXPR14 size_t FNV_hash(const byte_t *first, const size_t count) noexcept
FNV-1a哈希算法
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
NEFORCE_ALWAYS_INLINE_INLINE bool set_name(const char *name)
设置当前线程名称
NEFORCE_ALWAYS_INLINE_INLINE thread::id id() noexcept
获取当前线程标识符
NEFORCE_ALWAYS_INLINE_INLINE bool name(char *buffer, size_t size)
获取当前线程名称
NEFORCE_ALWAYS_INLINE_INLINE thread::native_handle_type handle() noexcept
获取当前线程句柄
NEFORCE_NODISCARD constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
constexpr decltype(auto) apply(Func &&f, Tuple &&t) noexcept(inner::__apply_unpack_tuple< _NEFORCE is_nothrow_invocable, Func, Tuple >::value)
将元组元素解包作为参数调用函数
typename decay< T >::type decay_t
decay的便捷别名
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
NEFORCE_INLINE17 constexpr bool is_same_v
is_same的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
NEFORCE_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型
static void add_hook(callback_t hook)
添加钩子回调
static void invoke(point point, id thread_id)
调用钩子回调
void(*)(point point, id thread_id) callback_t
钩子回调函数类型
static void remove_hook(callback_t hook)
移除钩子回调
NEFORCE_NODISCARD bool operator==(const id &rhs) const noexcept
等于比较运算符
NEFORCE_NODISCARD bool operator!=(const id &rhs) const noexcept
不等于比较运算符
id() noexcept=default
默认构造函数
NEFORCE_NODISCARD size_t to_hash() const noexcept
计算哈希值
NEFORCE_NODISCARD native_id_type native_handle() const noexcept
获取原生线程ID