diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/inc/workqueue.h | 11 | ||||
-rw-r--r-- | utils/src/workqueue.cpp | 35 |
2 files changed, 42 insertions, 4 deletions
diff --git a/utils/inc/workqueue.h b/utils/inc/workqueue.h index 584803e..51a0fdc 100644 --- a/utils/inc/workqueue.h +++ b/utils/inc/workqueue.h @@ -32,9 +32,11 @@ public: */ ~WorkQueue(); - /* start & stop work thread */ - int StartWork(void); + /* start & stop & pause & resume work thread */ + int StartWork(bool executing); void StopWork(void); + void PauseWork(void); + void ResumeWork(void); /* the class inheriting WorkQueue uses this method */ void ScheduleWork(void); @@ -89,6 +91,11 @@ private: pthread_mutex_t wlock; pthread_cond_t wcond; + /* executing & pause */ + bool executing; + pthread_mutex_t executing_lock; + pthread_cond_t executing_wait; + int stop; }; diff --git a/utils/src/workqueue.cpp b/utils/src/workqueue.cpp index 132def7..153ca94 100644 --- a/utils/src/workqueue.cpp +++ b/utils/src/workqueue.cpp @@ -11,10 +11,14 @@ WorkQueue::WorkQueue() { stop = false; + executing = true; works = NULL; pthread_mutex_init(&wlock, NULL); pthread_cond_init(&wcond, NULL); + + pthread_mutex_init(&executing_lock, NULL); + pthread_cond_init(&executing_wait, NULL); } WorkQueue::~WorkQueue() @@ -30,13 +34,18 @@ WorkQueue::~WorkQueue() pthread_mutex_destroy(&wlock); } -int WorkQueue::StartWork(void) +int WorkQueue::StartWork(bool executing) { + if (!executing) + PauseWork(); + return Start(); } void WorkQueue::StopWork(void) { + ResumeWork(); + FlushWork(); pthread_mutex_lock(&wlock); @@ -47,6 +56,21 @@ void WorkQueue::StopWork(void) Join(); } +void WorkQueue::PauseWork(void) +{ + pthread_mutex_lock(&executing_lock); + executing = false; + pthread_mutex_unlock(&executing_lock); +} + +void WorkQueue::ResumeWork(void) +{ + pthread_mutex_lock(&executing_lock); + executing = true; + pthread_cond_signal(&executing_wait); + pthread_mutex_unlock(&executing_lock); +} + void WorkQueue::Run(void) { while (!stop) { @@ -74,8 +98,15 @@ void WorkQueue::Run(void) void WorkQueue::DoWork(WorkableInterface *wi) { - if (wi) + if (wi) { + pthread_mutex_lock(&executing_lock); + if (!executing) + pthread_cond_wait(&executing_wait, &executing_lock); + wi->Work(); + + pthread_mutex_unlock(&executing_lock); + } } void WorkQueue::Work(void) |