mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 18:21:16 +02:00
Merge pull request #8924 from huffman/fix/batch-loader-race
Fix race condition in BatchLoader
This commit is contained in:
commit
e7ea30b1cc
2 changed files with 28 additions and 12 deletions
|
@ -44,33 +44,40 @@ void BatchLoader::start() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (const auto& rawURL : _urls) {
|
for (const auto& rawURL : _urls) {
|
||||||
QUrl url = expandScriptUrl(normalizeScriptURL(rawURL));
|
QUrl url = expandScriptUrl(normalizeScriptURL(rawURL));
|
||||||
|
|
||||||
qCDebug(scriptengine) << "Loading script at " << url;
|
qCDebug(scriptengine) << "Loading script at " << url;
|
||||||
|
|
||||||
QPointer<BatchLoader> self = this;
|
auto scriptCache = DependencyManager::get<ScriptCache>();
|
||||||
DependencyManager::get<ScriptCache>()->getScriptContents(url.toString(), [this, self](const QString& url, const QString& contents, bool isURL, bool success) {
|
|
||||||
if (!self) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Because the ScriptCache may call this callback from differents threads,
|
// Use a proxy callback to handle the call and emit the signal in a thread-safe way.
|
||||||
// we need to make sure this is thread-safe.
|
// If BatchLoader is deleted before the callback is called, the subsequent "emit" call will not do
|
||||||
std::lock_guard<std::mutex> lock(_dataLock);
|
// anything.
|
||||||
|
ScriptCacheSignalProxy* proxy = new ScriptCacheSignalProxy(scriptCache.data());
|
||||||
|
scriptCache->getScriptContents(url.toString(), [proxy](const QString& url, const QString& contents, bool isURL, bool success) {
|
||||||
|
proxy->receivedContent(url, contents, isURL, success);
|
||||||
|
proxy->deleteLater();
|
||||||
|
}, false);
|
||||||
|
|
||||||
|
connect(proxy, &ScriptCacheSignalProxy::contentAvailable, this, [this](const QString& url, const QString& contents, bool isURL, bool success) {
|
||||||
if (isURL && success) {
|
if (isURL && success) {
|
||||||
_data.insert(url, contents);
|
_data.insert(url, contents);
|
||||||
qCDebug(scriptengine) << "Loaded: " << url;
|
qCDebug(scriptengine) << "Loaded: " << url;
|
||||||
} else {
|
} else {
|
||||||
_data.insert(url, QString());
|
_data.insert(url, QString());
|
||||||
qCDebug(scriptengine) << "Could not load" << url;
|
qCDebug(scriptengine) << "Could not load: " << url;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_finished && _urls.size() == _data.size()) {
|
if (!_finished && _urls.size() == _data.size()) {
|
||||||
_finished = true;
|
_finished = true;
|
||||||
emit finished(_data);
|
emit finished(_data);
|
||||||
}
|
}
|
||||||
}, false);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptCacheSignalProxy::receivedContent(const QString& url, const QString& contents, bool isURL, bool success) {
|
||||||
|
emit contentAvailable(url, contents, isURL, success);
|
||||||
|
}
|
||||||
|
|
|
@ -21,10 +21,20 @@
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
class ScriptCacheSignalProxy : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
ScriptCacheSignalProxy(QObject* parent) : QObject(parent) { }
|
||||||
|
void receivedContent(const QString& url, const QString& contents, bool isURL, bool success);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void contentAvailable(const QString& url, const QString& contents, bool isURL, bool success);
|
||||||
|
};
|
||||||
|
|
||||||
class BatchLoader : public QObject {
|
class BatchLoader : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
BatchLoader(const QList<QUrl>& urls) ;
|
BatchLoader(const QList<QUrl>& urls);
|
||||||
|
|
||||||
void start();
|
void start();
|
||||||
bool isFinished() const { return _finished; };
|
bool isFinished() const { return _finished; };
|
||||||
|
@ -39,7 +49,6 @@ private:
|
||||||
bool _finished;
|
bool _finished;
|
||||||
QSet<QUrl> _urls;
|
QSet<QUrl> _urls;
|
||||||
QMap<QUrl, QString> _data;
|
QMap<QUrl, QString> _data;
|
||||||
std::mutex _dataLock;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_BatchLoader_h
|
#endif // hifi_BatchLoader_h
|
||||||
|
|
Loading…
Reference in a new issue