Commit acf28118 authored by Kevin Vigor's avatar Kevin Vigor Committed by Facebook GitHub Bot

use folly::Function for callback in async IO interface.

Summary:
folly::Function is non-copyable. This means it can be used with lambdas that capture
a unique_ptr, whereas std::function can not.

See https://www.internalfb.com/intern/wiki/Folly/Function/

Differential Revision: D23883275

fbshipit-source-id: a26cda0d3833e3ea822beef99c6a28a6dd90fa83
parent 73d254b5
...@@ -219,8 +219,9 @@ void AsyncBaseQueue::maybeDequeue() { ...@@ -219,8 +219,9 @@ void AsyncBaseQueue::maybeDequeue() {
queue_.pop_front(); queue_.pop_front();
// Interpose our completion callback // Interpose our completion callback
auto& nextCb = op->notificationCallback(); auto nextCb = op->getNotificationCallback();
op->setNotificationCallback([this, nextCb](AsyncBaseOp* op2) { op->setNotificationCallback(
[this, nextCb{std::move(nextCb)}](AsyncBaseOp* op2) mutable {
this->onCompleted(op2); this->onCompleted(op2);
if (nextCb) { if (nextCb) {
nextCb(op2); nextCb(op2);
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <folly/Function.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/portability/SysUio.h> #include <folly/portability/SysUio.h>
...@@ -44,7 +45,7 @@ class AsyncBaseOp { ...@@ -44,7 +45,7 @@ class AsyncBaseOp {
friend class AsyncBase; friend class AsyncBase;
public: public:
using NotificationCallback = std::function<void(AsyncBaseOp*)>; using NotificationCallback = folly::Function<void(AsyncBaseOp*)>;
explicit AsyncBaseOp(NotificationCallback cb = NotificationCallback()); explicit AsyncBaseOp(NotificationCallback cb = NotificationCallback());
AsyncBaseOp(const AsyncBaseOp&) = delete; AsyncBaseOp(const AsyncBaseOp&) = delete;
...@@ -119,8 +120,16 @@ class AsyncBaseOp { ...@@ -119,8 +120,16 @@ class AsyncBaseOp {
void setNotificationCallback(NotificationCallback cb) { void setNotificationCallback(NotificationCallback cb) {
cb_ = std::move(cb); cb_ = std::move(cb);
} }
const NotificationCallback& notificationCallback() const {
return cb_; /**
* Get the notification callback from the op.
*
* Note that this moves the callback out, leaving the callback in an
* uninitialized state! You must call setNotificationCallback before
* submitting the operation!
*/
NotificationCallback getNotificationCallback() {
return std::move(cb_);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment