Thread.cpp refinement

Hide thread mutex
Safe notify() method
Other refactoring
This commit is contained in:
Nekotekina 2016-09-07 01:38:52 +03:00
parent da878c36bd
commit a5a2d43d7c
35 changed files with 532 additions and 591 deletions

View file

@ -7,6 +7,9 @@
#include <string>
#include <memory>
#include "sema.h"
#include "cond.h"
// Will report exception and call std::abort() if put in catch(...)
[[noreturn]] void catch_all_exceptions();
@ -17,19 +20,19 @@ class task_stack
{
std::unique_ptr<task_base> next;
virtual ~task_base() = default;
virtual ~task_base();
virtual void exec()
virtual void invoke()
{
if (next)
{
next->exec();
next->invoke();
}
}
};
template<typename F>
struct task_type : task_base
template <typename F>
struct task_type final : task_base
{
std::remove_reference_t<F> func;
@ -38,10 +41,10 @@ class task_stack
{
}
void exec() override
void invoke() final override
{
func();
task_base::exec();
task_base::invoke();
}
};
@ -50,7 +53,7 @@ class task_stack
public:
task_stack() = default;
template<typename F>
template <typename F>
task_stack(F&& func)
: m_stack(new task_type<F>(std::forward<F>(func)))
{
@ -70,11 +73,11 @@ public:
m_stack.reset();
}
void exec() const
void invoke() const
{
if (m_stack)
{
m_stack->exec();
m_stack->invoke();
}
}
};
@ -82,23 +85,41 @@ public:
// Thread control class
class thread_ctrl final
{
public: // TODO
struct internal;
private:
// Current thread
static thread_local thread_ctrl* g_tls_this_thread;
// Thread handle storage
std::aligned_storage_t<16> m_thread;
// Self pointer
std::shared_ptr<thread_ctrl> m_self;
// Thread join contention counter
atomic_t<u32> m_joining{};
// Thread handle (platform-specific)
atomic_t<std::uintptr_t> m_thread{0};
// Thread mutex
mutable semaphore<> m_mutex;
// 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;
// Thread interrupt guard counter
volatile u32 m_guard = 0x80000000;
// Thread internals
atomic_t<internal*> m_data{};
// Thread interrupt condition variable
cond_variable m_icv;
// Interrupt function
atomic_t<void(*)()> m_iptr{nullptr};
// Fixed name
std::string m_name;
@ -110,19 +131,19 @@ private:
void initialize();
// Called at the thread end
void finalize() noexcept;
void finalize(std::exception_ptr) noexcept;
// Get atexit function
void push_atexit(task_stack);
// Add task (atexit)
static void _push(task_stack);
// Start waiting
void wait_start(u64 timeout);
// Internal waiting function, may throw. Infinite value is -1.
static bool _wait_for(u64 usec);
// Proceed waiting
bool wait_wait(u64 timeout);
// Internal throwing function. Mutex must be locked and will be unlocked.
[[noreturn]] void _throw();
// Check exception
void test();
// Internal notification function
void _notify(cond_variable thread_ctrl::*);
public:
thread_ctrl(std::string&& name);
@ -137,63 +158,22 @@ public:
return m_name;
}
// Initialize internal data
void initialize_once();
// Get exception
std::exception_ptr get_exception() const;
// Set exception
void set_exception(std::exception_ptr ptr);
// Get thread result (may throw, simultaneous joining allowed)
void join();
// 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();
// Lock, unlock, notify the thread (required if the condition changed locklessly)
void lock_notify();
// Notify the thread (internal data must be initialized)
// Notify the thread
void notify();
// Set exception (internal data must be initialized, thread mutex must be locked)
void set_exception(std::exception_ptr);
// Internal
static void handle_interrupt();
// Interrupt thread with specified handler call (thread mutex must be locked)
// Interrupt thread with specified handler call
void interrupt(void(*handler)());
// Interrupt guard recursive enter
@ -226,90 +206,45 @@ public:
// Check interrupt if delayed by guard scope
void test_interrupt();
// 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);
// Wait until pred(). Abortable, may throw. Thread must be locked.
// Timeout in microseconds (zero means infinite).
template<typename F>
static inline auto wait_for(u64 useconds, F&& pred)
// Wait once with timeout. Abortable, may throw. May spuriously return false.
static inline bool wait_for(u64 usec)
{
if (useconds)
{
g_tls_this_thread->wait_start(useconds);
}
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;
}
}
return _wait_for(usec);
}
// Wait once. Abortable, may throw. Thread must be locked.
// Timeout in microseconds (zero means infinite).
static inline bool wait_for(u64 useconds = 0)
{
if (useconds)
{
g_tls_this_thread->wait_start(useconds);
}
g_tls_this_thread->test();
if (!g_tls_this_thread->wait_wait(useconds) && useconds)
{
return false;
}
g_tls_this_thread->test();
return true;
}
// 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. Abortable, may throw. Thread must be locked.
// Wait. Abortable, may throw.
static inline void wait()
{
g_tls_this_thread->test();
g_tls_this_thread->wait_wait(0);
g_tls_this_thread->test();
_wait_for(-1);
}
// Wait eternally. Abortable, may throw. Thread must be locked.
// Wait until pred(). Abortable, may throw.
template<typename F, typename RT = std::result_of_t<F()>>
static inline RT wait(F&& pred)
{
while (true)
{
if (RT result = pred())
{
return result;
}
_wait_for(-1);
}
}
// Wait eternally until aborted.
[[noreturn]] static inline void eternalize()
{
while (true)
{
g_tls_this_thread->test();
g_tls_this_thread->wait_wait(0);
_wait_for(-1);
}
}
// Test exception (may throw).
static void test();
// Get current thread (may be nullptr)
static thread_ctrl* get_current()
{
@ -320,14 +255,14 @@ public:
template<typename F>
static inline void atexit(F&& func)
{
return g_tls_this_thread->push_atexit(std::forward<F>(func));
_push(std::forward<F>(func));
}
// Named thread factory
// Create detached named thread
template<typename N, typename F>
static inline void spawn(N&& name, F&& func)
{
auto&& out = std::make_shared<thread_ctrl>(std::forward<N>(name));
auto out = std::make_shared<thread_ctrl>(std::forward<N>(name));
thread_ctrl::start(out, std::forward<F>(func));
}
@ -382,7 +317,7 @@ public:
}
// Access thread_ctrl
thread_ctrl* operator->() const
thread_ctrl* get() const
{
return m_thread.get();
}
@ -392,60 +327,12 @@ public:
return m_thread->join();
}
void lock() const
{
return m_thread->lock();
}
void unlock() const
{
return m_thread->unlock();
}
void lock_notify() const
{
return m_thread->lock_notify();
}
void notify() const
{
return m_thread->notify();
}
};
// 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->())
{
}
// Lock current thread
thread_lock()
: thread_lock(thread_ctrl::get_current())
{
}
~thread_lock()
{
m_thread->unlock();
}
};
// Interrupt guard scope
class thread_guard final
{
@ -455,24 +342,24 @@ public:
thread_guard(const thread_guard&) = delete;
thread_guard(thread_ctrl* thread)
: m_thread(thread)
//: m_thread(thread)
{
m_thread->guard_enter();
//m_thread->guard_enter();
}
thread_guard(named_thread& thread)
: thread_guard(thread.operator->())
//: thread_guard(thread.get())
{
}
thread_guard()
: thread_guard(thread_ctrl::get_current())
//: thread_guard(thread_ctrl::get_current())
{
}
~thread_guard() noexcept(false)
{
m_thread->guard_leave();
//m_thread->guard_leave();
}
};
@ -498,7 +385,7 @@ public:
}
// Access thread_ctrl
thread_ctrl* operator->() const
thread_ctrl* get() const
{
return m_thread.get();
}