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.OARcorp.com/rtems/license.html. |
---|
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 | } |
---|
99 | |
---|
100 | /* task B */ |
---|
101 | |
---|
102 | void * task_b (void *arg) |
---|
103 | { |
---|
104 | struct timespec my_period; |
---|
105 | int my_sig, received_sig; |
---|
106 | struct itimerspec timerdata; |
---|
107 | timer_t timer_id; |
---|
108 | time_t clock; |
---|
109 | struct sigevent event; |
---|
110 | sigset_t set; |
---|
111 | |
---|
112 | int x; /* value to be copied to the shared datum */ |
---|
113 | |
---|
114 | my_period = ((struct periodic_params*) arg)->period; |
---|
115 | my_sig = ((struct periodic_params*) arg)->signo; |
---|
116 | |
---|
117 | x = 1; |
---|
118 | |
---|
119 | /* timer create */ |
---|
120 | event.sigev_notify = SIGEV_SIGNAL; |
---|
121 | event.sigev_signo = my_sig; |
---|
122 | if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { |
---|
123 | perror ("Error in timer creation\n"); |
---|
124 | pthread_exit ((void *) -1); |
---|
125 | } |
---|
126 | |
---|
127 | /* block the timer signal */ |
---|
128 | sigemptyset (&set); |
---|
129 | sigaddset (&set,my_sig); |
---|
130 | pthread_sigmask(SIG_BLOCK,&set,NULL); |
---|
131 | |
---|
132 | /* set the timer in periodic mode */ |
---|
133 | timerdata.it_interval = my_period; |
---|
134 | timerdata.it_value = my_period; |
---|
135 | if (timer_settime(timer_id, 0, &timerdata, NULL) == -1) { |
---|
136 | perror ("Error in timer setting\n"); |
---|
137 | pthread_exit ((void *) -1); |
---|
138 | } |
---|
139 | |
---|
140 | /* periodic activity */ |
---|
141 | while(1) { |
---|
142 | if (sigwait(&set,&received_sig) == -1) { |
---|
143 | perror ("Error in sigwait\n"); |
---|
144 | pthread_exit ((void *) -1); |
---|
145 | } |
---|
146 | pthread_mutex_lock (&data.mutex); |
---|
147 | clock = time(NULL); |
---|
148 | printf("Executing task B with x = %i %s", x, ctime(&clock)); |
---|
149 | data.x = x; |
---|
150 | data.updated = TRUE; |
---|
151 | pthread_cond_signal (&data.sync); |
---|
152 | pthread_mutex_unlock (&data.mutex); |
---|
153 | x++; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /* task C */ |
---|
158 | |
---|
159 | void * task_c (void *arg) |
---|
160 | { |
---|
161 | struct timespec my_period; |
---|
162 | int my_sig, received_sig; |
---|
163 | struct itimerspec timerdata; |
---|
164 | timer_t timer_id; |
---|
165 | time_t clock; |
---|
166 | struct sigevent event; |
---|
167 | sigset_t set; |
---|
168 | |
---|
169 | int x; /* value to be copied to the shared datum */ |
---|
170 | |
---|
171 | my_period = ((struct periodic_params*) arg)->period; |
---|
172 | my_sig = ((struct periodic_params*) arg)->signo; |
---|
173 | |
---|
174 | x = 0; |
---|
175 | |
---|
176 | /* timer create */ |
---|
177 | event.sigev_notify = SIGEV_SIGNAL; |
---|
178 | event.sigev_signo = my_sig; |
---|
179 | if (timer_create (CLOCK_REALTIME, &event, &timer_id) == -1) { |
---|
180 | perror ("Error in timer creation\n"); |
---|
181 | pthread_exit ((void *) -1); |
---|
182 | } |
---|
183 | |
---|
184 | /* block the timer signal */ |
---|
185 | sigemptyset (&set); |
---|
186 | sigaddset (&set,my_sig); |
---|
187 | pthread_sigmask(SIG_BLOCK,&set,NULL); |
---|
188 | |
---|
189 | /* set the timer in periodic mode */ |
---|
190 | timerdata.it_interval = my_period; |
---|
191 | timerdata.it_value = my_period; |
---|
192 | if (timer_settime(timer_id, 0, &timerdata, NULL) == -1) { |
---|
193 | perror ("Error in timer setting\n"); |
---|
194 | pthread_exit ((void *) -1); |
---|
195 | } |
---|
196 | |
---|
197 | /* periodic activity */ |
---|
198 | while(1) { |
---|
199 | if (sigwait(&set,&received_sig) == -1) { |
---|
200 | perror ("Error in sigwait\n"); |
---|
201 | pthread_exit ((void *) -1); |
---|
202 | } |
---|
203 | pthread_mutex_lock (&data.mutex); |
---|
204 | while (data.updated == FALSE) { |
---|
205 | pthread_cond_wait (&data.sync,&data.mutex); |
---|
206 | } |
---|
207 | x = data.x; |
---|
208 | clock = time(NULL); |
---|
209 | printf("Executing task C with x = %i %s", x, ctime(&clock)); |
---|
210 | pthread_mutex_unlock (&data.mutex); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | /* main */ |
---|
216 | |
---|
217 | void *POSIX_Init ( |
---|
218 | void *argument |
---|
219 | ) |
---|
220 | |
---|
221 | { |
---|
222 | pthread_mutexattr_t mutexattr; /* mutex attributes */ |
---|
223 | pthread_condattr_t condattr; /* condition attributes */ |
---|
224 | pthread_attr_t attr; /* task attributes */ |
---|
225 | pthread_t ta,tb,tc; /* threads */ |
---|
226 | sigset_t set; /* signals */ |
---|
227 | |
---|
228 | struct sched_param sch_param; /* schedule parameters */ |
---|
229 | struct periodic_params params_a, params_b, params_c; |
---|
230 | |
---|
231 | puts( "\n\n*** POSIX Timers Test ***" ); |
---|
232 | |
---|
233 | data.updated = FALSE; |
---|
234 | data.x = 0; |
---|
235 | |
---|
236 | /* mask signal */ |
---|
237 | sigemptyset (&set); |
---|
238 | sigaddset (&set,SIGALRM); |
---|
239 | pthread_sigmask (SIG_BLOCK,&set,NULL); |
---|
240 | |
---|
241 | /* set mutex attributes */ |
---|
242 | if (pthread_mutexattr_init (&mutexattr) != 0) { |
---|
243 | perror ("Error in mutex attribute init\n"); |
---|
244 | } |
---|
245 | |
---|
246 | /* init mutex */ |
---|
247 | if (pthread_mutex_init (&data.mutex,&mutexattr) != 0) { |
---|
248 | perror ("Error in mutex init"); |
---|
249 | } |
---|
250 | |
---|
251 | /* init condition attributes */ |
---|
252 | if (pthread_condattr_init (&condattr) != 0) { |
---|
253 | perror ("Error in condition attribute init\n"); |
---|
254 | } |
---|
255 | |
---|
256 | /* init condition */ |
---|
257 | if (pthread_cond_init (&data.sync,&condattr) != 0) { |
---|
258 | perror ("Error in condition init"); |
---|
259 | } |
---|
260 | |
---|
261 | /* init task attributes */ |
---|
262 | if (pthread_attr_init(&attr) != 0) { |
---|
263 | perror ("Error in attribute init\n"); |
---|
264 | } |
---|
265 | |
---|
266 | /* set explicit schedule for every task */ |
---|
267 | if (pthread_attr_setinheritsched (&attr, |
---|
268 | PTHREAD_EXPLICIT_SCHED) != 0) { |
---|
269 | perror("Error in attribute inheritsched\n"); |
---|
270 | } |
---|
271 | |
---|
272 | /* set task independent (join will not use) */ |
---|
273 | if (pthread_attr_setdetachstate (&attr, |
---|
274 | PTHREAD_CREATE_DETACHED) != 0) { |
---|
275 | perror ("Error in attribute detachstate\n"); |
---|
276 | } |
---|
277 | |
---|
278 | /* schedule policy POSIX_FIFO (priority preemtive and FIFO within the same |
---|
279 | priority) */ |
---|
280 | if (pthread_attr_setschedpolicy (&attr, |
---|
281 | SCHED_FIFO) != 0) { |
---|
282 | perror ("Error in attribute setschedpolicy\n"); |
---|
283 | } |
---|
284 | |
---|
285 | /* set and create thread A with priority 1 */ |
---|
286 | |
---|
287 | sch_param.sched_priority = 1; |
---|
288 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0) { |
---|
289 | perror ("Error in attribute schedparam\n"); |
---|
290 | } |
---|
291 | |
---|
292 | /* Temporal parameters (1 sec. periodicity) */ |
---|
293 | |
---|
294 | params_a.period.tv_sec = 1; /* seconds */ |
---|
295 | params_a.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
296 | params_a.signo = SIGALRM; |
---|
297 | if (pthread_create (&ta, &attr, task_a, ¶ms_a) != 0 ) { |
---|
298 | perror ("Error in thread create for task a\n"); |
---|
299 | } |
---|
300 | |
---|
301 | /* set and create thread B with priority 15 */ |
---|
302 | |
---|
303 | sch_param.sched_priority = 15; |
---|
304 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0) { |
---|
305 | perror ("Error in attribute schedparam"); |
---|
306 | } |
---|
307 | |
---|
308 | /* Temporal parameters (2 sec. periodicity) */ |
---|
309 | params_b.period.tv_sec = 2; /* seconds */ |
---|
310 | params_b.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
311 | params_b.signo = SIGALRM; |
---|
312 | if (pthread_create (&tb, &attr, task_b, ¶ms_b) != 0) { |
---|
313 | perror ("Error in thread create for task b\n"); |
---|
314 | } |
---|
315 | |
---|
316 | /* set and create thread B with priority 14 */ |
---|
317 | |
---|
318 | sch_param.sched_priority = 14; |
---|
319 | if (pthread_attr_setschedparam(&attr, &sch_param) != 0 ) { |
---|
320 | perror ("Error in attribute schedparam\n"); |
---|
321 | } |
---|
322 | |
---|
323 | /* Temporal parameters (3 sec. periodicity) */ |
---|
324 | params_c.period.tv_sec = 3; /* seconds */ |
---|
325 | params_c.period.tv_nsec = 000000000; /* nanoseconds */ |
---|
326 | params_c.signo = SIGALRM; |
---|
327 | if (pthread_create (&tc, &attr, task_c, ¶ms_c) != 0) { |
---|
328 | perror ("Error in trhead create for task c\n"); |
---|
329 | } |
---|
330 | |
---|
331 | |
---|
332 | /* execute 20 seconds and finish */ |
---|
333 | sleep (20); |
---|
334 | puts( "\n\n*** End of POSIX Timers Test ***" ); |
---|
335 | exit (0); |
---|
336 | } |
---|
337 | |
---|