Browse Source

Added Pause() & Resume() functions to WorkQueue.

Lasse Öörni 14 years ago
parent
commit
f2b5b45f59
2 changed files with 30 additions and 7 deletions
  1. 24 5
      Engine/Core/WorkQueue.cpp
  2. 6 2
      Engine/Core/WorkQueue.h

+ 24 - 5
Engine/Core/WorkQueue.cpp

@@ -45,14 +45,13 @@ WorkQueue::WorkQueue(Context* context) :
 WorkQueue::~WorkQueue()
 WorkQueue::~WorkQueue()
 {
 {
     // Stop the worker threads. First make sure they are not waiting for work items
     // Stop the worker threads. First make sure they are not waiting for work items
+    shutDown_ = true;
     if (paused_)
     if (paused_)
     {
     {
         queueMutex_.Release();
         queueMutex_.Release();
         paused_ = false;
         paused_ = false;
     }
     }
     
     
-    shutDown_ = true;
-    
     for (unsigned i = 0; i < threads_.Size(); ++i)
     for (unsigned i = 0; i < threads_.Size(); ++i)
         threads_[i]->Stop();
         threads_[i]->Stop();
 }
 }
@@ -65,8 +64,7 @@ void WorkQueue::CreateThreads(unsigned numThreads)
         return;
         return;
     
     
     // Start threads in paused mode
     // Start threads in paused mode
-    queueMutex_.Acquire();
-    paused_ = true;
+    Pause();
     
     
     for (unsigned i = 0; i < numThreads; ++i)
     for (unsigned i = 0; i < numThreads; ++i)
     {
     {
@@ -97,10 +95,31 @@ void WorkQueue::AddWorkItem(const WorkItem& item)
         item.workFunction_(&item, 0);
         item.workFunction_(&item, 0);
 }
 }
 
 
+void WorkQueue::Pause()
+{
+    if (!paused_)
+    {
+        queueMutex_.Acquire();
+        paused_ = true;
+    }
+}
+
+void WorkQueue::Resume()
+{
+    if (paused_)
+    {
+        queueMutex_.Release();
+        paused_ = false;
+    }
+}
+
+
 void WorkQueue::Complete()
 void WorkQueue::Complete()
 {
 {
     if (threads_.Size())
     if (threads_.Size())
     {
     {
+        Resume();
+        
         for (;;)
         for (;;)
         {
         {
             queueMutex_.Acquire();
             queueMutex_.Acquire();
@@ -119,7 +138,7 @@ void WorkQueue::Complete()
                 {
                 {
                     // All work items are done. Leave the mutex locked and re-enter pause mode
                     // All work items are done. Leave the mutex locked and re-enter pause mode
                     paused_ = true;
                     paused_ = true;
-                    break;
+                    return;
                 }
                 }
             }
             }
         }
         }

+ 6 - 2
Engine/Core/WorkQueue.h

@@ -58,9 +58,13 @@ public:
     
     
     /// Create worker threads. Can only be called once.
     /// Create worker threads. Can only be called once.
     void CreateThreads(unsigned numThreads);
     void CreateThreads(unsigned numThreads);
-    /// Add a work item and unpause. If no threads, will process it immediately.
+    /// Add a work item and resume work. If no threads, will process it immediately.
     void AddWorkItem(const WorkItem& item);
     void AddWorkItem(const WorkItem& item);
-    /// Finish all current work items and pause.
+    /// Pause work.
+    void Pause();
+    /// Resume work.
+    void Resume();
+    /// Finish all queued work, then pause.
     void Complete();
     void Complete();
     
     
     /// Return number of worker threads.
     /// Return number of worker threads.