SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WorkUnitConsumer.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2012 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9  License for the specific language governing rights and limitations under
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 #ifndef ENGINE_SCHEDULER_DYNAMICEXECUTOR_WORKUNITCONSUMER_H
30 #define ENGINE_SCHEDULER_DYNAMICEXECUTOR_WORKUNITCONSUMER_H
31 
36 #include <Core/Logging/Log.h>
37 #include <boost/thread.hpp>
38 
40 
41 namespace SCIRun {
42 namespace Dataflow {
43 namespace Engine {
44 namespace DynamicExecutor {
45 
46 
47  class SCISHARE ModuleConsumer : boost::noncopyable
48  {
49  public:
51  work_(workQueue), producer_(producer), lookup_(lookup), shouldLog_(SCIRun::Core::Logging::Log::get().verbose())
52  {
53  log_.setVerbose(shouldLog_);
54  if (shouldLog_)
55  log_ << Core::Logging::DEBUG_LOG << "Consumer created." << std::endl;
56  }
57  void operator()() const
58  {
59  if (!producer_)
60  {
61  if (shouldLog_)
62  log_ << Core::Logging::DEBUG_LOG << "Consumer quitting due to no producer pointer." << std::endl;
63  return;
64  }
65 
66  log_ << Core::Logging::DEBUG_LOG << "Consumer started." << std::endl;
67 
68  while (!producer_->isDone() || moreWork())
69  {
70  if (moreWork())
71  {
72  if (shouldLog_)
73  log_ << Core::Logging::DEBUG_LOG << "\tConsumer thinks work queue is not empty.";
74 
75  if (shouldLog_)
76  log_ << Core::Logging::DEBUG_LOG << "\tConsumer accessing front of work queue.";
78  work_->pop(unit);
79  if (shouldLog_)
80  log_ << Core::Logging::DEBUG_LOG << "\tConsumer popping front of work queue.";
81 
82  if (unit)
83  {
84  if (shouldLog_)
85  log_ << Core::Logging::DEBUG_LOG << "~~~Processing " << unit->get_id();
86 
87  ModuleExecutor executor(unit, lookup_, producer_);
88  /// @todo: thread pool
89  boost::thread t(boost::bind(&ModuleExecutor::run, executor));
90  }
91  else
92  {
93  if (shouldLog_)
94  log_ << Core::Logging::DEBUG_LOG << "\tConsumer received null module";
95  }
96  }
97  }
98  log_ << Core::Logging::DEBUG_LOG << "Consumer done." << std::endl;
99  }
100 
101  bool moreWork() const
102  {
103  return !work_->empty();
104  }
105 
106  private:
107  ModuleWorkQueuePtr work_;
108  ProducerInterfacePtr producer_;
109  const Networks::ExecutableLookup* lookup_;
110  static Core::Logging::Log& log_;
111  bool shouldLog_;
112  };
113 
114  typedef boost::shared_ptr<ModuleConsumer> ModuleConsumerPtr;
115 
116 }}
117 }}
118 
119 #endif
void run()
Definition: WorkUnitExecutor.h:50
#define SCISHARE
Definition: share.h:39
ModuleConsumer(ModuleWorkQueuePtr workQueue, const Networks::ExecutableLookup *lookup, ProducerInterfacePtr producer)
Definition: WorkUnitConsumer.h:50
void operator()() const
Definition: WorkUnitConsumer.h:57
boost::shared_ptr< ModuleWorkQueue > ModuleWorkQueuePtr
Definition: WorkQueue.h:49
bool moreWork() const
Definition: WorkUnitConsumer.h:101
boost::shared_ptr< ModuleInterface > ModuleHandle
Definition: NetworkFwd.h:74
boost::shared_ptr< ProducerInterface > ProducerInterfacePtr
Definition: WorkUnitProducerInterface.h:47
Definition: NetworkInterface.h:48
Definition: Log.h:64
boost::shared_ptr< ModuleConsumer > ModuleConsumerPtr
Definition: WorkUnitConsumer.h:114