27 #include <olp/core/client/ApiError.h>
28 #include <olp/core/client/CancellationContext.h>
29 #include <olp/core/client/CancellationToken.h>
30 #include <olp/core/client/Condition.h>
56 template <
typename Exec,
typename Callback>
58 Exec execute_func, Callback callback,
61 task.
SetExecutors(std::move(execute_func), std::move(callback),
70 void Execute()
const { impl_->Execute(); }
81 std::chrono::milliseconds timeout = std::chrono::seconds(60))
const {
82 return impl_->BlockingCancel(timeout);
102 return impl_ == other.
impl_;
111 template <
typename Exec,
typename Callback,
112 #if defined(__cpp_lib_is_invocable) && __cpp_lib_is_invocable >= 201703
113 typename ExecResult =
114 std::invoke_result_t<Exec, client::CancellationContext>
116 typename ExecResult =
130 impl_ = std::make_shared<TaskContextImpl<ExecResult>>(
131 std::move(execute_func), std::move(callback), std::move(context));
141 virtual ~
Impl() =
default;
175 template <
typename Response>
193 : execute_func_(std::move(execute_func)),
194 callback_(std::move(callback)),
195 context_(std::move(context)),
196 state_{
State::PENDING} {}
205 State expected_state = State::PENDING;
207 if (!state_.compare_exchange_strong(expected_state, State::IN_PROGRESS)) {
217 std::lock_guard<std::mutex> lock(mutex_);
218 function = std::move(execute_func_);
219 callback = std::move(callback_);
222 Response user_response =
225 if (
function && !context_.IsCancelled()) {
226 auto response =
function(context_);
229 if (!context_.IsCancelled() ||
230 (!response.IsSuccessful() &&
232 user_response = std::move(response);
240 callback(std::move(user_response));
249 state_.store(State::COMPLETED);
261 if (state_.load() == State::COMPLETED) {
266 if (!context_.IsCancelled()) {
267 context_.CancelOperation();
271 std::lock_guard<std::mutex> lock(mutex_);
272 execute_func_ =
nullptr;
275 return condition_.Wait(timeout);
284 auto context = context_;
286 [context]()
mutable { context.CancelOperation(); });
332 return std::hash<std::shared_ptr<TaskContext::Impl>>()(task_context.
impl_);
A wrapper around an internal error or HTTP status code.
Definition: ApiError.h:37
A wrapper that manages the cancellation state of an asynchronous operation in a thread-safe way.
Definition: CancellationContext.h:40
Cancels service requests.
Definition: CancellationToken.h:33
A helper class that allows one thread to call and wait for a notification in the other thread.
Definition: Condition.h:35
An implementation helper interface used to declare the Execute, BlockingCancel, and CancelToken funct...
Definition: TaskContext.h:139
virtual client::CancellationToken CancelToken()=0
Provides a token to cancel the task.
virtual void Execute()=0
Checks for the cancellation, executes the task, and calls the callback with the result or error.
virtual bool BlockingCancel(std::chrono::milliseconds timeout)=0
Cancels the operation and waits for the notification.
Implements the Impl interface.
Definition: TaskContext.h:176
void Execute() override
Checks for the cancellation, executes the task, and calls the callback with the result or error.
Definition: TaskContext.h:204
std::function< void(Response)> UserCallback
Consumes the Response instance.
Definition: TaskContext.h:181
std::mutex mutex_
Definition: TaskContext.h:303
TaskContextImpl(ExecuteFunc execute_func, UserCallback callback, client::CancellationContext context)
Creates the TaskContextImpl instance.
Definition: TaskContext.h:191
State
Indicates the state of the request.
Definition: TaskContext.h:292
bool BlockingCancel(std::chrono::milliseconds timeout) override
Cancels the operation and waits for the notification.
Definition: TaskContext.h:260
client::Condition condition_
The Condition instance.
Definition: TaskContext.h:311
ExecuteFunc execute_func_
The ExecuteFunc instance.
Definition: TaskContext.h:305
client::CancellationContext context_
The CancellationContext instance.
Definition: TaskContext.h:309
std::atomic< State > state_
The State enum of the atomic type.
Definition: TaskContext.h:313
UserCallback callback_
The UserCallback instance.
Definition: TaskContext.h:307
client::CancellationToken CancelToken() override
Provides a token to cancel the task.
Definition: TaskContext.h:283
std::function< Response(client::CancellationContext)> ExecuteFunc
The task that produces the Response instance.
Definition: TaskContext.h:179
Encapsulates the execution of an asynchronous task and invocation of a callback in a guaranteed manne...
Definition: TaskContext.h:42
std::shared_ptr< Impl > impl_
The Impl instance.
Definition: TaskContext.h:317
bool BlockingCancel(std::chrono::milliseconds timeout=std::chrono::seconds(60)) const
Cancels the operation and waits for the notification.
Definition: TaskContext.h:80
void SetExecutors(Exec execute_func, Callback callback, client::CancellationContext context)
Sets the executors for the request.
Definition: TaskContext.h:128
void Execute() const
Checks for the cancellation, executes the task, and calls the callback with the result or error.
Definition: TaskContext.h:70
static TaskContext Create(Exec execute_func, Callback callback, client::CancellationContext context=client::CancellationContext())
Creates the TaskContext instance with the provided task and callback.
Definition: TaskContext.h:57
client::CancellationToken CancelToken() const
Provides a token to cancel the task.
Definition: TaskContext.h:90
bool operator==(const TaskContext &other) const
Checks whether the values of the TaskContext parameter are the same as the values of the other parame...
Definition: TaskContext.h:101
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24
A helper for unordered containers.
Definition: TaskContext.h:323
size_t operator()(const TaskContext &task_context) const
The hash function for the TaskContext instance.
Definition: TaskContext.h:331