2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-12-10 00:27:05 -05:00
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "common/scope_exit.h"
|
2020-12-29 04:06:39 -05:00
|
|
|
#include "common/thread.h"
|
2021-04-13 20:48:37 -04:00
|
|
|
#include "core/hle/kernel/k_session.h"
|
2022-01-17 21:06:33 -05:00
|
|
|
#include "core/hle/kernel/k_thread.h"
|
2020-12-10 00:27:05 -05:00
|
|
|
#include "core/hle/kernel/kernel.h"
|
|
|
|
#include "core/hle/kernel/service_thread.h"
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
class ServiceThread::Impl final {
|
|
|
|
public:
|
2020-12-29 04:06:39 -05:00
|
|
|
explicit Impl(KernelCore& kernel, std::size_t num_threads, const std::string& name);
|
2020-12-10 00:27:05 -05:00
|
|
|
~Impl();
|
|
|
|
|
2021-04-13 20:48:37 -04:00
|
|
|
void QueueSyncRequest(KSession& session, std::shared_ptr<HLERequestContext>&& context);
|
2020-12-10 00:27:05 -05:00
|
|
|
|
|
|
|
private:
|
2021-11-28 16:05:18 -05:00
|
|
|
std::vector<std::jthread> threads;
|
2020-12-10 00:27:05 -05:00
|
|
|
std::queue<std::function<void()>> requests;
|
|
|
|
std::mutex queue_mutex;
|
2021-11-28 16:05:18 -05:00
|
|
|
std::condition_variable_any condition;
|
2020-12-29 04:06:39 -05:00
|
|
|
const std::string service_name;
|
2020-12-10 00:27:05 -05:00
|
|
|
};
|
|
|
|
|
2020-12-29 04:06:39 -05:00
|
|
|
ServiceThread::Impl::Impl(KernelCore& kernel, std::size_t num_threads, const std::string& name)
|
|
|
|
: service_name{name} {
|
2021-11-28 16:05:18 -05:00
|
|
|
for (std::size_t i = 0; i < num_threads; ++i) {
|
|
|
|
threads.emplace_back([this, &kernel](std::stop_token stop_token) {
|
2020-12-29 19:40:47 -05:00
|
|
|
Common::SetCurrentThreadName(std::string{"yuzu:HleService:" + service_name}.c_str());
|
2020-12-29 04:06:39 -05:00
|
|
|
|
2020-12-10 00:27:05 -05:00
|
|
|
// Wait for first request before trying to acquire a render context
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
2021-11-28 16:05:18 -05:00
|
|
|
condition.wait(lock, stop_token, [this] { return !requests.empty(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_token.stop_requested()) {
|
|
|
|
return;
|
2020-12-10 00:27:05 -05:00
|
|
|
}
|
|
|
|
|
2022-03-12 06:06:57 -05:00
|
|
|
// Allocate a dummy guest thread for this host thread.
|
2020-12-10 00:27:05 -05:00
|
|
|
kernel.RegisterHostThread();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
std::function<void()> task;
|
|
|
|
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
2021-11-28 16:05:18 -05:00
|
|
|
condition.wait(lock, stop_token, [this] { return !requests.empty(); });
|
|
|
|
|
|
|
|
if (stop_token.stop_requested()) {
|
2020-12-10 00:27:05 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-11-28 16:05:18 -05:00
|
|
|
|
|
|
|
if (requests.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-10 00:27:05 -05:00
|
|
|
task = std::move(requests.front());
|
|
|
|
requests.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
task();
|
|
|
|
}
|
|
|
|
});
|
2021-11-28 16:05:18 -05:00
|
|
|
}
|
2020-12-10 00:27:05 -05:00
|
|
|
}
|
|
|
|
|
2021-04-13 20:48:37 -04:00
|
|
|
void ServiceThread::Impl::QueueSyncRequest(KSession& session,
|
2020-12-10 00:27:05 -05:00
|
|
|
std::shared_ptr<HLERequestContext>&& context) {
|
|
|
|
{
|
|
|
|
std::unique_lock lock{queue_mutex};
|
2020-12-29 04:06:39 -05:00
|
|
|
|
2021-05-21 01:39:44 -04:00
|
|
|
auto* server_session{&session.GetServerSession()};
|
|
|
|
|
2021-04-13 20:48:37 -04:00
|
|
|
// Open a reference to the session to ensure it is not closes while the service request
|
|
|
|
// completes asynchronously.
|
2021-05-21 01:39:44 -04:00
|
|
|
server_session->Open();
|
2021-04-13 20:48:37 -04:00
|
|
|
|
2021-05-21 01:39:44 -04:00
|
|
|
requests.emplace([server_session, context{std::move(context)}]() {
|
2021-04-13 20:48:37 -04:00
|
|
|
// Close the reference.
|
2021-05-21 01:39:44 -04:00
|
|
|
SCOPE_EXIT({ server_session->Close(); });
|
2021-04-13 20:48:37 -04:00
|
|
|
|
|
|
|
// Complete the service request.
|
|
|
|
server_session->CompleteSyncRequest(*context);
|
2020-12-10 00:27:05 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
condition.notify_one();
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:42:58 -05:00
|
|
|
ServiceThread::Impl::~Impl() {
|
|
|
|
condition.notify_all();
|
|
|
|
for (auto& thread : threads) {
|
|
|
|
thread.request_stop();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 00:27:05 -05:00
|
|
|
|
2020-12-29 04:06:39 -05:00
|
|
|
ServiceThread::ServiceThread(KernelCore& kernel, std::size_t num_threads, const std::string& name)
|
|
|
|
: impl{std::make_unique<Impl>(kernel, num_threads, name)} {}
|
2020-12-10 00:27:05 -05:00
|
|
|
|
|
|
|
ServiceThread::~ServiceThread() = default;
|
|
|
|
|
2021-04-13 20:48:37 -04:00
|
|
|
void ServiceThread::QueueSyncRequest(KSession& session,
|
2020-12-10 00:27:05 -05:00
|
|
|
std::shared_ptr<HLERequestContext>&& context) {
|
|
|
|
impl->QueueSyncRequest(session, std::move(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Kernel
|