Changeset 2955 in Sophya for trunk/SophyaLib


Ignore:
Timestamp:
May 29, 2006, 7:27:35 PM (19 years ago)
Author:
ansari
Message:

Ajout gestionnaire signal ds ZThread et correction/adaptation gestion des threads (killthr) dans Commander - Reza 29 mai 2006

Location:
trunk/SophyaLib/SysTools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/SysTools/commander.cc

    r2943 r2955  
    443443static Commander* cur_commander = NULL;
    444444/* --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*/
     449Commander::Commander(bool fgsigzt)
    447450{
    448451system("cp history.pic hisold.pic");
     
    512515usage += "  > thrlist       # List of command execution threads (& as the last character) \n";
    513516usage += "  > clearthrlist  # Removes finished threads from the list  \n";
    514 usage += "  > stopthr Id    # Try to stop a given thread (ThrId=id) sending SIGUSR1 \n";
     517usage += "  > killthr Id    # Try to stop a given thread (ThrId=id) by sending SIGUSR1 \n";
     518usage += "  > cancelthr Id  # Try to cancel a given thread (ThrId=id)  \n";
    515519usage += "  > waitthr       # Waits until all active threads have finished (join()) \n";
    516520usage += "  > exec filename # Execute commands from file \n";
     
    577581AddInterpreter(this);
    578582curcmdi = this;
     583if (fgsigzt) ZThread::ActivateExitOnSignal(SIGUSR1);
    579584}
    580585
     
    18781883  return(0);
    18791884}
    1880 else if (kw == "stopthr") {
    1881   if (tokens.size() < 1) { cout << "Commander::Interpret() Usage: stopthr thrid" << endl;  return(0); }
     1885else if ( (kw == "killthr") || (kw == "cancelthr") )  {
     1886  if (tokens.size() < 1) {
     1887    cout << "Commander::Interpret() Usage: killthr/cancelthr thrid" << endl; 
     1888    return(0);
     1889  }
    18821890  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);
    18841894  return (0);
    18851895}
     
    20262036void Commander::ListThreads()
    20272037{
    2028   cout << "---- Commander::ListThreads()  List of separate execution threads NThread="
    2029        <<  CmdThrExeList.size() << " ----- " << endl;
     2038  cout << "--- Commander::ListThreads()- command execution threads NThread="
     2039       <<  CmdThrExeList.size() << " ---" << endl;
    20302040  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20312041      tit != CmdThrExeList.end(); tit++) {
    20322042    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-- */
     2052void Commander::StopThr(uint_8 id, bool fgkill)
    20402053{
    20412054  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20422055      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      }
    20462065      return;
    20472066    }
     
    20552074  cout << "---- Commander::CleanThrList()  Cleaning thrlist  ----- \n";
    20562075  list<CommandExeThr *> thrcopie;
     2076  int ncl = 0;
    20572077  for(list<CommandExeThr *>::iterator tit = CmdThrExeList.begin();
    20582078      tit != CmdThrExeList.end(); tit++) {
    2059     if ( (*tit)->IfDone() ) {
     2079    if ( ((*tit)->IfEnded() || (*tit)->IfStopped()) && (ncl < 3) ) {
    20602080      cout << " Thread Id= " << (*tit)->Id() << " rc= " << (*tit)->getRC() << " Cleaned" << endl;
    20612081      delete (*tit);
     2082      ncl++;
    20622083    }
    20632084    else thrcopie.push_back((*tit));
  • trunk/SophyaLib/SysTools/commander.h

    r2943 r2955  
    7171  static Commander*     GetInterpreter();
    7272
    73                         Commander();
     73                        Commander(bool fgsigzt=true);
    7474  virtual               ~Commander();
    7575  virtual string        Name();
     
    171171  //   Gestion des threads d'execution de commandes
    172172  void          ListThreads();
    173   void          StopThr(uint_8 thrid);
     173  void          StopThr(uint_8 thrid, bool fgkill=true);
    174174  void          CleanThrList();
    175175  void          WaitThreads();
  • trunk/SophyaLib/SysTools/zthread.cc

    r2943 r2955  
    4747extern "C" {
    4848  void * zthr_run( void * xthr);
    49 }
    50 */
    51 
     49  void zthr_sig_exit(int s);
     50}
     51*/
     52
     53static int _RCR_ = 99;
    5254static void * zthr_run(void * xthr)
    5355{
    5456  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
     61static void zthr_cleanup(void * xthr)
     62{
     63  ZThread * thr = (ZThread *)xthr;
     64  if(thr->_status == 1) thr->_status = 2;
     65  return;
     66}
     67
     68static int _RCS_ = 97;
     69static 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}
    6074
    6175/* ------ Classe ZThread  -------  */
     
    6579ZThread::ZThread(size_t stacksize)
    6680{
    67   _initok = false;
     81  _status = 0;
    6882  _ssize = 0;
    6983  _act = NULL;
     
    8296void ZThread::start()
    8397{
    84   if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
     98  if ( IfStarted() ) throw ZThreadExc("ZThread::Start() - Already started thread !");
    8599  int rc;
    86100  pthread_attr_t tha;
     
    91105  rc = pthread_create(&_thr, &tha, zthr_run, this);
    92106  CheckSt(rc,"ZThread::start() - Pb creating the thread object");     
    93   _initok = true;
    94107  setRC(rc);     
    95108}
     
    100113void ZThread::cancel()
    101114{
    102   if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
     115  if ( !IfStarted() ) throw ZThreadExc("ZThread::cancel() - thread not started !");
    103116  int rc = pthread_cancel(_thr);
    104117  CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() "); 
     
    111124void ZThread::kill(int sig)
    112125{
    113   if (!_initok) throw ZThreadExc("ZThread::kill() - thread not started !");
     126  if ( !IfStarted() ) throw ZThreadExc("ZThread::kill() - thread not started !");
    114127  int rc = pthread_kill(_thr, sig);
    115128  CheckSt(rc,"ZThread::kill() - Pb pthread_kill() "); 
     
    119132
    120133/*!
    121   Waits for the thread to terminate.
     134  \brief Waits for the thread to terminate (call pthread_join() )
    122135*/
    123136void ZThread::join()
    124137{
    125   if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
     138  if ( !IfStarted() ) throw ZThreadExc("ZThread::join() - thread not started !");
    126139  void * x;
    127140  int rc = pthread_join(_thr, &x);
     
    131144
    132145/*!
     146  \brief Method which does the actual computation
    133147  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 
    135151*/
    136152void ZThread::run()
     
    139155  setRC(0);     
    140156  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*/
     164void 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
     176void 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);
    141192}
    142193
  • trunk/SophyaLib/SysTools/zthread.h

    r2943 r2955  
    88#include "machdefs.h"
    99#include <pthread.h>
     10#include <signal.h>
    1011#include "pexceptions.h"
    1112
     
    3637  virtual void  run();
    3738
     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
    3848  //! Sets the return code for the thread object
    3949  inline void   setRC(int rc) { _rc = rc; }
     
    4151  inline int    getRC() { return(_rc);
    4252 }
     53  //! Defines the function which is executed by the default run() method
    4354  inline void   setAction(ZThreadAction act, void * usp=NULL)
    4455       {  _act = act;  _usp = usp; }
    4556
     57  //! Call this method to activate the stop/exit handler on signal \b sig
     58  static void ActivateExitOnSignal(int sig=SIGUSR1);
    4659
     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  */
    4765  static inline void setCancelState(int st= PTHREAD_CANCEL_ENABLE)
    4866       { int ocs; pthread_setcancelstate(st, &ocs); }
    4967  // 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  */
    5074  static inline void setCancelType(int ct= PTHREAD_CANCEL_DEFERRED)
    5175       { int oct; pthread_setcanceltype(ct, &oct); }
    5276  // PTHREAD_CANCEL_DEFERRED  ou  PTHREAD_CANCEL_ASYNCHRONOUS
    5377
     78  virtual void  run_p();
    5479
    5580  // ---- Attribute variables ----
     
    5782  size_t _ssize;
    5883  int _rc;
    59   bool _initok;
     84  int _status; // 0 : not started, 1: running, 2: ended, 3:stopped
    6085
    6186  ZThreadAction _act;
Note: See TracChangeset for help on using the changeset viewer.