-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimentation on cancellable awaiter.
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Created by wanchen.he on 2023/12/29. | ||
// | ||
#ifdef __cpp_impl_coroutine | ||
#include <drogon/utils/coroutine.h> | ||
|
||
namespace drogon | ||
{ | ||
class CancelHandleImpl : public CancelHandle | ||
{ | ||
public: | ||
CancelHandleImpl() = default; | ||
|
||
void cancel() override | ||
{ | ||
std::function<void()> handle; | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
cancelRequested_ = true; | ||
handle = std::move(cancelHandle_); | ||
} | ||
if (handle) | ||
handle(); | ||
} | ||
|
||
bool isCancelRequested() override | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
return cancelRequested_; | ||
} | ||
|
||
void registerCancelCallback(std::function<void()> callback) override | ||
{ | ||
} | ||
|
||
void setCancelHandle(std::function<void()> handle) | ||
{ | ||
bool cancelled{false}; | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
if (cancelRequested_) | ||
{ | ||
cancelled = true; | ||
} | ||
else | ||
{ | ||
cancelHandle_ = std::move(handle); | ||
} | ||
} | ||
if (cancelled) | ||
{ | ||
handle(); | ||
} | ||
} | ||
|
||
private: | ||
std::mutex mutex_; | ||
bool cancelRequested_{false}; | ||
|
||
std::shared_ptr<std::atomic_bool> flagPtr_; | ||
std::function<void()> cancelHandle_; | ||
}; | ||
|
||
CancelHandlePtr CancelHandle::create() | ||
{ | ||
return std::make_shared<CancelHandleImpl>(); | ||
} | ||
|
||
void internal::CancellableTimeAwaiter::await_suspend( | ||
std::coroutine_handle<> handle) | ||
{ | ||
auto execFlagPtr = std::make_shared<std::atomic_bool>(false); | ||
if (cancelHandle_) | ||
{ | ||
static_cast<CancelHandleImpl *>(cancelHandle_.get()) | ||
->setCancelHandle([this, handle, execFlagPtr, loop = loop_]() { | ||
if (!execFlagPtr->exchange(true)) | ||
{ | ||
setException(std::make_exception_ptr( | ||
TaskCancelledException("Task cancelled"))); | ||
loop->queueInLoop([handle]() { handle.resume(); }); | ||
return; | ||
} | ||
}); | ||
} | ||
loop_->runAfter(delay_, [handle, execFlagPtr = std::move(execFlagPtr)]() { | ||
if (!execFlagPtr->exchange(true)) | ||
{ | ||
handle.resume(); | ||
} | ||
}); | ||
} | ||
} // namespace drogon | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters