3 #include <time.h> // for nanosleep
6 #include <stdlib.h> // for exit in assert
7 #include <string.h> // for memset
11 /*****************************/
12 /* D E F I N I T I O N S */
13 /*****************************/
14 #define RT_ERROR_SIGWAIT -2
15 #define RT_ERROR_TIMER -3
17 #define SIGNAL_TIMER FOSA_SIGNAL_MAX - 1
19 /***************************/
20 /* P R O T O T Y P E S */
21 /***************************/
22 static void * thread_body(void *thread_arg);
24 /*************************************/
25 /* S T A T I C V A R I A B L E S */
26 /*************************************/
27 static fosa_abs_time_t start_execution;
28 static fosa_abs_time_t signal_reception;
34 fosa_thread_attr_t attr;
37 fosa_signal_t signal_received;
38 fosa_signal_info_t info_received;
40 fosa_rel_time_t work_interval = fosa_msec_to_rel_time(3200);
41 fosa_rel_time_t elapsed_time;
43 fosa_signal_t signal_set[1];
45 /* Set the signal mask */
46 /***********************/
47 signal_set[0] = SIGNAL_TIMER;
48 if (fosa_set_accepted_signals(signal_set, 1) !=0)
50 printf ("Error while setting the signal mask\n");
54 /* Create the thread attributes and define its priority */
56 /* At the same time ensure that the main priority is */
58 /********************************************************/
59 if (fosa_thread_attr_init (&attr) != 0) {
60 printf("Error while initializing the attributes\n");
64 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
65 printf("Error while setting schedparam\n");
69 /* create the periodic thread. It won't execute yet */
70 /* because it has lower priority than main(). */
71 /*****************************************************/
72 err = fosa_thread_create(&tid, &attr, thread_body, NULL);
74 printf("pthread_create failed thread\n");
79 /* We execute a little bit to ensure that execution time */
80 /* and real time differ */
81 /*********************************************************/
82 printf("Main works for some time...\n");
83 fosa_eat(&work_interval);
85 /* Now we do the wait in order to allow the thread to run */
86 /**********************************************************/
87 printf("About to do the wait\n");
88 err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
90 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
91 elapsed_time = fosa_abs_time_extract_interval(start_execution, signal_reception);
93 printf("signal received=%d value=%d (42?), err=%d\n",
94 signal_received, info_received.sival_int, err);
96 printf("Elapsed time between sigwait and timer expiration: %ld msecs\n",
97 fosa_rel_time_to_msec(elapsed_time) );
103 // ----------------------------------------------------------------
105 static void * thread_body(void *thread_arg)
107 fosa_abs_time_t before_work_time;
108 fosa_abs_time_t after_work_time;
109 fosa_rel_time_t work_interval = fosa_msec_to_rel_time(1400);
112 fosa_clock_id_t clockid;
113 fosa_timer_id_t timerid;
114 fosa_rel_time_t budget;
115 fosa_rel_time_t elapsed_time;
117 fosa_signal_info_t info_programmed;
121 /* Get the thread's cputime clock */
122 /**********************************/
123 if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0)
125 exit(RT_ERROR_TIMER);
128 /* Create a timer and arm it with a given budget */
129 /*************************************************/
130 info_programmed.sival_int = 42;
131 err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
132 printf("timer created, err=%d\n", err);
135 budget = fosa_msec_to_rel_time(2500);
136 err = fosa_rel_timer_arm(timerid, &budget);
137 printf("timer armed for 2.5 secs, err=%d\n", err);
140 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
144 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
146 printf("Start periodic work at %ld msecs\n", fosa_abs_time_to_msec(before_work_time) );
148 fosa_eat(&work_interval);
150 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
153 printf("End periodic work at %ld msecs\n", fosa_abs_time_to_msec(after_work_time) );
155 elapsed_time = fosa_abs_time_extract_interval(before_work_time, after_work_time);
157 printf("Elapsed time: %ld msec\n", fosa_rel_time_to_msec(elapsed_time) );