k_session.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2021 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "core/hle/kernel/k_client_port.h"
  6. #include "core/hle/kernel/k_client_session.h"
  7. #include "core/hle/kernel/k_scoped_resource_reservation.h"
  8. #include "core/hle/kernel/k_server_session.h"
  9. #include "core/hle/kernel/k_session.h"
  10. namespace Kernel {
  11. KSession::KSession(KernelCore& kernel_)
  12. : KAutoObjectWithSlabHeapAndContainer{kernel_}, server{kernel_}, client{kernel_} {}
  13. KSession::~KSession() = default;
  14. void KSession::Initialize(KClientPort* port_, const std::string& name_) {
  15. // Increment reference count.
  16. // Because reference count is one on creation, this will result
  17. // in a reference count of two. Thus, when both server and client are closed
  18. // this object will be destroyed.
  19. Open();
  20. // Create our sub sessions.
  21. KAutoObject::Create(std::addressof(server));
  22. KAutoObject::Create(std::addressof(client));
  23. // Initialize our sub sessions.
  24. server.Initialize(this, name_ + ":Server");
  25. client.Initialize(this, name_ + ":Client");
  26. // Set state and name.
  27. SetState(State::Normal);
  28. name = name_;
  29. // Set our owner process.
  30. process = kernel.CurrentProcess();
  31. process->Open();
  32. // Set our port.
  33. port = port_;
  34. if (port != nullptr) {
  35. port->Open();
  36. }
  37. // Mark initialized.
  38. initialized = true;
  39. }
  40. void KSession::Finalize() {
  41. if (port == nullptr) {
  42. return;
  43. }
  44. port->OnSessionFinalized();
  45. port->Close();
  46. }
  47. void KSession::OnServerClosed() {
  48. if (GetState() != State::Normal) {
  49. return;
  50. }
  51. SetState(State::ServerClosed);
  52. client.OnServerClosed();
  53. }
  54. void KSession::OnClientClosed() {
  55. if (GetState() != State::Normal) {
  56. return;
  57. }
  58. SetState(State::ClientClosed);
  59. server.OnClientClosed();
  60. }
  61. void KSession::PostDestroy(uintptr_t arg) {
  62. // Release the session count resource the owner process holds.
  63. KProcess* owner = reinterpret_cast<KProcess*>(arg);
  64. // owner->GetResourceLimit()->Release(LimitableResource::Sessions, 1);
  65. owner->Close();
  66. }
  67. } // namespace Kernel