WorkQueue.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2008-2013 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 "List.h"
  24. #include "Mutex.h"
  25. #include "Object.h"
  26. namespace Urho3D
  27. {
  28. /// Work item completed event.
  29. EVENT(E_WORKITEMCOMPLETED, WorkItemCompleted)
  30. {
  31. PARAM(P_ITEM, Item); // WorkItem ptr
  32. }
  33. class WorkerThread;
  34. /// Work queue item.
  35. struct WorkItem
  36. {
  37. // Construct
  38. WorkItem() :
  39. priority_(M_MAX_UNSIGNED),
  40. sendEvent_(false),
  41. completed_(false)
  42. {
  43. }
  44. /// Work function. Called with the work item and thread index (0 = main thread) as parameters.
  45. void (*workFunction_)(const WorkItem*, unsigned);
  46. /// Data start pointer.
  47. void* start_;
  48. /// Data end pointer.
  49. void* end_;
  50. /// Auxiliary data pointer.
  51. void* aux_;
  52. /// Priority. Higher value = will be completed first.
  53. unsigned priority_;
  54. /// Whether to send event on completion.
  55. bool sendEvent_;
  56. /// Completed flag.
  57. volatile bool completed_;
  58. };
  59. /// Work queue subsystem for multithreading.
  60. class URHO3D_API WorkQueue : public Object
  61. {
  62. OBJECT(WorkQueue);
  63. friend class WorkerThread;
  64. public:
  65. /// Construct.
  66. WorkQueue(Context* context);
  67. /// Destruct.
  68. ~WorkQueue();
  69. /// Create worker threads. Can only be called once.
  70. void CreateThreads(unsigned numThreads);
  71. /// Add a work item and resume worker threads.
  72. void AddWorkItem(const WorkItem& item);
  73. /// Pause worker threads.
  74. void Pause();
  75. /// Resume worker threads.
  76. void Resume();
  77. /// Finish all queued work which has at least the specified priority. Main thread will also execute priority work. Pause worker threads if no more work remains.
  78. void Complete(unsigned priority);
  79. /// Return number of worker threads.
  80. unsigned GetNumThreads() const { return threads_.Size(); }
  81. /// Return whether all work with at least the specified priority is finished.
  82. bool IsCompleted(unsigned priority) const;
  83. private:
  84. /// Process work items until shut down. Called by the worker threads.
  85. void ProcessItems(unsigned threadIndex);
  86. /// Purge completed work items and send completion events as necessary.
  87. void PurgeCompleted();
  88. /// Handle frame start event. Purge completed work from the main thread queue, and perform work if no threads at all.
  89. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  90. /// Worker threads.
  91. Vector<SharedPtr<WorkerThread> > threads_;
  92. /// Work item collection. Accessed only by the main thread.
  93. List<WorkItem> workItems_;
  94. /// Work item prioritized queue for worker threads. Pointers are guaranteed to be valid (point to workItems.)
  95. List<WorkItem*> queue_;
  96. /// Worker queue mutex.
  97. Mutex queueMutex_;
  98. /// Shutting down flag.
  99. volatile bool shutDown_;
  100. /// Pausing flag. Indicates the worker threads should not contend for the queue mutex.
  101. volatile bool pausing_;
  102. /// Paused flag. Indicates the queue mutex being locked to prevent worker threads using up CPU time.
  103. bool paused_;
  104. };
  105. }