-
Notifications
You must be signed in to change notification settings - Fork 1
/
ptask.c
198 lines (164 loc) · 3.76 KB
/
ptask.c
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
#include "ptask.h"
#include <sched.h>
#include <time.h>
#include <pthread.h>
#include <math.h>
#define NT 50 //Maximum Number of Tasks
struct task_par {
int arg;
long wcet;
int period; // [micro sec]
int deadline; // [micro sec]
int priority;
int dmiss;
struct timespec at;
struct timespec dl;
};
// Mutex for managing task informations
static pthread_mutex_t mux_task[NT];
void init_mutex()
{
int i;
for(i=0; i<NT; i++)
pthread_mutex_init(&mux_task[i], NULL);
}
static struct task_par tp[NT];
pthread_t tid[NT];
int policy = SCHED_FIFO;
//----------------------------
// Time management functions
//----------------------------
void time_copy(struct timespec *td, struct timespec ts)
{
td->tv_sec = ts.tv_sec;
td->tv_nsec = ts.tv_nsec;
}
void time_add_us (struct timespec *t, int us)
{
t->tv_sec += us/1000000;
t->tv_nsec += (us%1000000)*1000;
if (t->tv_nsec > (1000000000)) {
t->tv_nsec -= 1000000000;
t->tv_sec += 1;
}
}
int time_cmp (struct timespec t1, struct timespec t2)
{
if (t1.tv_sec > t2.tv_sec) return 1;
if (t1.tv_sec < t2.tv_sec) return -1;
if (t1.tv_nsec > t2.tv_nsec) return 1;
if (t1.tv_nsec > t2.tv_nsec) return -1;
return 0;
}
int time_diff_nsec (struct timespec t1, struct timespec t2)
{
return (t1.tv_sec-t2.tv_sec)*pow(10, 9)+(t1.tv_nsec-t2.tv_nsec);
}
//----------------------------
// Task management functions
//----------------------------
void pt_ptask_init (int scheduler)
{
policy = scheduler;
}
int pt_task_create(void* (*task) (void*), int i, int period, int drel, int prio)
{
pthread_attr_t myatt;
struct sched_param mypar;
int tret;
if (i >= NT) return -1;
tp[i].arg = i;
pthread_mutex_lock(&mux_task[i]);
tp[i].period = period;
tp[i].deadline = drel;
pthread_mutex_unlock(&mux_task[i]);
tp[i].priority = prio;
tp[i].dmiss = 0;
pthread_attr_init(&myatt);
pthread_attr_setinheritsched(&myatt, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&myatt, SCHED_FIFO);
mypar.sched_priority = tp[i].priority;
pthread_attr_setschedparam(&myatt, &mypar);
tret = pthread_create(&tid[i], &myatt, task, (void *)(&tp[i]));
return tret;
}
int pt_get_index(void* arg)
{
struct task_par *tpar;
tpar = (struct task_par *)arg;
return tpar->arg;
}
int pt_get_period(int i)
{
int per;
pthread_mutex_lock(&mux_task[i]);
per = tp[i].period;
pthread_mutex_unlock(&mux_task[i]);
return per;
}
void pt_set_period(int i, int per)
{
pthread_mutex_lock(&mux_task[i]);
tp[i].period = per;
pthread_mutex_unlock(&mux_task[i]);
}
void pt_set_deadline(int i, int drel)
{
pthread_mutex_lock(&mux_task[i]);
tp[i].deadline = drel;
pthread_mutex_unlock(&mux_task[i]);
}
int pt_get_deadline(int i)
{
int drel;
pthread_mutex_lock(&mux_task[i]);
drel = tp[i].deadline;
pthread_mutex_unlock(&mux_task[i]);
return drel;
}
int pt_get_dmiss(int i)
{
int misses;
pthread_mutex_lock(&mux_task[i]);
misses = tp[i].dmiss;
pthread_mutex_unlock(&mux_task[i]);
return misses;
}
void pt_set_activation (int i)
{
struct timespec t;
int per, drel;
clock_gettime(CLOCK_MONOTONIC, &t);
time_copy(&(tp[i].at), t);
time_copy(&(tp[i].dl), t);
pthread_mutex_lock(&mux_task[i]);
per = tp[i].period;
drel = tp[i].deadline;
pthread_mutex_unlock(&mux_task[i]);
time_add_us(&(tp[i].at), per);
time_add_us(&(tp[i].dl), drel);
}
int pt_deadline_miss (int i)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (time_cmp(now, tp[i].dl) > 0) {
tp[i].dmiss++;
return 1;
}
return 0;
}
void pt_wait_for_period (int i)
{
int per;
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &(tp[i].at), NULL);
pthread_mutex_lock(&mux_task[i]);
per = tp[i].period;
pthread_mutex_unlock(&mux_task[i]);
time_add_us(&(tp[i].at), per);
time_add_us(&(tp[i].dl), per);
}
void pt_wait_for_end (int i)
{
pthread_join(tid[i], NULL);
}