-
Notifications
You must be signed in to change notification settings - Fork 0
/
art_thread.cpp
269 lines (221 loc) · 6.54 KB
/
art_thread.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "art_thread.h"
ArtThreadBase:: ArtThreadBase ( string name_init, int priority_init, int policy_init ,bool verbose_init )
{
pid = -1;
errors.error = 0;
is_running = false;
setPriority(priority_init);
//cout<<"Priority="<<getPriority()<<endl;
setPolicy(policy_init);
setVerbose(verbose_init);
if (name_init.length() == 0) name = ART_THREAD_DEF_NAME;
else
{
name = name_init;
//fprintf (stderr, "NAME = %s \n", name.c_str());
}
}
ArtThreadBase:: ~ArtThreadBase ()
{
stop(true);
if (verbose) fprintf(stderr, "WARNING :: %s :: The thread is deleted\n", name.c_str() );
}
void ArtThreadBase:: start()
{
pthread_attr_t attr;
pthread_attr_init ( &attr );//Initialisation for the thread's attributes by default (inherited from the thread creater)
pthread_attr_setinheritsched(&(attr), PTHREAD_EXPLICIT_SCHED);
sched_param param;
param.sched_priority = priority;
pthread_attr_setschedparam (&attr, ¶m);
pthread_attr_setschedpolicy(&attr, policy);
//return;
run_thread = true;
int create_return = 0;
//ArtThreadBaseContainer container(this);
//create_return = pthread_create ( &pid, &attr, thread_function, &container ); //Thread creation
//thread_function(this);
create_return = pthread_create ( &pid, &attr, thread_function, this ); //Thread creation
//cout<<"Create return="<<create_return<<endl;
//return;
// !!!!! PRINTING A MESSAGE
if (create_return)
{
if (verbose) fprintf (stderr, "\nERROR!!! :: %s :: Can't creat thread !!!\n", name.c_str());
errors.CREATING_ERROR = 1;
}
else
{
setPriority(priority);
setPolicy(policy);
errors.CREATING_ERROR = 0;
if (verbose) fprintf (stderr, "\n%s :: PID=%d :: The thread has been created!!!\n", name.c_str(), pid );
}
}
//Stops the thread (requires reimplementation of the exit() function )
void ArtThreadBase:: stop(bool force )
{
exit();
if (force) pthread_cancel(pid);
pthread_join(pid, NULL);
if (verbose) fprintf (stderr, "WARNING :: %s :: The thread is stopped\n", name.c_str() );
}
bool ArtThreadBase:: isRunning() {return is_running;}
int ArtThreadBase:: setPriority (int priority_init )
{
int return_val = 0;
if (priority_init >=0)
{
priority = priority_init;
}
else
{
priority = getParentPriority();
}
//if (verbose) fprintf(stderr, "\n%s :: PID=%ld :: Current priority == %d !!!\n", name.c_str(), pid, getPriority() );
//if ( isRunning() )
//{
// struct sched_param param;
// sched_getparam(pid, ¶m);
// param.sched_priority = priority;
//
// return_val = sched_setparam ( pid, ¶m );
// if (return_val && verbose) fprintf(stderr, "\nERROR!!! :: %s :: PID=%ld :: Can't set priority == %d. Current priority == %d !!!\n", name.c_str(), pid ,priority, getPriority() );
// else if (verbose) fprintf(stderr, "\n%s:: PID=%ld :: Current priority is == %d !!!\n", name.c_str(), pid, getPriority() );
//}
return return_val;
}
int ArtThreadBase:: getPriority ()
{
if (isRunning())
{
int policy;
struct sched_param sched_param;
pthread_getschedparam (pid, &policy, &sched_param);
return sched_param.sched_priority;
}
else
{
return priority;
}
}
int ArtThreadBase:: getParentPriority ()
{
pthread_t pid_root;
pthread_attr_t attr_root;
int root_policy;
struct sched_param root_sched_param;
pid_root=pthread_self ();
int result = pthread_getschedparam (pid_root, &root_policy, &root_sched_param);
if (result) fprintf(stderr, "\nERROR!!! :: ArtThreadBase :: Can't get parent priority !!!\n" );
return root_sched_param.sched_priority;
}
int ArtThreadBase::getParentParam(ArtThreadParam &root_param)
{
int result = -1;
pthread_t pid_root;
pthread_attr_t attr_root;
int root_policy;
struct sched_param root_sched_param;
pid_root=pthread_self ();
result = pthread_getschedparam (pid_root, &root_policy, &root_sched_param);
if (result) fprintf(stderr, "\nERROR!!! :: ArtThreadBase :: Can't get parent priority !!!\n" );
else
{
root_param.policy = root_policy;
root_param.priority = root_sched_param.sched_priority;
}
return result;
}
void ArtThreadBase:: setVerbose (bool verbose_ ) {verbose = true;}
pthread_t ArtThreadBase:: getPID () {return pid;}//Using pid to change attributes of the thread isn't recommended
int ArtThreadBase:: priorityMin()
{
return priorityMin( getPolicy() );
}
int ArtThreadBase:: priorityMin(int policy_init)
{
return sched_get_priority_min( policy_init );
}
int ArtThreadBase:: priorityMax()
{
return priorityMax( getPolicy() );
}
//If policy < 0 the function returns max priority of the policy of the current thread
int ArtThreadBase:: priorityMax(int policy_init)
{
return sched_get_priority_max( policy_init );
}
int ArtThreadBase:: setPolicy (int policy_init)
{
int result = -1;
if(policy_init < 0)
{
ArtThreadParam root_param;
getParentParam(root_param);
policy_init = root_param.policy;
}
policy = policy_init;
// if (isRunning())
// {
// int policy_cur = -1;
// sched_param param;
// result = pthread_getschedparam(pid, &policy_cur, ¶m);
//
// if (result)
// {
// policy_cur = -1;
// if (verbose) fprintf(stderr, "\nERROR!!! :: %s :: PID=%ld :: Can't get current parameters. \n", name.c_str(), pid );
// }
//
// result = pthread_setschedparam(pid, policy_init, ¶m);
// if (result)
// {
// if (verbose) fprintf(stderr, "\nERROR!!! :: %s :: PID=%ld :: Can't set the policy == %d\n", name.c_str(), pid, policy_init );
// }
//
// }
return result;
}
int ArtThreadBase:: getPolicy ()
{
int policy_out = -1;
if (isRunning())
{
sched_param param;
int result = pthread_getschedparam(pid, &policy_out, ¶m);
if (result)
{
policy_out = -1;
if (verbose) fprintf(stderr, "\nERROR!!! :: %s :: PID=%ld :: Can't get policy.\n", name.c_str(), pid );
}
}
else
{
policy_out = policy;
}
return policy_out;
}
void ArtThreadBase:: run()
{
while (run_thread)
{
sleep(1);
if(verbose) cout<<name<<": Works!"<<" Priorityy="<<getPriority()<<endl;
}
}
int ArtThreadBase:: exit()
{
run_thread = false;
pthread_join(pid, NULL);
}
void * ArtThreadBase:: thread_function(void * par)
{
//ArtThreadBase * obj = reinterpret_cast<ArtThreadBase *> (par);
ArtThreadBase * obj = (ArtThreadBase *) par;
//ArtThreadBaseContainer * cont = (ArtThreadBaseContainer *) par;
//ArtThreadBase * obj = cont->object;
obj->is_running = true;
obj->run();
obj->is_running = false;
}