Jump to content

  • Log In with Google      Sign In   
  • Create Account

Multithreaded Resource Manager


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 nekitusan   Members   -  Reputation: 172

Like
0Likes
Like

Posted 28 November 2013 - 03:11 PM

I have some conceptual problems for a resource manager to be thread safe (can be called from any thread). The manager has a list of worker threads (one per bundle, a bundle is a zip file).
Traditionally, in a single-threaded manager we do something like this:
 
// sync load of a texture, loads and returns after texture is fully loaded
Texture* pTex = resourceManager()->load("maka.png");
// we got the texture, use it
int w = pTex->width();
int h = pTex->height();
// do something with w and h
But in an async manager how we do that?
Currently I'm using an observer list with callbacks for when a resource is completely loaded, the Observer is called and you can use the pointer to the resource. In this manager, resources are used as IDs, you do not have direct access to the pointer because you do not know in what state is at some point, it may be removed or modified from another thread.
ResourceId myTextureId = 3493; // or we can use: resourceId("maka.png") to get the id

struct MyResObserver : public IResourceObserver
{
    void onResourceLoaded(Resource* pRes)
    {
       Texture* pTex = dynamic_cast<Texture*>(pRes);
       
       if (pTex && pTex->id() == myTextureId)
       {
           int w = pTex->width();
           int h = pTex->height();
           // use w and h
       }
    }
} myObserver;

///....................

void someClass::someInit()
{
    // we add our observer into the manager's list
    resourceManager()->addObserver(&myObserver);
    // async loading, returns right away, the resource will be loaded later on
    resourceManager()->load(myTextureId);
    // or we could do: resourceManager()->load(someTextureId, &myObserver);
}

Any other ideas would be welcomed, thanks.

 



Sponsor:

#2 TheComet   Crossbones+   -  Reputation: 1642

Like
1Likes
Like

Posted 28 November 2013 - 03:25 PM

That seems clean to me. The other, less complicated way, would be to poll the resource and check if it has loaded yet or not. I believe this is the way Ogre handles it as well:

 

https://bitbucket.org/sinbad/ogre/src/28c23646a65374ea9b4cb27ae1365a7972f09f80/OgreMain/include/OgreResource.h?at=default


YOUR_OPINION >/dev/null

#3 Hodgman   Moderators   -  Reputation: 31800

Like
2Likes
Like

Posted 28 November 2013 - 03:29 PM

The main two choices are callbacks, or polling. You can use a callback across all assets as you've shown above, or you can attach callbacks to specific assets. Alternatively, you can allow the user of the resource manager to continually query whether an asset has finished loading yet or not.

onLoaded = ...
Asset* asset = resourceManager()->load(assetName, onLoaded);
void Scene::Precache()
{
  m_asset = resourceManager()->load(assetName);
}
bool Scene::PollLoaded()
{
  bool loaded = m_asset->IsLoaded();
  return loaded;
}

One issue that you have to be careful with regarding callbacks, is which thread calls the callback!

//main thread
struct Game
{
  Foo gamestate;
};
Game g_game;
...
struct { void operator()(Texture* tex) {
  g_game.gamestate.DoStuff(tex); // callback modifies the game-state directly, which is owned by the main thread!
}} onLoaded;
resourceManager()->load("foo.texture", onLoaded);
//Worker thread:
template<class Fn> void LoadTexture( const char* name, Fn& onLoaded )
{
  Texture* tex = ...
  //load the texture...
  onLoaded( tex ); // Uh oh! Worker thread calls the callback, which creates race conditions with the main thread!
}

To avoid these kinds of problems, if you require callbacks to be called by a certain thread, then the workers can queue them up, and then the main thread can call resourceManager->PollForCallbacks() etc, which goes ahead and executes all the callbacks that have been queued up by the workers.


Edited by Hodgman, 28 November 2013 - 03:30 PM.


#4 nekitusan   Members   -  Reputation: 172

Like
0Likes
Like

