Audacity 3.2.0
CancellationContext.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-or-later
3 * SPDX-FileName: CancellationContext.cpp
4 * SPDX-FileContributor: Dmitry Vedenko
5 */
6
8
9#include <algorithm>
10
11#include "ICancellable.h"
12
14{
16{
17}
18
19std::shared_ptr<CancellationContext> CancellationContext::Create()
20{
21 return std::make_shared<CancellationContext>(Tag {});
22}
23
25{
26 if (mCancelled.exchange(true))
27 return;
28
29 std::vector<CancellableWPtr> cancellableObjects;
30
31 {
32 auto lock = std::lock_guard { mCancellableObjectsMutex };
33 std::swap(cancellableObjects, mCancellableObjects);
34 }
35
36 std::for_each(
37 cancellableObjects.begin(), cancellableObjects.end(),
38 [](auto& wptr)
39 {
40 if (auto lock = wptr.lock())
41 lock->Cancel();
42 });
43}
44
46{
47 auto locked = cancellable.lock();
48 if (!locked)
49 return;
50
51 if (mCancelled.load(std::memory_order_acquire))
52 {
53 locked->Cancel();
54 return;
55 }
56
57 auto lock = std::lock_guard { mCancellableObjectsMutex };
59 std::remove_if(
61 [](auto& wptr) { return wptr.expired(); }),
63
64 mCancellableObjects.push_back(std::move(cancellable));
65}
66} // namespace audacity::concurrency
void OnCancelled(CancellableWPtr cancellable)
std::weak_ptr< ICancellable > CancellableWPtr
std::vector< CancellableWPtr > mCancellableObjects
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:628