OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
workqueue.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_SCENEUTIL_WORKQUEUE_H
2 #define OPENMW_COMPONENTS_SCENEUTIL_WORKQUEUE_H
3 
4 #include <OpenThreads/Atomic>
5 #include <OpenThreads/Mutex>
6 #include <OpenThreads/Condition>
7 #include <OpenThreads/Thread>
8 
9 #include <osg/Referenced>
10 #include <osg/ref_ptr>
11 
12 #include <queue>
13 
14 namespace SceneUtil
15 {
16 
17  class WorkItem : public osg::Referenced
18  {
19  public:
20  WorkItem();
21  virtual ~WorkItem();
22 
24  virtual void doWork() {}
25 
26  bool isDone() const;
27 
29  void waitTillDone();
30 
32  void signalDone();
33 
35  virtual void abort() {}
36 
37  protected:
38  OpenThreads::Atomic mDone;
39  OpenThreads::Mutex mMutex;
40  OpenThreads::Condition mCondition;
41  };
42 
43  class WorkThread;
44 
48  class WorkQueue : public osg::Referenced
49  {
50  public:
51  WorkQueue(int numWorkerThreads=1);
52  ~WorkQueue();
53 
57  void addWorkItem(osg::ref_ptr<WorkItem> item, bool front=false);
58 
62  osg::ref_ptr<WorkItem> removeWorkItem();
63 
64  unsigned int getNumItems() const;
65 
66  unsigned int getNumActiveThreads() const;
67 
68  private:
70  std::deque<osg::ref_ptr<WorkItem> > mQueue;
71 
72  mutable OpenThreads::Mutex mMutex;
73  OpenThreads::Condition mCondition;
74 
75  std::vector<WorkThread*> mThreads;
76  };
77 
79  class WorkThread : public OpenThreads::Thread
80  {
81  public:
82  WorkThread(WorkQueue* workQueue);
83 
84  virtual void run();
85 
86  bool isActive() const;
87 
88  private:
90  volatile bool mActive;
91  };
92 
93 
94 }
95 
96 #endif
Internally used by WorkQueue.
Definition: workqueue.hpp:79
virtual void run()
Definition: workqueue.cpp:126
virtual void abort()
Set abort flag in order to return from doWork() as soon as possible. May not be respected by all Work...
Definition: workqueue.hpp:35
A work queue that users can push work items onto, to be completed by one or more background threads...
Definition: workqueue.hpp:48
~WorkQueue()
Definition: workqueue.cpp:53
std::vector< WorkThread * > mThreads
Definition: workqueue.hpp:75
WorkQueue * mWorkQueue
Definition: workqueue.hpp:89
WorkThread(WorkQueue *workQueue)
Definition: workqueue.cpp:120
WorkItem()
Definition: workqueue.cpp:29
void waitTillDone()
Wait until the work is completed. Usually called from the main thread.
Definition: workqueue.cpp:8
unsigned int getNumActiveThreads() const
Definition: workqueue.cpp:109
osg::ref_ptr< WorkItem > removeWorkItem()
Definition: workqueue.cpp:86
bool mIsReleased
Definition: workqueue.hpp:69
OpenThreads::Mutex mMutex
Definition: workqueue.hpp:39
Definition: workqueue.hpp:17
volatile bool mActive
Definition: workqueue.hpp:90
OpenThreads::Condition mCondition
Definition: workqueue.hpp:73
unsigned int getNumItems() const
Definition: workqueue.cpp:103
bool isDone() const
Definition: workqueue.cpp:37
OpenThreads::Mutex mMutex
Definition: workqueue.hpp:72
virtual void doWork()
Override in a derived WorkItem to perform actual work.
Definition: workqueue.hpp:24
OpenThreads::Atomic mDone
Definition: workqueue.hpp:38
WorkQueue(int numWorkerThreads=1)
Definition: workqueue.cpp:42
void addWorkItem(osg::ref_ptr< WorkItem > item, bool front=false)
Definition: workqueue.cpp:70
OpenThreads::Condition mCondition
Definition: workqueue.hpp:40
virtual ~WorkItem()
Definition: workqueue.cpp:33
std::deque< osg::ref_ptr< WorkItem > > mQueue
Definition: workqueue.hpp:70
bool isActive() const
Definition: workqueue.cpp:140
void signalDone()
Internal use by the WorkQueue.
Definition: workqueue.cpp:20