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 typename ExecResult =
typename std::result_of<
124 impl_ = std::make_shared<TaskContextImpl<ExecResult>>(
125 std::move(execute_func), std::move(callback), std::move(context));
135 virtual ~
Impl() =
default;
169 template <
typename Response>
187 : execute_func_(std::move(execute_func)),
188 callback_(std::move(callback)),
189 context_(std::move(context)),
190 state_{
State::PENDING} {}
199 State expected_state = State::PENDING;
201 if (!state_.compare_exchange_strong(expected_state, State::IN_PROGRESS)) {
211 std::lock_guard<std::mutex> lock(mutex_);
212 function = std::move(execute_func_);
213 callback = std::move(callback_);
216 Response user_response =
219 if (
function && !context_.IsCancelled()) {
220 auto response =
function(context_);
223 if (!context_.IsCancelled() ||
224 (!response.IsSuccessful() &&
226 user_response = std::move(response);
234 callback(std::move(user_response));
243 state_.store(State::COMPLETED);
255 if (state_.load() == State::COMPLETED) {
260 if (!context_.IsCancelled()) {
261 context_.CancelOperation();
265 std::lock_guard<std::mutex> lock(mutex_);
266 execute_func_ =
nullptr;
269 return condition_.Wait(timeout);
278 auto context = context_;
280 [context]()
mutable { context.CancelOperation(); });
326 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:133
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:170
void Execute() override
Checks for the cancellation, executes the task, and calls the callback with the result or error.
Definition: TaskContext.h:198
std::function< void(Response)> UserCallback
Consumes the Response instance.
Definition: TaskContext.h:175
std::mutex mutex_
Definition: TaskContext.h:297
TaskContextImpl(ExecuteFunc execute_func, UserCallback callback, client::CancellationContext context)
Creates the TaskContextImpl instance.
Definition: TaskContext.h:185
State
Indicates the state of the request.
Definition: TaskContext.h:286
bool BlockingCancel(std::chrono::milliseconds timeout) override
Cancels the operation and waits for the notification.
Definition: TaskContext.h:254
client::Condition condition_
The Condition instance.
Definition: TaskContext.h:305
ExecuteFunc execute_func_
The ExecuteFunc instance.
Definition: TaskContext.h:299
client::CancellationContext context_
The CancellationContext instance.
Definition: TaskContext.h:303
std::atomic< State > state_
The State enum of the atomic type.
Definition: TaskContext.h:307
UserCallback callback_
The UserCallback instance.
Definition: TaskContext.h:301
client::CancellationToken CancelToken() override
Provides a token to cancel the task.
Definition: TaskContext.h:277
std::function< Response(client::CancellationContext)> ExecuteFunc
The task that produces the Response instance.
Definition: TaskContext.h:173
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:311
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:122
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:317
size_t operator()(const TaskContext &task_context) const
The hash function for the TaskContext instance.
Definition: TaskContext.h:325