k_session.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "core/hle/kernel/k_client_port.h"
  4. #include "core/hle/kernel/k_client_session.h"
  5. #include "core/hle/kernel/k_scoped_resource_reservation.h"
  6. #include "core/hle/kernel/k_server_session.h"
  7. #include "core/hle/kernel/k_session.h"
  8. namespace Kernel {
  9. KSession::KSession(KernelCore& kernel)
  10. : KAutoObjectWithSlabHeapAndContainer{kernel}, m_server{kernel}, m_client{kernel} {}
  11. KSession::~KSession() = default;
  12. void KSession::Initialize(KClientPort* client_port, uintptr_t name) {
  13. // Increment reference count.
  14. // Because reference count is one on creation, this will result
  15. // in a reference count of two. Thus, when both server and client are closed
  16. // this object will be destroyed.
  17. this->Open();
  18. // Create our sub sessions.
  19. KAutoObject::Create(std::addressof(m_server));
  20. KAutoObject::Create(std::addressof(m_client));
  21. // Initialize our sub sessions.
  22. m_server.Initialize(this);
  23. m_client.Initialize(this);
  24. // Set state and name.
  25. this->SetState(State::Normal);
  26. m_name = name;
  27. // Set our owner process.
  28. //! FIXME: this is the wrong process!
  29. m_process = m_kernel.ApplicationProcess();
  30. m_process->Open();
  31. // Set our port.
  32. m_port = client_port;
  33. if (m_port != nullptr) {
  34. m_port->Open();
  35. }
  36. // Mark initialized.
  37. m_initialized = true;
  38. }
  39. void KSession::Finalize() {
  40. if (m_port != nullptr) {
  41. m_port->OnSessionFinalized();
  42. m_port->Close();
  43. }
  44. }
  45. void KSession::OnServerClosed() {
  46. if (this->GetState() == State::Normal) {
  47. this->SetState(State::ServerClosed);
  48. m_client.OnServerClosed();
  49. }
  50. }
  51. void KSession::OnClientClosed() {
  52. if (this->GetState() == State::Normal) {
  53. SetState(State::ClientClosed);
  54. m_server.OnClientClosed();
  55. }
  56. }
  57. void KSession::PostDestroy(uintptr_t arg) {
  58. // Release the session count resource the owner process holds.
  59. KProcess* owner = reinterpret_cast<KProcess*>(arg);
  60. owner->GetResourceLimit()->Release(LimitableResource::SessionCountMax, 1);
  61. owner->Close();
  62. }
  63. } // namespace Kernel