BackgroundLoader.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "HashMap.h"
  24. #include "HashSet.h"
  25. #include "Mutex.h"
  26. #include "Ptr.h"
  27. #include "RefCounted.h"
  28. #include "StringHash.h"
  29. #include "Thread.h"
  30. namespace Urho3D
  31. {
  32. class Resource;
  33. class ResourceCache;
  34. /// Queue item for background loading of a resource.
  35. struct BackgroundLoadItem
  36. {
  37. /// Resource.
  38. SharedPtr<Resource> resource_;
  39. /// Resources depended on for loading.
  40. HashSet<Pair<StringHash, StringHash> > dependencies_;
  41. /// Resources that depend on this resource's loading.
  42. HashSet<Pair<StringHash, StringHash> > dependents_;
  43. /// Whether to send failure event.
  44. bool sendEventOnFailure_;
  45. };
  46. /// Background loader of resources. Owned by the ResourceCache.
  47. class BackgroundLoader : public RefCounted, public Thread
  48. {
  49. public:
  50. /// Construct.
  51. BackgroundLoader(ResourceCache* owner);
  52. /// Resource background loading loop.
  53. virtual void ThreadFunction();
  54. /// Queue loading of a resource. The name must be sanitated to ensure consistent format. Return true if queued (not a duplicate and resource was a known type).
  55. bool QueueResource(StringHash type, const String& name, bool sendEventOnFailure, Resource* caller);
  56. /// Wait and finish possible loading of a resource when being requested from the cache.
  57. void WaitForResource(StringHash type, StringHash nameHash);
  58. /// Process resources that are ready to finish.
  59. void FinishResources(int maxMs);
  60. /// Return amount of resources in the load queue.
  61. unsigned GetNumQueuedResources() const;
  62. private:
  63. /// Finish one background loaded resource.
  64. void FinishBackgroundLoading(BackgroundLoadItem& item);
  65. /// Resource cache.
  66. ResourceCache* owner_;
  67. /// Mutex for thread-safe access to the background load queue.
  68. mutable Mutex backgroundLoadMutex_;
  69. /// Resources that are queued for background loading.
  70. HashMap<Pair<StringHash, StringHash>, BackgroundLoadItem> backgroundLoadQueue_;
  71. };
  72. }