MachPortServer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Platform.h>
  8. #if !defined(AK_OS_MACH)
  9. # error "This file is only for Mach kernel-based OS's"
  10. #endif
  11. #include <AK/Atomic.h>
  12. #include <AK/String.h>
  13. #include <LibCore/MachPort.h>
  14. #include <LibThreading/Thread.h>
  15. namespace Ladybird {
  16. class MachPortServer {
  17. public:
  18. MachPortServer();
  19. ~MachPortServer();
  20. void start();
  21. void stop();
  22. bool is_initialized();
  23. Function<void(pid_t, Core::MachPort)> on_receive_child_mach_port;
  24. struct BackingStoresMessage {
  25. pid_t pid { -1 };
  26. u64 page_id { 0 };
  27. i32 front_backing_store_id { 0 };
  28. i32 back_backing_store_id { 0 };
  29. Core::MachPort front_backing_store_port;
  30. Core::MachPort back_backing_store_port;
  31. };
  32. Function<void(BackingStoresMessage)> on_receive_backing_stores;
  33. ByteString const& server_port_name() const { return m_server_port_name; }
  34. private:
  35. void thread_loop();
  36. ErrorOr<void> allocate_server_port();
  37. NonnullRefPtr<Threading::Thread> m_thread;
  38. ByteString const m_server_port_name;
  39. Core::MachPort m_server_port_recv_right;
  40. Core::MachPort m_server_port_send_right;
  41. Atomic<bool> m_should_stop { false };
  42. };
  43. }