Skip to content

Commit

Permalink
Introduce HandlerCallbackPtr alias
Browse files Browse the repository at this point in the history
Summary:
This introduces the following aliases:
- `HandlerCallbackPtr<T>` → `std::unique_ptr<HandlerCallback<T>>`
- `HandlerCallbackBase::Ptr` → `std::unique_ptr<HandlerCallbackBase`
- `HandlerCallback<T>::Ptr` (same as `HandlerCallbackPtr<T>`)

This allows a no-effect codemod after which we can change the underlying type from `unique_ptr` to something similar to `folly::Executor::KeepAlive`. Such a change would effectively allow shared ownership rather than unique ownership and consequently let us invoke asynchronous functionality safely in `HandlerCallback::result` (required for `ServiceInterceptor::onResponse`).

Reviewed By: iahs

Differential Revision: D55932261

fbshipit-source-id: b58fe5cb839d8f20eca55fae5abcdda9dfabaa55
  • Loading branch information
praihan authored and facebook-github-bot committed Apr 10, 2024
1 parent 5ab2186 commit b165232
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions thrift/lib/cpp2/async/AsyncProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,12 @@ class ServerInterface : public virtual AsyncProcessorFactory,
void setNameOverride(std::string name) { nameOverride_ = std::move(name); }
};

template <class T>
class HandlerCallback;

template <class T>
using HandlerCallbackPtr = std::unique_ptr<HandlerCallback<T>>;

/**
* HandlerCallback class for async callbacks.
*
Expand Down Expand Up @@ -1201,6 +1207,8 @@ class HandlerCallbackBase {
}

public:
using Ptr = std::unique_ptr<HandlerCallbackBase>;

HandlerCallbackBase() : eb_(nullptr), reqCtx_(nullptr), protoSeqId_(0) {}

HandlerCallbackBase(
Expand Down Expand Up @@ -1366,6 +1374,7 @@ class HandlerCallback : public HandlerCallbackBase {
using cob_ptr = typename Helper::CobPtr;

public:
using Ptr = std::unique_ptr<HandlerCallback<T>>;
using ResultType = std::decay_t<typename Helper::InputType>;

public:
Expand Down Expand Up @@ -1413,6 +1422,7 @@ class HandlerCallback<void> : public HandlerCallbackBase {
using cob_ptr = SerializedResponse (*)(ContextStack*);

public:
using Ptr = std::unique_ptr<HandlerCallback<void>>;
using ResultType = void;

HandlerCallback() : cp_(nullptr) {}
Expand Down Expand Up @@ -1466,6 +1476,8 @@ template <typename InteractionIf, typename Response>
class HandlerCallback<TileAndResponse<InteractionIf, Response>> final
: public HandlerCallback<Response> {
public:
using Ptr = std::unique_ptr<HandlerCallback>;

void result(TileAndResponse<InteractionIf, Response>&& r) {
if (this->fulfillTilePromise(std::move(r.tile))) {
if constexpr (!std::is_void_v<Response>) {
Expand Down

0 comments on commit b165232

Please sign in to comment.