1 | /* |
---|
2 | * |
---|
3 | * This is a simple real-time applications which contains 3 periodic tasks. |
---|
4 | * |
---|
5 | * Task A is an independent task. |
---|
6 | * |
---|
7 | * Task B and C share a data. |
---|
8 | * |
---|
9 | * Tasks are implemented as POSIX threads. |
---|
10 | * |
---|
11 | * The share data is protected with a POSIX mutex. |
---|
12 | * |
---|
13 | * Other POSIX facilities such as timers, condition, .. is also used |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | * |
---|
19 | * $Id$ |
---|
20 | */ |
---|
21 | |
---|
22 | #define CONFIGURE_INIT |
---|
23 | #include "system.h" |
---|
24 | #include <pthread.h> /* thread facilities */ |
---|
25 | #include <signal.h> /* signal facilities */ |
---|
26 | #include <unistd.h> /* sleep facilities */ |
---|
27 | #include <sched.h> /* schedule facilities */ |
---|
28 | #include <time.h> /* time facilities */ |
---|
29 | #include <stdio.h> /* console facilities */ |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | /* temporal parameters of a task */ |
---|
34 | |
---|
35 | struct periodic_params { |
---|
36 | struct timespec period; |
---|
37 | int signo; /* signal number */ |
---|
38 | int id; /* task identification */ |
---|
39 | }; |
---|
40 | |
---|
41 | pthread_attr_t attr; |
---|
42 | |
---|
43 | /* shared datum */ |
---|
44 | |
---|
45 | struct shared_data { |
---|
46 | pthread_mutex_t mutex; |
---|
47 | pthread_cond_t sync; |
---|
48 | int updated; |
---|
49 | int x; |
---|
50 | }; |
---|
51 | |
---|
52 | struct shared_data data; |
---|
53 | |
---|
54 | /* task A */ |
---|
55 | |
---|
56 | void * task_a (void *arg) |
---|
57 | { |
---|
58 | struct timespec my_period; |
---|
59 | int my_sig, received_sig; |
---|
60 | struct itimerspec timerdata; |
---|
61 | timer_t timer_id; |
---|
62 | time_t clock; |
---|
63 | struct sigevent event; |
---|
64 | sigset_t set; |
---|
65 | |
---|
66 | my_period = ((struct periodic_params*) arg)->period; |
---|
67 | my_sig = ((struct periodic_params*) arg)->signo; |
---|
68 | |
---|
69 | /* timer create */ |
---|
70 | event.sigev_notify = SIGEV_SIGNAL; |
---|
71 | event.sigev_signo = my_sig; |
---|
72 | if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { |
---|
73 | perror ("Error in timer creation\n"); |
---|
74 | pthread_exit ((void *) -1); |
---|
75 | } |
---|
76 | |
---|
77 | /* block the timer signal */ |
---|
78 | sigemptyset (&set); |
---|
79 | sigaddset (&set,my_sig); |
---|
80 | pthread_sigmask(SIG_BLOCK,&set,NULL); |
---|
81 | |
---|
82 | /* set the timer in periodic mode */ |
---|
83 | timerdata.it_interval = my_period; |
---|
84 | timerdata.it_value = my_period; |
---|
85 | if (timer_settime(timer_id, 0, &timerdata, NULL) == -1) { |
---|
86 | perror ("Error in timer setting\n"); |
---|
87 | pthread_exit ((void *) -1); |
---|
88 | } |
---|
89 | |
---|
90 | /* periodic activity */ |
---|
91 | while(1) { |
---|
92 | if (sigwait(&set,&received_sig) == -1) { |
---|
93 | perror ("Error in sigwait\n"); |
---|
94 | } |
---|
95 | clock = time(NULL); |
---|
96 | printf("Executing task A %s", ctime(&clock)); |
---|
97 | } |
---|
98 | return NULL; |
---|
99 | } |
---|
100 | |
---|
101 | /* task B */ |
---|
102 | |
---|
103 | void * task_b (void *arg) |
---|
104 | { |
---|
105 | struct timespec my_period; |
---|
106 | int my_sig, received_sig; |
---|
107 | struct itimerspec timerdata; |
---|
108 | timer_t timer_id; |
---|
109 | time_t clock; |
---|
110 | struct sigevent event; |
---|
111 | sigset_t set; |
---|
112 | |
---|
113 | int x; /* value to be copied to the shared datum */ |
---|
114 | |
---|
115 | my_period = ((struct periodic_params*) arg)->period; |
---|
116 | my_sig = ((struct periodic_params*) arg)->signo; |
---|
117 | |
---|
118 | x = 1; |
---|
119 | |
---|
120 | /* timer create */ |
---|
121 | event.sigev_notify = SIGEV_SIGNAL; |
---|
122 | event.sigev_signo = my_sig; |
---|
123 | if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { |
---|
124 | perror ("Error in timer creation\n"); |
---|
125 | pthread_exit ((void *) -1); |
---|
126 | } |
---|
127 | |
---|
128 | /* block the timer signal */ |
---|
129 | sigemptyset (&set); |
---|
130 | sigaddset (&set,my_sig); |
---|
131 | pthread_sigmask(SIG_BLOCK,&set,NULL); |
---|
132 | |
---|
133 | /* set the timer in periodic mode */ |
---|
134 | timerdata.it_interval = my_period; |
---|
135 | timerdata.it_value = my_period; |
---|
136 | if (timer_settime(timer_id, 0, &timerdata, NULL) == -1) { |
---|
137 | perror ("Error in timer setting\n"); |
---|
138 | pthread_exit ((void *) -1); |
---|
139 | } |
---|
140 | |
---|
141 | /* periodic activity */ |
---|
142 | while(1) { |
---|
143 | if (sigwait(&set,&received_sig) == -1) { |
---|
144 | perror ("Error in sigwait\n"); |
---|
145 | pthread_exit ((void *) -1); |
---|
146 | } |
---|
147 | pthread_mutex_lock (&data.mutex); |
---|
148 | clock = time(NULL); |
---|
149 | printf("Executing task B with x = %i %s", x, ctime(&clock)); |
---|
150 | data.x = x; |
---|
151 | data.updated = TRUE; |
---|
152 | pthread_cond_signal (&data.sync); |
---|
153 | pthread_mutex_unlock (&data.mutex); |
---|
154 | x++; |
---|
155 | } |
---|
156 | return NULL; |
---|
157 | } |
---|
158 | |
---|
159 | /* task C */ |
---|
160 | |
---|
161 | void * task_c (void *arg) |
---|
162 | { |
---|
163 | struct timespec my_period; |
---|
164 | int my_sig, received_sig; |
---|
165 | struct itimerspec timerdata; |
---|
166 | timer_t timer_id; |
---|
167 | time_t clock; |
---|
168 | struct sigevent event; |
---|
169 | sigset_t set; |
---|
170 | |
---|
171 | int x; /* value to be copied to the shared datum */ |
---|
172 | |
---|
173 | my_period = ((struct periodic_params*) arg)->period; |
---|
174 | my_sig = ((struct periodic_params*) arg)->signo; |
---|
175 | |
---|
176 | x = 0; |
---|
177 | |
---|
178 | /* timer create */ |
---|
179 | event.sigev_notify = SIGEV_SIGNAL; |
---|
180 | event.sigev_signo = my_sig; |
---|
181 | if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { |
---|
182 | perror ("Error in timer creation\n"); |
---|
183 | pthread_exit ((void *) -1); |
---|
184 | } |
---|
185 | |
---|
186 | /* block the timer signal */ |
---|
187 | sigemptyset (&set); |
---|
188 | sigaddset (&set,my_sig); |
---|
189 | pthread_sigmask(SIG_BLOCK,&set,NULL); |
---|
190 | |
---|
191 | /* set the timer in periodic mode */ |
---|
192 | timerdata.it_interval = my_period; |
---|
193 | timerdata.it_value = my_period; |
---|
194 | if (timer_settime(timer_id, 0, &timerdata, NULL) == -1) { |
---|
195 | perror ("Error in timer setting\n"); |
---|
196 | pthread_exit ((void *) -1); |
---|
197 | } |
---|
198 | |
---|
199 | /* periodic activity */ |
---|
200 | while(1) { |
---|
201 | if (sigwait(&set,&received_sig) == -1) { |
---|
202 | perror ("Error in sigwait\n"); |
---|
203 | pthread_exit ((void *) -1); |
---|
204 | } |
---|
205 | pthread_mutex_lock (&data.mutex); |
---|
206 | while (data.updated == FALSE) { |
---|
207 | pthread_cond_wait (&data.sync,&data.mutex); |
---|
208 | } |
---|
209 | x = data.x; |
---|
210 | clock = time(NULL); |
---|
211 | printf("Executing task C with x = %i %s", x, ctime(&clock)); |
---|
212 | pthread_mutex_unlock (&data.mutex); |
---|
213 | } |
---|
214 | return NULL; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | /* main */ |
---|
219 | |
---|
220 | void *POSIX_Init ( |
---|
221 | void *argument |
---|
222 | ) |
---|
223 | |
---|
224 | { |
---|
225 | pthread_mutexattr_t mutexattr; /* mutex attributes */ |
---|
226 | pthread_condattr_t condattr; /* condition attributes */ |
---|
227 | pthread_attr_t attr; /* task attributes */ |
---|
228 | pthread_t ta,tb,tc; /* threads */ |
---|
229 | sigset_t set; /* signals */ |
---|
230 | |
---|
231 | struct sched_param sch_param; /* schedule parameters */ |
---|
232 | struct periodic_params params_a, params_b, params_c; |
---|
233 | |
---|
234 | puts( "\n\n*** POSIX Timers Test ***" ); |
---|
235 | |
---|
236 | data.updated = FALSE; |
---|
237 | data.x = 0; |
---|
238 | |
---|
239 | /* mask signal */ |
---|
240 | sigemptyset (&set); |
---|
241 | sigaddset (&set,SIGALRM); |
---|
242 | pthread_sigmask (SIG_BLOCK,&set,NULL); |
---|
243 | |
---|
244 | /* set mutex attributes */ |
---|
245 | if (pthread_mutexattr_init (&mutexattr) != 0) { |
---|
246 | perror ("Error in mutex attribute init\n"); |
---|
247 | } |
---|
248 | |
---|
249 | /* init mutex */ |
---|
250 | if (pthread_mutex_init (&data.mutex,&mutexattr) != 0) { |
---|
251 | perror ("Error in mutex init"); |
---|
252 | } |
---|
253 | |
---|
254 | /* init condition attributes */ |
---|
255 | if (pthread_condattr_init (&condattr) != 0) { |
---|
256 | perror ("Error in condition attribute init\n"); |
---|
257 | } |
---|
258 | |
---|
259 | /* init condition */ |
---|
260 | if (pthread_cond_init (&data.sync,&condattr) != 0) { |
---|
261 | perror ("Error in condition init"); |
---|
262 | } |
---|
263 | |
---|
264 | /* init task attributes */ |
---|
265 | if (pthread_attr_init(&attr) != 0) { |
---|
266 | perror ("Error in attribute init\n"); |
---|
267 | } |
---|
268 | |
---|
269 | /* set explicit schedule for every task */ |
---|
270 | if (pthread_attr_setinheritsched (&attr, |
---|
271 | PTHREAD_EXPLICIT_SCHED) != 0) { |
---|
272 | perror("Error in attribute inheritsched\n"); |
---|
273 | } |
---|
274 | |
---|
275 | /* set task independent (join will not use) */ |
---|
276 | if (pthread_attr_setdetachstate (&attr, |
---|
277 | PTHREAD_CREATE_DETACHED) != 0) { |
---|
278 | perror ("Error in attribute detachstate\n"); |
---|
279 | } |
---|
280 | |
---|
281 | /* schedule policy POSIX_FIFO (priority preemtive and FIFO within the same |
---|
282 | priority) */ |
---|
283 | if (pthread_attr_setschedpolicy (&attr, |
---|
284 | SCHED_FIFO) != 0) { |
---|
285 | perror ("Error in attribute setschedpolicy\n"); |
---|
286 | } |
---|
287 | |
---|
288 | /* set and create thread A with priority 1 */ |
---|
289 | |
---|
290 | sch_param.sched_priority = 1; |
---|
291 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0) { |
---|
292 | perror ("Error in attribute schedparam\n"); |
---|
293 | } |
---|
294 | |
---|
295 | /* Temporal parameters (1 sec. periodicity) */ |
---|
296 | |
---|
297 | params_a.period.tv_sec = 1; /* seconds */ |
---|
298 | params_a.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
299 | params_a.signo = SIGALRM; |
---|
300 | if (pthread_create (&ta, &attr, task_a, ¶ms_a) != 0 ) { |
---|
301 | perror ("Error in thread create for task a\n"); |
---|
302 | } |
---|
303 | |
---|
304 | /* set and create thread B with priority 15 */ |
---|
305 | |
---|
306 | sch_param.sched_priority = 15; |
---|
307 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0) { |
---|
308 | perror ("Error in attribute schedparam"); |
---|
309 | } |
---|
310 | |
---|
311 | /* Temporal parameters (2 sec. periodicity) */ |
---|
312 | params_b.period.tv_sec = 2; /* seconds */ |
---|
313 | params_b.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
314 | params_b.signo = SIGALRM; |
---|
315 | if (pthread_create (&tb, &attr, task_b, ¶ms_b) != 0) { |
---|
316 | perror ("Error in thread create for task b\n"); |
---|
317 | } |
---|
318 | |
---|
319 | /* set and create thread B with priority 14 */ |
---|
320 | |
---|
321 | sch_param.sched_priority = 14; |
---|
322 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0 ) { |
---|
323 | perror ("Error in attribute schedparam\n"); |
---|
324 | } |
---|
325 | |
---|
326 | /* Temporal parameters (3 sec. periodicity) */ |
---|
327 | params_c.period.tv_sec = 3; /* seconds */ |
---|
328 | params_c.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
329 | params_c.signo = SIGALRM; |
---|
330 | if (pthread_create (&tc, &attr, task_c, ¶ms_c) != 0) { |
---|
331 | perror ("Error in thread create for task c\n"); |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | /* execute 20 seconds and finish */ |
---|
336 | sleep (20); |
---|
337 | puts( "\n\n*** End of POSIX Timers Test ***" ); |
---|
338 | rtems_test_exit (0); |
---|
339 | } |
---|