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>
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
#include "sema.h"
|
|
|
|
#include "cond.h"
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-26 09:06:29 +01:00
|
|
|
// Thread control class
|
|
|
|
class thread_ctrl final
|
2015-07-01 00:25:52 +02:00
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
// Current thread
|
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-09-07 00:38:52 +02:00
|
|
|
// Self pointer
|
|
|
|
std::shared_ptr<thread_ctrl> m_self;
|
|
|
|
|
|
|
|
// Thread handle (platform-specific)
|
|
|
|
atomic_t<std::uintptr_t> m_thread{0};
|
|
|
|
|
|
|
|
// Thread mutex
|
|
|
|
mutable semaphore<> 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
|
|
|
|
cond_variable m_jcv;
|
|
|
|
|
|
|
|
// Remotely set or caught exception
|
|
|
|
std::exception_ptr m_exception;
|
|
|
|
|
|
|
|
// Thread initial task or atexit task
|
|
|
|
task_stack m_task;
|
2016-05-13 16:01:48 +02:00
|
|
|
|
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-09-07 00:38:52 +02:00
|
|
|
void finalize(std::exception_ptr) noexcept;
|
2016-04-25 12:49:12 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Add task (atexit)
|
|
|
|
static void _push(task_stack);
|
2016-05-13 16:01:48 +02:00
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Internal waiting function, may throw. Infinite value is -1.
|
|
|
|
static bool _wait_for(u64 usec);
|
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
|
|
|
|
void _notify(cond_variable thread_ctrl::*);
|
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-09-07 00:38:52 +02:00
|
|
|
// Get exception
|
|
|
|
std::exception_ptr get_exception() const;
|
|
|
|
|
|
|
|
// Set exception
|
|
|
|
void set_exception(std::exception_ptr ptr);
|
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-09-07 00:38:52 +02:00
|
|
|
// Notify the thread
|
2016-05-13 16:01:48 +02:00
|
|
|
void notify();
|
|
|
|
|
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.
|
|
|
|
template<typename F, typename RT = std::result_of_t<F()>>
|
|
|
|
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)
|
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-09-07 00:38:52 +02:00
|
|
|
_push(std::forward<F>(func));
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:52 +02:00
|
|
|
// Create detached named thread
|
2016-05-13 16:01:48 +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
|
|
|
{
|
2016-09-07 00:38:52 +02:00
|
|
|
auto out = std::make_shared<thread_ctrl>(std::forward<N>(name));
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
thread_ctrl::start(out, std::forward<F>(func));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Named thread factory
|
|
|
|
template<typename N, typename F>
|
|
|
|
static inline void spawn(std::shared_ptr<thread_ctrl>& out, N&& name, F&& func)
|
|
|
|
{
|
|
|
|
out = std::make_shared<thread_ctrl>(std::forward<N>(name));
|
2015-11-26 09:06:29 +01:00
|
|
|
|
2016-07-27 23:43:22 +02:00
|
|
|
thread_ctrl::start(out, std::forward<F>(func));
|
2015-11-26 09:06:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-24 01:56:03 +02:00
|
|
|
class 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:
|
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() {}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-13 16:01:48 +02:00
|
|
|
// Access thread_ctrl
|
2016-09-07 00:38:52 +02:00
|
|
|
thread_ctrl* 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
|
|
|
};
|
|
|
|
|
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-07-04 01:22:24 +02:00
|
|
|
{
|
2016-07-27 23:43:22 +02:00
|
|
|
thread_ctrl::spawn(m_thread, 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
|
|
|
}
|
2016-07-27 23:43:22 +02:00
|
|
|
|
|
|
|
// Access thread_ctrl
|
2016-09-07 00:38:52 +02:00
|
|
|
thread_ctrl* get() const
|
2016-07-27 23:43:22 +02:00
|
|
|
{
|
|
|
|
return m_thread.get();
|
|
|
|
}
|
2015-07-04 01:22:24 +02:00
|
|
|
};
|