k_session.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_}, server{kernel_}, client{kernel_} {}
  11. KSession::~KSession() = default;
  12. void KSession::Initialize(KClientPort* port_, const std::string& 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. Open();
  18. // Create our sub sessions.
  19. KAutoObject::Create(std::addressof(server));
  20. KAutoObject::Create(std::addressof(client));
  21. // Initialize our sub sessions.
  22. server.Initialize(this, name_ + ":Server");
  23. client.Initialize(this, name_ + ":Client");
  24. // Set state and name.
  25. SetState(State::Normal);
  26. name = name_;
  27. // Set our owner process.
  28. //! FIXME: this is the wrong process!
  29. process = kernel.ApplicationProcess();
  30. process->Open();
  31. // Set our port.
  32. port = port_;
  33. if (port != nullptr) {
  34. port->Open();
  35. }
  36. // Mark initialized.
  37. initialized = true;
  38. }
  39. void KSession::Finalize() {
  40. if (port == nullptr) {
  41. return;
  42. }
  43. port->OnSessionFinalized();
  44. port->Close();
  45. }
  46. void KSession::OnServerClosed() {
  47. if (GetState() != State::Normal) {
  48. return;
  49. }
  50. SetState(State::ServerClosed);
  51. client.OnServerClosed();
  52. }
  53. void KSession::OnClientClosed() {
  54. if (GetState() != State::Normal) {
  55. return;
  56. }
  57. SetState(State::ClientClosed);
  58. server.OnClientClosed();
  59. }
  60. void KSession::PostDestroy(uintptr_t arg) {
  61. // Release the session count resource the owner process holds.
  62. KProcess* owner = reinterpret_cast<KProcess*>(arg);
  63. owner->GetResourceLimit()->Release(LimitableResource::SessionCountMax, 1);
  64. owner->Close();
  65. }
  66. } // namespace Kernel