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 
34  protected:
35  OpenThreads::Atomic mDone;
36  OpenThreads::Mutex mMutex;
37  OpenThreads::Condition mCondition;
38  };
39 
40  class WorkThread;
41 
45  class WorkQueue : public osg::Referenced
46  {
47  public:
48  WorkQueue(int numWorkerThreads=1);
49  ~WorkQueue();
50 
54  void addWorkItem(osg::ref_ptr<WorkItem> item, bool front=false);
55 
59  osg::ref_ptr<WorkItem> removeWorkItem();
60 
61  private:
63  std::deque<osg::ref_ptr<WorkItem> > mQueue;
64 
65  OpenThreads::Mutex mMutex;
66  OpenThreads::Condition mCondition;
67 
68  std::vector<WorkThread*> mThreads;
69  };
70 
72  class WorkThread : public OpenThreads::Thread
73  {
74  public:
75  WorkThread(WorkQueue* workQueue);
76 
77  virtual void run();
78 
79  private:
81  };
82 
83 
84 }
85 
86 #endif
Internally used by WorkQueue.
Definition: workqueue.hpp:72
virtual void run()
Definition: workqueue.cpp:108
A work queue that users can push work items onto, to be completed by one or more background threads...
Definition: workqueue.hpp:45
~WorkQueue()
Definition: workqueue.cpp:53
std::vector< WorkThread * > mThreads
Definition: workqueue.hpp:68
WorkQueue * mWorkQueue
Definition: workqueue.hpp:80
WorkThread(WorkQueue *workQueue)
Definition: workqueue.cpp:103
WorkItem()
Definition: workqueue.cpp:29
void waitTillDone()
Wait until the work is completed. Usually called from the main thread.
Definition: workqueue.cpp:8
osg::ref_ptr< WorkItem > removeWorkItem()
Definition: workqueue.cpp:86
bool mIsReleased
Definition: workqueue.hpp:62
OpenThreads::Mutex mMutex
Definition: workqueue.hpp:36
Definition: workqueue.hpp:17
OpenThreads::Condition mCondition
Definition: workqueue.hpp:66
bool isDone() const
Definition: workqueue.cpp:37
OpenThreads::Mutex mMutex
Definition: workqueue.hpp:65
virtual void doWork()
Override in a derived WorkItem to perform actual work.
Definition: workqueue.hpp:24
OpenThreads::Atomic mDone
Definition: workqueue.hpp:35
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:37
virtual ~WorkItem()
Definition: workqueue.cpp:33
std::deque< osg::ref_ptr< WorkItem > > mQueue
Definition: workqueue.hpp:63
void signalDone()
Internal use by the WorkQueue.
Definition: workqueue.cpp:20