All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
TaskWorkPool.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "Tasks.h"
7 #include "TaskWorker.h"
8 #include "ActiveObject.h"
9 #include "ITaskPoolMessage.h"
10 #include "ITaskPoolMessageDispatcher.h"
11 #include "WorkItem.h"
12 #include "TaskWorkPoolState.h"
13 #include "ITaskWorkerRunner.h"
14 #include "ThreadedTaskWorkerRunner.h"
15 #include "IWorkPool.h"
16 #include "PauseWorkItem.h"
17 #include <vector>
18 
19 namespace Eegeo
20 {
21  namespace Concurrency
22  {
23  namespace Tasks
24  {
25  class TaskWorkPool : public Eegeo::Messaging::ActiveObject<Messages::ITaskPoolMessage*, Messages::ITaskPoolMessageDispatcher>, public IWorkPool
26  {
27  public:
30  std::vector<ITaskWorkerRunner*>& workers);
31 
32  ~TaskWorkPool();
33 
34  void QueueWork(WorkItem* workItem);
35 
36  TaskWorkPoolState::Values GetState() const;
37 
38  void Start();
39  void Stop();
40  void Shutdown();
41  void OnMessageHandled(Messages::ITaskPoolMessage* message);
42 
43  bool HandleMessage(const Messages::WorkCompleteMessage& workCompleteMessage);
44  bool HandleMessage(const Messages::WorkerStoppedMessage& workerStoppedMessage);
45 
46  void Flush();
47 
48  private:
49 
50  void CreateThreads();
51  void ShutdownThreads();
52 
53  std::vector<ITaskWorkerRunner*> m_workers;
54  std::vector<PauseWorkItem*> m_pauseWorkItems;
56  TaskWorkPoolState::Values m_state;
57  int m_stoppedWorkers;
58  };
59  }
60  }
61 }
62