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

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