$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/WorkLoopManager/WorkLoop.h" 00002 #include "otsdaq-core/Macros/CoutMacros.h" 00003 #include "otsdaq-core/MessageFacility/MessageFacility.h" 00004 00005 #include <toolbox/task/WorkLoopFactory.h> 00006 00007 #include <unistd.h> 00008 #include <iostream> 00009 00010 using namespace ots; 00011 00012 #undef __MF_SUBJECT__ 00013 #define __MF_SUBJECT__ (std::string("Workloop-") + WorkLoop::workLoopName_) 00014 00015 //======================================================================================================================== 00016 WorkLoop::WorkLoop(const std::string& name) 00017 : continueWorkLoop_(false) 00018 , workLoopName_(name) 00019 , workLoopType_("waiting") 00020 , workLoop_(0) 00021 , job_(toolbox::task::bind(this, &WorkLoop::workLoopThread, workLoopName_)) 00022 { 00023 __COUT__ << "Constructed." << __E__; 00024 } 00025 00026 //======================================================================================================================== 00027 WorkLoop::~WorkLoop(void) 00028 { 00029 __COUT__ << "Destructor." << __E__; 00030 if(stopWorkLoop()) 00031 toolbox::task::getWorkLoopFactory()->removeWorkLoop(workLoopName_, workLoopType_); 00032 __COUT__ << "Destructed." << __E__; 00033 } 00034 00035 //======================================================================================================================== 00036 void WorkLoop::startWorkLoop(void) 00037 { 00038 __COUT__ << "Starting WorkLoop: " << workLoopName_ << __E__; 00039 continueWorkLoop_ = true; 00040 try 00041 { 00042 workLoop_ = toolbox::task::getWorkLoopFactory()->getWorkLoop(workLoopName_, 00043 workLoopType_); 00044 } 00045 catch(xcept::Exception& e) 00046 { 00047 __COUT__ << "ERROR: Can't create WorkLoop job for " << workLoopName_ << __E__; 00048 stopWorkLoop(); 00049 } 00050 00051 if(workLoop_->isActive()) 00052 return; // This might be a consumer producer running at the same time so it has 00053 // been activated already 00054 00055 try 00056 { 00057 workLoop_->submit(job_); 00058 } 00059 catch(xcept::Exception& e) 00060 { 00061 __COUT__ << "ERROR: Can't submit WorkLoop job for " << workLoopName_ << __E__; 00062 stopWorkLoop(); 00063 } 00064 00065 try 00066 { 00067 workLoop_->activate(); 00068 } 00069 catch(xcept::Exception& e) 00070 { 00071 __COUT__ << "ERROR: Can't activate WorkLoop job for " << workLoopName_ 00072 << " Very likely because the name " << workLoopName_ << " is not unique!" 00073 << __E__; 00074 stopWorkLoop(); 00075 } 00076 } // end startWorkLoop() 00077 00078 //======================================================================================================================== 00079 bool WorkLoop::stopWorkLoop() 00080 { 00081 __COUT__ << "Stopping WorkLoop: " << workLoopName_ << __E__; 00082 00083 continueWorkLoop_ = false; 00084 if(workLoop_ == 0) 00085 { 00086 __COUT__ 00087 << "MEASSAGE: WorkLoop " << workLoopName_ 00088 << " was not created at all! This message will be commented in the future" 00089 << __E__; 00090 return false; 00091 } 00092 00093 __COUT__ << "initially workLoop_->isActive() " 00094 << (workLoop_->isActive() ? "yes" : "no") << __E__; 00095 00096 try 00097 { 00098 // THis method waits until the workloop job returns! Super cool! 00099 workLoop_->cancel(); 00100 } 00101 catch(xcept::Exception& e) 00102 { 00103 __COUT__ << "WARNING: Can't cancel WorkLoop job for " << workLoopName_ 00104 << " because probably it has never been activated!" << __E__; 00105 00106 __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") 00107 << __E__; 00108 return true; 00109 } 00110 00111 try 00112 { 00113 workLoop_->remove(job_); 00114 } 00115 catch(xcept::Exception& e) 00116 { 00117 // ATTENTION! 00118 // If the workloop job thread returns false, then the workloop job is 00119 // automatically removed and it can't be removed again Leaving this try catch 00120 // allows me to be general in the job threads so I can return true (repeat loop) 00121 // or false ( loop only once) without crashing 00122 __COUT__ << "WARNING: Can't remove request WorkLoop: " << workLoopName_ << __E__; 00123 __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") 00124 << __E__; 00125 } 00126 00127 __COUT__ << "Stopped WorkLoop: " << workLoopName_ << __E__; 00128 __COUT__ << "workLoop_->isActive() " << (workLoop_->isActive() ? "yes" : "no") 00129 << __E__; 00130 return true; 00131 } // end stopWorkLoop() 00132 00133 //======================================================================================================================== 00134 bool WorkLoop::isActive(void) const 00135 { 00136 return workLoop_->isActive() && continueWorkLoop_; 00137 } // end isActive