mirror of
https://github.com/hyprwm/hyprgraphics.git
synced 2025-12-20 04:00:04 +01:00
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
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:
parent
ffc999d980
commit
1fb5bfbd62
5 changed files with 39 additions and 1 deletions
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
8
src/resource/resources/AsyncResource.cpp
Normal file
8
src/resource/resources/AsyncResource.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include "AsyncResource.hpp"
|
||||
|
||||
using namespace Hyprgraphics;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
IAsyncResource::IAsyncResource() : m_impl(makeUnique<SAsyncResourceImpl>()) {
|
||||
;
|
||||
}
|
||||
11
src/resource/resources/AsyncResource.hpp
Normal file
11
src/resource/resources/AsyncResource.hpp
Normal 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;
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue