Merge branch 'fix_async_load' into 'master'

Fix crash related to async content loading (#7508)

Closes #7508

See merge request OpenMW/openmw!3290
This commit is contained in:
psi29a 2023-07-31 10:49:33 +00:00
commit b2f669ca48
7 changed files with 82 additions and 28 deletions

View file

@ -292,7 +292,7 @@ add_component_dir (terrain
)
add_component_dir (loadinglistener
loadinglistener
loadinglistener asynclistener
)
add_component_dir (myguiplatform

View file

@ -0,0 +1,67 @@
#ifndef COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H
#define COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H
#include <mutex>
#include <optional>
#include <string>
#include "loadinglistener.hpp"
namespace Loading
{
class AsyncListener : public Listener
{
public:
AsyncListener(Listener& baseListener)
: mBaseListener(baseListener)
{
}
void setLabel(const std::string& label, bool important) override
{
std::lock_guard<std::mutex> guard(mMutex);
mLabelUpdate = label;
mImportantLabel = important;
}
void setProgressRange(size_t range) override
{
std::lock_guard<std::mutex> guard(mMutex);
mRangeUpdate = range;
}
void setProgress(size_t value) override
{
std::lock_guard<std::mutex> guard(mMutex);
mProgressUpdate = value;
}
void increaseProgress(size_t increase) override
{ /* not implemented */
}
void update()
{
std::lock_guard<std::mutex> guard(mMutex);
if (mLabelUpdate)
mBaseListener.setLabel(*mLabelUpdate, mImportantLabel);
if (mRangeUpdate)
mBaseListener.setProgressRange(*mRangeUpdate);
if (mProgressUpdate)
mBaseListener.setProgress(*mProgressUpdate);
mLabelUpdate = std::nullopt;
mRangeUpdate = std::nullopt;
mProgressUpdate = std::nullopt;
}
private:
Listener& mBaseListener;
std::mutex mMutex;
std::optional<std::string> mLabelUpdate;
bool mImportantLabel = false;
std::optional<size_t> mRangeUpdate;
std::optional<size_t> mProgressUpdate;
};
}
#endif // COMPONENTS_LOADINGLISTENER_ASYNCLISTENER_H