diff options
author | Ho-Eun Ryu <ho-eun.ryu@windriver.com> | 2009-10-30 12:40:42 +0900 |
---|---|---|
committer | Patrick Tjin <pattjin@google.com> | 2014-07-21 22:03:33 -0700 |
commit | 766e767ab0e2e46ca93175bc3debf23963944cd4 (patch) | |
tree | 04e5eade913e700c998bb5e8783df41fb4a52140 /utils | |
parent | d3ca9b0c0fa0f09f3bf45dbf237ee67f685a3b94 (diff) | |
download | wrs_omxil_core-766e767ab0e2e46ca93175bc3debf23963944cd4.tar.gz |
utils:workqueue: add Pause/Resume for workqueue control
we have following workqueue control functions
int StartWork(bool executing);
executing == true : start workqueue thread with executing state
!= true : start workqueue thread with paused state
void StopWork(void);
join workqueue thread and exit
void PauseWork(void);
pause workqueue thread, it guarantees any workqueue callback's not
running when it returns.
void ResumeWork(void);
resume paused workqueue thread
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) |