Skip to content

Commit

Permalink
cc core: hide includes in task_base.hpp
Browse files Browse the repository at this point in the history
commit_hash:3e1508645023971f30dba2ee4ed5ef9233f3829d
  • Loading branch information
segoon committed Oct 29, 2024
1 parent 3140e6b commit 5a2e434
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 32 deletions.
1 change: 1 addition & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@
"core/src/engine/task/sleep_state.hpp":"taxi/uservices/userver/core/src/engine/task/sleep_state.hpp",
"core/src/engine/task/task.cpp":"taxi/uservices/userver/core/src/engine/task/task.cpp",
"core/src/engine/task/task_base.cpp":"taxi/uservices/userver/core/src/engine/task/task_base.cpp",
"core/src/engine/task/task_base_impl.hpp":"taxi/uservices/userver/core/src/engine/task/task_base_impl.hpp",
"core/src/engine/task/task_benchmark.cpp":"taxi/uservices/userver/core/src/engine/task/task_benchmark.cpp",
"core/src/engine/task/task_context.cpp":"taxi/uservices/userver/core/src/engine/task/task_context.cpp",
"core/src/engine/task/task_context.hpp":"taxi/uservices/userver/core/src/engine/task/task_context.hpp",
Expand Down
7 changes: 3 additions & 4 deletions core/include/userver/engine/task/task_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
#include <memory>
#include <string>

#include <boost/smart_ptr/intrusive_ptr.hpp>

#include <userver/engine/deadline.hpp>
#include <userver/engine/exception.hpp>
#include <userver/engine/task/cancel.hpp>
#include <userver/engine/task/task_processor_fwd.hpp>
#include <userver/utils/fast_pimpl.hpp>

USERVER_NAMESPACE_BEGIN

Expand Down Expand Up @@ -163,7 +161,8 @@ class [[nodiscard]] TaskBase {
friend class impl::DetachedTasksSyncBlock;
friend class TaskCancellationToken;

boost::intrusive_ptr<impl::TaskContext> context_;
struct Impl;
utils::FastPimpl<Impl, 8, 8> pimpl_;
};

/// @brief Namespace with functions to work with current task from within it
Expand Down
3 changes: 2 additions & 1 deletion core/src/engine/impl/detached_tasks_sync_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <atomic>
#include <optional>

#include <engine/task/task_base_impl.hpp>
#include <userver/utils/assert.hpp>
#include <userver/utils/impl/wait_token_storage.hpp>

Expand Down Expand Up @@ -61,7 +62,7 @@ void DetachedTasksSyncBlock::Add(TaskContext& context) {
}

