mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/338482889
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: Ic2bdb234e89f3c5860d1b483dd4bce689f13d057
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user