Files
ladybird/Libraries/LibWebView/MachPortServer.h
Aliaksandr Kalenik e47f4cf90f Everywhere: Simplify Mach bootstrap transport handshake
Previously, the bootstrap handshake used a two-state machine
(WaitingForPorts / WaitingForReplyPort) to handle a race: the parent
registering transport ports and the child sending a bootstrap request
could arrive in either order, so whichever came first stored its half
and the second completed the handshake.

Eliminate the race by holding a mutex across spawn() and
register_child_transport(). Since the child cannot send a bootstrap
request before it exists, and the lock isn't released until its
transport is registered, handle_bootstrap_request() is guaranteed to
find the entry. This reduces the pending map to a simple pid-to-ports
lookup and collapses the two-variant state into two straightforward
branches: known child, or on-demand (non-child) caller like WebDriver.
2026-03-24 19:51:52 +01:00

55 lines
1.1 KiB
C++

/*
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Atomic.h>
#include <AK/Platform.h>
#include <AK/String.h>
#include <LibCore/MachPort.h>
#include <LibThreading/Forward.h>
#include <LibWebView/Forward.h>
#if !defined(AK_OS_MACH)
# error "This file is only for Mach kernel-based OS's"
#endif
namespace WebView {
class WEBVIEW_API MachPortServer {
public:
explicit MachPortServer(ByteString server_port_name);
~MachPortServer();
void start();
void stop();
bool is_initialized();
struct BootstrapRequest {
pid_t pid { -1 };
Core::MachPort task_port;
Core::MachPort reply_port;
};
Function<void(BootstrapRequest)> on_bootstrap_request;
ByteString const& server_port_name() const { return m_server_port_name; }
private:
void thread_loop();
ErrorOr<void> allocate_server_port();
NonnullRefPtr<Threading::Thread> m_thread;
ByteString const m_server_port_name;
Core::MachPort m_server_port_recv_right;
Core::MachPort m_server_port_send_right;
Atomic<bool> m_should_stop { false };
};
}