[1612] | 1 | // Interface C++ aux Thread POSIX - R. Ansari 02/2001
|
---|
| 2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 3 |
|
---|
[2615] | 4 | #include "sopnamsp.h"
|
---|
[1612] | 5 | #include "zthread.h"
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include <stdio.h>
|
---|
[2943] | 8 | #include <pthread.h>
|
---|
| 9 | #include <signal.h>
|
---|
[1612] | 10 |
|
---|
[2212] | 11 | /*!
|
---|
| 12 | \class SOPHYA::ZThread
|
---|
| 13 | \ingroup SysTools
|
---|
[2598] | 14 | \brief Simple class for creating and controlling threads.
|
---|
| 15 |
|
---|
[2212] | 16 | This class provides an interface for creating and controlling threads.
|
---|
| 17 | The implementation uses the POSIX thread interface.
|
---|
| 18 | The ZThread objects can be sub classed with the redefinition of
|
---|
| 19 | the \c run() method which then performs the task.
|
---|
| 20 | The default \c run() method of the base class can be used directly
|
---|
| 21 | to perform computation through a function (see \b setAction() method)
|
---|
| 22 | \sa SOPHYA::ZMutex
|
---|
| 23 | The following sample code shows the usage of ZThread object
|
---|
[2487] | 24 | to run simultaneously two functions to perform computation.
|
---|
[2212] | 25 |
|
---|
| 26 | \code
|
---|
| 27 | // The functions to perform computing
|
---|
| 28 | void fun1(void *arg) { }
|
---|
| 29 | void fun2(void *arg) { }
|
---|
| 30 | // ...
|
---|
| 31 | ZThread zt1;
|
---|
| 32 | zt1.setAction(fun1, arg[1]);
|
---|
| 33 | ZThread zt2;
|
---|
| 34 | zt2.setAction(fun2, arg[1]);
|
---|
| 35 | cout << " Starting threads ... " << endl;
|
---|
| 36 | zt1.start();
|
---|
| 37 | zt2.start();
|
---|
| 38 | cout << " Waiting for threads to end ... " << endl;
|
---|
| 39 | zt1.join();
|
---|
| 40 | zt2.join();
|
---|
| 41 | \endcode
|
---|
| 42 | */
|
---|
| 43 |
|
---|
[1612] | 44 | #define CheckSt(st_, strg_) if (st_ != 0) perror(strg_);
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | extern "C" {
|
---|
| 48 | void * zthr_run( void * xthr);
|
---|
[2955] | 49 | void zthr_sig_exit(int s);
|
---|
[1612] | 50 | }
|
---|
| 51 | */
|
---|
| 52 |
|
---|
[2955] | 53 | static int _RCR_ = 99;
|
---|
[1612] | 54 | static void * zthr_run(void * xthr)
|
---|
| 55 | {
|
---|
| 56 | ZThread * thr = (ZThread *)xthr;
|
---|
[2955] | 57 | thr->run_p();
|
---|
| 58 | return &_RCR_;
|
---|
[1612] | 59 | }
|
---|
| 60 |
|
---|
[2955] | 61 | static void zthr_cleanup(void * xthr)
|
---|
| 62 | {
|
---|
| 63 | ZThread * thr = (ZThread *)xthr;
|
---|
| 64 | if(thr->_status == 1) thr->_status = 2;
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
[1612] | 67 |
|
---|
[2955] | 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 | }
|
---|
| 74 |
|
---|
[1612] | 75 | /* ------ Classe ZThread ------- */
|
---|
[2212] | 76 | /*!
|
---|
| 77 | Constructor, with optional specification of the thread stack size.
|
---|
| 78 | */
|
---|
[1612] | 79 | ZThread::ZThread(size_t stacksize)
|
---|
| 80 | {
|
---|
[2955] | 81 | _status = 0;
|
---|
[1612] | 82 | _ssize = 0;
|
---|
| 83 | _act = NULL;
|
---|
| 84 | _usp = NULL;
|
---|
| 85 | _rc = -99;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | ZThread::~ZThread()
|
---|
| 89 | {
|
---|
| 90 | // Que faut-il faire ?
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[2212] | 93 | /*!
|
---|
| 94 | Method which starts the thread.
|
---|
| 95 | */
|
---|
[1612] | 96 | void ZThread::start()
|
---|
| 97 | {
|
---|
[2955] | 98 | if ( IfStarted() ) throw ZThreadExc("ZThread::Start() - Already started thread !");
|
---|
[1612] | 99 | int rc;
|
---|
| 100 | pthread_attr_t tha;
|
---|
| 101 | rc = pthread_attr_init(&tha);
|
---|
| 102 | CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
|
---|
| 103 | if (_ssize > 0)
|
---|
| 104 | rc = pthread_attr_setstacksize(&tha, _ssize);
|
---|
| 105 | rc = pthread_create(&_thr, &tha, zthr_run, this);
|
---|
| 106 | CheckSt(rc,"ZThread::start() - Pb creating the thread object");
|
---|
| 107 | setRC(rc);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[2212] | 110 | /*!
|
---|
| 111 | Calls the pthread_cancel. Can be used to stop a running thread.
|
---|
| 112 | */
|
---|
[1612] | 113 | void ZThread::cancel()
|
---|
| 114 | {
|
---|
[2955] | 115 | if ( !IfStarted() ) throw ZThreadExc("ZThread::cancel() - thread not started !");
|
---|
[1612] | 116 | int rc = pthread_cancel(_thr);
|
---|
| 117 | CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");
|
---|
| 118 | setRC(-77);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[2943] | 121 | /*!
|
---|
| 122 | Calls the pthread_kill to deliver a signal to the corresponding thread
|
---|
| 123 | */
|
---|
| 124 | void ZThread::kill(int sig)
|
---|
| 125 | {
|
---|
[2955] | 126 | if ( !IfStarted() ) throw ZThreadExc("ZThread::kill() - thread not started !");
|
---|
[2943] | 127 | int rc = pthread_kill(_thr, sig);
|
---|
| 128 | CheckSt(rc,"ZThread::kill() - Pb pthread_kill() ");
|
---|
| 129 | setRC(-76);
|
---|
| 130 | }
|
---|
[1612] | 131 |
|
---|
[2943] | 132 |
|
---|
[2212] | 133 | /*!
|
---|
[2955] | 134 | \brief Waits for the thread to terminate (call pthread_join() )
|
---|
[2212] | 135 | */
|
---|
[1612] | 136 | void ZThread::join()
|
---|
| 137 | {
|
---|
[2955] | 138 | if ( !IfStarted() ) throw ZThreadExc("ZThread::join() - thread not started !");
|
---|
[1612] | 139 | void * x;
|
---|
| 140 | int rc = pthread_join(_thr, &x);
|
---|
| 141 | CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
|
---|
| 142 | return;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[2212] | 145 | /*!
|
---|
[2955] | 146 | \brief Method which does the actual computation
|
---|
[2212] | 147 | This virtual method can be redefined in the derived class, in order
|
---|
[2955] | 148 | to perform the actual computation. The default implementation call
|
---|
| 149 | the action function (if defined by setAction() )
|
---|
| 150 |
|
---|
[2212] | 151 | */
|
---|
[1612] | 152 | void ZThread::run()
|
---|
| 153 | {
|
---|
| 154 | if (_act) _act(_usp);
|
---|
| 155 | setRC(0);
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[2955] | 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 | }
|
---|
[1612] | 174 |
|
---|
[2955] | 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);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 |
|
---|
[1612] | 195 | /* ------ Classe ZMutex ------- */
|
---|
[2212] | 196 | /*!
|
---|
| 197 | \class SOPHYA::ZMutex
|
---|
| 198 | \ingroup SysTools
|
---|
[3209] | 199 | \brief Wrapper for a pair of (Mutex,Cond.Var) objects.
|
---|
[2598] | 200 |
|
---|
[2212] | 201 | This class implements an interface to the Mutual Exclusion objects
|
---|
| 202 | of the POSIX threads.
|
---|
| 203 | The ZMutex objects should be used to control acces from different threads
|
---|
[2759] | 204 | to common objects through the \b lock() and \b unlock() methods.
|
---|
| 205 | When the \b signal() method is called, one of the waiting threads
|
---|
| 206 | on the corresponding mutex object (\b ZMutex::wait()) is awakened.
|
---|
| 207 | All waiting threads on the mutex object are awakened when \b broadcast()
|
---|
| 208 | is called.
|
---|
[2212] | 209 | \sa SOPHYA::ZSync
|
---|
| 210 | */
|
---|
[1612] | 211 |
|
---|
[2212] | 212 | /*!
|
---|
| 213 | Constructor: Creates a associated pair of POSIX \c pthread_mutex_t
|
---|
| 214 | and \c pthread_cond_t objects
|
---|
| 215 | */
|
---|
[1612] | 216 | ZMutex::ZMutex()
|
---|
| 217 | {
|
---|
| 218 | _mutx = new pthread_mutex_t;
|
---|
| 219 | _condv = new pthread_cond_t;
|
---|
| 220 | int rc;
|
---|
| 221 | rc = pthread_mutex_init(_mutx, NULL);
|
---|
| 222 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
|
---|
| 223 | rc = pthread_cond_init(_condv, NULL);
|
---|
| 224 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | ZMutex::~ZMutex()
|
---|
| 228 | {
|
---|
[3209] | 229 | int rc;
|
---|
| 230 | rc = pthread_mutex_destroy(_mutx);
|
---|
| 231 | CheckSt(rc,"ZMutex::~ZMutex() Pb pthread_mutex_destroy");
|
---|
| 232 | rc = pthread_cond_destroy(_condv);
|
---|
[3217] | 233 | CheckSt(rc,"ZMutex::~ZMutex() Pb pthread_cond_destroy");
|
---|
[1612] | 234 | delete _mutx;
|
---|
| 235 | delete _condv;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[2212] | 238 | /*!
|
---|
| 239 | \class SOPHYA::ZSync
|
---|
| 240 | \ingroup SysTools
|
---|
[2598] | 241 | \brief Wrapper/utility class ensuring synchronised execution of an instruction bloc.
|
---|
| 242 |
|
---|
[2212] | 243 | This class can be used to insure that the execution of a given
|
---|
| 244 | part of the code is synchronised, i.e. only a single thread
|
---|
| 245 | goes through this at a given time.
|
---|
| 246 | The constructor acquires a lock on given \b ZMutex object,
|
---|
| 247 | which is released by the destructor.
|
---|
| 248 | The ZSync object should then be instanciated at the beginning of
|
---|
| 249 | the synchronised instruction bloc.
|
---|
| 250 | \sa SOPHYA::ZMutex
|
---|
| 251 | */
|
---|