service_thread: convert to map for session management
This commit is contained in:
parent
983f2b7074
commit
7837185f0a
|
@ -2,6 +2,7 @@
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -37,8 +38,7 @@ private:
|
||||||
|
|
||||||
std::jthread m_thread;
|
std::jthread m_thread;
|
||||||
std::mutex m_session_mutex;
|
std::mutex m_session_mutex;
|
||||||
std::vector<KServerSession*> m_sessions;
|
std::map<KServerSession*, std::shared_ptr<SessionRequestManager>> m_sessions;
|
||||||
std::vector<std::shared_ptr<SessionRequestManager>> m_managers;
|
|
||||||
KEvent* m_wakeup_event;
|
KEvent* m_wakeup_event;
|
||||||
KProcess* m_process;
|
KProcess* m_process;
|
||||||
std::atomic<bool> m_shutdown_requested;
|
std::atomic<bool> m_shutdown_requested;
|
||||||
|
@ -51,19 +51,21 @@ void ServiceThread::Impl::WaitAndProcessImpl() {
|
||||||
std::vector<std::shared_ptr<SessionRequestManager>> managers;
|
std::vector<std::shared_ptr<SessionRequestManager>> managers;
|
||||||
|
|
||||||
{
|
{
|
||||||
// Lock to get the list.
|
// Lock to get the set.
|
||||||
std::scoped_lock lk{m_session_mutex};
|
std::scoped_lock lk{m_session_mutex};
|
||||||
|
|
||||||
// Resize to the needed quantity.
|
// Reserve the needed quantity.
|
||||||
objs.resize(m_sessions.size() + 1);
|
objs.reserve(m_sessions.size() + 1);
|
||||||
managers.resize(m_managers.size());
|
managers.reserve(m_sessions.size());
|
||||||
|
|
||||||
// Copy to our local list.
|
// Copy to our local list.
|
||||||
std::copy(m_sessions.begin(), m_sessions.end(), objs.begin());
|
for (const auto& [session, manager] : m_sessions) {
|
||||||
std::copy(m_managers.begin(), m_managers.end(), managers.begin());
|
objs.push_back(session);
|
||||||
|
managers.push_back(manager);
|
||||||
|
}
|
||||||
|
|
||||||
// Insert the wakeup event at the end.
|
// Insert the wakeup event at the end.
|
||||||
objs.back() = &m_wakeup_event->GetReadableEvent();
|
objs.push_back(&m_wakeup_event->GetReadableEvent());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait on the list of sessions.
|
// Wait on the list of sessions.
|
||||||
|
@ -116,17 +118,11 @@ void ServiceThread::Impl::WaitAndProcessImpl() {
|
||||||
void ServiceThread::Impl::SessionClosed(KServerSession* server_session,
|
void ServiceThread::Impl::SessionClosed(KServerSession* server_session,
|
||||||
std::shared_ptr<SessionRequestManager> manager) {
|
std::shared_ptr<SessionRequestManager> manager) {
|
||||||
{
|
{
|
||||||
// Lock to get the list.
|
// Lock to get the set.
|
||||||
std::scoped_lock lk{m_session_mutex};
|
std::scoped_lock lk{m_session_mutex};
|
||||||
|
|
||||||
// Get the index of the session.
|
// Erase the session.
|
||||||
const auto index =
|
ASSERT(m_sessions.erase(server_session) == 1);
|
||||||
std::find(m_sessions.begin(), m_sessions.end(), server_session) - m_sessions.begin();
|
|
||||||
ASSERT(index < static_cast<s64>(m_sessions.size()));
|
|
||||||
|
|
||||||
// Remove the session and its manager.
|
|
||||||
m_sessions.erase(m_sessions.begin() + index);
|
|
||||||
m_managers.erase(m_managers.begin() + index);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close our reference to the server session.
|
// Close our reference to the server session.
|
||||||
|
@ -149,12 +145,11 @@ void ServiceThread::Impl::RegisterServerSession(KServerSession* server_session,
|
||||||
server_session->Open();
|
server_session->Open();
|
||||||
|
|
||||||
{
|
{
|
||||||
// Lock to get the list.
|
// Lock to get the set.
|
||||||
std::scoped_lock lk{m_session_mutex};
|
std::scoped_lock lk{m_session_mutex};
|
||||||
|
|
||||||
// Insert the session and manager.
|
// Insert the session and manager.
|
||||||
m_sessions.push_back(server_session);
|
m_sessions[server_session] = manager;
|
||||||
m_managers.push_back(manager);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signal the wakeup event.
|
// Signal the wakeup event.
|
||||||
|
@ -171,10 +166,13 @@ ServiceThread::Impl::~Impl() {
|
||||||
m_session_mutex.lock();
|
m_session_mutex.lock();
|
||||||
|
|
||||||
// Close all remaining sessions.
|
// Close all remaining sessions.
|
||||||
for (size_t i = 0; i < m_sessions.size(); i++) {
|
for (const auto& [server_session, manager] : m_sessions) {
|
||||||
m_sessions[i]->Close();
|
server_session->Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Destroy remaining managers.
|
||||||
|
m_sessions.clear();
|
||||||
|
|
||||||
// Close event.
|
// Close event.
|
||||||
m_wakeup_event->GetReadableEvent().Close();
|
m_wakeup_event->GetReadableEvent().Close();
|
||||||
m_wakeup_event->Close();
|
m_wakeup_event->Close();
|
||||||
|
|
Loading…
Reference in a new issue