Merge pull request #8824 from huffman/fix/batch-load-crash

Fix race condition in BatchLoader
This commit is contained in:
Chris Collins 2016-10-17 20:55:38 -07:00 committed by GitHub
commit b43301cd8c
2 changed files with 13 additions and 10 deletions

View file

@ -48,6 +48,11 @@ void BatchLoader::start() {
if (!self) {
return;
}
// Because the ScriptCache may call this callback from differents threads,
// we need to make sure this is thread-safe.
std::lock_guard<std::mutex> lock(_dataLock);
if (isURL && success) {
_data.insert(url, contents);
qCDebug(scriptengine) << "Loaded: " << url;
@ -55,16 +60,11 @@ void BatchLoader::start() {
_data.insert(url, QString());
qCDebug(scriptengine) << "Could not load" << url;
}
checkFinished();
if (!_finished && _urls.size() == _data.size()) {
_finished = true;
emit finished(_data);
}
}, false);
}
checkFinished();
}
void BatchLoader::checkFinished() {
if (!_finished && _urls.size() == _data.size()) {
_finished = true;
emit finished(_data);
}
}

View file

@ -19,6 +19,8 @@
#include <QString>
#include <QUrl>
#include <mutex>
class BatchLoader : public QObject {
Q_OBJECT
public:
@ -37,6 +39,7 @@ private:
bool _finished;
QSet<QUrl> _urls;
QMap<QUrl, QString> _data;
std::mutex _dataLock;
};
#endif // hifi_BatchLoader_h