Changeset 2943 in Sophya for trunk/SophyaLib/SysTools
- Timestamp:
- Apr 26, 2006, 5:22:09 PM (19 years ago)
- Location:
- trunk/SophyaLib/SysTools
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/SysTools/commander.cc
r2914 r2943 6 6 #include <ctype.h> 7 7 #include <math.h> 8 #include <signal.h> 8 9 9 10 #include "strutil.h" … … 341 342 \ingroup SysTools 342 343 Class for internal use by class Commander for command execution in separate threads. 344 The command execution is carried in method run() which has its own exception handling bloc. 343 345 */ 344 346 class CommandExeThr : public ZThread { … … 377 379 void CommandExeThr::run() 378 380 { 379 int rc = _cmdex->Execute(_keyw, _args, _toks); 381 int rc = 0; 382 try { 383 rc = _cmdex->Execute(_keyw, _args, _toks); 384 } 385 catch (PThrowable& exc) { 386 cout << "CommandExeThr::run() Catched Exception Msg()= " 387 << exc.Msg() << endl; 388 rc = -77; 389 } 390 catch (std::exception& sex) { 391 cout << "CommandExeThr::run() Catched std::exception what()= " 392 << sex.what() << endl; 393 rc = -78; 394 } 395 catch (...) { 396 cout << "CommandExeThr::run() Catched unknown (...) exception " << endl; 397 rc = -79; 398 } 380 399 _fgdone = true; 381 400 setRC(rc); … … 402 421 - Simple and vector variables 403 422 - Script definition 423 - Command execution in separate threads 404 424 - Dynamic Load 405 425 … … 492 512 usage += " > thrlist # List of command execution threads (& as the last character) \n"; 493 513 usage += " > clearthrlist # Removes finished threads from the list \n"; 494 usage += " > cancelthr Id # Cancel a given thread (ThrId=id)\n";514 usage += " > stopthr Id # Try to stop a given thread (ThrId=id) sending SIGUSR1 \n"; 495 515 usage += " > waitthr # Waits until all active threads have finished (join()) \n"; 496 516 usage += " > exec filename # Execute commands from file \n"; … … 704 724 705 725 /* --Methode-- */ 706 //! Select an interpreter by its name . The corresponding Interpret method is then called726 //! Select an interpreter by its name as the current interpreter. 707 727 void Commander::SelInterpreter(string& name) 708 728 { … … 1081 1101 } 1082 1102 1103 //! Can be called asynchronously (from a separate thread) to break (halt) execution (in loops, scripts ...) 1083 1104 void Commander::StopExecution() 1084 1105 { … … 1857 1878 return(0); 1858 1879 } 1859 else if (kw == " cancelthr") {1860 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: cancelthr thrid" << endl; return(0); }1880 else if (kw == "stopthr") { 1881 if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: stopthr thrid" << endl; return(0); } 1861 1882 uint_8 id = atol(tokens[0].c_str()); 1862 CancelThr(id);1883 StopThr(id); 1863 1884 return (0); 1864 1885 } … … 1938 1959 CmdThrExeList.push_back(thr); 1939 1960 cout << " Commander::ExecuteCommand() : Thread execution of command " << keyw << endl; 1940 thr->start(); 1961 thr->start(); 1962 if (CmdThrExeList.size() > 5) CleanThrList(); 1941 1963 rc = 0; 1942 1964 } 1943 else rc = (*it).second.cex->Execute(keyw, args, toks);1965 else rc = (*it).second.cex->Execute(keyw, args, toks); 1944 1966 } 1945 1967 else cout << "Dont know how to execute " << keyw << " ? " << endl; 1946 1968 } 1947 1969 return(rc); 1948 1970 } … … 2015 2037 } 2016 2038 /* --Methode-- */ 2017 void Commander:: CancelThr(uint_8 id)2039 void Commander::StopThr(uint_8 id) 2018 2040 { 2019 2041 for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin(); 2020 2042 tit != CmdThrExeList.end(); tit++) { 2021 if ( (*tit)->Id() == id) {2022 (*tit)-> cancel();2023 cout << "Commander:: CancelThr() Thread Id= " << id << " cancelled"<< endl;2043 if ( ((*tit)->Id() == id) && !((*tit)->IfDone()) ) { 2044 (*tit)->kill(SIGUSR1); 2045 cout << "Commander::StopThr() Send signal SIGUSR1 to Thread Id= " << id << endl; 2024 2046 return; 2025 2047 } 2026 2048 } 2027 cout << "Commander:: CancelThr()/Error: Nothread with Id= " << id << endl;2049 cout << "Commander::StopThr()/Error: No active thread with Id= " << id << endl; 2028 2050 } 2029 2051 … … 2143 2165 2144 2166 /* --Methode-- */ 2145 //! Produces a LaTeX file containing the registered command helps 2167 /*! 2168 \brief Produces a LaTeX file containing the registered command helps 2169 The file \b fname is created and can be inserted into a LaTeX document 2170 in order to produce the list of registered commands and corresponding description 2171 texts. 2172 The LaTeX file should contain the following definitions: 2173 \verbatim 2174 \newcommand{\piacommand}[1]{ 2175 \framebox{\bf \Large #1 } \index{#1} % (Command) 2176 } 2177 2178 \newcommand{\piahelpitem}[1]{ 2179 \framebox{\bf \Large #1 } \index{#1} (Help item) 2180 } 2181 2182 \newcommand{\myppageref}[1]{ (p. \pageref{#1} ) } 2183 2184 \endverbatim 2185 */ 2146 2186 void Commander::HelptoLaTeX(string const & fname) 2147 2187 { -
trunk/SophyaLib/SysTools/commander.h
r2804 r2943 55 55 //! command execution method for a command defined by keyword and its arguments. 56 56 virtual int Execute(string& keyw, vector<string>& args, string& toks)=0; 57 //! Return true if the command \b keyw is thread compatible 57 //! Return true if the command \b keyw is thread compatible (can be executed in a separate thread) 58 58 virtual bool IsThreadable(string const & keyw) { return false; } 59 59 }; … … 105 105 virtual void HelptoLaTeX(string const & flnm); 106 106 107 //! return the current selected interpreter (default : this) 107 108 inline CmdInterpreter* CurrentInterpreter() { return(curcmdi); } 108 109 … … 170 171 // Gestion des threads d'execution de commandes 171 172 void ListThreads(); 172 void CancelThr(uint_8 thrid);173 void StopThr(uint_8 thrid); 173 174 void CleanThrList(); 174 175 void WaitThreads(); -
trunk/SophyaLib/SysTools/zthread.cc
r2759 r2943 6 6 #include <stdlib.h> 7 7 #include <stdio.h> 8 #include <pthread.h> 9 #include <signal.h> 8 10 9 11 /*! … … 104 106 } 105 107 108 /*! 109 Calls the pthread_kill to deliver a signal to the corresponding thread 110 */ 111 void ZThread::kill(int sig) 112 { 113 if (!_initok) throw ZThreadExc("ZThread::kill() - thread not started !"); 114 int rc = pthread_kill(_thr, sig); 115 CheckSt(rc,"ZThread::kill() - Pb pthread_kill() "); 116 setRC(-76); 117 } 118 106 119 107 120 /*! -
trunk/SophyaLib/SysTools/zthread.h
r2759 r2943 31 31 void start(); 32 32 void cancel(); 33 inline void stop() { cancel(); }33 void kill(int sig); 34 34 35 35 void join();
Note:
See TracChangeset
for help on using the changeset viewer.