From 4061e14c2d11b03d47186e161dfd445b706ba3dc Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 4 Feb 2015 13:24:37 -0800 Subject: [PATCH 1/3] move static members of ResourceManager into DependencyManager --- interface/src/Application.cpp | 7 ++++++ libraries/networking/src/ResourceCache.cpp | 21 +++++++++--------- libraries/networking/src/ResourceCache.h | 25 ++++++++++++++++++---- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f1ead1ac4f..3c1806b00b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -73,6 +73,7 @@ #include #include #include +#include #include #include #include @@ -188,6 +189,7 @@ bool setupEssentials(int& argc, char** argv) { auto jsConsole = DependencyManager::set(); auto dialogsManager = DependencyManager::set(); auto bandwidthRecorder = DependencyManager::set(); + auto resouceCacheSharedItems = DependencyManager::set(); #if defined(Q_OS_MAC) || defined(Q_OS_WIN) auto speechRecognizer = DependencyManager::set(); #endif @@ -514,6 +516,11 @@ Application::~Application() { _myAvatar = NULL; DependencyManager::destroy(); + DependencyManager::destroy(); + DependencyManager::destroy(); + DependencyManager::destroy(); + DependencyManager::destroy(); + DependencyManager::destroy(); } void Application::initializeGL() { diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 613eb93228..59275ea8e3 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -112,27 +112,31 @@ void ResourceCache::reserveUnusedResource(qint64 resourceSize) { } void ResourceCache::attemptRequest(Resource* resource) { + auto sharedItems = DependencyManager::get(); if (_requestLimit <= 0) { // wait until a slot becomes available - _pendingRequests.append(resource); + sharedItems->_pendingRequests.append(resource); return; } _requestLimit--; - _loadingRequests.append(resource); + sharedItems->_loadingRequests.append(resource); resource->makeRequest(); } void ResourceCache::requestCompleted(Resource* resource) { - _loadingRequests.removeOne(resource); + + auto sharedItems = DependencyManager::get(); + sharedItems->_loadingRequests.removeOne(resource); _requestLimit++; // look for the highest priority pending request int highestIndex = -1; float highestPriority = -FLT_MAX; - for (int i = 0; i < _pendingRequests.size(); ) { - Resource* resource = _pendingRequests.at(i).data(); + int pendingRequests = sharedItems->_pendingRequests.size(); + for (int i = 0; i < pendingRequests; ) { + Resource* resource = sharedItems->_pendingRequests.at(i).data(); if (!resource) { - _pendingRequests.removeAt(i); + sharedItems->_pendingRequests.removeAt(i); continue; } float priority = resource->getLoadPriority(); @@ -143,16 +147,13 @@ void ResourceCache::requestCompleted(Resource* resource) { i++; } if (highestIndex >= 0) { - attemptRequest(_pendingRequests.takeAt(highestIndex)); + attemptRequest(sharedItems->_pendingRequests.takeAt(highestIndex)); } } const int DEFAULT_REQUEST_LIMIT = 10; int ResourceCache::_requestLimit = DEFAULT_REQUEST_LIMIT; -QList > ResourceCache::_pendingRequests; -QList ResourceCache::_loadingRequests; - Resource::Resource(const QUrl& url, bool delayLoad) : _url(url), _request(url) { diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h index 519b205d46..62fca71ea5 100644 --- a/libraries/networking/src/ResourceCache.h +++ b/libraries/networking/src/ResourceCache.h @@ -22,6 +22,8 @@ #include #include +#include + class QNetworkReply; class QTimer; @@ -40,6 +42,21 @@ static const qint64 DEFAULT_UNUSED_MAX_SIZE = 1024 * BYTES_PER_MEGABYTES; static const qint64 MIN_UNUSED_MAX_SIZE = 0; static const qint64 MAX_UNUSED_MAX_SIZE = 10 * BYTES_PER_GIGABYTES; +// We need to make sure that these items are available for all instances of +// ResourceCache derived classes. Since we can't count on the ordering of +// static members destruction, we need to use this Dependency manager implemented +// object instead +class ResouceCacheSharedItems : public Dependency { + SINGLETON_DEPENDENCY +public: + QList > _pendingRequests; + QList _loadingRequests; +private: + ResouceCacheSharedItems() { } + virtual ~ResouceCacheSharedItems() { } +}; + + /// Base class for resource caches. class ResourceCache : public QObject { Q_OBJECT @@ -51,9 +68,11 @@ public: void setUnusedResourceCacheSize(qint64 unusedResourcesMaxSize); qint64 getUnusedResourceCacheSize() const { return _unusedResourcesMaxSize; } - static const QList& getLoadingRequests() { return _loadingRequests; } + static const QList& getLoadingRequests() + { return DependencyManager::get()->_loadingRequests; } - static int getPendingRequestCount() { return _pendingRequests.size(); } + static int getPendingRequestCount() + { return DependencyManager::get()->_pendingRequests.size(); } ResourceCache(QObject* parent = NULL); virtual ~ResourceCache(); @@ -90,8 +109,6 @@ private: int _lastLRUKey = 0; static int _requestLimit; - static QList > _pendingRequests; - static QList _loadingRequests; }; /// Base class for resources. From 698fcd81164997dc29eaf892426c826b3e99914e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 4 Feb 2015 13:54:14 -0800 Subject: [PATCH 2/3] fix crash on shutdown --- libraries/networking/src/ResourceCache.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 59275ea8e3..7eadb0a3dd 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -132,8 +132,7 @@ void ResourceCache::requestCompleted(Resource* resource) { // look for the highest priority pending request int highestIndex = -1; float highestPriority = -FLT_MAX; - int pendingRequests = sharedItems->_pendingRequests.size(); - for (int i = 0; i < pendingRequests; ) { + for (int i = 0; i < sharedItems->_pendingRequests.size(); ) { Resource* resource = sharedItems->_pendingRequests.at(i).data(); if (!resource) { sharedItems->_pendingRequests.removeAt(i); From 8fd11e70ccb6cfcf20aeff8c9dbd6d4879896d1e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 4 Feb 2015 15:16:18 -0800 Subject: [PATCH 3/3] clamp dt for skipTimeForward to prevent near infinite loops --- libraries/entities/src/EntityItem.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 690e6d2c1a..eeb7282814 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -548,7 +548,10 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // is sending us data with a known "last simulated" time. That time is likely in the past, and therefore // this "new" data is actually slightly out of date. We calculate the time we need to skip forward and // use our simulation helper routine to get a best estimate of where the entity should be. - float skipTimeForward = (float)(now - _lastSimulated) / (float)(USECS_PER_SECOND); + const float MIN_TIME_SKIP = 0.0f; + const float MAX_TIME_SKIP = 1.0f; // in seconds + float skipTimeForward = glm::clamp((float)(now - _lastSimulated) / (float)(USECS_PER_SECOND), + MIN_TIME_SKIP, MAX_TIME_SKIP); if (skipTimeForward > 0.0f) { #ifdef WANT_DEBUG qDebug() << "skipTimeForward:" << skipTimeForward;