Changeset 2955 in Sophya for trunk/SophyaLib/SysTools/zthread.cc
- Timestamp:
- May 29, 2006, 7:27:35 PM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.