| [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>
 | 
|---|
 | 8 | 
 | 
|---|
| [2212] | 9 | /*!
 | 
|---|
 | 10 |   \class SOPHYA::ZThread
 | 
|---|
 | 11 |   \ingroup SysTools
 | 
|---|
| [2598] | 12 |   \brief Simple class for creating and controlling threads.
 | 
|---|
 | 13 | 
 | 
|---|
| [2212] | 14 |   This class provides an interface for creating and controlling threads.
 | 
|---|
 | 15 |   The implementation uses the POSIX thread interface.
 | 
|---|
 | 16 |   The ZThread objects can be sub classed with the redefinition of 
 | 
|---|
 | 17 |   the \c run() method which then performs the task.
 | 
|---|
 | 18 |   The default \c run() method of the base class can be used directly
 | 
|---|
 | 19 |   to perform computation through a function (see \b setAction() method)
 | 
|---|
 | 20 |   \sa SOPHYA::ZMutex
 | 
|---|
 | 21 |   The following sample code shows the usage of ZThread object 
 | 
|---|
| [2487] | 22 |   to run simultaneously two functions to perform computation.
 | 
|---|
| [2212] | 23 | 
 | 
|---|
 | 24 |   \code
 | 
|---|
 | 25 |   // The functions to perform computing
 | 
|---|
 | 26 |   void fun1(void *arg) { }
 | 
|---|
 | 27 |   void fun2(void *arg) { }
 | 
|---|
 | 28 |   // ...
 | 
|---|
 | 29 |   ZThread zt1;
 | 
|---|
 | 30 |   zt1.setAction(fun1, arg[1]);
 | 
|---|
 | 31 |   ZThread zt2;
 | 
|---|
 | 32 |   zt2.setAction(fun2, arg[1]);
 | 
|---|
 | 33 |   cout << " Starting threads ... " << endl;
 | 
|---|
 | 34 |   zt1.start();
 | 
|---|
 | 35 |   zt2.start();
 | 
|---|
 | 36 |   cout << " Waiting for threads to end ... " << endl;  
 | 
|---|
 | 37 |   zt1.join();
 | 
|---|
 | 38 |   zt2.join();
 | 
|---|
 | 39 |   \endcode
 | 
|---|
 | 40 | */
 | 
|---|
 | 41 | 
 | 
|---|
| [1612] | 42 | #define CheckSt(st_, strg_)  if (st_ != 0)  perror(strg_); 
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 | /*
 | 
|---|
 | 45 | extern "C" {
 | 
|---|
 | 46 |   void * zthr_run( void * xthr);
 | 
|---|
 | 47 | }
 | 
|---|
 | 48 | */
 | 
|---|
 | 49 | 
 | 
|---|
 | 50 | static void * zthr_run(void * xthr)
 | 
|---|
 | 51 | {
 | 
|---|
 | 52 |   ZThread * thr = (ZThread *)xthr;
 | 
|---|
 | 53 |   thr->run();
 | 
|---|
 | 54 |   pthread_exit(NULL);
 | 
|---|
 | 55 |   return NULL;
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /* ------ Classe ZThread  -------  */
 | 
|---|
| [2212] | 60 | /*!
 | 
|---|
 | 61 |   Constructor, with optional specification of the thread stack size.
 | 
|---|
 | 62 | */
 | 
|---|
| [1612] | 63 | ZThread::ZThread(size_t stacksize)
 | 
|---|
 | 64 | {
 | 
|---|
 | 65 |   _initok = false;
 | 
|---|
 | 66 |   _ssize = 0;
 | 
|---|
 | 67 |   _act = NULL;
 | 
|---|
 | 68 |   _usp = NULL;
 | 
|---|
 | 69 |   _rc = -99;
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | ZThread::~ZThread()
 | 
|---|
 | 73 | {
 | 
|---|
 | 74 |   // Que faut-il faire ?
 | 
|---|
 | 75 | }
 | 
|---|
 | 76 | 
 | 
|---|
| [2212] | 77 | /*!
 | 
|---|
 | 78 |   Method which starts the thread.
 | 
|---|
 | 79 | */
 | 
|---|
| [1612] | 80 | void ZThread::start()
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
 | 
|---|
 | 83 |   int rc;
 | 
|---|
 | 84 |   pthread_attr_t tha;
 | 
|---|
 | 85 |   rc = pthread_attr_init(&tha);
 | 
|---|
 | 86 |   CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
 | 
|---|
 | 87 |   if (_ssize > 0) 
 | 
|---|
 | 88 |     rc = pthread_attr_setstacksize(&tha, _ssize);
 | 
|---|
 | 89 |   rc = pthread_create(&_thr, &tha, zthr_run, this);
 | 
|---|
 | 90 |   CheckSt(rc,"ZThread::start() - Pb creating the thread object");     
 | 
|---|
 | 91 |   _initok = true;
 | 
|---|
 | 92 |   setRC(rc);      
 | 
|---|
 | 93 | }
 | 
|---|
 | 94 | 
 | 
|---|
| [2212] | 95 | /*!
 | 
|---|
 | 96 |   Calls the pthread_cancel. Can be used to stop a running thread.
 | 
|---|
 | 97 | */
 | 
|---|
| [1612] | 98 | void ZThread::cancel()
 | 
|---|
 | 99 | {
 | 
|---|
 | 100 |   if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
 | 
|---|
 | 101 |   int rc = pthread_cancel(_thr);
 | 
|---|
 | 102 |   CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");  
 | 
|---|
 | 103 |   setRC(-77);      
 | 
|---|
 | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 | 
 | 
|---|
| [2212] | 107 | /*!
 | 
|---|
 | 108 |   Waits for the thread to terminate. 
 | 
|---|
 | 109 | */
 | 
|---|
| [1612] | 110 | void ZThread::join()
 | 
|---|
 | 111 | {
 | 
|---|
 | 112 |   if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
 | 
|---|
 | 113 |   void * x;
 | 
|---|
 | 114 |   int rc = pthread_join(_thr, &x);
 | 
|---|
 | 115 |   CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
 | 
|---|
 | 116 |   return;  
 | 
|---|
 | 117 | }
 | 
|---|
 | 118 | 
 | 
|---|
| [2212] | 119 | /*!
 | 
|---|
 | 120 |   This virtual method can be redefined in the derived class, in order 
 | 
|---|
 | 121 |   to perform the actual computation.
 | 
|---|
 | 122 | */
 | 
|---|
| [1612] | 123 | void ZThread::run()
 | 
|---|
 | 124 | {
 | 
|---|
 | 125 |   if (_act)  _act(_usp);
 | 
|---|
 | 126 |   setRC(0);      
 | 
|---|
 | 127 |   return;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 | /* ------ Classe ZMutex -------  */
 | 
|---|
| [2212] | 132 | /*!
 | 
|---|
 | 133 |   \class SOPHYA::ZMutex
 | 
|---|
 | 134 |   \ingroup SysTools
 | 
|---|
| [2598] | 135 |   \brief Wrapper for Mutex objects.
 | 
|---|
 | 136 | 
 | 
|---|
| [2212] | 137 |   This class implements an interface to the Mutual Exclusion objects
 | 
|---|
 | 138 |   of the POSIX threads. 
 | 
|---|
 | 139 |   The ZMutex objects should be used to control acces from different threads 
 | 
|---|
 | 140 |   to common objects through the \b lock() and \b unlock() methods.  
 | 
|---|
 | 141 |   \sa SOPHYA::ZSync
 | 
|---|
 | 142 | */
 | 
|---|
| [1612] | 143 | 
 | 
|---|
| [2212] | 144 | /*!
 | 
|---|
 | 145 |   Constructor: Creates a associated pair of POSIX \c pthread_mutex_t 
 | 
|---|
 | 146 |   and \c pthread_cond_t objects
 | 
|---|
 | 147 | */
 | 
|---|
| [1612] | 148 | ZMutex::ZMutex()
 | 
|---|
 | 149 | {
 | 
|---|
 | 150 |   _mutx = new pthread_mutex_t;
 | 
|---|
 | 151 |   _condv = new pthread_cond_t;
 | 
|---|
 | 152 |   int rc;
 | 
|---|
 | 153 |   rc = pthread_mutex_init(_mutx, NULL);
 | 
|---|
 | 154 |   CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
 | 
|---|
 | 155 |   rc = pthread_cond_init(_condv, NULL);
 | 
|---|
 | 156 |   CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | ZMutex::~ZMutex()
 | 
|---|
 | 160 | {
 | 
|---|
 | 161 |   delete _mutx;
 | 
|---|
 | 162 |   delete _condv;
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | 
 | 
|---|
| [2212] | 165 | /*!
 | 
|---|
 | 166 |   \class SOPHYA::ZSync
 | 
|---|
 | 167 |   \ingroup SysTools
 | 
|---|
| [2598] | 168 |   \brief Wrapper/utility class ensuring synchronised execution of an instruction bloc.
 | 
|---|
 | 169 | 
 | 
|---|
| [2212] | 170 |   This class can be used to insure that the execution of a given 
 | 
|---|
 | 171 |   part of the code is synchronised, i.e. only a single thread 
 | 
|---|
 | 172 |   goes through this at a given time.
 | 
|---|
 | 173 |   The constructor acquires a lock on given \b ZMutex object,
 | 
|---|
 | 174 |   which is released by the destructor.
 | 
|---|
 | 175 |   The ZSync object should then be instanciated at the beginning of 
 | 
|---|
 | 176 |   the synchronised instruction bloc.
 | 
|---|
 | 177 |   \sa SOPHYA::ZMutex
 | 
|---|
 | 178 | */
 | 
|---|