mirror of https://github.com/OpenMW/openmw.git
Make sync terrain preloading sleep free
This reduces average time spent on in. 5 milliseconds as a base precision is quite a lot considering that for 60 FPS frame time is 1000/16 = ~16.67 ms when it's a cell loading frame and there is more important work to do rather than sleeping.pull/3096/head
parent
aef6ec464c
commit
605cb8db7c
@ -0,0 +1,41 @@
|
|||||||
|
#include "reporter.hpp"
|
||||||
|
#include "loadinglistener.hpp"
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace Loading
|
||||||
|
{
|
||||||
|
void Reporter::addTotal(std::size_t value)
|
||||||
|
{
|
||||||
|
const std::lock_guard lock(mMutex);
|
||||||
|
mTotal += value;
|
||||||
|
mUpdated.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reporter::addProgress(std::size_t value)
|
||||||
|
{
|
||||||
|
const std::lock_guard lock(mMutex);
|
||||||
|
mProgress += value;
|
||||||
|
mUpdated.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reporter::complete()
|
||||||
|
{
|
||||||
|
const std::lock_guard lock(mMutex);
|
||||||
|
mDone = true;
|
||||||
|
mUpdated.notify_all();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Reporter::wait(Listener& listener) const
|
||||||
|
{
|
||||||
|
std::unique_lock lock(mMutex);
|
||||||
|
while (!mDone)
|
||||||
|
{
|
||||||
|
listener.setProgressRange(mTotal);
|
||||||
|
listener.setProgress(mProgress);
|
||||||
|
mUpdated.wait(lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef COMPONENTS_LOADINGLISTENER_REPORTER_H
|
||||||
|
#define COMPONENTS_LOADINGLISTENER_REPORTER_H
|
||||||
|
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace Loading
|
||||||
|
{
|
||||||
|
class Listener;
|
||||||
|
|
||||||
|
class Reporter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void addTotal(std::size_t value);
|
||||||
|
|
||||||
|
void addProgress(std::size_t value);
|
||||||
|
|
||||||
|
void complete();
|
||||||
|
|
||||||
|
void wait(Listener& listener) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::size_t mProgress = 0;
|
||||||
|
std::size_t mTotal = 0;
|
||||||
|
bool mDone = false;
|
||||||
|
mutable std::mutex mMutex;
|
||||||
|
mutable std::condition_variable mUpdated;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue