otsdaq  v2_05_02_indev
WorkLoop.cc
1 #include "otsdaq/WorkLoopManager/WorkLoop.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
4 
5 #include <toolbox/task/WorkLoopFactory.h>
6 
7 #include <unistd.h>
8 #include <iostream>
9 
10 using namespace ots;
11 
12 #undef __MF_SUBJECT__
13 #define __MF_SUBJECT__ "Workloop"
14 #undef __COUT_HDR__
15 #define __COUT_HDR__ (std::string("Workloop-") + WorkLoop::workLoopName_ + "\t<>")
16 
17 //==============================================================================
18 WorkLoop::WorkLoop(const std::string& name)
19  : continueWorkLoop_(false)
20  , workLoopName_(name)
21  , workLoopType_("waiting")
22  , workLoop_(0)
23  , job_(toolbox::task::bind(this, &WorkLoop::workLoopThread, workLoopName_))
24 {
25  __COUT__ << "Constructed." << __E__;
26 }
27 
28 //==============================================================================
29 WorkLoop::~WorkLoop(void)
30 {
31  __COUT__ << "Destructor." << __E__;
32  if(stopWorkLoop())
33  toolbox::task::getWorkLoopFactory()->removeWorkLoop(workLoopName_, workLoopType_);
34  __COUT__ << "Destructed." << __E__;
35 }
36 
37 //==============================================================================
38 void WorkLoop::startWorkLoop(void)
39 {
40  __COUT__ << "Starting WorkLoop: " << workLoopName_ << __E__;
41  continueWorkLoop_ = true;
42  try
43  {
44  workLoop_ = toolbox::task::getWorkLoopFactory()->getWorkLoop(workLoopName_, workLoopType_);
45  }
46  catch(xcept::Exception& e)
47  {
48  __COUT__ << "ERROR: Can't create WorkLoop job for " << workLoopName_ << __E__;
49  stopWorkLoop();
50  }
51 
52  if(workLoop_->isActive())
53  return; // This might be a consumer producer running at the same time so it has
54  // been activated already
55 
56  try
57  {
58  workLoop_->submit(job_);
59  }
60  catch(xcept::Exception& e)
61  {
62  __COUT__ << "ERROR: Can't submit WorkLoop job for " << workLoopName_ << __E__;
63  stopWorkLoop();
64  }
65 
66  try
67  {
68  workLoop_->activate();
69  }
70  catch(xcept::Exception& e)
71  {
72  __COUT__ << "ERROR: Can't activate WorkLoop job for " << workLoopName_ << " Very likely because the name " << workLoopName_ << " is not unique!"
73  << __E__;
74  stopWorkLoop();
75  }
76 } // end startWorkLoop()
77 
78 //==============================================================================
79 bool WorkLoop::stopWorkLoop()
80 {
81  __COUT__ << "Stopping WorkLoop: " << workLoopName_ << __E__;
82 
83  continueWorkLoop_ = false;
84  if(workLoop_ == 0)
85  {
86  __COUT__ << "MEASSAGE: WorkLoop " << workLoopName_ << " was not created at all! This message will be commented in the future" << __E__;
87  return false;
88  }
89 
90  __COUT__ << "initially workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") << __E__;
91 
92  try
93  {
94  // THis method waits until the workloop job returns! Super cool!
95  workLoop_->cancel();
96  }
97  catch(xcept::Exception& e)
98  {
99  __COUT__ << "WARNING: Can't cancel WorkLoop job for " << workLoopName_ << " because probably it has never been activated!" << __E__;
100 
101  __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") << __E__;
102  return true;
103  }
104 
105  try
106  {
107  workLoop_->remove(job_);
108  }
109  catch(xcept::Exception& e)
110  {
111  // ATTENTION!
112  // If the workloop job thread returns false, then the workloop job is
113  // automatically removed and it can't be removed again Leaving this try catch
114  // allows me to be general in the job threads so I can return true (repeat loop)
115  // or false ( loop only once) without crashing
116  __COUT__ << "WARNING: Can't remove request WorkLoop: " << workLoopName_ << __E__;
117  __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") << __E__;
118  }
119 
120  __COUT__ << "Stopped WorkLoop: " << workLoopName_ << __E__;
121  __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") << __E__;
122  return true;
123 } // end stopWorkLoop()
124 
125 //==============================================================================
126 bool WorkLoop::isActive(void) const { return workLoop_->isActive() && continueWorkLoop_; } // end isActive