CmResource.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "CmResource.h"
  2. #include "CmResourceRTTI.h"
  3. #include "CmUUID.h"
  4. #include "CmRenderSystem.h"
  5. namespace CamelotEngine
  6. {
  7. CM_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mResourceLoadedCondition, Resource)
  8. CM_STATIC_MUTEX_CLASS_INSTANCE(mResourceLoadedMutex, Resource)
  9. Resource::Resource()
  10. :mSize(0), mIsInitialized(false)
  11. {
  12. // We always generate a random UUID, and then overwrite it with the actual one
  13. // during loading if one was previously generated and saved.
  14. mUUID = UUIDGenerator::generateRandom();
  15. }
  16. void Resource::initialize_internal()
  17. {
  18. {
  19. CM_LOCK_MUTEX(mResourceLoadedMutex);
  20. mIsInitialized = true;
  21. }
  22. CM_THREAD_NOTIFY_ALL(mResourceLoadedCondition);
  23. }
  24. void Resource::destroy(bool internalCall)
  25. {
  26. if(internalCall)
  27. destroy_internal();
  28. else
  29. RenderSystem::instancePtr()->queueCommand(boost::bind(&Texture::destroy_internal, this));
  30. }
  31. void Resource::waitUntilInitialized()
  32. {
  33. #if CM_DEBUG_MODE
  34. if(CM_THREAD_CURRENT_ID == RenderSystem::instancePtr()->getRenderThreadId())
  35. CM_EXCEPT(InternalErrorException, "You cannot call this method on the render thread. It will cause a deadlock!");
  36. #endif
  37. if(!mIsInitialized)
  38. {
  39. CM_LOCK_MUTEX_NAMED(mResourceLoadedMutex, lock);
  40. while(!mIsInitialized)
  41. {
  42. CM_THREAD_WAIT(mResourceLoadedCondition, mResourceLoadedMutex, lock);
  43. }
  44. }
  45. }
  46. RTTITypeBase* Resource::getRTTIStatic()
  47. {
  48. return ResourceRTTI::instance();
  49. }
  50. RTTITypeBase* Resource::getRTTI() const
  51. {
  52. return Resource::getRTTIStatic();
  53. }
  54. }