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

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