Skip to content

Commit b5a84d9

Browse files
authored
[k2] solve few runtime-light issues (#1452)
* delete utils directory * fix using of coroutine with void return type in when_any and io_scheduler::schedule
1 parent d63c8a5 commit b5a84d9

11 files changed

Lines changed: 57 additions & 12 deletions

File tree

runtime-light/coroutine/detail/when-any.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "runtime-light/coroutine/concepts.h"
1616
#include "runtime-light/coroutine/type-traits.h"
1717
#include "runtime-light/coroutine/void-value.h"
18+
#include "runtime-light/metaprogramming/type-functions.h"
1819
#include "runtime-light/stdlib/diagnostics/logs.h"
1920

2021
namespace kphp::coro::detail::when_any {
@@ -120,7 +121,10 @@ class when_any_ready_awaitable<std::tuple<task_types...>> {
120121

121122
const auto task_result_processor{[&result = m_awaitable.m_result](auto&& task) noexcept {
122123
if (auto task_result{std::forward<decltype(task)>(task).result()}; !result.has_value() && task_result.has_value()) {
123-
result = std::move(task_result);
124+
using result_variant_type = std::remove_cvref<decltype(result)>::type::value_type;
125+
using task_result_type = decltype(task_result)::value_type;
126+
result =
127+
result_variant_type{std::in_place_index<kphp::type_functions::variant_index<result_variant_type, task_result_type>()>, *std::move(task_result)};
124128
}
125129
}};
126130
std::apply([&task_result_processor](auto&&... tasks) noexcept { (std::invoke(task_result_processor, std::forward<decltype(tasks)>(tasks)), ...); },
@@ -290,8 +294,11 @@ class when_any_task {
290294

291295
template<kphp::coro::concepts::awaitable awaitable_type>
292296
auto make_when_any_task(awaitable_type awaitable) noexcept -> when_any_task<typename kphp::coro::awaitable_traits<awaitable_type>::awaiter_return_type> {
293-
co_yield co_await std::move(awaitable);
294-
co_return;
297+
if constexpr (std::is_void_v<typename kphp::coro::awaitable_traits<awaitable_type>::awaiter_return_type>) {
298+
co_await std::move(awaitable);
299+
} else {
300+
co_yield co_await std::move(awaitable);
301+
}
295302
}
296303

297304
} // namespace kphp::coro::detail::when_any

runtime-light/coroutine/io-scheduler.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <memory>
1717
#include <optional>
1818
#include <ranges>
19+
#include <type_traits>
1920
#include <utility>
2021
#include <variant>
2122

@@ -463,13 +464,24 @@ auto io_scheduler::schedule(coroutine_type coroutine, std::chrono::duration<rep_
463464
using expected_return_type = typename kphp::coro::coroutine_traits<coroutine_type>::return_type;
464465

465466
if (timeout <= std::chrono::duration<rep_type, period_type>{0}) [[unlikely]] {
466-
co_return std::expected<expected_return_type, timeout_status>{co_await schedule(std::move(coroutine))};
467+
if constexpr (std::is_void_v<expected_return_type>) {
468+
co_await schedule(std::move(coroutine));
469+
co_return std::expected<expected_return_type, timeout_status>{};
470+
} else {
471+
co_return std::expected<expected_return_type, timeout_status>{co_await schedule(std::move(coroutine))};
472+
}
467473
}
474+
468475
auto result{co_await kphp::coro::when_any(std::move(coroutine), make_timeout_task(timeout))};
469476
if (std::holds_alternative<timeout_status>(result)) [[unlikely]] {
470477
co_return std::unexpected{std::move(std::get<1>(result))};
471478
}
472-
co_return std::expected<expected_return_type, timeout_status>{std::move(std::get<0>(result))};
479+
480+
if constexpr (std::is_void_v<expected_return_type>) {
481+
co_return std::expected<expected_return_type, timeout_status>{};
482+
} else {
483+
co_return std::expected<expected_return_type, timeout_status>{std::move(std::get<0>(result))};
484+
}
473485
}
474486

475487
template<typename rep_type, typename period_type>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <cstddef>
99
#include <functional>
1010

11+
namespace kphp::concepts {
12+
1113
template<typename T>
1214
concept standard_layout = std::is_standard_layout_v<T>;
1315

@@ -18,3 +20,5 @@ concept hashable = requires(T t) {
1820

1921
template<typename type, typename... types>
2022
concept in_types = (std::same_as<type, types> || ...);
23+
24+
} // namespace kphp::concepts
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Compiler for PHP (aka KPHP)
2+
// Copyright (c) 2025 LLC «V Kontakte»
3+
// Distributed under the GPL v3 License, see LICENSE.notice.txt
4+
5+
#pragma once
6+
7+
#include <cstddef>
8+
#include <type_traits>
9+
#include <variant>
10+
11+
namespace kphp::type_functions {
12+
13+
template<typename variant_type, typename T, size_t index = 0>
14+
requires(index < std::variant_size_v<variant_type>)
15+
consteval size_t variant_index() noexcept {
16+
if constexpr (std::is_same_v<std::variant_alternative_t<index, variant_type>, T>) {
17+
return index;
18+
} else {
19+
return variant_index<variant_type, T, index + 1>();
20+
}
21+
}
22+
23+
} // namespace kphp::type_functions

runtime-light/runtime-light.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ include(${RUNTIME_LIGHT_DIR}/coroutine/coroutine.cmake)
2222
include(${RUNTIME_LIGHT_DIR}/server/server.cmake)
2323
include(${RUNTIME_LIGHT_DIR}/stdlib/stdlib.cmake)
2424
include(${RUNTIME_LIGHT_DIR}/tl/tl.cmake)
25-
include(${RUNTIME_LIGHT_DIR}/utils/utils.cmake)
2625
include(${RUNTIME_LIGHT_DIR}/state/state.cmake)
2726
include(${RUNTIME_LIGHT_DIR}/memory-resource-impl/memory-resource-impl.cmake)
2827

File renamed without changes.
File renamed without changes.

runtime-light/stdlib/stdlib.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ prepend(
99
diagnostics/backtrace.cpp
1010
diagnostics/contextual-logger.cpp
1111
diagnostics/error-handling-state.cpp
12+
diagnostics/php-assert.cpp
1213
file/resource.cpp
1314
fork/fork-state.cpp
1415
fork/wait-queue-state.cpp

runtime-light/stdlib/string/regex-state.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "common/mixin/not_copyable.h"
1111
#include "runtime-common/core/allocator/script-allocator.h"
1212
#include "runtime-common/core/std/containers.h"
13+
#include "runtime-light/metaprogramming/concepts.h"
1314
#include "runtime-light/stdlib/string/regex-include.h"
14-
#include "runtime-light/utils/concepts.h"
1515

1616
struct RegexInstanceState final : private vk::not_copyable {
17-
template<hashable Key, typename Value>
17+
template<kphp::concepts::hashable Key, typename Value>
1818
using unordered_map = kphp::stl::unordered_map<Key, Value, kphp::memory::script_allocator>;
1919

2020
static constexpr size_t MAX_SUBPATTERNS_COUNT = 512;

runtime-light/tl/tl-core.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "common/algorithms/find.h"
1616
#include "runtime-common/core/allocator/script-allocator.h"
1717
#include "runtime-common/core/std/containers.h"
18+
#include "runtime-light/metaprogramming/concepts.h"
1819
#include "runtime-light/stdlib/diagnostics/logs.h"
19-
#include "runtime-light/utils/concepts.h"
2020

2121
namespace tl {
2222

@@ -70,7 +70,7 @@ class storer {
7070
m_buffer.append_range(bytes);
7171
}
7272

73-
template<standard_layout T, standard_layout U>
73+
template<kphp::concepts::standard_layout T, kphp::concepts::standard_layout U>
7474
requires std::convertible_to<U, T>
7575
void store_trivial(const U& t) noexcept {
7676
store_bytes({reinterpret_cast<const std::byte*>(std::addressof(t)), sizeof(T)});
@@ -140,7 +140,7 @@ class fetcher {
140140
return bytes;
141141
}
142142

143-
template<standard_layout T>
143+
template<kphp::concepts::standard_layout T>
144144
std::optional<T> fetch_trivial() noexcept {
145145
if (m_remaining < sizeof(T)) {
146146
return std::nullopt;

0 commit comments

Comments
 (0)