hle: kernel: service_thread: Add thread name and take weak_ptr of ServerSession.

This commit is contained in:
bunnei 2020-12-29 01:06:39 -08:00
parent 7d77a3f88f
commit f57be2e626
3 changed files with 22 additions and 11 deletions

View File

@ -34,7 +34,7 @@ ResultVal<std::shared_ptr<ServerSession>> ServerSession::Create(KernelCore& kern
session->name = std::move(name); session->name = std::move(name);
session->parent = std::move(parent); session->parent = std::move(parent);
session->service_thread = std::make_unique<ServiceThread>(kernel, 1); session->service_thread = std::make_unique<ServiceThread>(kernel, 1, session->name);
return MakeResult(std::move(session)); return MakeResult(std::move(session));
} }

View File

@ -11,6 +11,7 @@
#include "common/assert.h" #include "common/assert.h"
#include "common/scope_exit.h" #include "common/scope_exit.h"
#include "common/thread.h"
#include "core/core.h" #include "core/core.h"
#include "core/hle/kernel/kernel.h" #include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/server_session.h"
@ -22,7 +23,7 @@ namespace Kernel {
class ServiceThread::Impl final { class ServiceThread::Impl final {
public: public:
explicit Impl(KernelCore& kernel, std::size_t num_threads); explicit Impl(KernelCore& kernel, std::size_t num_threads, const std::string& name);
~Impl(); ~Impl();
void QueueSyncRequest(ServerSession& session, std::shared_ptr<HLERequestContext>&& context); void QueueSyncRequest(ServerSession& session, std::shared_ptr<HLERequestContext>&& context);
@ -32,12 +33,16 @@ private:
std::queue<std::function<void()>> requests; std::queue<std::function<void()>> requests;
std::mutex queue_mutex; std::mutex queue_mutex;
std::condition_variable condition; std::condition_variable condition;
const std::string service_name;
bool stop{}; bool stop{};
}; };
ServiceThread::Impl::Impl(KernelCore& kernel, std::size_t num_threads) { ServiceThread::Impl::Impl(KernelCore& kernel, std::size_t num_threads, const std::string& name)
: service_name{name} {
for (std::size_t i = 0; i < num_threads; ++i) for (std::size_t i = 0; i < num_threads; ++i)
threads.emplace_back([&] { threads.emplace_back([this, &kernel] {
Common::SetCurrentThreadName(std::string{"Hle_" + service_name}.c_str());
// Wait for first request before trying to acquire a render context // Wait for first request before trying to acquire a render context
{ {
std::unique_lock lock{queue_mutex}; std::unique_lock lock{queue_mutex};
@ -52,7 +57,7 @@ ServiceThread::Impl::Impl(KernelCore& kernel, std::size_t num_threads) {
{ {
std::unique_lock lock{queue_mutex}; std::unique_lock lock{queue_mutex};
condition.wait(lock, [this] { return stop || !requests.empty(); }); condition.wait(lock, [this] { return stop || !requests.empty(); });
if (stop && requests.empty()) { if (stop || requests.empty()) {
return; return;
} }
task = std::move(requests.front()); task = std::move(requests.front());
@ -68,9 +73,14 @@ void ServiceThread::Impl::QueueSyncRequest(ServerSession& session,
std::shared_ptr<HLERequestContext>&& context) { std::shared_ptr<HLERequestContext>&& context) {
{ {
std::unique_lock lock{queue_mutex}; std::unique_lock lock{queue_mutex};
requests.emplace([session{SharedFrom(&session)}, context{std::move(context)}]() {
session->CompleteSyncRequest(*context); // ServerSession owns the service thread, so we cannot caption a strong pointer here in the
return; // event that the ServerSession is terminated.
std::weak_ptr<ServerSession> weak_ptr{SharedFrom(&session)};
requests.emplace([weak_ptr, context{std::move(context)}]() {
if (auto strong_ptr = weak_ptr.lock()) {
strong_ptr->CompleteSyncRequest(*context);
}
}); });
} }
condition.notify_one(); condition.notify_one();
@ -87,8 +97,8 @@ ServiceThread::Impl::~Impl() {
} }
} }
ServiceThread::ServiceThread(KernelCore& kernel, std::size_t num_threads) ServiceThread::ServiceThread(KernelCore& kernel, std::size_t num_threads, const std::string& name)
: impl{std::make_unique<Impl>(kernel, num_threads)} {} : impl{std::make_unique<Impl>(kernel, num_threads, name)} {}
ServiceThread::~ServiceThread() = default; ServiceThread::~ServiceThread() = default;

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <memory> #include <memory>
#include <string>
namespace Kernel { namespace Kernel {
@ -14,7 +15,7 @@ class ServerSession;
class ServiceThread final { class ServiceThread final {
public: public:
explicit ServiceThread(KernelCore& kernel, std::size_t num_threads); explicit ServiceThread(KernelCore& kernel, std::size_t num_threads, const std::string& name);
~ServiceThread(); ~ServiceThread();
void QueueSyncRequest(ServerSession& session, std::shared_ptr<HLERequestContext>&& context); void QueueSyncRequest(ServerSession& session, std::shared_ptr<HLERequestContext>&& context);