k_session.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. process = kernel.CurrentProcess();
  29. process->Open();
  30. // Set our port.
  31. port = port_;
  32. if (port != nullptr) {
  33. port->Open();
  34. }
  35. // Mark initialized.
  36. initialized = true;
  37. }
  38. void KSession::Finalize() {
  39. if (port == nullptr) {
  40. return;
  41. }
  42. port->OnSessionFinalized();
  43. port->Close();
  44. }
  45. void KSession::OnServerClosed() {
  46. if (GetState() != State::Normal) {
  47. return;
  48. }
  49. SetState(State::ServerClosed);
  50. client.OnServerClosed();
  51. }
  52. void KSession::OnClientClosed() {
  53. if (GetState() != State::Normal) {
  54. return;
  55. }
  56. SetState(State::ClientClosed);
  57. server.OnClientClosed();
  58. }
  59. void KSession::PostDestroy(uintptr_t arg) {
  60. // Release the session count resource the owner process holds.
  61. KProcess* owner = reinterpret_cast<KProcess*>(arg);
  62. owner->GetResourceLimit()->Release(LimitableResource::SessionCountMax, 1);
  63. owner->Close();
  64. }
  65. } // namespace Kernel