Posted 28 November 2013 - 03:36 PM

alright guys, thanks, Hodgman good point with the queue filled from any thread with "onloaded" messages to be consumed by the main thread.



#5 SeanMiddleditch   Members   -  Reputation: 7165

Like
1Likes
Like

Posted 28 November 2013 - 10:36 PM

The manager has a list of worker threads (one per bundle, a bundle is a zip file).

 

 

Why so many threads?  The I/O subsystem can only efficiently read one file at a time anyway.  There's not necessarily a lot of benefit to having more than one thread when I/O bound.

 

 

You might find it way, way easier to use something like std::function<> instead of an interface for your callbacks.  It can significantly reduce the verbosity of your code.  Most STL implementations will do a small-function optimization so that std::function<> doesn't allocate, and if you're hardcore about game frameworks you can make your own that has a fixed static size (and raises a compilation error rather accepting something needing memory allocation to store).

 

I have a more complex job system that the resource system uses.  Job can be dispatched to a named thread pool (with the resource system having a dedicated thread "pool" with only one thread), callbacks can be registered for individual jobs or for job groups, and the callback can be flagged as being destined for the main thread, any available thread in the pool, or the thread that registered the callback.  Threads can query a threadpool and execute any queued jobs/callbacks allowed on that thread, which the main thread of course does in its main loop and other threads can do at their discretion.  Worker threads in thread pools just deque items as their thread loop.  This system has a huge benefit outside of just resource loading, too, which is partly why something vaguely similar is likely to be added to C++17 or a concurrency TS before that.



#6 nekitusan   Members   -  Reputation: 172

Like
0Likes
Like

Posted 29 November 2013 - 04:06 AM

SeanMiddleditch, I was thinking of that, thread/bundle, and you're right, wouldn't help, even worse, since its serial reading from hdd, so I will have one thread for the resource manager, thanks for reassuring it :). I have also a job/task scheduler which will be used by the resource manager, but I think I will use polling since its more safe/clear for me at least, instead of callbacks (which I also support). I dont use STL :) for various reasons, more or less logical.



#7 AllEightUp   Moderators   -  Reputation: 4268

Like
3Likes
Like

Posted 29 November 2013 - 11:12 AM


Why so many threads?  The I/O subsystem can only efficiently read one file at a time anyway.  There's not necessarily a lot of benefit to having more than one thread when I/O bound.

 

Actually, while there can be only one active io operation at a time, it is not always a linear process through a specific file.  Using multiple threads is questionable but in the case of using something simplistic like fread, it can make sense and provide a performance boost.  The reason is that the underlying IO for just about every OS anymore is based on a scatter/gather solution which doesn't process the IO in the order you supply your requests, heck it doesn't even have to return the data in front to back order, you often get bits from the end/middle before you get the start of the file.  The IO is optimized to reduce physical head movement instead of sequential reading.  So, in the case of fread, if you have multiple freads outstanding (obviously from different threads) you generally get better throughput since the underlying IO will fulfill the requested reads in the order the head moves over the media, not the order you requested the files.  (NOTE: the IO requests will always complete and the OS's have a fairness system which will eventually cause any long standing request to complete even if it has to move the head to the other side to get it.)

 

Most OS's supply API's with better exposure of this underlying IO which, typically, when used means you can go back to a single threaded solution and get the gains.  Win32 supplies the overlapped IO flags, multiple source/target buffers and the oh so overly complicated completion ports (and buggy up till about Vista).  Most *nix variations have AIO or for Linux you have epoll which I believe is the latest/greatest version right now and BSD variations have kevent/kqueue.

 

There are of course variations, limitations and often goofball implementation details involved with all of these.  Fortunately, with a bit of work the result is quite often a pretty massive latency reduction and much higher utilization of the physical media throughput rates.  This even applies to SSD's due to how their access is performed by the controllers.  If dealing with this is worth it, that's up to your game, but really high performance resource systems generally need to use at least the lowest variation supplied which is just a bit more complicated than fread.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS