Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve threads #7589

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,8 @@ DECLARE(thread_ctrl::g_native_core_layout) { native_core_arrangement::undefined

void thread_base::start(native_entry entry)
{
thread_ctrl::g_thread_count++;

#ifdef _WIN32
m_thread = ::_beginthreadex(nullptr, 0, entry, this, CREATE_SUSPENDED, nullptr);
verify("thread_ctrl::start" HERE), m_thread, ::ResumeThread(reinterpret_cast<HANDLE>(+m_thread)) != -1;
Expand Down Expand Up @@ -1863,6 +1865,7 @@ void thread_base::finalize() noexcept
{
g_tls_log_prefix = []() -> std::string { return {}; };
thread_ctrl::g_tls_this_thread = nullptr;
thread_ctrl::g_thread_count--;
}

void thread_ctrl::_wait_for(u64 usec, bool alert /* true */)
Expand Down
8 changes: 8 additions & 0 deletions Utilities/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class thread_ctrl final
// Target cpu core layout
static atomic_t<native_core_arrangement> g_native_core_layout;

// Global thread counter
static inline atomic_t<u64> g_thread_count = 0;

// Internal waiting function, may throw. Infinite value is -1.
static void _wait_for(u64 usec, bool alert);

Expand Down Expand Up @@ -275,6 +278,11 @@ class thread_ctrl final
return g_tls_this_thread;
}

static u64 get_count()
{
return g_thread_count.load();
}

// Detect layout
static void detect_cpu_layout();

Expand Down
3 changes: 0 additions & 3 deletions rpcs3/Emu/CPU/CPUThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,6 @@ void cpu_thread::stop_all() noexcept
std::this_thread::sleep_for(10ms);
}

// Workaround for remaining threads (TODO)
std::this_thread::sleep_for(1300ms);

sys_log.notice("All CPU threads have been stopped.");
}

Expand Down
12 changes: 4 additions & 8 deletions rpcs3/Emu/RSX/RSXThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ namespace rsx

vblank_count = 0;

auto vblank_body = [this]()
g_fxo->init_crtp<named_thread>("VBlank Thread", [this]()
{
// See sys_timer_usleep for details
#ifdef __linux__
Expand Down Expand Up @@ -562,11 +562,9 @@ namespace rsx

thread_ctrl::wait_for(100);
}
};

g_fxo->init<named_thread<decltype(vblank_body)>>("VBlank Thread", std::move(vblank_body));
});

auto decomp_body = [this]
g_fxo->init_crtp<named_thread>("RSX Decompiler Thread", [this]
{
if (g_cfg.video.disable_asynchronous_shader_compiler)
{
Expand Down Expand Up @@ -597,9 +595,7 @@ namespace rsx
}

on_decompiler_exit();
};

g_fxo->init<named_thread<decltype(decomp_body)>>("RSX Decompiler Thread", std::move(decomp_body));
});

// Raise priority above other threads
thread_ctrl::set_native_priority(1);
Expand Down
13 changes: 10 additions & 3 deletions rpcs3/Emu/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,7 @@ void Emulator::Load(const std::string& title_id, bool add_only, bool force_globa
// Workaround for analyser glitches
vm::falloc(0x10000, 0xf0000, vm::main);

auto sprx_loader_body = [this]
g_fxo->init_crtp<named_thread>("SPRX Loader"sv, [this]
{
std::vector<std::string> dir_queue;
dir_queue.emplace_back(m_path + '/');
Expand Down Expand Up @@ -983,9 +983,8 @@ void Emulator::Load(const std::string& title_id, bool add_only, bool force_globa
{
Emu.Stop();
});
};
});

g_fxo->init<named_thread<decltype(sprx_loader_body)>>("SPRX Loader"sv, std::move(sprx_loader_body));
return;
}

Expand Down Expand Up @@ -1600,6 +1599,14 @@ void Emulator::Stop(bool restart)

cpu_thread::stop_all();
g_fxo->reset();

while (thread_ctrl::get_count())
{
std::this_thread::sleep_for(10ms);
}

sys_log.notice("All threads have been stopped.");

lv2_obj::cleanup();
idm::clear();

Expand Down
8 changes: 8 additions & 0 deletions rpcs3/util/fixed_typemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ namespace stx
return obj;
}

// Special stuff
template <template <class...> typename CTAD, typename... Args>
auto init_crtp(Args&&... args) noexcept
{
using T = decltype(CTAD{std::forward<Args>(args)...});
return init<T>(std::forward<Args>(args)...);
}

// Obtain object pointer (the only thread safe function)
template <typename T>
T* get() const noexcept
Expand Down