-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement wait queue #1061
Draft
astrophysik
wants to merge
10
commits into
master
Choose a base branch
from
vsadokhov/k2-wait-queue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
implement wait queue #1061
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
345fffa
add wait queue
astrophysik 9eebcb3
version 2
astrophysik 945e92d
add include
astrophysik 806e581
small fix
astrophysik 080eac3
add const ref
astrophysik 4232a27
add timeout in tests
astrophysik 784a97e
delete move constructor for wait_queue
astrophysik b8db5f6
add #include
astrophysik bd4a3ad
add #include
astrophysik 7419da9
small improvements
astrophysik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#include "runtime-light/stdlib/fork/wait-queue-context.h" | ||
|
||
#include "runtime-light/component/component.h" | ||
#include "runtime-light/coroutine/awaitable.h" | ||
#include "runtime-light/stdlib/fork/fork-context.h" | ||
|
||
WaitQueueContext &WaitQueueContext::get() noexcept { | ||
return ForkComponentContext::get().wait_queue_context; | ||
} | ||
|
||
int64_t WaitQueueContext::create_queue(const array<Optional<int64_t>> &fork_ids) noexcept { | ||
auto &memory_resource{get_component_context()->runtime_allocator.memory_resource}; | ||
unordered_set<int64_t> forks_ids(unordered_set<int64_t>::allocator_type{memory_resource}); | ||
std::for_each(fork_ids.begin(), fork_ids.end(), [&forks_ids](const auto &it) { | ||
Optional<int64_t> fork_id = it.get_value(); | ||
if (fork_id.has_value()) { | ||
forks_ids.insert(fork_id.val()); | ||
} | ||
}); | ||
int64_t queue_id{++next_wait_queue_id}; | ||
wait_queues.emplace(std::piecewise_construct, std::forward_as_tuple(queue_id), std::forward_as_tuple(memory_resource, std::move(forks_ids))); | ||
php_debug("WaitQueueContext: create queue %ld with %ld forks", queue_id, fork_ids.size().size); | ||
return queue_id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "runtime-core/core-types/decl/optional.h" | ||
#include "runtime-core/memory-resource/resource_allocator.h" | ||
#include "runtime-core/memory-resource/unsynchronized_pool_resource.h" | ||
#include "runtime-light/utils/concepts.h" | ||
#include "runtime-light/stdlib/fork/wait-queue.h" | ||
|
||
class WaitQueueContext { | ||
template<hashable Key, typename Value> | ||
using unordered_map = memory_resource::stl::unordered_map<Key, Value, memory_resource::unsynchronized_pool_resource>; | ||
|
||
template<hashable T> | ||
using unordered_set = memory_resource::stl::unordered_set<T, memory_resource::unsynchronized_pool_resource>; | ||
|
||
unordered_map<int64_t, wait_queue_t> wait_queues; | ||
static constexpr auto WAIT_QUEUE_INIT_ID = 0; | ||
int64_t next_wait_queue_id{WAIT_QUEUE_INIT_ID}; | ||
|
||
public: | ||
explicit WaitQueueContext(memory_resource::unsynchronized_pool_resource &memory_resource) noexcept | ||
: wait_queues(unordered_map<int64_t, wait_queue_t>::allocator_type{memory_resource}) {} | ||
|
||
static WaitQueueContext &get() noexcept; | ||
|
||
int64_t create_queue(const array<Optional<int64_t>> &fork_ids) noexcept; | ||
|
||
Optional<wait_queue_t *> get_queue(int64_t queue_id) noexcept { | ||
if (auto it = wait_queues.find(queue_id); it != wait_queues.end()) { | ||
return std::addressof(it->second); | ||
} | ||
return {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#include <algorithm> | ||
#include <coroutine> | ||
|
||
#include "runtime-light/stdlib/fork/wait-queue.h" | ||
|
||
#include "runtime-core/utils/kphp-assert-core.h" | ||
#include "runtime-light/stdlib/fork/fork-context.h" | ||
|
||
wait_queue_t::wait_queue_t(memory_resource::unsynchronized_pool_resource &memory_resource, unordered_set<int64_t> &&forks_ids_) noexcept | ||
: forks_ids(std::move(forks_ids_)) | ||
, awaiters(deque<awaiter_t>::allocator_type{memory_resource}) { | ||
std::for_each(forks_ids.begin(), forks_ids.end(), [](int64_t fork_id) { ForkComponentContext::get().reserve_fork(fork_id); }); | ||
} | ||
|
||
void wait_queue_t::push(int64_t fork_id) noexcept { | ||
forks_ids.insert(fork_id); | ||
ForkComponentContext::get().reserve_fork(fork_id); | ||
if (!awaiters.empty()) { | ||
auto &task = ForkComponentContext::get().forks[fork_id].first; | ||
task_t<fork_result>::awaiter_t awaiter{std::addressof(task)}; | ||
awaiter.await_suspend(awaiters.front().awaited_handle); | ||
} | ||
} | ||
|
||
void wait_queue_t::insert_awaiter(const awaiter_t &awaiter) noexcept { | ||
if (awaiters.empty()) { | ||
std::for_each(forks_ids.begin(), forks_ids.end(), [&](int64_t fork_id) { | ||
task_t<fork_result>::awaiter_t task_awaiter{std::addressof(ForkComponentContext::get().forks[fork_id].first)}; | ||
task_awaiter.await_suspend(awaiter.awaited_handle); | ||
}); | ||
} | ||
awaiters.push_back(awaiter); | ||
} | ||
|
||
void wait_queue_t::erase_awaiter(const awaiter_t &awaiter) noexcept { | ||
auto it = std::find(awaiters.begin(), awaiters.end(), awaiter); | ||
if (it != awaiters.end()) { | ||
awaiters.erase(it); | ||
} | ||
std::coroutine_handle<> next_awaiter = awaiters.empty() ? std::noop_coroutine() : awaiters.front().awaited_handle; | ||
std::for_each(forks_ids.begin(), forks_ids.end(), [&](int64_t fork_id) { | ||
task_t<fork_result>::awaiter_t task_awaiter{std::addressof(ForkComponentContext::get().forks[fork_id].first)}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this part. I think should be method in |
||
task_awaiter.await_suspend(next_awaiter); | ||
}); | ||
} | ||
|
||
int64_t wait_queue_t::pop_ready_fork() noexcept { | ||
auto it = std::find_if(forks_ids.begin(), forks_ids.end(), [](int64_t fork_id) { return ForkComponentContext::get().is_ready(fork_id); }); | ||
if (it == forks_ids.end()) { | ||
php_critical_error("there is no fork to pop from queue"); | ||
} | ||
int64_t ready_fork = *it; | ||
forks_ids.erase(it); | ||
ForkComponentContext::get().unreserve_fork(ready_fork); | ||
return ready_fork; | ||
} | ||
|
||
bool wait_queue_t::has_ready_fork() const noexcept { | ||
return std::any_of(forks_ids.begin(), forks_ids.end(), [](int64_t fork_id) { return ForkComponentContext::get().is_ready(fork_id); }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Compiler for PHP (aka KPHP) | ||
// Copyright (c) 2024 LLC «V Kontakte» | ||
// Distributed under the GPL v3 License, see LICENSE.notice.txt | ||
|
||
#pragma once | ||
|
||
#include <coroutine> | ||
|
||
#include "runtime-light/allocator/allocator.h" | ||
#include "runtime-light/utils/concepts.h" | ||
|
||
class wait_queue_t { | ||
template<hashable T> | ||
using unordered_set = memory_resource::stl::unordered_set<T, memory_resource::unsynchronized_pool_resource>; | ||
|
||
template<typename T> | ||
using deque = memory_resource::stl::deque<T, memory_resource::unsynchronized_pool_resource>; | ||
|
||
public: | ||
wait_queue_t(const wait_queue_t &) = delete; | ||
wait_queue_t(wait_queue_t &&other) = delete; | ||
wait_queue_t &operator=(const wait_queue_t &) = delete; | ||
wait_queue_t &operator=(wait_queue_t &&) = delete; | ||
|
||
explicit wait_queue_t(memory_resource::unsynchronized_pool_resource &memory_resource, unordered_set<int64_t> &&forks_ids_) noexcept; | ||
|
||
struct awaiter_t { | ||
wait_queue_t *wait_queue; | ||
std::coroutine_handle<> awaited_handle; | ||
explicit awaiter_t(wait_queue_t *wait_queue_) noexcept | ||
: wait_queue(wait_queue_) | ||
, awaited_handle(std::noop_coroutine()) {} | ||
|
||
bool await_ready() const noexcept { | ||
return wait_queue->has_ready_fork(); | ||
} | ||
|
||
void await_suspend(std::coroutine_handle<> coro) noexcept { | ||
awaited_handle = coro; | ||
wait_queue->insert_awaiter(*this); | ||
} | ||
|
||
int64_t await_resume() noexcept { | ||
wait_queue->erase_awaiter(*this); | ||
return wait_queue->pop_ready_fork(); | ||
} | ||
|
||
bool resumable() noexcept { | ||
return wait_queue->has_ready_fork(); | ||
} | ||
|
||
void cancel() noexcept { | ||
wait_queue->erase_awaiter(*this); | ||
} | ||
|
||
bool operator==(const awaiter_t &other) const { | ||
return awaited_handle == other.awaited_handle; | ||
} | ||
}; | ||
|
||
void push(int64_t fork_id) noexcept; | ||
|
||
awaiter_t pop() noexcept { | ||
return awaiter_t{this}; | ||
} | ||
|
||
bool empty() const noexcept { | ||
return forks_ids.empty(); | ||
} | ||
|
||
private: | ||
unordered_set<int64_t> forks_ids; | ||
deque<awaiter_t> awaiters; | ||
|
||
void insert_awaiter(const awaiter_t &awaiter) noexcept; | ||
void erase_awaiter(const awaiter_t &awaiter) noexcept; | ||
int64_t pop_ready_fork() noexcept; | ||
bool has_ready_fork() const noexcept; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
int64_t f$rand() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it defined in that file? |
||
std::random_device rd; | ||
int64_t dice_roll = rd(); | ||
std::uniform_int_distribution<> dis(0, rd.max()); | ||
int64_t dice_roll = dis(rd); | ||
return dice_roll; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it changed?