Fixed a deadlock regression in 4.3.0RC2 when bitmap texture cache was released
@@ -12,7 +12,7 @@ namespace carto {
}
GLResourceManager::~GLResourceManager() {
- if (std::this_thread::get_id() == _glThreadId) {
+ if (std::this_thread::get_id() == getGLThreadId()) {
for (const std::unique_ptr<GLResource>& resource : _deleteQueue) {
resource->destroy();
}
@@ -25,39 +25,35 @@ namespace carto {
std::thread::id GLResourceManager::getGLThreadId() const {
std::lock_guard<std::mutex> lock(_mutex);
-
return _glThreadId;
}
void GLResourceManager::setGLThreadId(std::thread::id id) {
std::lock_guard<std::mutex> lock(_mutex);
-
_glThreadId = id;
}
void GLResourceManager::processResources() {
+ if (std::this_thread::get_id() != getGLThreadId()) {
+ Log::Warn("GLResourceManager::processResources: Method called from wrong thread!");
+ return;
+ }
+
std::vector<std::weak_ptr<GLResource> > createQueue;
std::vector<std::unique_ptr<GLResource> > deleteQueue;
{
std::lock_guard<std::mutex> lock(_mutex);
-
- if (std::this_thread::get_id() != _glThreadId) {
- Log::Warn("GLResourceManager::processResources: Method called from wrong thread!");
- return;
+ std::swap(deleteQueue, _deleteQueue);
+ std::swap(createQueue, _createQueue);
}
-
- for (const std::unique_ptr<GLResource>& resource : _deleteQueue) {
+ for (const std::unique_ptr<GLResource>& resource : deleteQueue) {
resource->destroy();
}
- std::swap(deleteQueue, _deleteQueue);
-
- for (const std::weak_ptr<GLResource>& resourceWeak : _createQueue) {
+ for (const std::weak_ptr<GLResource>& resourceWeak : createQueue) {
if (auto resource = resourceWeak.lock()) {
resource->create();
}
}
- std::swap(createQueue, _createQueue); // release the resources only after lock is released
- }
}
std::shared_ptr<GLResource> GLResourceManager::registerResource(GLResource* resourcePtr) {
@@ -73,23 +69,21 @@ namespace carto {
throw;
}
- std::lock_guard<std::mutex> lock(_mutex);
-
- if (std::this_thread::get_id() == _glThreadId) {
+ if (std::this_thread::get_id() == getGLThreadId()) {
resource->create();
} else {
+ std::lock_guard<std::mutex> lock(_mutex);
_createQueue.push_back(resource);
}
return resource;
}
void GLResourceManager::deleteResource(std::unique_ptr<GLResource> resource) {
- std::lock_guard<std::mutex> lock(_mutex);
-
if (resource) {
- if (std::this_thread::get_id() == _glThreadId) {
+ if (std::this_thread::get_id() == getGLThreadId()) {
resource->destroy();
} else {
+ std::lock_guard<std::mutex> lock(_mutex);
_deleteQueue.push_back(std::move(resource));
}
}
