Improvement: background work thread

Minor issues with the Ogre API that can be trivial to fix
Post Reply
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Improvement: background work thread

Post by oiram »

These day, I work for load resource in background thread for my project. I'm using "OGRE_THREAD_SUPPORT 2" and "OGRE_THREAD_PROVIDER 3".
Some issues I found below:
1. ResourceGroupManager::getSingleton().unloadResourceGroup is called in ResourceBackgroundQueue::handleRequest. However, resource cannot be unloaded in work thread. For unload resource group, It must be called in ResourceBackgroundQueue::handleResponse which is in main thread.

2. With ResourceGroupManager, I suggest remove OGRE_LOCK_AUTO_MUTEX with createResourceGroup, getResourceGroup and isResourceGroupInGlobalPool and add comment they are not thread safe, that means these three methods must be called in main thread. Another option is add more mutexes for different using like "add/remove ResourceGroupListener". Using only one OGRE_AUTO_MUTEX for all methods will cause deadlock and lag.

Deadlock situation generally because main thread lock ResourceGroupManager::mutex first and lock ResourceManager::mutex second, and work thread lock ResourceManager::mutex first and lock ResourceGroupManager::mutex second.

Call stack with main/work thread below.

"AB" in Main thread.
main thrad.jpg
"BA" in work thread
work thread.jpg
3. For smooth with loading single resources that prepared in work thread, and then load in main thread. If smooth with loading resource group, we can initialiseResourceGroup and prepareResourceGroup in work thread, and then load resource in this resource group one by one in main thread.
I wrote my own ResourceGroupBackgroundLoader class to do this, it works fine, code attached for your reference. Review is welcome. :P
resourceGroupBackgroundLoader.7z
(2.68 KiB) Downloaded 393 times
robert82h
Gnoblar
Posts: 23
Joined: Thu Jun 13, 2013 5:41 pm
x 7

Re: Improvement: background work thread

Post by robert82h »

There are more locking issues in Ogre's resource system when used with background threads. I don't know the ins and outs of Ogre's resource groups and resource system in general, but I figured I could get rid of the worst locking issues by releasing locks and relying on thread-safe archive implementations. See my post http://www.ogre3d.org/forums/viewtopic. ... 68#p507168 for details. (Note: duplicate thread at http://www.ogre3d.org/forums/viewtopic. ... 9&p=510821)
Post Reply