2012-11-15 00:39:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-14 02:22:19 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "Atomic.h"
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
#include <exception>
|
2016-02-01 22:55:43 +01:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2018-09-25 14:21:04 +02:00
|
|
|
#include <string_view>
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
#include "mutex.h"
|
2016-09-07 00:38:52 +02:00
|
|
|
#include "cond.h"
|
2018-09-25 14:21:04 +02:00
|
|
|
#include "lockless.h"
|
2016-09-07 00:38:52 +02:00
|
|
|
|
2017-11-23 16:37:08 +01:00
|
|
|
// Report error and call std::abort(), defined in main.cpp
|
|
|
|
[[noreturn]] void report_fatal_error(const std::string&);
|
|
|
|
|
2015-12-18 12:11:18 +01:00
|
|
|
// Will report exception and call std::abort() if put in catch(...)
|
|
|
|
[[noreturn]] void catch_all_exceptions();
|
|
|
|
|
2017-10-21 13:21:37 +02:00
|
|
|
// Hardware core layout
|
|
|
|
enum class native_core_arrangement : u32
|
|
|
|
{
|
|
|
|
undefined,
|
|
|
|
generic,
|
|
|
|
intel_ht,
|
|
|
|
amd_ccx
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class thread_class : u32
|
|
|
|
{
|
|
|
|
general,
|
|
|
|
rsx,
|
|
|
|
spu,
|
|
|
|
ppu
|
|
|
|
};
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
enum class thread_state
|
|
|
|
{
|
|
|
|
created, // Initial state
|
|
|
|
detached, // Set if the thread has been detached successfully (only possible via shared_ptr)
|
|
|
|
aborting, // Set if the thread has been joined in destructor (mutually exclusive with detached)
|
|
|
|
finished // Final state, always set at the end of thread execution
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Context>
|
|
|
|
class named_thread;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct result_storage
|
|
|
|
{
|
|
|
|
alignas(T) std::byte data[sizeof(T)];
|
|
|
|
|
|
|
|
static constexpr bool empty = false;
|
|
|
|
|
|
|
|
using type = T;
|
|
|
|
|
|
|
|
T* get()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<T*>(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* get() const
|
|
|
|
{
|
|
|
|
return reinterpret_cast<const T*>(&data);
|
|
|
|
}
|
2018-10-01 19:05:47 +02:00
|
|
|
|
|
|
|
void destroy() noexcept
|
|
|
|
{
|
|
|
|
get()->~T();
|
|
|
|
}
|
2018-09-25 14:21:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct result_storage<void>
|
|
|
|
{
|
|
|
|
static constexpr bool empty = true;
|
|
|
|
|
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Context, typename... Args>
|
|
|
|
using result_storage_t = result_storage<std::invoke_result_t<Context, Args...>>;
|
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
// Detect on_abort() method (should return void)
|
2018-09-25 14:21:04 +02:00
|
|
|
template <typename T, typename = void>
|
2018-10-02 16:49:53 +02:00
|
|
|
struct thread_on_abort : std::bool_constant<false> {};
|
2018-09-25 14:21:04 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2018-10-02 16:49:53 +02:00
|
|
|
struct thread_on_abort<T, decltype(std::declval<named_thread<T>&>().on_abort())> : std::bool_constant<true> {};
|
|
|
|
|
|
|
|
// Detect on_cleanup() static function (should return void)
|
|
|
|
template <typename T, typename = void>
|
|
|
|
struct thread_on_cleanup : std::bool_constant<false> {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct thread_on_cleanup<T, decltype(named_thread<T>::on_cleanup(std::declval<named_thread<T>*>()))> : std::bool_constant<true> {};
|
2018-09-25 14:21:04 +02:00
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
// Simple list of void() functors
|
|
|
|
class task_stack
|
|
|
|
{
|
|
|
|
struct task_base
|
|
|
|
{
|
|
|
|
std::unique_ptr<task_base> next;
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
virtual ~task_base();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
virtual void invoke()
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
if (next)
|
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
next->invoke();
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
template <typename F>
|
|
|
|
struct task_type final : task_base
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
std::remove_reference_t<F> func;
|
|
|
|
|
|
|
|
task_type(F&& func)
|
|
|
|
: func(std::forward<F>(func))
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
void invoke() final override
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
func();
|
2016-09-07 00:38:52 +02:00
|
|
|
task_base::invoke();
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
};
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
std::unique_ptr<task_base> m_stack;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
public:
|
|
|
|
task_stack() = default;
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
template <typename F>
|
2016-05-13 16:01:48 +02:00
|
|
|
task_stack(F&& func)
|
|
|
|
: m_stack(new task_type<F>(std::forward<F>(func)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void push(task_stack stack)
|
|
|
|
{
|
|
|
|
auto _top = stack.m_stack.release();
|
2016-04-25 12:49:12 +02:00
|
|
|
auto _next = m_stack.release();
|
|
|
|
m_stack.reset(_top);
|
2016-05-13 16:01:48 +02:00
|
|
|
while (UNLIKELY(_top->next)) _top = _top->next.get();
|
2016-04-25 12:49:12 +02:00
|
|
|
_top->next.reset(_next);
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
m_stack.reset();
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
void invoke() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
if (m_stack)
|
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
m_stack->invoke();
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
// Thread base class (TODO: remove shared_ptr, make private base)
|
2018-09-25 14:21:04 +02:00
|
|
|
class thread_base : public std::enable_shared_from_this<thread_base>
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2018-09-25 14:21:04 +02:00
|
|
|
// Native thread entry point function type
|
|
|
|
#ifdef _WIN32
|
|
|
|
using native_entry = uint(__stdcall*)(void* arg);
|
|
|
|
#else
|
|
|
|
using native_entry = void*(*)(void* arg);
|
|
|
|
#endif
|
2015-08-21 13:07:31 +02:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Self pointer for detached thread
|
|
|
|
std::shared_ptr<thread_base> m_self;
|
2016-09-07 00:38:52 +02:00
|
|
|
|
|
|
|
// Thread handle (platform-specific)
|
|
|
|
atomic_t<std::uintptr_t> m_thread{0};
|
|
|
|
|
|
|
|
// Thread mutex
|
2018-09-25 14:21:04 +02:00
|
|
|
mutable shared_mutex m_mutex;
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Thread condition variable
|
|
|
|
cond_variable m_cond;
|
|
|
|
|
|
|
|
// Thread flags
|
|
|
|
atomic_t<u32> m_signal{0};
|
|
|
|
|
|
|
|
// Thread joining condition variable
|
2018-09-25 14:21:04 +02:00
|
|
|
mutable cond_variable m_jcv;
|
|
|
|
|
|
|
|
// Thread state
|
|
|
|
atomic_t<thread_state> m_state = thread_state::created;
|
2016-09-07 00:38:52 +02:00
|
|
|
|
|
|
|
// Remotely set or caught exception
|
|
|
|
std::exception_ptr m_exception;
|
|
|
|
|
2018-09-15 13:42:14 +02:00
|
|
|
// Thread initial task
|
2016-09-07 00:38:52 +02:00
|
|
|
task_stack m_task;
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Thread name
|
|
|
|
lf_value<std::string> m_name;
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2018-05-24 21:22:07 +02:00
|
|
|
// CPU cycles thread has run for
|
|
|
|
u64 m_cycles{0};
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Start thread
|
2018-09-25 14:21:04 +02:00
|
|
|
static void start(const std::shared_ptr<thread_base>&, task_stack);
|
|
|
|
|
|
|
|
void start(native_entry);
|
2015-11-26 09:06:29 +01:00
|
|
|
|
|
|
|
// Called at the thread start
|
2016-04-25 12:49:12 +02:00
|
|
|
void initialize();
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Called at the thread end, returns moved m_self (may be null)
|
|
|
|
std::shared_ptr<thread_base> finalize(std::exception_ptr) noexcept;
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
static void finalize() noexcept;
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Internal throwing function. Mutex must be locked and will be unlocked.
|
|
|
|
[[noreturn]] void _throw();
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Internal notification function
|
2018-09-25 14:21:04 +02:00
|
|
|
void _notify(cond_variable thread_base::*);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
friend class thread_ctrl;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
template <class Context>
|
|
|
|
friend class named_thread;
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
public:
|
|
|
|
thread_base(std::string_view name);
|
2018-09-22 21:35:52 +02:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
~thread_base();
|
2015-09-26 22:46:04 +02:00
|
|
|
|
|
|
|
// Get thread name
|
2016-02-01 22:55:43 +01:00
|
|
|
const std::string& get_name() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Set thread name (not recommended)
|
|
|
|
void set_name(std::string_view name)
|
|
|
|
{
|
|
|
|
m_name.assign(name);
|
|
|
|
}
|
|
|
|
|
2018-05-24 21:22:07 +02:00
|
|
|
// Get CPU cycles since last time this function was called. First call returns 0.
|
|
|
|
u64 get_cycles();
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Set exception
|
|
|
|
void set_exception(std::exception_ptr ptr);
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Wait for the thread (it does NOT change thread state, and can be called from multiple threads)
|
|
|
|
void join() const;
|
|
|
|
|
|
|
|
// Make thread to manage a shared_ptr of itself
|
|
|
|
void detach();
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Notify the thread
|
2016-05-13 16:01:48 +02:00
|
|
|
void notify();
|
2018-09-25 14:21:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Collection of global function for current thread
|
|
|
|
class thread_ctrl final
|
|
|
|
{
|
|
|
|
// Current thread
|
|
|
|
static thread_local thread_base* g_tls_this_thread;
|
|
|
|
|
|
|
|
// Target cpu core layout
|
|
|
|
static atomic_t<native_core_arrangement> g_native_core_layout;
|
|
|
|
|
|
|
|
// Internal waiting function, may throw. Infinite value is -1.
|
|
|
|
static bool _wait_for(u64 usec);
|
|
|
|
|
|
|
|
friend class thread_base;
|
|
|
|
|
|
|
|
public:
|
2018-10-01 19:05:47 +02:00
|
|
|
// Get current thread name
|
|
|
|
static std::string_view get_name()
|
|
|
|
{
|
|
|
|
return g_tls_this_thread->m_name.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get thread name
|
|
|
|
template <typename T>
|
|
|
|
static std::string_view get_name(const named_thread<T>& thread)
|
|
|
|
{
|
|
|
|
return static_cast<const thread_base&>(thread).m_name.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set current thread name (not recommended)
|
|
|
|
static void set_name(std::string_view name)
|
|
|
|
{
|
|
|
|
g_tls_this_thread->m_name.assign(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set thread name (not recommended)
|
|
|
|
template <typename T>
|
|
|
|
static void set_name(named_thread<T>& thread, std::string_view name)
|
|
|
|
{
|
|
|
|
static_cast<thread_base&>(thread).m_name.assign(name);
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Read current state
|
|
|
|
static inline thread_state state()
|
|
|
|
{
|
|
|
|
return g_tls_this_thread->m_state;
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Wait once with timeout. Abortable, may throw. May spuriously return false.
|
|
|
|
static inline bool wait_for(u64 usec)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
return _wait_for(usec);
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Wait. Abortable, may throw.
|
|
|
|
static inline void wait()
|
2016-07-27 23:43:22 +02:00
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
_wait_for(-1);
|
2016-07-27 23:43:22 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Wait until pred(). Abortable, may throw.
|
2018-09-25 14:21:04 +02:00
|
|
|
template <typename F, typename RT = std::invoke_result_t<F>>
|
2016-09-07 00:38:52 +02:00
|
|
|
static inline RT wait(F&& pred)
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
if (RT result = pred())
|
2016-05-13 16:01:48 +02:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
_wait_for(-1);
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Wait eternally until aborted.
|
2016-05-13 16:01:48 +02:00
|
|
|
[[noreturn]] static inline void eternalize()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
_wait_for(-1);
|
2016-05-13 16:01:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Test exception (may throw).
|
|
|
|
static void test();
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Get current thread (may be nullptr)
|
2018-09-25 14:21:04 +02:00
|
|
|
static thread_base* get_current()
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
|
|
|
return g_tls_this_thread;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Create detached named thread
|
2018-09-25 14:21:04 +02:00
|
|
|
template <typename N, typename F>
|
2016-07-27 23:43:22 +02:00
|
|
|
static inline void spawn(N&& name, F&& func)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
2018-09-25 14:21:04 +02:00
|
|
|
auto out = std::make_shared<thread_base>(std::forward<N>(name));
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
thread_base::start(out, std::forward<F>(func));
|
2016-07-27 23:43:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Named thread factory
|
2018-09-25 14:21:04 +02:00
|
|
|
template <typename N, typename F>
|
|
|
|
static inline void spawn(std::shared_ptr<thread_base>& out, N&& name, F&& func)
|
2016-07-27 23:43:22 +02:00
|
|
|
{
|
2018-09-25 14:21:04 +02:00
|
|
|
out = std::make_shared<thread_base>(std::forward<N>(name));
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
thread_base::start(out, std::forward<F>(func));
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
2017-06-24 17:36:49 +02:00
|
|
|
|
2017-10-21 13:21:37 +02:00
|
|
|
// Detect layout
|
|
|
|
static void detect_cpu_layout();
|
|
|
|
|
|
|
|
// Returns a core affinity mask. Set whether to generate the high priority set or not
|
|
|
|
static u16 get_affinity_mask(thread_class group);
|
|
|
|
|
|
|
|
// Sets the native thread priority
|
2017-06-24 17:36:49 +02:00
|
|
|
static void set_native_priority(int priority);
|
2017-10-21 13:21:37 +02:00
|
|
|
|
|
|
|
// Sets the preferred affinity mask for this thread
|
|
|
|
static void set_thread_affinity_mask(u16 mask);
|
2018-09-25 14:21:04 +02:00
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
static inline std::shared_ptr<named_thread<F>> make_shared(std::string_view name, F&& lambda)
|
|
|
|
{
|
|
|
|
return std::make_shared<named_thread<F>>(name, std::forward<F>(lambda));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
static inline std::shared_ptr<named_thread<T>> make_shared(std::string_view name, Args&&... args)
|
|
|
|
{
|
|
|
|
return std::make_shared<named_thread<T>>(name, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Derived from the callable object Context, possibly a lambda
|
|
|
|
template <class Context>
|
|
|
|
class named_thread final : public Context, result_storage_t<Context>, public thread_base
|
|
|
|
{
|
|
|
|
using result = result_storage_t<Context>;
|
|
|
|
using thread = thread_base;
|
|
|
|
|
|
|
|
// Type-erased thread entry point
|
|
|
|
#ifdef _WIN32
|
|
|
|
static inline uint __stdcall entry_point(void* arg) try
|
|
|
|
#else
|
|
|
|
static inline void* entry_point(void* arg) try
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
const auto maybe_last_ptr = static_cast<named_thread*>(static_cast<thread*>(arg))->entry_point();
|
|
|
|
thread::finalize();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
catch_all_exceptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<thread> entry_point()
|
|
|
|
{
|
|
|
|
thread::initialize();
|
|
|
|
|
|
|
|
if constexpr (result::empty)
|
|
|
|
{
|
|
|
|
// No result
|
|
|
|
Context::operator()();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Construct the result using placement new (copy elision should happen)
|
|
|
|
new (result::get()) typename result::type(Context::operator()());
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread::finalize(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
friend class thread_ctrl;
|
2018-09-25 14:21:04 +02:00
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
public:
|
2018-09-25 14:21:04 +02:00
|
|
|
// Normal forwarding constructor
|
|
|
|
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<Context, Args&&...>>>
|
|
|
|
named_thread(std::string_view name, Args&&... args)
|
|
|
|
: Context(std::forward<Args>(args)...)
|
|
|
|
, thread(name)
|
|
|
|
{
|
|
|
|
thread::start(&named_thread::entry_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lambda constructor, also the implicit deduction guide candidate
|
|
|
|
named_thread(std::string_view name, Context&& f)
|
|
|
|
: Context(std::forward<Context>(f))
|
|
|
|
, thread(name)
|
|
|
|
{
|
|
|
|
thread::start(&named_thread::entry_point);
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
named_thread(const named_thread&) = delete;
|
|
|
|
|
|
|
|
named_thread& operator=(const named_thread&) = delete;
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Wait for the completion and access result (if not void)
|
|
|
|
[[nodiscard]] decltype(auto) operator()()
|
|
|
|
{
|
|
|
|
thread::join();
|
|
|
|
|
|
|
|
if constexpr (!result::empty)
|
|
|
|
{
|
|
|
|
return *result::get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the completion and access result (if not void)
|
|
|
|
[[nodiscard]] decltype(auto) operator()() const
|
|
|
|
{
|
|
|
|
thread::join();
|
|
|
|
|
|
|
|
if constexpr (!result::empty)
|
|
|
|
{
|
|
|
|
return *result::get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Access thread state
|
|
|
|
operator thread_state() const
|
|
|
|
{
|
|
|
|
return thread::m_state.load();
|
|
|
|
}
|
|
|
|
|
2018-10-01 19:05:47 +02:00
|
|
|
// Try to set thread_state::aborting
|
|
|
|
named_thread& operator=(thread_state s)
|
2018-09-25 14:21:04 +02:00
|
|
|
{
|
2018-10-01 19:05:47 +02:00
|
|
|
if (s != thread_state::aborting)
|
|
|
|
{
|
|
|
|
ASSUME(0);
|
|
|
|
}
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Notify thread if not detached or terminated
|
|
|
|
if (thread::m_state.compare_and_swap_test(thread_state::created, thread_state::aborting))
|
|
|
|
{
|
2018-10-01 19:05:47 +02:00
|
|
|
// Call on_abort() method if it's available
|
2018-10-02 16:49:53 +02:00
|
|
|
if constexpr (thread_on_abort<Context>())
|
2018-09-25 14:21:04 +02:00
|
|
|
{
|
2018-10-01 19:05:47 +02:00
|
|
|
Context::on_abort();
|
2018-09-25 14:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
thread::notify();
|
2018-10-01 19:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context type doesn't need virtual destructor
|
|
|
|
~named_thread()
|
|
|
|
{
|
|
|
|
*this = thread_state::aborting;
|
|
|
|
thread::join();
|
|
|
|
|
|
|
|
if constexpr (!result::empty)
|
|
|
|
{
|
|
|
|
result::destroy();
|
2018-09-25 14:21:04 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
};
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
// Old named_thread
|
|
|
|
class old_thread
|
2014-01-31 19:40:18 +01:00
|
|
|
{
|
2015-09-26 22:46:04 +02:00
|
|
|
// Pointer to managed resource (shared with actual thread)
|
2018-09-25 14:21:04 +02:00
|
|
|
std::shared_ptr<thread_base> m_thread;
|
2015-01-16 15:36:53 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
public:
|
2018-09-25 14:21:04 +02:00
|
|
|
old_thread();
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
virtual ~old_thread();
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
old_thread(const old_thread&) = delete;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
old_thread& operator=(const old_thread&) = delete;
|
2018-09-22 21:35:52 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Get thread name
|
|
|
|
virtual std::string get_name() const;
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
protected:
|
2016-07-24 01:56:03 +02:00
|
|
|
// Start thread (cannot be called from the constructor: should throw in such case)
|
|
|
|
void start_thread(const std::shared_ptr<void>& _this);
|
2016-04-25 12:49:12 +02:00
|
|
|
|
|
|
|
// Thread task (called in the thread)
|
|
|
|
virtual void on_task() = 0;
|
|
|
|
|
|
|
|
// Thread finalization (called after on_task)
|
|
|
|
virtual void on_exit() {}
|
|
|
|
|
2017-04-23 22:09:27 +02:00
|
|
|
// Called once upon thread spawn within the thread's own context
|
|
|
|
virtual void on_spawn() {}
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
public:
|
2016-02-01 22:55:43 +01:00
|
|
|
// ID initialization
|
2016-07-24 01:56:03 +02:00
|
|
|
virtual void on_init(const std::shared_ptr<void>& _this)
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-07-24 01:56:03 +02:00
|
|
|
return start_thread(_this);
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID finalization
|
|
|
|
virtual void on_stop()
|
|
|
|
{
|
2016-04-25 12:49:12 +02:00
|
|
|
m_thread->join();
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
|
|
|
|
2018-09-25 14:21:04 +02:00
|
|
|
thread_base* get() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
return m_thread.get();
|
|
|
|
}
|
2016-07-27 23:43:22 +02:00
|
|
|
|
|
|
|
void join() const
|
|
|
|
{
|
|
|
|
return m_thread->join();
|
|
|
|
}
|
|
|
|
|
|
|
|
void notify() const
|
|
|
|
{
|
|
|
|
return m_thread->notify();
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
};
|