resource/AsyncResource: add await() for resources
Some checks failed
Build & Test (Arch) / Arch: Build and Test (gcc) (push) Has been cancelled
Build & Test (Arch) / Arch: Build and Test (clang) (push) Has been cancelled
Build & Test / nix (hyprgraphics) (push) Has been cancelled
Build & Test / nix (hyprgraphics-with-tests) (push) Has been cancelled

This commit is contained in:
Vaxry 2025-11-18 17:43:36 +00:00
parent ffc999d980
commit 1fb5bfbd62
Signed by: vaxry
GPG key ID: 665806380871D640
5 changed files with 39 additions and 1 deletions

View file

@ -17,6 +17,9 @@ namespace Hyprgraphics {
void enqueue(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource);
// Synchronously await the resource being available
void await(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource);
private:
std::thread m_gatherThread;

View file

@ -7,9 +7,11 @@
#include <atomic>
namespace Hyprgraphics {
struct SAsyncResourceImpl;
class IAsyncResource {
public:
IAsyncResource() = default;
IAsyncResource();
virtual ~IAsyncResource() = default;
virtual void render() = 0;
@ -29,5 +31,7 @@ namespace Hyprgraphics {
Hyprutils::Memory::CSharedPointer<CCairoSurface> cairoSurface;
Hyprutils::Math::Vector2D pixelSize;
} m_asset;
Hyprutils::Memory::CUniquePointer<SAsyncResourceImpl> m_impl;
};
}

View file

@ -1,4 +1,5 @@
#include <hyprgraphics/resource/AsyncResourceGatherer.hpp>
#include "resources/AsyncResource.hpp"
using namespace Hyprgraphics;
@ -28,6 +29,13 @@ void CAsyncResourceGatherer::enqueue(Hyprutils::Memory::CAtomicSharedPointer<IAs
wakeUpMainThread();
}
void CAsyncResourceGatherer::await(Hyprutils::Memory::CAtomicSharedPointer<IAsyncResource> resource) {
resource->m_impl->awaitingCv = Hyprutils::Memory::makeUnique<std::condition_variable>();
std::unique_lock<std::mutex> lk(resource->m_impl->awaitingMtx);
resource->m_impl->awaitingCv->wait(lk, [&resource] { return resource->m_impl->awaitingEvent; });
resource->m_impl->awaitingCv.reset();
}
void CAsyncResourceGatherer::asyncAssetSpinLock() {
while (!m_asyncLoopState.exit) {
@ -56,6 +64,10 @@ void CAsyncResourceGatherer::asyncAssetSpinLock() {
for (auto& r : requests) {
r->render();
if (r->m_impl->awaitingCv) {
r->m_impl->awaitingEvent = true;
r->m_impl->awaitingCv->notify_all();
}
r->m_ready = true;
r->m_events.finished.emit();
}

View file

@ -0,0 +1,8 @@
#include "AsyncResource.hpp"
using namespace Hyprgraphics;
using namespace Hyprutils::Memory;
IAsyncResource::IAsyncResource() : m_impl(makeUnique<SAsyncResourceImpl>()) {
;
}

View file

@ -0,0 +1,11 @@
#include <hyprgraphics/resource/resources/AsyncResource.hpp>
#include <condition_variable>
namespace Hyprgraphics {
struct SAsyncResourceImpl {
Hyprutils::Memory::CUniquePointer<std::condition_variable> awaitingCv;
std::mutex awaitingMtx;
bool awaitingEvent = false;
};
}