[1612] | 1 | // Interface C++ aux Thread POSIX - R. Ansari 02/2001
|
---|
| 2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 3 |
|
---|
| 4 | #include "zthread.h"
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #include <stdio.h>
|
---|
| 7 |
|
---|
| 8 | #define CheckSt(st_, strg_) if (st_ != 0) perror(strg_);
|
---|
| 9 |
|
---|
| 10 | /*
|
---|
| 11 | extern "C" {
|
---|
| 12 | void * zthr_run( void * xthr);
|
---|
| 13 | }
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | static void * zthr_run(void * xthr)
|
---|
| 17 | {
|
---|
| 18 | ZThread * thr = (ZThread *)xthr;
|
---|
| 19 | thr->run();
|
---|
| 20 | pthread_exit(NULL);
|
---|
| 21 | return NULL;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 |
|
---|
| 25 | /* ------ Classe ZThread ------- */
|
---|
| 26 |
|
---|
| 27 | ZThread::ZThread(size_t stacksize)
|
---|
| 28 | {
|
---|
| 29 | _initok = false;
|
---|
| 30 | _ssize = 0;
|
---|
| 31 | _act = NULL;
|
---|
| 32 | _usp = NULL;
|
---|
| 33 | _rc = -99;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | ZThread::~ZThread()
|
---|
| 37 | {
|
---|
| 38 | // Que faut-il faire ?
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void ZThread::start()
|
---|
| 42 | {
|
---|
| 43 | if (_initok) throw ZThreadExc("ZThread::Start() - Already started thread !");
|
---|
| 44 | int rc;
|
---|
| 45 | pthread_attr_t tha;
|
---|
| 46 | rc = pthread_attr_init(&tha);
|
---|
| 47 | CheckSt(rc,"ZThread::start() - Pb creating tha attribute object");
|
---|
| 48 | if (_ssize > 0)
|
---|
| 49 | rc = pthread_attr_setstacksize(&tha, _ssize);
|
---|
| 50 | rc = pthread_create(&_thr, &tha, zthr_run, this);
|
---|
| 51 | CheckSt(rc,"ZThread::start() - Pb creating the thread object");
|
---|
| 52 | _initok = true;
|
---|
| 53 | setRC(rc);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | void ZThread::cancel()
|
---|
| 58 | {
|
---|
| 59 | if (!_initok) throw ZThreadExc("ZThread::cancel() - thread not started !");
|
---|
| 60 | int rc = pthread_cancel(_thr);
|
---|
| 61 | CheckSt(rc,"ZThread::cancel() - Pb pthread_cancel() ");
|
---|
| 62 | setRC(-77);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | void ZThread::join()
|
---|
| 67 | {
|
---|
| 68 | if (!_initok) throw ZThreadExc("ZThread::join() - thread not started !");
|
---|
| 69 | void * x;
|
---|
| 70 | int rc = pthread_join(_thr, &x);
|
---|
| 71 | CheckSt(rc,"ZThread::Join() - Pb pthread_join() ");
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void ZThread::run()
|
---|
| 76 | {
|
---|
| 77 | if (_act) _act(_usp);
|
---|
| 78 | setRC(0);
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /* ------ Classe ZMutex ------- */
|
---|
| 84 |
|
---|
| 85 | ZMutex::ZMutex()
|
---|
| 86 | {
|
---|
| 87 | _mutx = new pthread_mutex_t;
|
---|
| 88 | _condv = new pthread_cond_t;
|
---|
| 89 | int rc;
|
---|
| 90 | rc = pthread_mutex_init(_mutx, NULL);
|
---|
| 91 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_mutex_init");
|
---|
| 92 | rc = pthread_cond_init(_condv, NULL);
|
---|
| 93 | CheckSt(rc,"ZMutex::ZMutex() Pb pthread_cond_init");
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | ZMutex::~ZMutex()
|
---|
| 97 | {
|
---|
| 98 | delete _mutx;
|
---|
| 99 | delete _condv;
|
---|
| 100 | }
|
---|
| 101 |
|
---|