WorkQueue.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "List.h"
  25. #include "Mutex.h"
  26. #include "Object.h"
  27. namespace Urho3D
  28. {
  29. class WorkerThread;
  30. /// Work queue item.
  31. struct WorkItem
  32. {
  33. /// Work function. Called with the work item and thread index (0 = main thread) as parameters.
  34. void (*workFunction_)(const WorkItem*, unsigned);
  35. /// Data start pointer.
  36. void* start_;
  37. /// Data end pointer.
  38. void* end_;
  39. /// Auxiliary data pointer.
  40. void* aux_;
  41. };
  42. /// Work queue subsystem for multithreading.
  43. class WorkQueue : public Object
  44. {
  45. OBJECT(WorkQueue);
  46. friend class WorkerThread;
  47. public:
  48. /// Construct.
  49. WorkQueue(Context* context);
  50. /// Destruct.
  51. ~WorkQueue();
  52. /// Create worker threads. Can only be called once.
  53. void CreateThreads(unsigned numThreads);
  54. /// Add a work item and resume work. If no threads, will process it immediately.
  55. void AddWorkItem(const WorkItem& item);
  56. /// Pause work.
  57. void Pause();
  58. /// Resume work.
  59. void Resume();
  60. /// Finish all queued work, then pause.
  61. void Complete();
  62. /// Return number of worker threads.
  63. unsigned GetNumThreads() const { return threads_.Size(); }
  64. /// Return whether all work is completed.
  65. bool IsCompleted() const;
  66. private:
  67. /// Process work items until shut down. Called by the worker threads.
  68. void ProcessItems(unsigned threadIndex);
  69. /// Worker threads.
  70. Vector<SharedPtr<WorkerThread> > threads_;
  71. /// Work item queue.
  72. List<WorkItem> queue_;
  73. /// Queue mutex.
  74. Mutex queueMutex_;
  75. /// Number of threads working on an item.
  76. volatile unsigned numActive_;
  77. /// Shutting down flag.
  78. volatile bool shutDown_;
  79. /// Pausing flag. Indicates the worker threads should not contend for the queue mutex.
  80. volatile bool pausing_;
  81. /// Paused flag. Indicates the queue mutex being locked to prevent worker threads using up CPU time.
  82. bool paused_;
  83. };
  84. }