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