00001 #include "Utilities/Threads/interface/Thread.h" 00002 #include "Utilities/General/interface/MutexUtils.h" 00003 #include "Utilities/General/interface/CMSexception.h" 00004 #include "Utilities/General/interface/UncatchedException.h" 00005 #include "Utilities/General/interface/ClassName.h" 00006 #include "Utilities/General/interface/GeneralVerbosity.h" 00007 #include "Utilities/General/interface/DecoratedSB.h" 00008 #include "Utilities/Timing/interface/LinuxCPUTime.h" 00009 #include "Utilities/General/interface/ioutils.h" 00010 00011 #include <ostream> 00012 #include <iostream> 00013 #include <string> 00014 00015 using std::endl; 00016 using std::string; 00017 00018 bool Thread::verbose=false; 00019 00020 void Thread::verboseOn() { verbose=true;} 00021 00022 void Thread::verboseOff() { verbose=false;} 00023 00024 00025 unsigned int Thread::self_tid() { return pthread_self();} 00026 00027 Thread::Thread() : t1(0), cout(std::cout.rdbuf()){} 00028 00029 void Thread::initialize() { 00030 Me me(*this); 00031 t1 = new boost::thread(me); 00032 } 00033 00034 Thread::~Thread(){ 00035 if (t1) 00036 GeneralUtilities::cout << " thread " << tid() << "still active??? " << std::endl; 00037 delete t1; t1=0; 00038 } 00039 00040 void Thread::finalize() { 00041 if (Thread::verbose) GeneralUtilities::cout << "wait for thread " << tid() << std::endl; 00042 (*t1).join(); 00043 delete t1; t1=0; 00044 } 00045 00046 void Thread::operator()() { 00047 tid_ = self_tid(); 00048 if (verbose) 00049 GeneralUtilities::cout << " thread starts.." << self_tid() << std::endl; 00050 00051 try{ 00052 #ifdef __linux__ 00053 LinuxElapsedTime lt(string("in Thread ")+toa()(tid_)); 00054 #endif 00055 run(); 00056 } 00057 catch ( Terminate ) { 00058 if (verbose) 00059 GeneralUtilities::cout << "thread " 00060 << self_tid() << " terminates " << std::endl; 00061 terminate(); 00062 } 00063 catch ( GenTerminate & cexp ) { 00064 GeneralUtilities::cout << "thread " 00065 << self_tid() << " termination triggered by " << std::endl; 00066 cexp.dump(cout,true); 00067 terminate(); 00068 } 00069 catch ( CMSexception & cexp) { 00070 GeneralUtilities::cout << "thread " 00071 << self_tid() << " caught CMS exception (" 00072 << className(cexp) 00073 << ") :" << endl; 00074 GeneralUtilities::cout << cexp.what() << std::endl; 00075 GeneralUtilities::cout << " inform producer...." << std::endl; 00076 UncatchedException uce; 00077 assert(&uce != 0); // Avoids warning message for unused variable uce. 00078 abort(); 00079 } 00080 catch (...) { 00081 GeneralUtilities::cout << "thread " 00082 << self_tid() << " caught unknown exception " << std::endl; 00083 GeneralUtilities::cout << " inform producer...." << std::endl; 00084 UncatchedException uce; 00085 assert(&uce != 0); // Avoids warning message for unused variable uce. 00086 abort(); 00087 } 00088 00089 } 00090 00091 LockMutex::Mutex Capri::glMutex; 00092 00093 namespace { 00094 struct LocBuilder { 00095 LocBuilder() : 00096 cyan(std::cout.rdbuf(),"",""), 00097 me(&cyan) { 00098 } 00099 DefDecoratedSB cyan; 00100 std::ostream me; 00101 }; 00102 00103 } 00104 00105 std::ostream & TSafeOstream() { 00106 return ThreadSingleton<LocBuilder>::instance().me; 00107 } 00108