Ignore:
Timestamp:
01/24/08 11:49:25 (17 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 }
Note: See TracChangeset for help on using the changeset viewer.