Changeset 2955 in Sophya for trunk/SophyaLib/SysTools/zthread.cc


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.