k_session.h 2.3 KB

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