Background work thread improvement

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Post Reply
oiram
Halfling
Posts: 87
Joined: Sun Dec 13, 2009 5:06 am
x 6

Background work thread improvement

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
main thrad.jpg (23.02 KiB) Viewed 3775 times

"BA" in work thread
work thread.jpg
work thread.jpg (32.77 KiB) Viewed 3775 times

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 102 times
User avatar
c6burns
Beholder
Posts: 1512
Joined: Fri Feb 22, 2013 4:44 am
Location: Deep behind enemy lines
x 138

Re: Background work thread improvement

Post by c6burns »

This is a duplicate of this post: http://www.ogre3d.org/forums/viewtopic.php?f=22&t=81173

Maybe a JIRA or a pull request is in order?
robert82h
Gnoblar
Posts: 23
Joined: Thu Jun 13, 2013 5:41 pm
x 7

Re: Background work thread improvement

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.
Post Reply