Changeset 8830 for trunk/MagicSoft


Ignore:
Timestamp:
01/24/08 11:49:25 (17 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Cosy
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/Makefile

    r8809 r8830  
    1919
    2020#
    21 PROGRAMS = cosy readcam
    22 SOLIB    = cosy.so
     21PROGRAMS = cosy readcam telesto
     22SOLIB    = libcosy.so
    2323CINT     = M
    2424MARS_LIB = libmars.so
    2525
    26 INCLUDES = -I. -Imain -Imars -Ibase -Icandrv -Iincl -Ivideodev -Igui -Imvideo -Itcpip
     26INCLUDES = -I. -Imain -Imars -Ibase -Icandrv -Iincl -Ivideodev \
     27           -Igui -Imvideo -Itcpip -Itpoint
    2728
    2829#
     
    4748          tcpip \
    4849          videodev \
    49           mvideo
     50          mvideo \
     51          tpoint
    5052
    5153#LIBRARIES = $(SUBDIRS:%=lib/lib%.a)
  • trunk/MagicSoft/Cosy/base/MThread.cc

    r8376 r8830  
    11#include <MThread.h>
    2 
    3 #include <iostream>
    4 
    5 #include <pthread.h>
    6 #include <sys/resource.h>  // PRIO_PROCESS
    7 
    8 #undef DEBUG
    9 //#define DEBUG
    102
    113using namespace std;
    124
    13 // ----------------------------------------------------------------------
    14 //
    15 // Constructor
    16 //
    17 // Starts the derived thread if you don't specify false.
    18 //
    19 MThread::MThread(bool start, int prio) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL)
     5TString MyThreadX::GetThreadStateStr() const
    206{
    21 #ifdef DEBUG
    22     cout << "MThread::MThread" << endl;
    23 #endif
    24     if (!start)
    25         return;
    26 
    27     SetPriority(prio);
    28     Start();
     7    switch (fThread.GetState())
     8    {
     9    case TThread::kInvalidState:
     10        return "Invalid - thread was not created properly";
     11    case TThread::kNewState:
     12        return "New - thread object exists but hasn't started";
     13    case TThread::kRunningState:
     14        return "Running - thread is running";
     15    case TThread::kTerminatedState:
     16        return "Terminated - thread has terminated but storage has not yet been reclaimed (i.e. waiting to be joined)";
     17    case TThread::kFinishedState:
     18        return "Finished - thread has finished";
     19    case TThread::kCancelingState:
     20        return "Canceling - thread in process of canceling";
     21    case TThread::kCanceledState:
     22        return "Canceled - thread has been canceled";
     23    case TThread::kDeletingState:
     24        return "Deleting - thread in process of deleting";
     25    };
     26    return "Unknown";
    2927}
    30 
    31 // ----------------------------------------------------------------------
    32 //
    33 // Destructor
    34 //
    35 // Stops the derived thread if it is still running.
    36 //
    37 MThread::~MThread()
    38 {
    39 #ifdef DEBUG
    40     cout << "~MThread::MThread" << endl;
    41 #endif
    42     Stop();
    43 }
    44 
    45 // ----------------------------------------------------------------------
    46 //
    47 // Detach the derived thread. This means, that if the thread ends the
    48 // needed resources (eg. for storing the return code) are freed.
    49 // In other word you cannot get any value back from the Thread.
    50 //
    51 void MThread::Detach()
    52 {
    53     if (fIsRunning)
    54         pthread_detach(*fThread);
    55 
    56     fIsDetached = true;
    57 }
    58 
    59 // ----------------------------------------------------------------------
    60 //
    61 // Sets the priority of the thread.
    62 //  -20 highest priority
    63 //    0 standard
    64 //  +20 lowest priority
    65 // This can only be done before the thread is started.
    66 //
    67 bool MThread::SetPriority(int prio)
    68 {
    69     if (fIsRunning)
    70         return false;
    71 
    72     fPriority = prio;
    73     return true;
    74 }
    75 
    76 // ----------------------------------------------------------------------
    77 //
    78 // Now we are back in a class instance, but running in new thread.
    79 // All class members can be accessed like before.
    80 // Set the flag for a running thread. Check if the thread should get
    81 // detached. Set the priority of the thread. Now reset the stop flag and
    82 // execute the thread. After the thread stopped it's execution reset the
    83 // running flag and exit.
    84 //
    85 void *MThread::RunThread()
    86 {
    87     fIsRunning = true;
    88 
    89     if (fIsDetached)
    90         pthread_detach(pthread_self());
    91 
    92     setpriority(PRIO_PROCESS, 0, fPriority); //lowest priority
    93 
    94     fStop = false;
    95 
    96 #ifdef DEBUG
    97     cout << "MThread::RunThread" << endl;
    98 #endif
    99 
    100     void *rc = Thread();
    101 
    102 #ifdef DEBUG
    103     cout << "MThread::RunThread...done." << endl;
    104 #endif
    105 
    106     fIsRunning = false;
    107 
    108     return rc;
    109 }
    110 
    111 // ----------------------------------------------------------------------
    112 //
    113 // Get the Instance back from the thread argument and call the
    114 // RunThread member function, which handles all MThread bspecific stuff.
    115 //
    116 void *MThread::MapThread(void *arg)
    117 {
    118     MThread *thread = (MThread*)arg;
    119 #ifdef DEBUG
    120     cout << "MThread::MapThread" << endl;
    121 #endif
    122     return thread->RunThread();
    123 }
    124 
    125 // ----------------------------------------------------------------------
    126 //
    127 // A thread is created and started (MapThread).
    128 // As an argument the actual instance is used.
    129 //
    130 void MThread::Start()
    131 {
    132     fThread = new pthread_t;
    133     pthread_create(fThread, NULL, MapThread, this);
    134 }
    135 
    136 // ----------------------------------------------------------------------
    137 //
    138 // Check if a thread is existing and running.
    139 // If the thread is detached, cancel the thread. Otherwise set the stop
    140 // flag and wait for the thread to exit.
    141 //
    142 void MThread::Stop()
    143 {
    144 #ifdef DEBUG
    145     cout << "MThread::Stop: fThread=" << fThread << ", fIsRunning=" << (int)fIsRunning << endl;
    146 #endif
    147 
    148     if (!fThread || !fIsRunning)
    149         return;
    150 
    151     if (fIsDetached)
    152     {
    153 #ifdef DEBUG
    154         cout << "Stopping detached thread..." << flush;
    155 #endif
    156         pthread_cancel(*fThread);
    157         fIsRunning = false;
    158     }
    159     else
    160     {
    161 #ifdef DEBUG
    162         cout << "Stopping thread..." << flush;
    163 #endif
    164         fStop = true;
    165         pthread_join(*fThread, &fReturn);
    166     }
    167 #ifdef DEBUG
    168     cout << "done." << endl;
    169 #endif
    170 
    171     delete fThread;
    172     fThread = NULL;
    173 
    174 #ifdef DEBUG
    175     cout << "MThread::Stop() done." << endl;
    176 #endif
    177 }
  • trunk/MagicSoft/Cosy/base/MThread.h

    r4105 r8830  
    1 #ifndef COSY_MThread
    2 #define COSY_MThread
     1#ifndef MARS_MThread
     2#define MARS_MThread
    33
    4 #ifdef __CINT__
    5 typedef unsigned long int pthread_t;
    6 #else
    7 #include <pthread.h>
     4#ifndef ROOT_TThread
     5#include <TThread.h>
    86#endif
    97
    10 class MThread
     8/*
     9class MyThread : public TThread
     10{
     11    friend class MyThreadX;
     12
     13private:
     14
     15    virtual Int_t Thread() = 0;
     16    static void *MapThread(void *arg)
     17    {
     18        // GetPriority();     High: -1 - -20, Norm: 0, Low: 1-20
     19        // pthread_setschedprio(SelfId(), priority);
     20        // 0: ok,
     21
     22        MyThread *th = (MyThread*)arg;
     23        return (void*)th->Thread();
     24    }
     25
     26public:
     27    MyThread(EPriority pri = kNormalPriority)
     28        : TThread(MapThread, this, pri) { }
     29    MyThread(const char *thname, EPriority pri = kNormalPriority)
     30        : TThread(thname, MapThread, this, pri) { }
     31};
     32*/
     33
     34class MyThreadX
    1135{
    1236private:
    13     bool fIsRunning;
    14     bool fIsDetached;
    15     bool fStop;
     37    TThread fThread;
    1638
    17     pthread_t *fThread;
     39    Int_t fNumCleanups;
    1840
    19     void *fReturn;
     41    virtual void CleanUp() { }
     42    static void MapCleanUp(void *arg)
     43    {
     44        MyThreadX *th = (MyThreadX*)arg;
     45        th->CleanUp();
     46    }
    2047
    21     int fPriority;
     48    virtual Int_t Thread() = 0;
     49    static void *MapThread(void *arg)
     50    {
     51        // GetPriority();     High: -1 - -20, Norm: 0, Low: 1-20
     52        // pthread_setschedprio(SelfId(), priority);
     53        // 0: ok,
    2254
    23     static void *MapThread(void *arg);
    24     void *RunThread();
    25     virtual void *Thread() = 0;
     55        TThread::CleanUpPush((void*)&MapCleanUp, arg);
     56
     57        MyThreadX *th = (MyThreadX*)arg;
     58        return (void*)th->Thread();
     59    }
    2660
    2761public:
    28     MThread(bool start=true, int prio=0);
    29     virtual ~MThread();
     62    MyThreadX(TThread::EPriority pri = TThread::kNormalPriority)
     63        : fThread(MapThread, this, pri), fNumCleanups(0) { }
     64    MyThreadX(const char *thname, TThread::EPriority pri = TThread::kNormalPriority)
     65        : fThread(thname, MapThread, this, pri), fNumCleanups(0) { }
     66    virtual ~MyThreadX() { }
    3067
    31     bool SetPriority(int prio);
     68//        Int_t            Kill() { return fThread.Kill(); }
     69        Int_t            RunThread(void *arg = 0) { return fThread.Run(); }
     70//    void             SetPriority(EPriority pri)
     71//    void             Delete(Option_t *option="") { TObject::Delete(option); }
     72//    EPriority        GetPriority() const { return fPriority; }
     73        TThread::EState  GetThreadState() const { return fThread.GetState(); }
     74        Long_t           GetThreadId() const { return fThread.GetId(); }
     75//        Long_t           Join(void **ret = 0) { return fThread.Join(ret); }
    3276
    33     void Detach();
     77        TString GetThreadStateStr() const;
    3478
    35     void Start();
    36     void Stop();
     79        Bool_t IsThreadRunning()  const { return fThread.GetState()==TThread::kRunningState; }
     80        Bool_t IsThreadCanceled() const { return fThread.GetState()==TThread::kCancelingState; }
    3781
    38     bool HasStopFlag() const { return fStop; }
     82        // Send cancel request and wait for cancellation
     83        // 13 is returned if thread is not running,
     84        // the return code of Join otherwise
     85        Int_t CancelThread(void **ret = 0) {
     86            const Int_t rc = fThread.Kill();
     87            if (rc==13) // Thread not running
     88                return rc;
     89            return fThread.Join(ret);
     90        }
     91
     92        // This is a version of usleep which is a cancel point
     93        static void Sleep(UInt_t us)
     94        {
     95            TThread::SetCancelOn();
     96            usleep(us);
     97            TThread::SetCancelOff();
     98        }
     99
     100
     101//   ClassDef(MyThreadX,0)  // Thread class
    39102};
    40103
  • trunk/MagicSoft/Cosy/base/coord.h

    r8376 r8830  
    22#define COORD_H
    33
    4 #include <math.h>          // floor
    5 #include <iostream>
     4#ifndef MARS_MPointing
     5#include "MPointing.h"
     6#endif
    67
    7 #include "MAGIC.h"
    8 
    9 /* pi/180:  degrees to radians */
    10 const double kDeg2Rad = 0.017453292519943295769236907684886127134428718885417;
    11 
    12 /* 180/pi:  radians to degrees */
    13 //const double kRad2Deg = 57.295779513082320876798154814105170332405472466564;
    14 
    15 /* pi/2:  90 degrees in radians */
    16 const double kPiDiv2 = 1.5707963267948966192313216916397514420985846996876;
    17 
    18 /* 2pi */
    19 const double k2Pi = 6.2831853071795864769252867665590057683943387987502;
    20 
    21 class Deg
    22 {
    23 protected:
    24     double fDeg;
    25 
    26 public:
    27     Deg(const double d) : fDeg(d) {}
    28     Deg(const Deg &c) { fDeg = c.fDeg; }
    29 
    30     void Set(double d) { fDeg=d; }
    31 
    32     double operator()() { return fDeg; }
    33 
    34     operator double() const { return fDeg*kDeg2Rad; }
    35 };
    36 
    37 class XY
    38 {
    39     friend istream& operator>>(istream &in, XY &xy);
    40     friend ostream& operator<<(ostream &in, XY &xy);
    41 
    42 protected:
    43     double fX;
    44     double fY;
    45 
    46 public:
    47     XY(double x=0, double y=0) : fX(x), fY(y) {}
    48     XY(const XY &c) { fX = c.fX; fY = c.fY; }
    49 
    50     void Set(double x, double y) { fX=x; fY=y; }
    51 
    52     double X() const { return fX; }
    53     double Y() const { return fY; }
    54 
    55     void X(double x) { fX=x; }
    56     void Y(double y) { fY=y; }
    57 
    58     void operator/=(double c) { fX/=c; fY/=c; }
    59     void operator*=(double c) { fX*=c; fY*=c; }
    60 
    61     XY operator/(double c) const { return XY(fX/c, fY/c); }
    62     XY operator*(double c) const { return XY(fX*c, fY*c); }
    63     XY operator*(const XY &c) const { return XY(fX*c.fX, fY*c.fY); }
    64     XY operator/(const XY &c) const { return XY(fX/c.fX, fY/c.fY); }
    65     XY operator+(const XY &c) const { return XY(fX+c.fX, fY+c.fY); }
    66     XY operator-(const XY &c) const { return XY(fX-c.fX, fY-c.fY); }
    67     XY operator-() const { return XY(-fX, -fY); }
    68 
    69     double Sqr()   const { return fX*fX + fY*fY; }
    70     double Sqrt()  const { return sqrt(Sqr()); }
    71     double Ratio() const { return fX/fY; }
    72     void Round()         { fX=(int)(floor(fX+.5)); fY=(int)(floor(fY+.5)); }
    73 };
    74 
    75 inline std::istream& operator>>(std::istream &in,  XY &xy) { in  >> xy.fX; in  >> xy.fY; return in; }
    76 inline std::ostream& operator<<(std::ostream &out, const XY &xy) { out << xy.X() << " " << xy.Y(); return out; }
    77 
    78 class AltAz : public XY
    79 {
    80 public:
    81     AltAz(double alt=0, double az=0) : XY(alt, az) {}
    82 
    83     double Alt() const { return fX; }
    84     double Az()  const { return fY; }
    85 
    86     void operator*=(double c) { fX*=c; fY*=c; }
    87     void operator/=(double c) { fX*=c; fY*=c; }
    88 
    89     void Alt(double d) { fX=d; }
    90     void Az(double d)  { fY=d; }
    91     void operator*=(const XY &c)    { fX*=c.X(); fY*=c.Y(); }
    92     void operator/=(const XY &c)    { fX/=c.X(); fY/=c.Y(); }
    93     void operator-=(const AltAz &c) { fX-=c.fX; fY-=c.fY; }
    94     void operator+=(const AltAz &c) { fX+=c.fX; fY+=c.fY; }
    95 
    96     AltAz operator/(double c) const { return AltAz(fX/c, fY/c); }
    97     AltAz operator*(double c) const { return AltAz(fX*c, fY*c); }
    98     AltAz operator*(const XY &c) const { return AltAz(fX*c.X(), fY*c.Y()); }
    99     AltAz operator/(const XY &c) const { return AltAz(fX/c.X(), fY/c.Y()); }
    100     AltAz operator+(const AltAz &c) const { return AltAz(fX+c.fX, fY+c.fY); }
    101     AltAz operator-(const AltAz &c) const { return AltAz(fX-c.fX, fY-c.fY); }
    102     AltAz operator-() const { return AltAz(-fX, -fY); }
    103 };
    104 
    105 class ZdAz : public XY
    106 {
    107 public:
    108     ZdAz(double zd=0, double az=0) : XY(zd, az) {}
    109     ZdAz(const ZdAz &c) : XY(c) {}
    110 
    111     void operator*=(double c) { fX*=c; fY*=c; }
    112     void operator/=(double c) { fX*=c; fY*=c; }
    113 
    114     double Zd() const { return fX; }
    115     double Az() const { return fY; }
    116 
    117     void Zd(double d) { fX=d; }
    118     void Az(double d) { fY=d; }
    119     void operator*=(const XY &c)   { fX*=c.X(); fY*=c.Y(); }
    120     void operator/=(const XY &c)   { fX/=c.X(); fY/=c.Y(); }
    121     void operator-=(const ZdAz &c) { fX-=c.fX; fY-=c.fY; }
    122     void operator+=(const ZdAz &c) { fX+=c.fX; fY+=c.fY; }
    123 
    124     ZdAz operator/(double c) const { return ZdAz(fX/c, fY/c); }
    125     ZdAz operator*(double c) const { return ZdAz(fX*c, fY*c); }
    126     ZdAz operator*(const XY &c) const { return ZdAz(fX*c.X(), fY*c.Y()); }
    127     ZdAz operator/(const XY &c) const { return ZdAz(fX/c.X(), fY/c.Y()); }
    128     ZdAz operator+(const ZdAz &c) const { return ZdAz(fX+c.fX, fY+c.fY); }
    129     ZdAz operator-(const ZdAz &c) const { return ZdAz(fX-c.fX, fY-c.fY); }
    130     ZdAz operator-() const { return ZdAz(-fX, -fY); }
    131 };
    132 
    133 class RaDec : public XY
    134 {
    135 public:
    136     RaDec(double ra=0, double dec=0) : XY(ra, dec) {}
    137 
    138     double Ra()  const { return fX; }
    139     double Dec() const { return fY; }
    140 
    141     void operator*=(double c) { fX*=c; fY*=c; }
    142     void operator/=(double c) { fX*=c; fY*=c; }
    143 
    144     void Ra(double x)  { fX = x; }
    145     void Dec(double y) { fY = y; }
    146 
    147     RaDec operator/(double c) const { return RaDec(fX/c, fY/c); }
    148     RaDec operator*(double c) const { return RaDec(fX*c, fY*c); }
    149     RaDec operator*(const XY &c) const { return RaDec(fX*c.X(), fY*c.Y()); }
    150     RaDec operator+(const RaDec &c) const { return RaDec(fX+c.fX, fY+c.fY); }
    151     RaDec operator-(const RaDec &c) const { return RaDec(fX-c.fX, fY-c.fY); }
    152     RaDec operator-() const { return RaDec(-fX, -fY); }
    153 };
    154 
    155 inline double Rad2Deg(double rad)
    156 {
    157     return kRad2Deg*rad;
    158 }
    159 
    160 inline double Deg2Rad(double rad)
    161 {
    162     return kDeg2Rad*rad;
    163 }
    1648#endif
  • trunk/MagicSoft/Cosy/base/msgqueue.cc

    r8376 r8830  
    11#include "msgqueue.h"
    2 
    3 #include <iostream>
    4 
    5 #include <unistd.h>        // usleep
    6 #include <sys/resource.h>  // PRIO_PROCESS
    7 
    8 #undef DEBUG
    92
    103using namespace std;
     
    147// This creates the Message queue thread,
    158//
    16 MsgQueue::MsgQueue() : fBreak(0)
     9MsgQueue::MsgQueue() : MyThreadX("MsqQueue"), fNextMsg(0), fNextPtr(0)
    1710{
    18     fMp = new unsigned char;
    19     pthread_create(&fThread, NULL, MapThread, this);
     11    RunThread();
    2012}
    2113
     
    2618MsgQueue::~MsgQueue()
    2719{
    28 #ifdef DEBUG
    29     cout << "~MsgQueue::MsgQueue" << endl;
    30 #endif
    31     pthread_cancel(fThread);
    32     delete (unsigned char*)fMp;
     20    CancelThread();
     21
     22    fMuxMsg.Lock();
     23
     24    if (fNextPtr)
     25    {
     26        delete [] fNextPtr;
     27        fNextPtr = 0;
     28    }
     29
     30    fMuxMsg.UnLock();
    3331}
    3432
     
    4543// the old action can be finished correctly by the user.
    4644//
    47 void *MsgQueue::Proc(int msg, void *mp)
     45Int_t MsgQueue::Proc(int msg, void *mp)
    4846{
    49     return NULL;
     47    return 0;
    5048}
    5149
    52 // --------------------------------------------------------------------------
    53 //
    54 // This is the thread mapper.
    55 //
    56 void *MsgQueue::MapThread(void *arg)
     50int MsgQueue::Break() const
    5751{
    58     pthread_detach(pthread_self());
    59 
    60     setpriority(PRIO_PROCESS, 0, -5);
    61 
    62     ((MsgQueue*)arg)->Thread();
    63 
    64     return NULL;
     52    return fNextMsg>0 || IsThreadCanceled();
    6553}
    6654
     
    7260// in this thread. This makes sure, that the calling program is not stalled.
    7361//
    74 void MsgQueue::Thread()
     62Int_t MsgQueue::Thread()
    7563{
    76     //
    77     // Tell the poster that processing is done
    78     //
    79     fStart = 0;
    80     while (!fBreak)
     64    while (1)
     65    {
     66        Int_t msg = 0;
     67        char *ptr = 0;
     68
     69        fMuxMsg.Lock();
     70
     71        if (fNextMsg)
     72        {
     73            msg = fNextMsg;
     74            ptr = fNextPtr;
     75
     76            fNextMsg = 0;
     77            fNextPtr = 0;
     78        }
     79
     80        fMuxMsg.UnLock();
     81
     82        if (ptr)
     83        {
     84            Proc(msg, ptr);
     85            delete [] ptr;
     86        }
     87
    8188        usleep(1);
     89        TThread::CancelPoint();
     90    }
     91    return 0;
     92}
    8293
    83     while(1)
    84     {
    85         while (!fStart)
    86             usleep(1);
    87         fStart = 0;
    88 
    89         //
    90         // This makes sure that also a very fast Break() after
    91         // a PostMsg is processed correctly
    92         //
    93         if (fMuxMsg.Lock()==13)
    94             cout << "MsgQueue::Thread - mutex is already locked by this thread." << endl;
    95         fBreak = 0;
    96         if (fMuxMsg.UnLock()==13)
    97             cout << "MsgQueue::Thread - tried to unlock mutex locked by other thread." << endl;
    98 
    99 #ifdef DEBUG
    100         cout << "MsgQueue::Thread: Processing Msg 0x" << hex << fMsg << endl;
    101 #endif
    102         // --- bool quit = fMsg==WM_QUIT;
    103         fRc=Proc(fMsg, fMp);
    104 #ifdef DEBUG
    105         cout << "MsgQueue::PostMsg: Msg 0x" << hex << fMsg << " processed (rc=" << fRc << ")" << endl;
    106 #endif
    107 
    108         // --- if (quit)
    109         // ---    break;
    110     }
    111 
    112     // --- fStart = 0;
    113     // --- cout << "WM_QUIT posted... leaving msg queue." << endl;
    114 }
    11594
    11695// --------------------------------------------------------------------------
     
    123102void *MsgQueue::PostMsg(int msg, void *mp, int size)
    124103{
    125     //
    126     // Lock Mutex, put msg on stack and tell thread to process message
    127     //
     104    fMuxMsg.Lock();
    128105
    129     //
    130     // Make sure that only one Proc() is running and can be stopped
    131     // stopped and the messages are processed serialized
    132     //
    133 #ifdef DEBUG
    134     cout << "MsgQueue::PostMsg: Locking MsgQueue mutex..." << flush;
    135 #endif
    136     if (fMuxMsg.Lock()==13)
    137         cout << "MsgQueue::PostMsg - mutex is already locked by this thread." << endl;
    138 #ifdef DEBUG
    139     cout << "done." << endl;
    140 #endif
     106    fNextMsg = msg;
    141107
    142     //
    143     // Set break state and wait until Proc() returned (break state deleted)
    144     // This means, that a new command is invoked and (if forseen) the
    145     // running command should stop execution.
    146     //
    147     // This is some kind of controlled user break without using signals
    148     //
    149     /**** NEW 20/01/2003 ****/
    150     if (fBreak)
    151     {
    152         if (fMuxMsg.UnLock()==13)
    153             cout << "MsgQueue::PostMsg - tried to unlock mutex locked by other thread." << endl;
    154 #ifdef DEBUG
    155         cout << "------------> MsgQueue::PostMsg: Proc still pending... Message IGNORED." << endl;
    156 #endif
    157         return NULL;
    158     }
    159     /**** NEW 20/01/2003 ****/
    160 #ifdef DEBUG
    161     cout << "MsgQueue::PostMsg: ---> Break <---" << endl;
    162 #endif
    163     fBreak = 1;
     108    if (fNextPtr)
     109        delete [] fNextPtr;
     110    fNextPtr = new char[size];
    164111
    165     //
    166     // copy return code from Proc() and set new message
    167     //
    168     void *rc = fRc;
     112    memcpy(fNextPtr, mp, size);
    169113
    170     fMsg = msg;
     114    fMuxMsg.UnLock();
    171115
    172     delete (unsigned char*)fMp;
    173     fMp = new unsigned char[size];
    174 
    175     memcpy(fMp, mp, size);
    176 
    177     //
    178     // Start Proc()
    179     //
    180 #ifdef DEBUG
    181     cout << "MsgQueue::PostMsg: Releasing MsgQueue mutex..." << flush;
    182 #endif
    183     fStart = 1;
    184     if (fMuxMsg.UnLock()==13)
    185         cout << "MsgQueue::PostMsg - tried to unlock mutex locked by other thread." << endl;
    186 #ifdef DEBUG
    187     cout << "done." << endl;
    188 #endif
    189 
    190     /*
    191      * **** NEW 20/01/2003 ***
    192      *
    193      * This can halt the main thread, because it is waiting until
    194      * Proc has finished its execution which can take a while
    195      *
    196      * A side effect is, because you don't wait for the end of
    197      * the execution of Proc, if a new message is posted fBreak
    198      * and fStart is set again, new values are copied to fMsg and
    199      * fMp (FIXME? Harmefull?) and the message is not processed at all.
    200      */
    201     //while (fStart)
    202     //    usleep(1);
    203  
    204 #ifdef DEBUG
    205     cout << "MsgQueue::PostMsg: Returning rc = " << hex << rc << endl;
    206 #endif
    207     return rc;
     116    return 0;
    208117}
    209 /*
    210 Start positioning.
    211 MsgQueue::PostMsg: Locking MsgQueue mutex...done.
    212 MsgQueue::PostMsg: ---> Break <---
    213 +++++ MsgQueue::PostMsg: Releasing MsgQueue mutex...done.
    214 
    215 ===== MsgQueue::PostMsg: Returning rc = (nil)
    216 PostMsg (WM_Position) returned.
    217 done.
    218 Stopping movement...Movement stopped.
    219 WM_Position: done. (return 0x7777)
    220 MsgQueue::PostMsg: Msg 0x1001 processed (rc=0x7777)
    221 MsgQueue::Thread: Processing Msg 0x1001
    222 WM_Position: start.
    223 Positioning to Target...
    224  */
    225 /*
    226 Start positioning.
    227 MsgQueue::PostMsg: Locking MsgQueue mutex...done.
    228 MsgQueue::PostMsg: ---> Break <---
    229 +++++ MsgQueue::PostMsg: Releasing MsgQueue mutex...done.
    230 
    231 done.
    232 Stopping movement...Movement stopped.
    233 WM_Position: done. (return 0x7777)
    234 MsgQueue::PostMsg: Msg 0x1001 processed (rc=0x7777)
    235 MsgQueue::Thread: Processing Msg 0x1001
    236 WM_Position: start.
    237 Positioning to Target...
    238 ===== MsgQueue::PostMsg: Returning rc = (nil)
    239 */
  • trunk/MagicSoft/Cosy/base/msgqueue.h

    r2518 r8830  
    1 #ifndef MSGQUEUE_H
    2 #define MSGQUEUE_H
    3 
    4 #include "threads.h"
    5 
    6 #define WM_NULL 0x0000
    7 #define WM_QUIT 0xffff
     1#ifndef COSY_MsgQueue
     2#define COSY_MsgQueue
    83
    94#ifndef ROOT_TMutex
     
    116#endif
    127
    13 class MsgQueue
     8#ifndef MARS_MThread
     9#include "MThread.h"
     10#endif
     11
     12#define WM_NULL 0x0000
     13#define WM_QUIT 0xffff
     14
     15class MsgQueue : public MyThreadX
    1416{
    1517private:
    16     int fBreak;
    17     int fStart;
    18     int fStop;
     18    int    fNextMsg;
     19    char  *fNextPtr;
    1920
    20     int fMsg;     // Message identifier
    21     void *fMp;    // Message Parameter
    22     void *fSize;  // Message Parameter Size
    23     void *fRc;    // Proc return code
     21    TMutex fMuxMsg;
    2422
    25     pthread_t fThread;
    26     TMutex    fMuxMsg;
    27 
    28     static void *MapThread(void *arg);
    29 
    30     void Thread();
     23    Int_t Thread();
    3124
    3225public:
     
    3427    virtual ~MsgQueue();
    3528
    36     int Break() const { return fBreak; }
     29    int Break() const;
    3730
    38     virtual void *Proc(int msg, void *mp1);
    39     void *Proc(int msg) { return Proc(msg, 0); }
     31    virtual Int_t Proc(int msg, void *mp1);
     32    Int_t Proc(int msg) { return Proc(msg, 0); }
    4033
    4134    void *PostMsg(int msg, void *mp1, int size);
Note: See TracChangeset for help on using the changeset viewer.