Skip to content

Commit

Permalink
C4Task::CancellablePromise: Simplify cancellation logic and fix cance…
Browse files Browse the repository at this point in the history
…llation being overwritten by RevokeCancellationCallback
  • Loading branch information
Fulgen301 committed Aug 8, 2023
1 parent 48343e3 commit 9627995
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/C4Coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ class CancellablePromise
cancel();
#else

const auto callback = cancellationCallback.exchange(CancellingSentinel, std::memory_order_acq_rel);
const auto callback = cancellationCallback.exchange(CancelledSentinel, std::memory_order_acq_rel);

if (callback && callback != CancellingSentinel && callback != CancelledSentinel)
if (callback && callback != CancelledSentinel)
{
const struct Cleanup
{
Expand Down Expand Up @@ -364,7 +364,17 @@ class CancellablePromise
revoke_canceller();
#else
cancellationArgument = nullptr;
cancellationCallback.store(nullptr, std::memory_order_release);

auto callback = cancellationCallback.load(std::memory_order_acquire);

do
{
if (!callback || callback == CancelledSentinel)
{
return;
}
}
while (!cancellationCallback.compare_exchange_weak(callback, nullptr, std::memory_order_release));
#endif
}

Expand All @@ -373,7 +383,8 @@ class CancellablePromise
#ifdef _WIN32
return cancelled.load(std::memory_order_acquire);
#else
return cancellationCallback.load(std::memory_order_acquire) == CancelledSentinel;
const auto callback = cancellationCallback.load(std::memory_order_acquire);
return callback == CancelledSentinel;
#endif
}

Expand All @@ -392,7 +403,6 @@ class CancellablePromise
std::atomic_bool cancelled{false};
#else
public:
static inline const auto CancellingSentinel = std::bit_cast<void(*)(void *)>(reinterpret_cast<void *>(0x01));
static inline const auto CancelledSentinel = std::bit_cast<void(*)(void *)>(reinterpret_cast<void *>(0x02));

protected:
Expand Down

0 comments on commit 9627995

Please sign in to comment.