k_session.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 KSession final : public KAutoObjectWithSlabHeapAndContainer<KSession, KAutoObjectWithList> {
  12. KERNEL_AUTOOBJECT_TRAITS(KSession, KAutoObject);
  13. private:
  14. enum class State : u8 {
  15. Invalid = 0,
  16. Normal = 1,
  17. ClientClosed = 2,
  18. ServerClosed = 3,
  19. };
  20. public:
  21. explicit KSession(KernelCore& kernel);
  22. virtual ~KSession() override;
  23. void Initialize(KClientPort* port_, std::string&& name_);
  24. virtual void Finalize() override;
  25. virtual bool IsInitialized() const override {
  26. return initialized;
  27. }
  28. virtual uintptr_t GetPostDestroyArgument() const override {
  29. return reinterpret_cast<uintptr_t>(process);
  30. }
  31. static void PostDestroy(uintptr_t arg);
  32. void OnServerClosed();
  33. void OnClientClosed();
  34. bool IsServerClosed() const {
  35. return this->GetState() != State::Normal;
  36. }
  37. bool IsClientClosed() const {
  38. return this->GetState() != State::Normal;
  39. }
  40. KClientSession& GetClientSession() {
  41. return client;
  42. }
  43. KServerSession& GetServerSession() {
  44. return server;
  45. }
  46. const KClientSession& GetClientSession() const {
  47. return client;
  48. }
  49. const KServerSession& GetServerSession() const {
  50. return server;
  51. }
  52. const KClientPort* GetParent() const {
  53. return port;
  54. }
  55. // DEPRECATED
  56. std::string GetName() const override {
  57. return name;
  58. }
  59. static constexpr HandleType HANDLE_TYPE = HandleType::Session;
  60. HandleType GetHandleType() const override {
  61. return HANDLE_TYPE;
  62. }
  63. private:
  64. void SetState(State state) {
  65. atomic_state = static_cast<u8>(state);
  66. }
  67. State GetState() const {
  68. return static_cast<State>(atomic_state.load());
  69. }
  70. private:
  71. KServerSession server;
  72. KClientSession client;
  73. std::atomic<std::underlying_type<State>::type> atomic_state{
  74. static_cast<std::underlying_type<State>::type>(State::Invalid)};
  75. KClientPort* port{};
  76. std::string name;
  77. Process* process{};
  78. bool initialized{};
  79. };
  80. } // namespace Kernel