k_session.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <string>
  6. #include "core/hle/kernel/k_client_session.h"
  7. #include "core/hle/kernel/k_server_session.h"
  8. #include "core/hle/kernel/slab_helpers.h"
  9. namespace Kernel {
  10. class SessionRequestManager;
  11. class KSession final : public KAutoObjectWithSlabHeapAndContainer<KSession, KAutoObjectWithList> {
  12. KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject);
  13. public:
  14. explicit KSession(KernelCore& kernel_);
  15. ~KSession() override;
  16. void Initialize(KClientPort* port_, const std::string& name_,
  17. std::shared_ptr<SessionRequestManager> manager_ = nullptr);
  18. void Finalize() override;
  19. bool IsInitialized() const override {
  20. return initialized;
  21. }
  22. uintptr_t GetPostDestroyArgument() const override {
  23. return reinterpret_cast<uintptr_t>(process);
  24. }
  25. static void PostDestroy(uintptr_t arg);
  26. void OnServerClosed();
  27. void OnClientClosed();
  28. bool IsServerClosed() const {
  29. return this->GetState() != State::Normal;
  30. }
  31. bool IsClientClosed() const {
  32. return this->GetState() != State::Normal;
  33. }
  34. KClientSession& GetClientSession() {
  35. return client;
  36. }
  37. KServerSession& GetServerSession() {
  38. return server;
  39. }
  40. const KClientSession& GetClientSession() const {
  41. return client;
  42. }
  43. const KServerSession& GetServerSession() const {
  44. return server;
  45. }
  46. const KClientPort* GetParent() const {
  47. return port;
  48. }
  49. KClientPort* GetParent() {
  50. return port;
  51. }
  52. private:
  53. enum class State : u8 {
  54. Invalid = 0,
  55. Normal = 1,
  56. ClientClosed = 2,
  57. ServerClosed = 3,
  58. };
  59. void SetState(State state) {
  60. atomic_state = static_cast<u8>(state);
  61. }
  62. State GetState() const {
  63. return static_cast<State>(atomic_state.load(std::memory_order_relaxed));
  64. }
  65. KServerSession server;
  66. KClientSession client;
  67. std::atomic<std::underlying_type_t<State>> atomic_state{
  68. static_cast<std::underlying_type_t<State>>(State::Invalid)};
  69. KClientPort* port{};
  70. KProcess* process{};
  71. bool initialized{};
  72. };
  73. } // namespace Kernel