Changeset 2955 in Sophya for trunk/SophyaLib/SysTools
- Timestamp:
- May 29, 2006, 7:27:35 PM (19 years ago)
- Location:
- trunk/SophyaLib/SysTools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/commander.cc
r2943 r2955 443 443 static Commander* cur_commander = NULL; 444 444 /* --Methode-- */ 445 //! Default constructor. Initializes variable list and copies \c history.pic to \c hisold.pic 446 Commander::Commander() 445 /*! 446 \brief Constructor. Initializes variable list and copies \c history.pic to \c hisold.pic 447 if \b fgsigstop == true , calls ZThread::ActivateExitOnSignal() 448 */ 449 Commander::Commander(bool fgsigzt) 447 450 { 448 451 system("cp history.pic hisold.pic"); … … 512 515 usage += " > thrlist # List of command execution threads (& as the last character) \n"; 513 516 usage += " > clearthrlist # Removes finished threads from the list \n"; 514 usage += " > stopthr Id # Try to stop a given thread (ThrId=id) sending SIGUSR1 \n"; 517 usage += " > killthr Id # Try to stop a given thread (ThrId=id) by sending SIGUSR1 \n"; 518 usage += " > cancelthr Id # Try to cancel a given thread (ThrId=id) \n"; 515 519 usage += " > waitthr # Waits until all active threads have finished (join()) \n"; 516 520 usage += " > exec filename # Execute commands from file \n"; … … 577 581 AddInterpreter(this); 578 582 curcmdi = this; 583 if (fgsigzt) ZThread::ActivateExitOnSignal(SIGUSR1); 579 584 } 580 585 … … 1878 1883 return(0); 1879 1884 } 1880 else if (kw == "stopthr") { 1881 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: stopthr thrid" << endl; return(0); } 1885 else if ( (kw == "killthr") || (kw == "cancelthr") ) { 1886 if (tokens.size() < 1) { 1887 cout << "Commander::Interpret() Usage: killthr/cancelthr thrid" << endl; 1888 return(0); 1889 } 1882 1890 uint_8 id = atol(tokens[0].c_str()); 1883 StopThr(id); 1891 bool fgkill = false; 1892 if (kw == "killthr") fgkill = true; 1893 StopThr(id, fgkill); 1884 1894 return (0); 1885 1895 } … … 2026 2036 void Commander::ListThreads() 2027 2037 { 2028 cout << "--- - Commander::ListThreads() List of separateexecution threads NThread="2029 << CmdThrExeList.size() << " --- --" << endl;2038 cout << "--- Commander::ListThreads()- command execution threads NThread=" 2039 << CmdThrExeList.size() << " ---" << endl; 2030 2040 for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 2031 2041 tit != CmdThrExeList.end(); tit++) { 2032 2042 cout << "Id=" << (*tit)->Id(); 2033 if ( (*tit)->IfDone() ) cout << " Finished , Rc= " << (*tit)->getRC(); 2034 else cout << " Executing"; 2035 cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens() << " )" << endl; 2036 } 2037 } 2038 /* --Methode-- */ 2039 void Commander::StopThr(uint_8 id) 2043 if ( (*tit)->IfRunning() ) cout << " Executing"; 2044 else if ( (*tit)->IfDone() ) cout << " Finished , Rc= " << (*tit)->getRC(); 2045 else cout << " Stopped/Canceled"; 2046 cout << " (Cmd= " << (*tit)->Keyword() << " " << (*tit)->Tokens().substr(0,35); 2047 if ((*tit)->Tokens().length() > 35) cout << "... )" << endl; 2048 else cout << " )" << endl; 2049 } 2050 } 2051 /* --Methode-- */ 2052 void Commander::StopThr(uint_8 id, bool fgkill) 2040 2053 { 2041 2054 for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 2042 2055 tit != CmdThrExeList.end(); tit++) { 2043 if ( ((*tit)->Id() == id) && !((*tit)->IfDone()) ) { 2044 (*tit)->kill(SIGUSR1); 2045 cout << "Commander::StopThr() Send signal SIGUSR1 to Thread Id= " << id << endl; 2056 if ( ((*tit)->Id() == id) && ((*tit)->IfRunning()) ) { 2057 if (fgkill) { 2058 (*tit)->kill(SIGUSR1); 2059 cout << "Commander::StopThr() Send signal SIGUSR1 to Thread Id= " << id << endl; 2060 } 2061 else { 2062 (*tit)->cancel(); 2063 cout << "Commander::StopThr() Canceling Thread Id= " << id << endl; 2064 } 2046 2065 return; 2047 2066 } … … 2055 2074 cout << "---- Commander::CleanThrList() Cleaning thrlist ----- \n"; 2056 2075 list<CommandExeThr *> thrcopie; 2076 int ncl = 0; 2057 2077 for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 2058 2078 tit != CmdThrExeList.end(); tit++) { 2059 if ( ( *tit)->IfDone() ) {2079 if ( ((*tit)->IfEnded() || (*tit)->IfStopped()) && (ncl < 3) ) { 2060 2080 cout << " Thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << " Cleaned" << endl; 2061 2081 delete (*tit); 2082 ncl++; 2062 2083 } 2063 2084 else thrcopie.push_back((*tit)); -
trunk/SophyaLib/SysTools/commander.h
r2943 r2955 71 71 static Commander* GetInterpreter(); 72 72 73 Commander( );73 Commander(bool fgsigzt=true); 74 74 virtual ~Commander(); 75 75 virtual string Name(); … … 171 171 // Gestion des threads d'execution de commandes 172 172 void ListThreads(); 173 void StopThr(uint_8 thrid );173 void StopThr(uint_8 thrid, bool fgkill=true); 174 174 void CleanThrList(); 175 175 void WaitThreads(); -
trunk/SophyaLib/SysTools/zthread.cc
r2943 r2955 47 47 extern "C" { 48 48 void * zthr_run( void * xthr); 49 } 50 */ 51 49 void zthr_sig_exit(int s); 50 } 51 */ 52 53 static int _RCR_ = 99; 52 54 static void * zthr_run(void * xthr) 53 55 { 54 56 ZThread * thr = (ZThread *)xthr; 55 thr->run(); 56 pthread_exit(NULL); 57 return NULL; 58 } 59 57 thr->run_p(); 58 return &_RCR_; 59 } 60 61 static void zthr_cleanup(void * xthr) 62 { 63 ZThread * thr = (ZThread *)xthr; 64 if(thr->_status == 1) thr->_status = 2; 65 return; 66 } 67 68 static int _RCS_ = 97; 69 static void zthr_sig_exit(int s) 70 { 71 printf("zthr_sig_exit(int s=%d) signal received -> pthread_exit()\n",s); 72 pthread_exit(&_RCS_); 73 } 60 74 61 75 /* ------ Classe ZThread ------- */ … … 65 79 ZThread::ZThread(size_t stacksize) 66 80 { 67 _ initok = false;81 _status = 0; 68 82 _ssize = 0; 69 83 _act = NULL; … … 82 96 void ZThread::start() 83 97 { 84 if ( _initok) throw ZThreadExc("ZThread::Start() - Already started thread !");98 if ( IfStarted() ) throw ZThreadExc("ZThread::Start() - Already started thread !"); 85 99 int rc; 86 100 pthread_attr_t tha; … … 91 105 rc = pthread_create(&_thr, &tha, zthr_run, this); 92 106 CheckSt(rc,"ZThread::start() - Pb creating the thread object"); 93 _initok = true;94 107 setRC(rc); 95 108 } … … 100 113 void ZThread::cancel() 101 114 { 102 if ( !_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");115 if ( !IfStarted() ) throw ZThreadExc("ZThread::cancel() - thread not started !"); 103 116 int rc = pthread_cancel(_thr); 104 117 CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() "); … … 111 124 void ZThread::kill(int sig) 112 125 { 113 if ( !_initok) throw ZThreadExc("ZThread::kill() - thread not started !");126 if ( !IfStarted() ) throw ZThreadExc("ZThread::kill() - thread not started !"); 114 127 int rc = pthread_kill(_thr, sig); 115 128 CheckSt(rc,"ZThread::kill() - Pb pthread_kill() "); … … 119 132 120 133 /*! 121 Waits for the thread to terminate.134 \brief Waits for the thread to terminate (call pthread_join() ) 122 135 */ 123 136 void ZThread::join() 124 137 { 125 if ( !_initok) throw ZThreadExc("ZThread::join() - thread not started !");138 if ( !IfStarted() ) throw ZThreadExc("ZThread::join() - thread not started !"); 126 139 void * x; 127 140 int rc = pthread_join(_thr, &x); … … 131 144 132 145 /*! 146 \brief Method which does the actual computation 133 147 This virtual method can be redefined in the derived class, in order 134 to perform the actual computation. 148 to perform the actual computation. The default implementation call 149 the action function (if defined by setAction() ) 150 135 151 */ 136 152 void ZThread::run() … … 139 155 setRC(0); 140 156 return; 157 } 158 159 /*! 160 \brief Method called by the function passed to pthread_create(). 161 This method sets the clean-up handler, changes the status variable 162 and call the run() method, and pthread_exit() at the end. 163 */ 164 void ZThread::run_p() 165 { 166 _status = 1; 167 pthread_cleanup_push(zthr_cleanup, (void *)this); 168 run(); 169 _status = 2; 170 pthread_exit(&(_rc)); 171 pthread_cleanup_pop(0); 172 return; 173 } 174 175 176 void ZThread::ActivateExitOnSignal(int sig) 177 { 178 struct sigaction ae, ad; 179 180 ae.sa_handler = zthr_sig_exit; 181 ad.sa_handler = SIG_DFL; 182 memset( &(ae.sa_mask), 0, sizeof(sigset_t) ); 183 ae.sa_flags = 0; 184 memset( &(ad.sa_mask), 0, sizeof(sigset_t) ); 185 ad.sa_flags = 0; 186 #ifdef OSF1 187 ae.sa_flags = SA_RESTART; 188 ad.sa_mask = 0; 189 #endif 190 printf(" Activating signal %d handling for threads -> pthread_exit()\n",sig); 191 sigaction(sig, &ae, NULL); 141 192 } 142 193 -
trunk/SophyaLib/SysTools/zthread.h
r2943 r2955 8 8 #include "machdefs.h" 9 9 #include <pthread.h> 10 #include <signal.h> 10 11 #include "pexceptions.h" 11 12 … … 36 37 virtual void run(); 37 38 39 //! Returns true if the thread has been created / started 40 bool IfStarted() { return ((_status > 0) ? true : false); } 41 //! Returns true if the thread is running 42 bool IfRunning() { return ((_status == 1) ? true : false); } 43 //! Returns true if the thread has ended normally 44 bool IfEnded() { return ((_status == 2) ? true : false); } 45 //! Returns true if the thread has been stopped (killed/canceled) 46 bool IfStopped() { return ((_status == 3) ? true : false); } 47 38 48 //! Sets the return code for the thread object 39 49 inline void setRC(int rc) { _rc = rc; } … … 41 51 inline int getRC() { return(_rc); 42 52 } 53 //! Defines the function which is executed by the default run() method 43 54 inline void setAction(ZThreadAction act, void * usp=NULL) 44 55 { _act = act; _usp = usp; } 45 56 57 //! Call this method to activate the stop/exit handler on signal \b sig 58 static void ActivateExitOnSignal(int sig=SIGUSR1); 46 59 60 /*! 61 \brief Set/changes the current thread cancelability state 62 see pthread_setcancelstate() 63 \b st = PTHREAD_CANCEL_ENABLE or PTHREAD_CANCEL_DISABLE 64 */ 47 65 static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE) 48 66 { int ocs; pthread_setcancelstate(st, &ocs); } 49 67 // PTHREAD_CANCEL_ENABLE ou PTHREAD_CANCEL_DISABLE 68 69 /*! 70 \brief Set/changes the current thread cancelability state 71 see pthread_setcanceltype() 72 \b ct = PTHREAD_CANCEL_DEFERRED or PTHREAD_CANCEL_ASYNCHRONOUS 73 */ 50 74 static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED) 51 75 { int oct; pthread_setcanceltype(ct, &oct); } 52 76 // PTHREAD_CANCEL_DEFERRED ou PTHREAD_CANCEL_ASYNCHRONOUS 53 77 78 virtual void run_p(); 54 79 55 80 // ---- Attribute variables ---- … … 57 82 size_t _ssize; 58 83 int _rc; 59 bool _initok;84 int _status; // 0 : not started, 1: running, 2: ended, 3:stopped 60 85 61 86 ZThreadAction _act;
Note:
See TracChangeset
for help on using the changeset viewer.