Postpone thread launching on g_fxo->init

This commit is contained in:
Eladash 2023-12-14 22:20:21 +02:00 committed by Elad Ashkenazi
parent a4bcba8971
commit 0c410f8a14
11 changed files with 112 additions and 44 deletions

View file

@ -5,6 +5,7 @@
#include "util/shared_ptr.hpp"
#include <string>
#include <execution>
#include "mutex.h"
#include "lockless.h"
@ -83,9 +84,9 @@ struct result_storage<Ctx, Args...>
};
template <typename T>
concept NamedThreadName = requires (const T& t)
concept NamedThreadName = requires (const T&)
{
std::string(t.thread_name);
std::string(T::thread_name);
};
// Base class for task queue (linked list)
@ -446,6 +447,11 @@ public:
}
};
namespace stx
{
struct launch_retainer;
}
// Derived from the callable object Context, possibly a lambda
template <class Context>
class named_thread final : public Context, result_storage<Context>, thread_base
@ -512,17 +518,27 @@ class named_thread final : public Context, result_storage<Context>, thread_base
public:
// Forwarding constructor with default name (also potentially the default constructor)
template <typename... Args> requires (std::is_constructible_v<Context, Args&&...>) && (NamedThreadName<Context>)
named_thread(Args&&... args)
template <typename... Args> requires (std::is_constructible_v<Context, Args&&...>) && (!(std::is_same_v<std::remove_cvref_t<Args>, stx::launch_retainer> || ...)) && (NamedThreadName<Context>)
named_thread(Args&&... args) noexcept
: Context(std::forward<Args>(args)...)
, thread(trampoline, std::string(Context::thread_name))
{
thread::start();
}
// Forwarding constructor with default name, does not automatically run the thread
template <typename... Args> requires (std::is_constructible_v<Context, Args&&...>) && (NamedThreadName<Context>)
named_thread(const stx::launch_retainer&, Args&&... args) noexcept
: Context(std::forward<Args>(args)...)
, thread(trampoline, std::string(Context::thread_name))
{
// Create a stand-by thread context
m_sync |= static_cast<u32>(thread_state::finished);
}
// Normal forwarding constructor
template <typename... Args> requires (std::is_constructible_v<Context, Args&&...>)
named_thread(std::string name, Args&&... args)
template <typename... Args> requires (std::is_constructible_v<Context, Args&&...>) && (!NamedThreadName<Context>)
named_thread(std::string name, Args&&... args) noexcept
: Context(std::forward<Args>(args)...)
, thread(trampoline, std::move(name))
{
@ -530,7 +546,7 @@ public:
}
// Lambda constructor, also the implicit deduction guide candidate
named_thread(std::string_view name, Context&& f)
named_thread(std::string_view name, Context&& f) noexcept requires (!NamedThreadName<Context>)
: Context(std::forward<Context>(f))
, thread(trampoline, std::string(name))
{
@ -644,12 +660,20 @@ public:
return static_cast<thread_state>(thread::m_sync.load() & 3);
}
// Try to abort by assigning thread_state::aborting/finished
// Join thread by thread_state::finished
named_thread& operator=(thread_state s)
{
if (s == thread_state::created)
{
// Run thread
ensure(operator thread_state() == thread_state::finished);
thread::start();
return *this;
}
bool notify_sync = false;
// Try to abort by assigning thread_state::aborting/finished
// Join thread by thread_state::finished
if (s >= thread_state::aborting && thread::m_sync.fetch_op([](u32& v) { return !(v & 3) && (v |= 1); }).second)
{
notify_sync = true;