WorkQueue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. class WorkerThread;
  29. /// Work queue item.
  30. struct WorkItem
  31. {
  32. /// Work function. Called with the work item and thread index (0 = main thread) as parameters.
  33. void (*workFunction_)(const WorkItem*, unsigned);
  34. /// Data start pointer.
  35. void* start_;
  36. /// Data end pointer.
  37. void* end_;
  38. /// Auxiliary data pointer.
  39. void* aux_;
  40. };
  41. /// Work queue subsystem for multithreading.
  42. class WorkQueue : public Object
  43. {
  44. OBJECT(WorkQueue);
  45. friend class WorkerThread;
  46. public:
  47. /// Construct.
  48. WorkQueue(Context* context);
  49. /// Destruct.
  50. ~WorkQueue();
  51. /// Create worker threads. Can only be called once.
  52. void CreateThreads(unsigned numThreads);
  53. /// Add a work item and resume work. If no threads, will process it immediately.
  54. void AddWorkItem(const WorkItem& item);
  55. /// Pause work.
  56. void Pause();
  57. /// Resume work.
  58. void Resume();
  59. /// Finish all queued work, then pause.
  60. void Complete();
  61. /// Return number of worker threads.
  62. unsigned GetNumThreads() const { return threads_.Size(); }
  63. /// Return whether all work is completed.
  64. bool IsCompleted() const;
  65. private:
  66. /// Process work items until shut down. Called by the worker threads.
  67. void ProcessItems(unsigned threadIndex);
  68. /// Worker threads.
  69. Vector<SharedPtr<WorkerThread> > threads_;
  70. /// Work item queue.
  71. List<WorkItem> queue_;
  72. /// Queue mutex.
  73. Mutex queueMutex_;
  74. /// Number of threads working on an item.
  75. volatile unsigned numActive_;
  76. /// Shutting down flag.
  77. volatile bool shutDown_;
  78. /// Pausing flag. Indicates the worker threads should not contend for the queue mutex.
  79. volatile bool pausing_;
  80. /// Paused flag. Indicates the queue mutex being locked to prevent worker threads using up CPU time.
  81. bool paused_;
  82. };
  83. }