void DetachedTasksSyncBlock::Add(Task&& task) {
const auto context = std::move(task.context_);
const auto context = std::move(task.pimpl_->context);
Add(*context);
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/engine/task/cancel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <userver/utils/assert.hpp>

#include <engine/task/coro_unwinder.hpp>
#include <engine/task/task_base_impl.hpp>
#include <engine/task/task_context.hpp>

USERVER_NAMESPACE_BEGIN
Expand Down Expand Up @@ -90,7 +91,7 @@ TaskCancellationToken::TaskCancellationToken() noexcept = default;

TaskCancellationToken::TaskCancellationToken(impl::TaskContext& context) noexcept : context_(&context) {}

TaskCancellationToken::TaskCancellationToken(Task& task) : context_(task.context_) { UASSERT(context_); }
TaskCancellationToken::TaskCancellationToken(Task& task) : context_(task.pimpl_->context) { UASSERT(context_); }

// clang-tidy insists on defaulting this,
// gcc complains about exception-specification mismatch with '= default'
Expand Down
53 changes: 27 additions & 26 deletions core/src/engine/task/task_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <future>

#include <engine/impl/generic_wait_list.hpp>
#include <engine/task/task_base_impl.hpp>
#include <engine/task/task_context.hpp>
#include <engine/task/task_processor.hpp>
#include <engine/task/task_processor_pools.hpp>
Expand All @@ -23,13 +24,13 @@ static_assert(

static_assert(!std::is_polymorphic_v<TaskBase>, "Slicing is used by derived types, virtual functions would not work.");

TaskBase::TaskBase(impl::TaskContextHolder&& context) : context_(std::move(context).Extract()) {
context_->Wakeup(impl::TaskContext::WakeupSource::kBootstrap, impl::SleepState::Epoch{0});
TaskBase::TaskBase(impl::TaskContextHolder&& context) : pimpl_(Impl{std::move(context).Extract()}) {
pimpl_->context->Wakeup(impl::TaskContext::WakeupSource::kBootstrap, impl::SleepState::Epoch{0});
}

bool TaskBase::IsValid() const { return !!context_; }
bool TaskBase::IsValid() const { return !!pimpl_->context; }

Task::State TaskBase::GetState() const { return context_ ? context_->GetState() : State::kInvalid; }
Task::State TaskBase::GetState() const { return pimpl_->context ? pimpl_->context->GetState() : State::kInvalid; }

const std::string& TaskBase::GetStateName(State state) {
static const std::string kInvalid = "kInvalid";
Expand Down Expand Up @@ -60,35 +61,35 @@ const std::string& TaskBase::GetStateName(State state) {
UINVARIANT(false, "Unexpected Task state");
}

bool TaskBase::IsFinished() const { return context_ && context_->IsFinished(); }
bool TaskBase::IsFinished() const { return pimpl_->context && pimpl_->context->IsFinished(); }

void TaskBase::Wait() const noexcept(false) {
UASSERT(context_);
context_->Wait();
UASSERT(pimpl_->context);
pimpl_->context->Wait();
}

void TaskBase::WaitUntil(Deadline deadline) const {
UASSERT(context_);
context_->WaitUntil(deadline);
UASSERT(pimpl_->context);
pimpl_->context->WaitUntil(deadline);
}

void TaskBase::RequestCancel() {
UASSERT(context_);
context_->RequestCancel(TaskCancellationReason::kUserRequest);
UASSERT(pimpl_->context);
pimpl_->context->RequestCancel(TaskCancellationReason::kUserRequest);
}

void TaskBase::SyncCancel() noexcept { Terminate(TaskCancellationReason::kUserRequest); }

TaskCancellationReason TaskBase::CancellationReason() const {
UASSERT(context_);
return context_->CancellationReason();
UASSERT(pimpl_->context);
return pimpl_->context->CancellationReason();
}

void TaskBase::BlockingWait() const {
UASSERT(context_);
UASSERT(pimpl_->context);
UASSERT(!current_task::IsTaskProcessorThread());

auto& context = *context_;
auto& context = *pimpl_->context;
if (context.IsFinished()) return;

std::packaged_task<void()> task([&context] {
Expand All @@ -105,7 +106,7 @@ void TaskBase::BlockingWait() const {

void TaskBase::Invalidate() noexcept {
Terminate(TaskCancellationReason::kAbandoned);
context_.reset();
pimpl_->context.reset();
}

TaskBase::TaskBase() = default;
Expand All @@ -115,43 +116,43 @@ TaskBase::TaskBase(TaskBase&&) noexcept = default;
TaskBase& TaskBase::operator=(TaskBase&&) noexcept = default;

// NOLINTNEXTLINE(hicpp-use-equals-default,modernize-use-equals-default)
TaskBase::TaskBase(const TaskBase& other) noexcept : context_(other.context_) {}
TaskBase::TaskBase(const TaskBase& other) noexcept : pimpl_(other.pimpl_) {}

// NOLINTNEXTLINE(hicpp-use-equals-default,modernize-use-equals-default,cert-oop54-cpp)
TaskBase& TaskBase::operator=(const TaskBase& other) noexcept {
context_ = other.context_;
pimpl_->context = other.pimpl_->context;
return *this;
}

impl::TaskContext& TaskBase::GetContext() const noexcept {
UASSERT(context_);
return *context_;
UASSERT(pimpl_->context);
return *pimpl_->context;
}

bool TaskBase::HasSameContext(const TaskBase& other) const noexcept { return context_ == other.context_; }
bool TaskBase::HasSameContext(const TaskBase& other) const noexcept { return pimpl_->context == other.pimpl_->context; }

utils::impl::WrappedCallBase& TaskBase::GetPayload() const noexcept {
UASSERT(context_);
return context_->GetPayload();
UASSERT(pimpl_->context);
return pimpl_->context->GetPayload();
}

void TaskBase::Terminate(TaskCancellationReason reason) noexcept {
if (!context_) {
if (!pimpl_->context) {
return;
}

if (!IsFinished()) {
// We are not providing an implicit sync from outside
// because it's really easy to get a deadlock this way
// e.g. between global event thread pool and task processor
context_->RequestCancel(reason);
pimpl_->context->RequestCancel(reason);

const TaskCancellationBlocker cancel_blocker;
Wait();
}

if (reason == TaskCancellationReason::kAbandoned) {
context_->ResetPayload();
pimpl_->context->ResetPayload();
}
}

Expand Down
17 changes: 17 additions & 0 deletions core/src/engine/task/task_base_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <boost/smart_ptr/intrusive_ptr.hpp>

#include <engine/task/task_context.hpp>

USERVER_NAMESPACE_BEGIN

namespace engine {

struct TaskBase::Impl {
boost::intrusive_ptr<impl::TaskContext> context;
};

} // namespace engine

USERVER_NAMESPACE_END
1 change: 1 addition & 0 deletions core/src/engine/wait_all_checked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <engine/impl/wait_any_utils.hpp>
#include <engine/task/task_context.hpp>
#include <userver/engine/exception.hpp>

USERVER_NAMESPACE_BEGIN

Expand Down

0 comments on commit 5a2e434

Please sign in to comment.