|
| 1 | +// Compiler for PHP (aka KPHP) |
| 2 | +// Copyright (c) 2026 LLC «V Kontakte» |
| 3 | +// Distributed under the GPL v3 License, see LICENSE.notice.txt |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <concepts> |
| 8 | +#include <cstddef> |
| 9 | +#include <cstdint> |
| 10 | +#include <expected> |
| 11 | +#include <functional> |
| 12 | +#include <memory> |
| 13 | +#include <optional> |
| 14 | +#include <utility> |
| 15 | +#include <variant> |
| 16 | + |
| 17 | +#include "runtime-light/coroutine/event.h" |
| 18 | +#include "runtime-light/coroutine/io-scheduler.h" |
| 19 | +#include "runtime-light/coroutine/task.h" |
| 20 | +#include "runtime-light/coroutine/type-traits.h" |
| 21 | +#include "runtime-light/coroutine/when-any.h" |
| 22 | +#include "runtime-light/k2-platform/k2-api.h" |
| 23 | + |
| 24 | +namespace kphp::component { |
| 25 | + |
| 26 | +class watcher { |
| 27 | + k2::descriptor m_descriptor{k2::INVALID_PLATFORM_DESCRIPTOR}; |
| 28 | + // TODO it should watch for specific poll_op |
| 29 | + std::optional<kphp::coro::event> m_unwatch_event; |
| 30 | + |
| 31 | + explicit watcher(k2::descriptor descriptor) noexcept |
| 32 | + : m_descriptor(descriptor) {} |
| 33 | + |
| 34 | +public: |
| 35 | + watcher(watcher&& other) noexcept |
| 36 | + : m_descriptor(std::exchange(other.m_descriptor, k2::INVALID_PLATFORM_DESCRIPTOR)), |
| 37 | + m_unwatch_event(std::exchange(other.m_unwatch_event, {})) {} |
| 38 | + |
| 39 | + watcher& operator=(watcher&& other) noexcept { |
| 40 | + if (this != std::addressof(other)) { |
| 41 | + m_descriptor = std::exchange(other.m_descriptor, k2::INVALID_PLATFORM_DESCRIPTOR); |
| 42 | + m_unwatch_event = std::exchange(other.m_unwatch_event, {}); |
| 43 | + } |
| 44 | + return *this; |
| 45 | + } |
| 46 | + |
| 47 | + watcher() = delete; |
| 48 | + watcher(const watcher&) = delete; |
| 49 | + watcher& operator=(const watcher&) = delete; |
| 50 | + |
| 51 | + ~watcher() { |
| 52 | + unwatch(); |
| 53 | + } |
| 54 | + |
| 55 | + static auto create(k2::descriptor descriptor) noexcept -> std::expected<watcher, int32_t>; |
| 56 | + |
| 57 | + template<std::invocable on_event_handler_type> |
| 58 | + auto watch(on_event_handler_type&& f) noexcept -> std::expected<void, int32_t>; |
| 59 | + |
| 60 | + auto unwatch() noexcept -> void; |
| 61 | +}; |
| 62 | + |
| 63 | +inline auto watcher::create(k2::descriptor descriptor) noexcept -> std::expected<watcher, int32_t> { |
| 64 | + if (descriptor == k2::INVALID_PLATFORM_DESCRIPTOR) { |
| 65 | + return std::unexpected{k2::errno_einval}; |
| 66 | + } |
| 67 | + return watcher{descriptor}; |
| 68 | +} |
| 69 | + |
| 70 | +template<std::invocable on_event_handler_type> |
| 71 | +auto watcher::watch(on_event_handler_type&& f) noexcept -> std::expected<void, int32_t> { |
| 72 | + if (m_unwatch_event) { // already watching |
| 73 | + return std::unexpected{k2::errno_ealready}; |
| 74 | + } |
| 75 | + |
| 76 | + k2::StreamStatus stream_status{}; |
| 77 | + k2::stream_status(m_descriptor, std::addressof(stream_status)); |
| 78 | + if (stream_status.libc_errno != k2::errno_ok) [[unlikely]] { |
| 79 | + return std::unexpected{stream_status.libc_errno}; |
| 80 | + } |
| 81 | + |
| 82 | + static constexpr auto watcher{[](k2::descriptor descriptor, kphp::coro::event& unwatch_event, on_event_handler_type f) noexcept -> kphp::coro::task<> { |
| 83 | + static constexpr auto unwatch_awaiter{[](kphp::coro::event& unwatch_event) noexcept -> kphp::coro::task<> { co_await unwatch_event; }}; |
| 84 | + static constexpr auto update_awaiter{[](k2::descriptor descriptor) noexcept -> kphp::coro::task<std::monostate> { |
| 85 | + k2::StreamStatus stream_status{}; |
| 86 | + auto& io_scheduler{kphp::coro::io_scheduler::get()}; |
| 87 | + for (;;) { |
| 88 | + k2::stream_status(descriptor, std::addressof(stream_status)); |
| 89 | + if (stream_status.write_status == k2::IOStatus::IOClosed) { |
| 90 | + co_return std::monostate{}; |
| 91 | + } |
| 92 | + |
| 93 | + using namespace std::chrono_literals; |
| 94 | + co_await io_scheduler.schedule(150ms); |
| 95 | + } |
| 96 | + }}; |
| 97 | + |
| 98 | + if (std::holds_alternative<std::monostate>(co_await kphp::coro::when_any(update_awaiter(descriptor), unwatch_awaiter(unwatch_event)))) { |
| 99 | + if constexpr (kphp::coro::is_async_function_v<on_event_handler_type>) { |
| 100 | + co_await std::invoke(std::move(f)); |
| 101 | + } else { |
| 102 | + std::invoke(std::move(f)); |
| 103 | + } |
| 104 | + } |
| 105 | + }}; |
| 106 | + |
| 107 | + if (!kphp::coro::io_scheduler::get().start(watcher(m_descriptor, m_unwatch_event.emplace(), std::forward<on_event_handler_type>(f)))) { |
| 108 | + m_unwatch_event.reset(); |
| 109 | + return std::unexpected{k2::errno_ebusy}; |
| 110 | + } |
| 111 | + return {}; |
| 112 | +} |
| 113 | + |
| 114 | +inline auto watcher::unwatch() noexcept -> void { |
| 115 | + if (!m_unwatch_event) { |
| 116 | + return; |
| 117 | + } |
| 118 | + m_unwatch_event->set(); |
| 119 | + m_unwatch_event.reset(); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace kphp::component |
0 commit comments