Skip to content

Commit

Permalink
Adds lib-concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Mar 5, 2024
1 parent b49ad49 commit 6c91fff
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 9 deletions.
1 change: 1 addition & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ set( LIBRARIES
lib-music-information-retrieval
lib-crypto
lib-fft
lib-concurrency
)

if ( ${_OPT}use_lv2 )
Expand Down
17 changes: 17 additions & 0 deletions libraries/lib-concurrency/CMakeLists.txt
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 libraries/lib-concurrency/concurrency/CancellationContext.cpp
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 libraries/lib-concurrency/concurrency/CancellationContext.h
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
18 changes: 18 additions & 0 deletions libraries/lib-concurrency/concurrency/ICancellable.h
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
3 changes: 2 additions & 1 deletion libraries/lib-network-manager/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ set( SOURCES
set ( LIBRARIES
lib-string-utils-interface
lib-preferences-interface
lib-concurrency-interface
PRIVATE
CURL::libcurl
threadpool::threadpool
lib-exceptions-interface
)

set ( DEFINES INTERFACE "HAS_NETWORKING" )
set ( DEFINES INTERFACE "HAS_NETWORKING" PRIVATE "WIN32_LEAN_AND_MEAN" )


audacity_library( ${TARGET} "${SOURCES}" "${LIBRARIES}" "${DEFINES}" "" )
Empty file.
9 changes: 8 additions & 1 deletion libraries/lib-network-manager/IResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "NetworkManagerApi.h"

#include "concurrency/ICancellable.h"

namespace audacity
{
namespace network_manager
Expand Down Expand Up @@ -107,7 +109,7 @@ enum


//! Interface, that provides access to the data from the HTTP response
class NETWORK_MANAGER_API IResponse
class NETWORK_MANAGER_API IResponse : public concurrency::ICancellable
{
public:
using RequestCallback = std::function<void (IResponse*)>;
Expand Down Expand Up @@ -174,6 +176,11 @@ class NETWORK_MANAGER_API IResponse

return result;
}

void Cancel() override
{
abort();
}
};

}
Expand Down
9 changes: 6 additions & 3 deletions libraries/lib-network-manager/curl/CurlResponseFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
Dmitry Vedenko
**********************************************************************/

#include "CurlResponseFactory.h"

#include <algorithm>

#include "CurlResponse.h"
#include "MultipartData.h"
#include "RequestPayload.h"

#include "ThreadPool/ThreadPool.h"

namespace audacity
{
namespace network_manager
Expand All @@ -33,6 +32,10 @@ CurlResponseFactory::CurlResponseFactory ()

}

CurlResponseFactory::~CurlResponseFactory ()
{
}

void CurlResponseFactory::setProxy (const std::string& proxy)
{
mHandleManager->setProxy (proxy);
Expand Down
7 changes: 3 additions & 4 deletions libraries/lib-network-manager/curl/CurlResponseFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@

#pragma once

#include <thread>
#include <vector>
#include <memory>
#include <mutex>

#include "../IResponseFactory.h"

#include "CurlHandleManager.h"
#include "ThreadPool/ThreadPool.h"

class ThreadPool;

namespace audacity
{
Expand All @@ -29,6 +27,7 @@ class CurlResponseFactory final : public IResponseFactory
{
public:
CurlResponseFactory ();
~CurlResponseFactory ();

void setProxy (const std::string& proxy) override;

Expand Down

0 comments on commit 6c91fff

Please sign in to comment.