2012-11-15 00:39:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
#include <exception>
|
2016-02-01 22:55:43 +01:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "Platform.h"
|
2016-04-25 12:49:12 +02:00
|
|
|
#include "Atomic.h"
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2015-12-18 12:11:18 +01:00
|
|
|
// Will report exception and call std::abort() if put in catch(...)
|
|
|
|
[[noreturn]] void catch_all_exceptions();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
virtual ~task_base() = default;
|
|
|
|
|
|
|
|
virtual void exec()
|
|
|
|
{
|
|
|
|
if (next)
|
|
|
|
{
|
|
|
|
next->exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename F>
|
2016-05-13 16:01:48 +02:00
|
|
|
struct task_type : 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-05-13 16:01:48 +02:00
|
|
|
void exec() override
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
task_base::exec();
|
|
|
|
}
|
|
|
|
};
|
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;
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void exec() const
|
|
|
|
{
|
|
|
|
if (m_stack)
|
|
|
|
{
|
|
|
|
m_stack->exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Thread control class
|
|
|
|
class thread_ctrl final
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
public: // TODO
|
2016-04-25 12:49:12 +02:00
|
|
|
struct internal;
|
2014-06-20 13:00:36 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
private:
|
2016-04-25 12:49:12 +02:00
|
|
|
static thread_local thread_ctrl* g_tls_this_thread;
|
2015-08-21 13:07:31 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Thread handle storage
|
|
|
|
std::aligned_storage_t<16> m_thread;
|
2015-02-18 17:22:06 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Thread join contention counter
|
2016-05-13 16:01:48 +02:00
|
|
|
atomic_t<u32> m_joining{};
|
|
|
|
|
|
|
|
// Thread internals
|
|
|
|
atomic_t<internal*> m_data{};
|
2016-04-25 12:49:12 +02:00
|
|
|
|
|
|
|
// Fixed name
|
|
|
|
std::string m_name;
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Start thread
|
|
|
|
static void start(const std::shared_ptr<thread_ctrl>&, task_stack);
|
2015-11-26 09:06:29 +01:00
|
|
|
|
|
|
|
// Called at the thread start
|
2016-04-25 12:49:12 +02:00
|
|
|
void initialize();
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Called at the thread end
|
2016-04-25 12:49:12 +02:00
|
|
|
void finalize() noexcept;
|
|
|
|
|
|
|
|
// Get atexit function
|
2016-05-13 16:01:48 +02:00
|
|
|
void push_atexit(task_stack);
|
|
|
|
|
|
|
|
// Start waiting
|
|
|
|
void wait_start(u64 timeout);
|
|
|
|
|
|
|
|
// Proceed waiting
|
|
|
|
bool wait_wait(u64 timeout);
|
|
|
|
|
|
|
|
// Check exception
|
|
|
|
void test();
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2014-01-31 19:40:18 +01:00
|
|
|
public:
|
2016-05-13 16:01:48 +02:00
|
|
|
thread_ctrl(std::string&& name);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
thread_ctrl(const thread_ctrl&) = delete;
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
~thread_ctrl();
|
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
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Initialize internal data
|
2016-05-13 16:01:48 +02:00
|
|
|
void initialize_once();
|
2016-02-01 22:55:43 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Get thread result (may throw, simultaneous joining allowed)
|
|
|
|
void join();
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Lock thread mutex
|
|
|
|
void lock();
|
|
|
|
|
|
|
|
// Lock conditionally (double-checked)
|
|
|
|
template<typename F>
|
|
|
|
bool lock_if(F&& pred)
|
|
|
|
{
|
|
|
|
if (pred())
|
|
|
|
{
|
|
|
|
lock();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (LIKELY(pred()))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock thread mutex (internal data must be initialized)
|
|
|
|
void unlock();
|
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Lock, unlock, notify the thread (required if the condition changed locklessly)
|
2016-05-13 16:01:48 +02:00
|
|
|
void lock_notify();
|
|
|
|
|
|
|
|
// Notify the thread (internal data must be initialized)
|
|
|
|
void notify();
|
|
|
|
|
|
|
|
// Set exception (internal data must be initialized, thread mutex must be locked)
|
|
|
|
void set_exception(std::exception_ptr);
|
|
|
|
|
|
|
|
// Current thread sleeps for specified amount of microseconds.
|
|
|
|
// Wrapper for std::this_thread::sleep, doesn't require valid thread_ctrl.
|
|
|
|
[[deprecated]] static void sleep(u64 useconds);
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Wait until pred(). Abortable, may throw. Thread must be locked.
|
|
|
|
// Timeout in microseconds (zero means infinite).
|
|
|
|
template<typename F>
|
|
|
|
static inline auto wait(u64 useconds, F&& pred)
|
|
|
|
{
|
|
|
|
g_tls_this_thread->wait_start(useconds);
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
g_tls_this_thread->test();
|
|
|
|
|
|
|
|
if (auto&& result = pred())
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (!g_tls_this_thread->wait_wait(useconds) && useconds)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until pred(). Abortable, may throw. Thread must be locked.
|
|
|
|
template<typename F>
|
|
|
|
static inline auto wait(F&& pred)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
g_tls_this_thread->test();
|
|
|
|
|
|
|
|
if (auto&& result = pred())
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_tls_this_thread->wait_wait(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait once. Thread must be locked.
|
|
|
|
static inline void wait()
|
|
|
|
{
|
|
|
|
g_tls_this_thread->test();
|
|
|
|
g_tls_this_thread->wait_wait(0);
|
|
|
|
g_tls_this_thread->test();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait unconditionally until aborted. Thread must be locked.
|
|
|
|
[[noreturn]] static inline void eternalize()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
g_tls_this_thread->test();
|
|
|
|
g_tls_this_thread->wait_wait(0);
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
|
|
|
|
// Get current thread (may be nullptr)
|
2016-05-13 16:01:48 +02:00
|
|
|
static thread_ctrl* get_current()
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
|
|
|
return g_tls_this_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register function at thread exit (for the current thread)
|
2016-02-01 22:55:43 +01:00
|
|
|
template<typename F>
|
2016-05-13 16:01:48 +02:00
|
|
|
static inline void atexit(F&& func)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
return g_tls_this_thread->push_atexit(std::forward<F>(func));
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Named thread factory
|
2016-05-13 16:01:48 +02:00
|
|
|
template<typename N, typename F>
|
|
|
|
static inline std::shared_ptr<thread_ctrl> spawn(N&& name, F&& func)
|
2015-11-26 09:06:29 +01:00
|
|
|
{
|
|
|
|
auto ctrl = std::make_shared<thread_ctrl>(std::forward<N>(name));
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
thread_ctrl::start(ctrl, std::forward<F>(func));
|
2015-11-26 09:06:29 +01:00
|
|
|
|
|
|
|
return ctrl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-01 22:55:43 +01:00
|
|
|
class named_thread : public std::enable_shared_from_this<named_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)
|
2015-11-26 09:06:29 +01:00
|
|
|
std::shared_ptr<thread_ctrl> m_thread;
|
2015-01-16 15:36:53 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
public:
|
2016-04-25 12:49:12 +02:00
|
|
|
named_thread();
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
virtual ~named_thread();
|
2015-07-01 00:25:52 +02:00
|
|
|
|
2016-04-25 12:49:12 +02:00
|
|
|
// Deleted copy/move constructors + copy/move operators
|
|
|
|
named_thread(const named_thread&) = delete;
|
2016-02-01 22:55:43 +01: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:
|
|
|
|
// Start thread (cannot be called from the constructor: should throw bad_weak_ptr in such case)
|
|
|
|
void start();
|
|
|
|
|
|
|
|
// Thread task (called in the thread)
|
|
|
|
virtual void on_task() = 0;
|
|
|
|
|
|
|
|
// Thread finalization (called after on_task)
|
|
|
|
virtual void on_exit() {}
|
|
|
|
|
|
|
|
public:
|
2016-02-01 22:55:43 +01:00
|
|
|
// ID initialization
|
|
|
|
virtual void on_init()
|
|
|
|
{
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Access thread_ctrl
|
|
|
|
thread_ctrl* operator->() const
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
|
|
|
return m_thread.get();
|
|
|
|
}
|
2016-05-13 16:01:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Simple thread mutex locker
|
|
|
|
class thread_lock final
|
|
|
|
{
|
|
|
|
thread_ctrl* m_thread;
|
|
|
|
|
|
|
|
public:
|
|
|
|
thread_lock(const thread_lock&) = delete;
|
|
|
|
|
|
|
|
// Lock specified thread
|
|
|
|
thread_lock(thread_ctrl* thread)
|
|
|
|
: m_thread(thread)
|
|
|
|
{
|
|
|
|
m_thread->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock specified named_thread
|
|
|
|
thread_lock(named_thread& thread)
|
|
|
|
: thread_lock(thread.operator->())
|
|
|
|
{
|
|
|
|
}
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Lock current thread
|
|
|
|
thread_lock()
|
|
|
|
: thread_lock(thread_ctrl::get_current())
|
2016-04-25 12:49:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
~thread_lock()
|
2016-02-01 22:55:43 +01:00
|
|
|
{
|
2016-05-13 16:01:48 +02:00
|
|
|
m_thread->unlock();
|
2016-02-01 22:55:43 +01:00
|
|
|
}
|
2014-10-17 22:13:25 +02:00
|
|
|
};
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Wrapper for named thread, joins automatically in the destructor, can only be used in function scope
|
2016-02-01 22:55:43 +01:00
|
|
|
class scope_thread final
|
2015-07-04 01:22:24 +02:00
|
|
|
{
|
2015-11-26 09:06:29 +01:00
|
|
|
std::shared_ptr<thread_ctrl> m_thread;
|
2015-07-04 01:22:24 +02:00
|
|
|
|
|
|
|
public:
|
2015-11-26 09:06:29 +01:00
|
|
|
template<typename N, typename F>
|
2016-02-01 22:55:43 +01:00
|
|
|
scope_thread(N&& name, F&& func)
|
2015-11-26 09:06:29 +01:00
|
|
|
: m_thread(thread_ctrl::spawn(std::forward<N>(name), std::forward<F>(func)))
|
2015-07-04 01:22:24 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Deleted copy/move constructors + copy/move operators
|
2016-02-01 22:55:43 +01:00
|
|
|
scope_thread(const scope_thread&) = delete;
|
2015-09-26 22:46:04 +02:00
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Destructor with exceptions allowed
|
2016-02-01 22:55:43 +01:00
|
|
|
~scope_thread() noexcept(false)
|
2015-07-04 01:22:24 +02:00
|
|
|
{
|
2015-11-26 09:06:29 +01:00
|
|
|
m_thread->join();
|
2015-07-04 01:22:24 +02:00
|
|
|
}
|
|
|
|
};
|