Repository navigation
Expand file tree
/
Copy pathtask.cpp
More file actions
136 lines (116 loc) · 4.61 KB
/
Copy pathtask.cpp
File metadata and controls
136 lines (116 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Copyright (C) 2026 Sebastiano Merlino; SPDX-License-Identifier: LGPL-2.1-or-later */
#include <httpserver/concurrency/task.hpp>
#include <memory>
#include <utility>
namespace httpserver::detail {
std::shared_ptr<void>& current_task_executor_lifetime() noexcept {
thread_local std::shared_ptr<void> lifetime;
return lifetime;
}
void guarded_resume(const std::shared_ptr<frame_witness>& witness, std::coroutine_handle<> frame) {
std::lock_guard<std::recursive_mutex> lock(witness->mu);
if (!witness->valid) return;
frame.resume();
}
task_frame_base::task_frame_base() : witness_(std::make_shared<frame_witness>()) {}
auto task_frame_base::try_consume() noexcept -> bool {
task_state expected = task_state::idle;
return state_.compare_exchange_strong(expected, task_state::running, std::memory_order_acq_rel, std::memory_order_acquire);
}
auto task_frame_base::set_affinity(executor* ex, std::shared_ptr<void> lifetime) noexcept -> void {
affinity_ = ex;
witness_->executor_lifetime = std::move(lifetime);
}
auto task_frame_base::register_consumer(std::coroutine_handle<> awaiting, executor* consumer_ex, std::shared_ptr<frame_witness> consumer_witness) noexcept -> void {
consumer_ = awaiting;
consumer_executor_ = consumer_ex;
consumer_witness_ = std::move(consumer_witness);
}
auto task_frame_base::set_exception(std::exception_ptr e) noexcept -> void {
kind_ = task_kind::exception;
exception_ = std::move(e);
}
auto task_frame_base::capture_exception() noexcept -> void {
try {
throw;
} catch (const cancelled_exception&) {
kind_ = task_kind::terminal;
terminal_ = http::outcome_code::cancelled;
} catch (...) {
kind_ = task_kind::exception;
exception_ = std::current_exception();
}
}
auto task_frame_base::register_spawn_block(std::shared_ptr<spawn_block_base> block, void* promise_erased, void (*pack)(void*, void*), concurrency::unique_function<void()> deliver) noexcept -> void {
spawn_block_ = std::move(block);
promise_erased_ = promise_erased;
pack_result_ = pack;
deliver_ = std::move(deliver);
}
auto task_frame_base::finish() noexcept -> std::coroutine_handle<> {
state_.store(task_state::done, std::memory_order_release);
if (spawn_block_) {
pack_result_(promise_erased_, spawn_block_.get());
auto deliver = std::move(deliver_);
deliver_ = concurrency::unique_function<void()>();
executor* const ex = spawn_block_->spawn_ex;
// post() is documented not to throw; a throw here would lose
// the delivery, so it is contained.
try {
if (ex != nullptr && !ex->is_current()) {
ex->post(std::move(deliver));
} else {
deliver();
}
} catch (...) {
}
}
if (consumer_) {
executor* const ex = consumer_executor_;
if (ex == nullptr || ex->is_current()) {
return consumer_;
}
const auto witness = consumer_witness_ ? consumer_witness_ : witness_;
std::coroutine_handle<> frame = consumer_;
try {
ex->post([witness, frame] { guarded_resume(witness, frame); });
} catch (...) {
}
}
return std::noop_coroutine();
}
auto task_frame_base::invalidate() noexcept -> void {
std::lock_guard<std::recursive_mutex> lock(witness_->mu);
witness_->valid = false;
}
} // namespace httpserver::detail
namespace httpserver {
task_result<void>::task_result(detail::task_kind kind, std::exception_ptr exception, http::outcome_code terminal) noexcept : kind_(kind), exception_(std::move(exception)), terminal_(terminal) {}
task<void>::promise_type::promise_type() {
self_ = std::coroutine_handle<promise_type>::from_promise(*this);
promise_erased_ = this;
}
auto task<void>::promise_type::pack_result(void* erased_promise, void* erased_block) -> void {
auto& promise = *static_cast<promise_type*>(erased_promise);
auto& block = *static_cast<detail::spawn_block<void>*>(erased_block);
block.kind = promise.kind_;
if (promise.kind_ == detail::task_kind::exception) {
block.exception = promise.exception_;
} else if (promise.kind_ == detail::task_kind::terminal) {
block.terminal = promise.terminal_;
}
}
auto task<void>::operator=(task&& other) noexcept -> task& {
if (this != &other) {
destroy_frame();
handle_ = std::exchange(other.handle_, {});
}
return *this;
}
auto task<void>::destroy_frame() noexcept -> void {
if (!handle_) return;
handle_.promise().invalidate();
handle_.destroy();
handle_ = {};
}
} // namespace httpserver