Roll forward to cl/338482889

Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: Ic2bdb234e89f3c5860d1b483dd4bce689f13d057
This commit is contained in:
Alexey Polyudov
2020-10-22 11:30:33 -07:00
parent 13f8fddfde
commit ce4807935e
564 changed files with 13720 additions and 48704 deletions
+17 -26
View File
@@ -16,40 +16,31 @@
#define PLATFORM_API_SUBMITTABLE_EXECUTOR_H_
#include <functional>
#include <memory>
#include "platform/api/executor.h"
#include "platform/api/future.h"
#include "platform/api/platform.h"
#include "platform/api/settable_future.h"
#include "platform/api/submittable_executor_def.h"
#include "platform/exception.h"
#include "platform/base/runnable.h"
namespace location {
namespace nearby {
namespace api {
// "Common" part of implementation.
// Placed here for textual compatibility to minimize scope of changes.
// Can be (and should be) moved to a separate file outside "api" folder.
// TODO(apolyudov): for API v2.0
template <typename T>
Ptr<Future<T>> SubmittableExecutor::submit(Ptr<Callable<T>> callable) {
using Platform = platform::ImplementationPlatform;
Ptr<SettableFuture<T>> future{Platform::createSettableFuture<T>()};
bool submitted = DoSubmit([callable, future]() {
ExceptionOr<T> result = callable->call();
if (result.ok()) {
future->set(std::move(result.result()));
} else {
future->setException({result.exception()});
}
});
if (!submitted) {
// Raise Exception::kExecution if we are shutting down.
future->setException({Exception::kExecution});
}
return future;
}
// Main interface to be used by platform as a base class for
// - MultiThreadExecutorWrapper
// - SingleThreadExecutorWrapper
// Platform must override bool submit(std::function<void()>) method.
class SubmittableExecutor : public Executor {
public:
~SubmittableExecutor() override = default;
// Submit a callable (with no delay).
// Returns true, if callable was submitted, false otherwise.
// Callable is not submitted if shutdown is in progress.
virtual bool DoSubmit(Runnable&& wrapped_callable) = 0;
};
} // namespace api
} // namespace nearby
} // namespace location