AsyncLoader.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "anki/core/AsyncLoader.h"
  2. #include "anki/core/Logger.h"
  3. #include "anki/core/App.h"
  4. #include "anki/core/Globals.h"
  5. namespace anki {
  6. //==============================================================================
  7. // start =
  8. //==============================================================================
  9. void AsyncLoader::start()
  10. {
  11. ANKI_INFO("Starting async loader thread...");
  12. thread = boost::thread(&AsyncLoader::workingFunc, this);
  13. }
  14. //==============================================================================
  15. // load =
  16. //==============================================================================
  17. void AsyncLoader::load(const char* filename, LoadCallback loadCallback,
  18. void* storage)
  19. {
  20. ANKI_INFO("New load request for \"" << filename << "\"");
  21. boost::mutex::scoped_lock lock(mutexReq);
  22. Request f = {filename, loadCallback, storage};
  23. requests.push_back(f);
  24. lock.unlock();
  25. condVar.notify_one();
  26. }
  27. //==============================================================================
  28. // workingFunc =
  29. //==============================================================================
  30. void AsyncLoader::workingFunc()
  31. {
  32. while(1)
  33. {
  34. Request req;
  35. // Wait for something
  36. {
  37. boost::mutex::scoped_lock lock(mutexReq);
  38. while(requests.empty())
  39. {
  40. ANKI_INFO("Waiting...");
  41. condVar.wait(lock);
  42. }
  43. req = requests.front();
  44. requests.pop_front();
  45. }
  46. // Exec the loader
  47. bool ok = true;
  48. try
  49. {
  50. req.loadCallback(req.filename.c_str(), req.storage);
  51. ANKI_INFO("File \"" << req.filename << "\" loaded");
  52. }
  53. catch(std::exception& e)
  54. {
  55. ANKI_ERROR("Loading \"" << req.filename <<
  56. "\" failed: " << e.what());
  57. ok = false;
  58. }
  59. // Put back the response
  60. {
  61. boost::mutex::scoped_lock lock(mutexResp);
  62. Response resp = {req.filename, req.storage, ok};
  63. responses.push_back(resp);
  64. }
  65. } // end thread loop
  66. }
  67. //==============================================================================
  68. // pollForFinished =
  69. //==============================================================================
  70. bool AsyncLoader::pollForFinished(std::string& filename, void* buff, bool& ok)
  71. {
  72. boost::mutex::scoped_lock lock(mutexResp);
  73. if(responses.empty())
  74. {
  75. return false;
  76. }
  77. Response resp = responses.front();
  78. responses.pop_front();
  79. lock.unlock();
  80. filename = resp.filename;
  81. buff = resp.storage;
  82. ok = resp.ok;
  83. return true;
  84. }
  85. } // end namespace