1#ifndef NEFORCE_CORE_ASYNC_THREAD_HPP__
2#define NEFORCE_CORE_ASYNC_THREAD_HPP__
16#ifdef NEFORCE_PLATFORM_LINUX
19NEFORCE_BEGIN_NAMESPACE__
32 explicit thread_exception(
const char* info =
"Thread Operation Failed.") noexcept :
35 explicit thread_exception(
const exception& e) :
38 ~thread_exception()
override =
default;
40 NEFORCE_NODISCARD
const char*
type() const noexcept
override {
return "thread_exception"; }
78#ifdef NEFORCE_PLATFORM_WINDOWS
93 constexpr id() noexcept = default;
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) {
277 auto data = _NEFORCE make_unique<thread_data<decay_t<F>>>(_NEFORCE forward<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));
339 template <
typename F,
typename... Args>
341 if (state_ != NOT_A_THREAD) {
342 NEFORCE_THROW_EXCEPTION(
thread_exception(
"Thread already started or has been joined/detached."));
346 return _NEFORCE
apply(_NEFORCE
move(func), _NEFORCE
move(args));
348 this->start_thread(_NEFORCE
move(func));
355 NEFORCE_NODISCARD
id get_id() const noexcept {
return id_; }
369 NEFORCE_NODISCARD
bool joinable() const noexcept {
return state_ == CREATED; }
431NEFORCE_BEGIN_THIS_THREAD__
450#ifdef NEFORCE_PLATFORM_WINDOWS
462#ifdef NEFORCE_PLATFORM_WINDOWS
463 return ::GetCurrentThread();
465 return ::pthread_self();
475NEFORCE_ALWAYS_INLINE_INLINE
bool name(
char* buffer,
size_t size) {
490NEFORCE_END_THIS_THREAD__
492NEFORCE_END_NAMESPACE__
thread & operator=(thread &&other) noexcept
移动赋值运算符
native_handle_type native_handle() const noexcept
获取原生句柄
id get_id() const noexcept
获取线程标识符
bool name(char *buffer, size_t size) const
获取线程名称
::HANDLE native_handle_type
系统线程句柄类型
static bool name(native_handle_type handle, char *buffer, size_t size)
获取指定线程的名称
void start(F &&f, Args &&... args)
延迟启动线程
thread(thread &&other) noexcept
移动构造函数
thread() noexcept=default
默认构造函数
bool set_name(const char *name)
设置线程名称
bool joinable() const noexcept
检查线程是否可被等待
void swap(thread &other) noexcept
交换两个线程对象
static bool set_name(native_handle_type handle, const char *name)
设置指定线程的名称
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
unsigned char byte_t
字节类型,定义为无符号字符
constexpr 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)))
移动范围元素
thread::id id() noexcept
获取当前线程标识符
bool set_name(const char *name)
设置当前线程名称
thread::native_handle_type handle() noexcept
获取当前线程句柄
bool name(char *buffer, size_t size)
获取当前线程名称
constexpr decltype(auto) apply(Func &&f, Tuple &&t) noexcept(inner::__apply_unpack_tuple< _NEFORCE is_nothrow_invocable, Func, Tuple >::value)
将元组元素解包作为参数调用函数
constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组
typename decay< T >::type decay_t
decay的便捷别名
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr bool is_same_v
is_same的便捷变量模板
exception(const char *info="")
构造函数
static void invoke(point point, id thread_id)
调用钩子回调
static void add_hook(callback_t hook)
添加钩子回调
static void remove_hook(callback_t hook)
移除钩子回调
void(*)(point point, id thread_id) callback_t
钩子回调函数类型
::DWORD native_id_type
系统线程标识符类型
constexpr native_id_type native_handle() const noexcept
获取原生线程ID
bool operator!=(const id &rhs) const noexcept
不等于比较运算符
bool operator==(const id &rhs) const noexcept
等于比较运算符
constexpr id() noexcept=default
默认构造函数
size_t to_hash() const noexcept
计算哈希值
const char * type() const noexcept override
获取异常类型