| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include "anki/core/AsyncLoader.h"
- #include "anki/core/Logger.h"
- #include "anki/core/App.h"
- #include "anki/core/Globals.h"
- namespace anki {
- //==============================================================================
- // start =
- //==============================================================================
- void AsyncLoader::start()
- {
- ANKI_INFO("Starting async loader thread...");
- thread = boost::thread(&AsyncLoader::workingFunc, this);
- }
- //==============================================================================
- // load =
- //==============================================================================
- void AsyncLoader::load(const char* filename, LoadCallback loadCallback,
- void* storage)
- {
- ANKI_INFO("New load request for \"" << filename << "\"");
- boost::mutex::scoped_lock lock(mutexReq);
- Request f = {filename, loadCallback, storage};
- requests.push_back(f);
- lock.unlock();
- condVar.notify_one();
- }
- //==============================================================================
- // workingFunc =
- //==============================================================================
- void AsyncLoader::workingFunc()
- {
- while(1)
- {
- Request req;
- // Wait for something
- {
- boost::mutex::scoped_lock lock(mutexReq);
- while(requests.empty())
- {
- ANKI_INFO("Waiting...");
- condVar.wait(lock);
- }
- req = requests.front();
- requests.pop_front();
- }
- // Exec the loader
- bool ok = true;
- try
- {
- req.loadCallback(req.filename.c_str(), req.storage);
- ANKI_INFO("File \"" << req.filename << "\" loaded");
- }
- catch(std::exception& e)
- {
- ANKI_ERROR("Loading \"" << req.filename <<
- "\" failed: " << e.what());
- ok = false;
- }
- // Put back the response
- {
- boost::mutex::scoped_lock lock(mutexResp);
- Response resp = {req.filename, req.storage, ok};
- responses.push_back(resp);
- }
- } // end thread loop
- }
- //==============================================================================
- // pollForFinished =
- //==============================================================================
- bool AsyncLoader::pollForFinished(std::string& filename, void* buff, bool& ok)
- {
- boost::mutex::scoped_lock lock(mutexResp);
- if(responses.empty())
- {
- return false;
- }
- Response resp = responses.front();
- responses.pop_front();
- lock.unlock();
- filename = resp.filename;
- buff = resp.storage;
- ok = resp.ok;
- return true;
- }
- } // end namespace
|