BackgroundLoader.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // Copyright (c) 2008-2016 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 "../Container/HashMap.h"
  24. #include "../Container/HashSet.h"
  25. #include "../Core/Mutex.h"
  26. #include "../Container/Ptr.h"
  27. #include "../Container/RefCounted.h"
  28. #include "../Core/Thread.h"
  29. #include "../Math/StringHash.h"
  30. namespace Atomic
  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. ATOMIC_REFCOUNTED(BackgroundLoader
  50. )
  51. public:
  52. /// Construct.
  53. BackgroundLoader(ResourceCache* owner);
  54. /// Destruct. Forcibly clear the load queue.
  55. ~BackgroundLoader();
  56. /// Resource background loading loop.
  57. virtual void ThreadFunction();
  58. /// 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).
  59. bool QueueResource(StringHash type, const String& name, bool sendEventOnFailure, Resource* caller);
  60. /// Wait and finish possible loading of a resource when being requested from the cache.
  61. void WaitForResource(StringHash type, StringHash nameHash);
  62. /// Process resources that are ready to finish.
  63. void FinishResources(int maxMs);
  64. /// Return amount of resources in the load queue.
  65. unsigned GetNumQueuedResources() const;
  66. private:
  67. /// Finish one background loaded resource.
  68. void FinishBackgroundLoading(BackgroundLoadItem& item);
  69. /// Resource cache.
  70. ResourceCache* owner_;
  71. /// Mutex for thread-safe access to the background load queue.
  72. mutable Mutex backgroundLoadMutex_;
  73. /// Resources that are queued for background loading.
  74. HashMap<Pair<StringHash, StringHash>, BackgroundLoadItem> backgroundLoadQueue_;
  75. };
  76. }