source: trunk/MagicSoft/Cosy/base/MThread.cc@ 8785

Last change on this file since 8785 was 8376, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 3.9 KB
Line 
1#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
10
11using namespace std;
12
13// ----------------------------------------------------------------------
14//
15// Constructor
16//
17// Starts the derived thread if you don't specify false.
18//
19MThread::MThread(bool start, int prio) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL)
20{
21#ifdef DEBUG
22 cout << "MThread::MThread" << endl;
23#endif
24 if (!start)
25 return;
26
27 SetPriority(prio);
28 Start();
29}
30
31// ----------------------------------------------------------------------
32//
33// Destructor
34//
35// Stops the derived thread if it is still running.
36//
37MThread::~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//
51void 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//
67bool 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//
85void *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//
116void *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//
130void 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//
142void 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 TracBrowser for help on using the repository browser.