#include <userver/dist_lock/dist_locked_task.hpp>
A task that tries to acquire a distributed lock and runs user callback once while the lock is held.
When dist lock starts, the lock worker tries to take a lock in the loop. If succeeded, a task is launched that executes the user code. In the background, dist lock tries to extend the lock. In case of loss of the lock, the user task is canceled.
Definition at line 48 of file dist_locked_task.hpp.
Inheritance diagram for dist_lock::DistLockedTask:Public Types | |
| using | WorkerFunc = std::function<void()> |
| enum class | Importance { kNormal , kCritical } |
| Task importance. More... | |
| enum class | State { kInvalid , kNew , kQueued , kRunning , kSuspended , kCancelled , kCompleted } |
| Task state. More... | |
| enum class | WaitMode { kSingleAwaiter , kMultipleAwaiters } |
| Task wait mode. More... | |
Public Member Functions | |
| DistLockedTask ()=default | |
| DistLockedTask (DistLockedTask &&)=delete | |
| DistLockedTask & | operator= (DistLockedTask &&)=delete |
| DistLockedTask (const DistLockedTask &)=delete | |
| DistLockedTask & | operator= (const DistLockedTask &&)=delete |
| DistLockedTask (std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, DistLockWaitingMode mode=DistLockWaitingMode::kWait, DistLockRetryMode retry_mode=DistLockRetryMode::kRetry) | |
| DistLockedTask (engine::TaskProcessor &task_processor, std::string name, WorkerFunc worker_func, std::shared_ptr< DistLockStrategyBase > strategy, const DistLockSettings &settings={}, DistLockWaitingMode mode=DistLockWaitingMode::kWait, DistLockRetryMode retry_mode=DistLockRetryMode::kRetry) | |
| Creates a DistLockedTask to be run in a specific engine::TaskProcessor. | |
| std::optional< std::chrono::steady_clock::duration > | GetLockedDuration () const |
| void | Get () noexcept(false) |
| bool | IsValid () const |
| Checks whether this object owns an actual task (not State::kInvalid) | |
| State | GetState () const |
| Gets the task State. | |
| bool | IsFinished () const |
| Returns whether the task finished execution. | |
| void | Wait () const noexcept(false) |
| Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait(). | |
| template<typename Rep, typename Period> | |
| void | WaitFor (const std::chrono::duration< Rep, Period > &) const noexcept(false) |
| Suspends execution until the task finishes or after the specified timeout or until caller is cancelled. | |
| template<typename Clock, typename Duration> | |
| void | WaitUntil (const std::chrono::time_point< Clock, Duration > &) const noexcept(false) |
| Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled. | |
| void | WaitUntil (Deadline) const |
| Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled. | |
| bool | WaitNothrow () const noexcept |
| Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait(). | |
| FutureStatus | WaitNothrowUntil (Deadline) const noexcept |
| Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled. | |
| void | RequestCancel () |
| Queues task cancellation request. | |
| void | SyncCancel () noexcept |
| Cancels the task and suspends execution until it is finished. Can be called from coroutine context only. For non-coroutine context use RequestCancel() + BlockingWait(). | |
| TaskCancellationReason | CancellationReason () const |
| Gets task cancellation reason. | |
| void | BlockingWait () const |
Static Public Member Functions | |
| static std::string_view | GetStateName (State state) |
| using dist_lock::DistLockedTask::WorkerFunc = std::function<void()> |
Definition at line 50 of file dist_locked_task.hpp.
|
stronginherited |
Task importance.
Definition at line 36 of file task_base.hpp.
|
stronginherited |
Task state.
| Enumerator | |
|---|---|
| kInvalid | Unusable. |
| kNew | just created, not registered with task processor |
| kQueued | awaits execution |
| kRunning | executing user code |
| kSuspended | suspended, e.g. waiting for blocking call to complete |
| kCancelled | The task is cancelled and was finished without returning a value or throwing a user-provided exception. This can happen for two reasons:
In both cases, engine::TaskWithResult::Get throws engine::TaskCancelledException. Unintuitively, this status is not set when the task was cancelled after it started running, which caused the user code to exit early. Use TaskBase::CancellationReason instead to check whether the task was cancelled. |
| kCompleted | Exited user code with return or throw. This includes cases where the task was cancelled after it started running, which caused the user code to exit early. Use TaskBase::IsFinished instead to check whether the task finished execution. |
Definition at line 48 of file task_base.hpp.
|
stronginherited |
Task wait mode.
| Enumerator | |
|---|---|
| kSingleAwaiter | Can be awaited by at most one task at a time. |
| kMultipleAwaiters | Can be awaited by multiple tasks simultaneously. |
Definition at line 85 of file task_base.hpp.
|
default |
Default constructor. Creates an invalid task.
| dist_lock::DistLockedTask::DistLockedTask | ( | std::string | name, |
| WorkerFunc | worker_func, | ||
| std::shared_ptr< DistLockStrategyBase > | strategy, | ||
| const DistLockSettings & | settings = {}, | ||
| DistLockWaitingMode | mode = DistLockWaitingMode::kWait, | ||
| DistLockRetryMode | retry_mode = DistLockRetryMode::kRetry ) |
Creates a DistLockedTask.
| name | name of the task |
| worker_func | a callback that is started once we've acquired the lock and is cancelled when the lock is lost. |
| settings | distributed lock settings |
| strategy | distributed locking strategy |
| mode | distributed lock waiting mode |
| retry_mode | run task continuously or once (needed mainly for tests) |
worker_func must honour task cancellation and stop ASAP when it is cancelled, otherwise brain split is possible (IOW, two different users do work assuming both of them hold the lock, which is not true).
|
inherited |
Waits for the task in non-coroutine context (e.g. non-TaskProcessor's std::thread).
| std::optional< std::chrono::steady_clock::duration > dist_lock::DistLockedTask::GetLockedDuration | ( | ) | const |
Returns for how long the lock is held (if held at all). Returned value may be less than the real duration.
|
inherited |
Checks whether this object owns an actual task (not State::kInvalid)
An invalid task cannot be used. The task becomes invalid after each of the following calls:
Notably, the task does not become invalid immediately after it finishes execution. (That would always cause race conditions when trying to await a task.) It means that some of the task's resources are held onto until the task object is invalidated or destroyed.
|
inherited |
Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
|
inherited |
Suspends execution until the task finishes or after the specified timeout or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
Definition at line 210 of file task_base.hpp.
|
nodiscardnoexceptinherited |
Suspends execution until the task finishes or caller is cancelled. Can be called from coroutine context only. For non-coroutine context use BlockingWait().
false when current_task::IsCancelRequested() and no TaskCancellationBlockers are present.
|
nodiscardnoexceptinherited |
Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
current_task::IsCancelRequested() and no TaskCancellationBlockers are present.
|
inherited |
Suspends execution until the task finishes or until the specified time point is reached or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |
Definition at line 215 of file task_base.hpp.
|
inherited |
Suspends execution until the task finishes or until the specified deadline is reached or until caller is cancelled.
| WaitInterruptedException | when current_task::IsCancelRequested() and no TaskCancellationBlockers are present. |