[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);
|
---|
| 49 | }
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | static void * zthr_run(void * xthr)
|
---|
| 53 | {
|
---|
| 54 | ZThread * thr = (ZThread *)xthr;
|
---|
| 55 | thr->run();
|
---|
| 56 | pthread_exit(NULL);
|
---|
| 57 | return NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /* ------ Classe ZThread ------- */
|
---|
[2212] | 62 | /*!
|
---|
| 63 | Constructor, with optional specification of the thread stack size.
|
---|
| 64 | */
|
---|
[1612] | 65 | ZThread::ZThread(size_t stacksize)
|
---|
| 66 | {
|
---|
| 67 | _initok = false;
|
---|
| 68 | _ssize = 0;
|
---|
| 69 | _act = NULL;
|
---|
| 70 | _usp = NULL;
|
---|
| 71 | _rc = -99;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | ZThread::~ZThread()
|
---|
| 75 | {
|
---|
| 76 | // Que faut-il faire ?
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[2212] | 79 | /*!
|
---|
| 80 | Method which starts the thread.
|
---|
| 81 | */
|
---|
[1612] | 82 | void ZThread::start()
|
---|
| 83 | {
|
---|
| 84 | if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
|
---|
| 85 | int rc;
|
---|
| 86 | pthread_attr_t tha;
|
---|
| 87 | rc = pthread_attr_init(&tha);
|
---|
| 88 | CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
|
---|
| 89 | if (_ssize > 0)
|
---|
| 90 | rc = pthread_attr_setstacksize(&tha, _ssize);
|
---|
| 91 | rc = pthread_create(&_thr, &tha, zthr_run, this);
|
---|
| 92 | CheckSt(rc,"ZThread::start() - Pb creating the thread object");
|
---|
| 93 | _initok = true;
|
---|
| 94 | setRC(rc);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[2212] | 97 | /*!
|
---|
| 98 | Calls the pthread_cancel. Can be used to stop a running thread.
|
---|
| 99 | */
|
---|
[1612] | 100 | void ZThread::cancel()
|
---|
| 101 | {
|
---|
| 102 | if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
|
---|
| 103 | int rc = pthread_cancel(_thr);
|
---|
| 104 | CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");
|
---|
| 105 | setRC(-77);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[2943] | 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 | }
|
---|
[1612] | 118 |
|
---|
[2943] | 119 |
|
---|
[2212] | 120 | /*!
|
---|
| 121 | Waits for the thread to terminate.
|
---|
| 122 | */
|
---|
[1612] | 123 | void ZThread::join()
|
---|
| 124 | {
|
---|
| 125 | if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
|
---|
| 126 | void * x;
|
---|
| 127 | int rc = pthread_join(_thr, &x);
|
---|
| 128 | CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[2212] | 132 | /*!
|
---|
| 133 | This virtual method can be redefined in the derived class, in order
|
---|
| 134 | to perform the actual computation.
|
---|
| 135 | */
|
---|
[1612] | 136 | void ZThread::run()
|
---|
| 137 | {
|
---|
| 138 | if (_act) _act(_usp);
|
---|
| 139 | setRC(0);
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | /* ------ Classe ZMutex ------- */
|
---|
[2212] | 145 | /*!
|
---|
| 146 | \class SOPHYA::ZMutex
|
---|
| 147 | \ingroup SysTools
|
---|
[2598] | 148 | \brief Wrapper for Mutex objects.
|
---|
| 149 |
|
---|
[2212] | 150 | This class implements an interface to the Mutual Exclusion objects
|
---|
| 151 | of the POSIX threads.
|
---|
| 152 | The ZMutex objects should be used to control acces from different threads
|
---|
[2759] | 153 | to common objects through the \b lock() and \b unlock() methods.
|
---|
| 154 | When the \b signal() method is called, one of the waiting threads
|
---|
| 155 | on the corresponding mutex object (\b ZMutex::wait()) is awakened.
|
---|
| 156 | All waiting threads on the mutex object are awakened when \b broadcast()
|
---|
| 157 | is called.
|
---|
[2212] | 158 | \sa SOPHYA::ZSync
|
---|
| 159 | */
|
---|
[1612] | 160 |
|
---|
[2212] | 161 | /*!
|
---|
| 162 | Constructor: Creates a associated pair of POSIX \c pthread_mutex_t
|
---|
| 163 | and \c pthread_cond_t objects
|
---|
| 164 | */
|
---|
[1612] | 165 | ZMutex::ZMutex()
|
---|
| 166 | {
|
---|
| 167 | _mutx = new pthread_mutex_t;
|
---|
| 168 | _condv = new pthread_cond_t;
|
---|
| 169 | int rc;
|
---|
| 170 | rc = pthread_mutex_init(_mutx, NULL);
|
---|
| 171 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
|
---|
| 172 | rc = pthread_cond_init(_condv, NULL);
|
---|
| 173 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | ZMutex::~ZMutex()
|
---|
| 177 | {
|
---|
| 178 | delete _mutx;
|
---|
| 179 | delete _condv;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[2212] | 182 | /*!
|
---|
| 183 | \class SOPHYA::ZSync
|
---|
| 184 | \ingroup SysTools
|
---|
[2598] | 185 | \brief Wrapper/utility class ensuring synchronised execution of an instruction bloc.
|
---|
| 186 |
|
---|
[2212] | 187 | This class can be used to insure that the execution of a given
|
---|
| 188 | part of the code is synchronised, i.e. only a single thread
|
---|
| 189 | goes through this at a given time.
|
---|
| 190 | The constructor acquires a lock on given \b ZMutex object,
|
---|
| 191 | which is released by the destructor.
|
---|
| 192 | The ZSync object should then be instanciated at the beginning of
|
---|
| 193 | the synchronised instruction bloc.
|
---|
| 194 | \sa SOPHYA::ZMutex
|
---|
| 195 | */
|
---|