forked from audacity/audacity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
171 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# SPDX-License-Identifier: GPL-2.0-or-later | ||
# SPDX-FileName: CMakeLists.txt | ||
# SPDX-FileContributor: Dmitry Vedenko | ||
#[[ | ||
A set of concurrency primitives | ||
]] | ||
|
||
set( SOURCES | ||
concurrency/CancellationContext.cpp | ||
concurrency/CancellationContext.h | ||
concurrency/ICancellable.h | ||
) | ||
set( LIBRARIES | ||
PUBLIC | ||
) | ||
audacity_library( lib-concurrency "${SOURCES}" "${LIBRARIES}" | ||
"" "" ) |
68 changes: 68 additions & 0 deletions
68
libraries/lib-concurrency/concurrency/CancellationContext.cpp
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,68 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* SPDX-FileName: CancellationContext.cpp | ||
* SPDX-FileContributor: Dmitry Vedenko | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "CancellationContext.h" | ||
|
||
#include <algorithm> | ||
|
||
#include "ICancellable.h" | ||
|
||
namespace audacity::concurrency | ||
{ | ||
CancellationContext::CancellationContext(Tag) | ||
{ | ||
} | ||
|
||
std::shared_ptr<CancellationContext> CancellationContext::Create() | ||
{ | ||
return std::make_shared<CancellationContext>(Tag {}); | ||
} | ||
|
||
void CancellationContext::Cancel() | ||
{ | ||
if (mCancelled.exchange(true)) | ||
return; | ||
|
||
std::vector<CancellableWPtr> cancellableObjects; | ||
|
||
{ | ||
auto lock = std::lock_guard { mCancellableObjectsMutex }; | ||
std::swap(cancellableObjects, mCancellableObjects); | ||
} | ||
|
||
std::for_each( | ||
cancellableObjects.begin(), cancellableObjects.end(), | ||
[](auto& wptr) | ||
{ | ||
if (auto lock = wptr.lock()) | ||
lock->Cancel(); | ||
}); | ||
} | ||
|
||
void CancellationContext::OnCancelled(CancellableWPtr cancellable) | ||
{ | ||
auto locked = cancellable.lock(); | ||
if (!locked) | ||
return; | ||
|
||
if (mCancelled.load(std::memory_order_acquire)) | ||
{ | ||
locked->Cancel(); | ||
return; | ||
} | ||
|
||
auto lock = std::lock_guard { mCancellableObjectsMutex }; | ||
mCancellableObjects.erase( | ||
std::remove_if( | ||
mCancellableObjects.begin(), mCancellableObjects.end(), | ||
[](auto& wptr) { return wptr.expired(); }), | ||
mCancellableObjects.end()); | ||
|
||
mCancellableObjects.push_back(std::move(cancellable)); | ||
} | ||
} // namespace audacity::concurrency |
48 changes: 48 additions & 0 deletions
48
libraries/lib-concurrency/concurrency/CancellationContext.h
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,48 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* SPDX-FileName: CancellationContext.h | ||
* SPDX-FileContributor: Dmitry Vedenko | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <atomic> | ||
#include <memory> | ||
#include <mutex> | ||
#include <vector> | ||
|
||
namespace audacity::concurrency | ||
{ | ||
class ICancellable; | ||
|
||
class CancellationContext; | ||
using CancellationContextPtr = std::shared_ptr<CancellationContext>; | ||
|
||
class CONCURRENCY_API CancellationContext final | ||
{ | ||
struct Tag final | ||
{ | ||
}; | ||
|
||
public: | ||
explicit CancellationContext(Tag); | ||
|
||
CancellationContext(const CancellationContext&) = delete; | ||
CancellationContext(CancellationContext&&) = delete; | ||
CancellationContext& operator=(const CancellationContext&) = delete; | ||
CancellationContext& operator=(CancellationContext&&) = delete; | ||
|
||
[[nodiscard]] static CancellationContextPtr Create(); | ||
|
||
void Cancel(); | ||
|
||
using CancellableWPtr = std::weak_ptr<ICancellable>; | ||
void OnCancelled(CancellableWPtr cancellable); | ||
|
||
private: | ||
std::atomic<bool> mCancelled { false }; | ||
|
||
std::mutex mCancellableObjectsMutex; | ||
std::vector<CancellableWPtr> mCancellableObjects; | ||
}; // struct CancellationContext | ||
} // namespace audacity::concurrency |
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,18 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* SPDX-FileName: ICancellable.cpp | ||
* SPDX-FileContributor: Dmitry Vedenko | ||
*/ | ||
|
||
#pragma once | ||
|
||
namespace audacity::concurrency | ||
{ | ||
class CONCURRENCY_API ICancellable | ||
{ | ||
public: | ||
virtual ~ICancellable() = default; | ||
|
||
virtual void Cancel() = 0; | ||
}; | ||
} // namespace audacity::concurrency |
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
Empty file.
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