BackgroundLoader.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Copyright (c) 2008-2015 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. REFCOUNTED(BackgroundLoader);
  50. public:
  51. /// Construct.
  52. BackgroundLoader(ResourceCache* owner);
  53. /// Resource background loading loop.
  54. virtual void ThreadFunction();
  55. /// 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).
  56. bool QueueResource(StringHash type, const String& name, bool sendEventOnFailure, Resource* caller);
  57. /// Wait and finish possible loading of a resource when being requested from the cache.
  58. void WaitForResource(StringHash type, StringHash nameHash);
  59. /// Process resources that are ready to finish.
  60. void FinishResources(int maxMs);
  61. /// Return amount of resources in the load queue.
  62. unsigned GetNumQueuedResources() const;
  63. private:
  64. /// Finish one background loaded resource.
  65. void FinishBackgroundLoading(BackgroundLoadItem& item);
  66. /// Resource cache.
  67. ResourceCache* owner_;
  68. /// Mutex for thread-safe access to the background load queue.
  69. mutable Mutex backgroundLoadMutex_;
  70. /// Resources that are queued for background loading.
  71. HashMap<Pair<StringHash, StringHash>, BackgroundLoadItem> backgroundLoadQueue_;
  72. };
  73. }