Skip to content

Commit

Permalink
make main_task a fork
Browse files Browse the repository at this point in the history
  • Loading branch information
apolyakov committed Feb 18, 2025
1 parent e2c43ea commit f2e414a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
11 changes: 9 additions & 2 deletions runtime-light/state/instance-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <chrono>
#include <cstdint>
#include <functional>
#include <memory>
#include <string_view>
#include <utility>
Expand All @@ -14,11 +15,13 @@
#include "runtime-common/core/utils/kphp-assert-core.h"
#include "runtime-light/core/globals/php-init-scripts.h"
#include "runtime-light/core/globals/php-script-globals.h"
#include "runtime-light/coroutine/shared-task.h"
#include "runtime-light/coroutine/task.h"
#include "runtime-light/k2-platform/k2-api.h"
#include "runtime-light/scheduler/scheduler.h"
#include "runtime-light/state/component-state.h"
#include "runtime-light/state/init-functions.h"
#include "runtime-light/stdlib/fork/fork-state.h"
#include "runtime-light/stdlib/time/time-functions.h"

namespace {
Expand All @@ -42,8 +45,12 @@ int32_t merge_output_buffers() noexcept {

void InstanceState::init_script_execution() noexcept {
runtime_context.init();
init_php_scripts_in_each_worker(php_script_mutable_globals_singleton, main_task_);
scheduler.suspend(std::make_pair(main_task_.get_handle(), WaitEvent::Rechedule{}));
task_t<void> main_task;
init_php_scripts_in_each_worker(php_script_mutable_globals_singleton, main_task);

shared_task_t<void> main_fork{std::invoke([](task_t<void> task) noexcept -> shared_task_t<void> { co_await task; }, std::move(main_task))};
ForkInstanceState::get().push_fork(main_fork);
scheduler.suspend(std::make_pair(main_fork.get_handle(), WaitEvent::Rechedule{}));
}

template<ImageKind kind>
Expand Down
2 changes: 0 additions & 2 deletions runtime-light/state/instance-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ struct InstanceState final : vk::not_copyable {
list<task_t<void>> shutdown_functions;

private:
task_t<void> main_task_;

enum class shutdown_state : uint8_t { not_started, in_progress, finished };
shutdown_state shutdown_state_{shutdown_state::not_started};

Expand Down
3 changes: 2 additions & 1 deletion runtime-light/stdlib/fork/fork-state.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ForkInstanceState final : private vk::not_copyable {
private:
static constexpr int64_t FORK_ID_INIT = 0;

int64_t next_fork_id{FORK_ID_INIT + 1};
int64_t next_fork_id{FORK_ID_INIT};
// type erased tasks that represent forks
kphp::stl::unordered_map<int64_t, fork_info, kphp::memory::script_allocator> forks;

Expand All @@ -44,6 +44,7 @@ struct ForkInstanceState final : private vk::not_copyable {

template<typename T>
friend class start_fork_t;
friend InstanceState;

public:
int64_t current_id{FORK_ID_INIT};
Expand Down

0 comments on commit f2e414a

Please sign in to comment.