k_session.h 2.1 KB

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