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