1 #include "fosa_threads_and_signals.h"
2 #include "fosa_clocks_and_timers.h"
5 #include <time.h> // for nanosleep
8 #include <stdlib.h> // for exit in assert
9 #include <string.h> // for memset
10 #include <timespec_operations.h>
13 /*****************************/
14 /* D E F I N I T I O N S */
15 /*****************************/
16 #define RT_ERROR_SIGWAIT -2
17 #define RT_ERROR_TIMER -3
19 #define SIGNAL_TIMER FOSA_SIGNAL_MAX - 1
21 /***************************/
22 /* P R O T O T Y P E S */
23 /***************************/
24 static void * thread_body(void *thread_arg);
26 /*************************************/
27 /* S T A T I C V A R I A B L E S */
28 /*************************************/
29 static struct timespec start_execution;
30 static struct timespec signal_reception;
36 frsh_thread_attr_t attr;
39 frsh_signal_t signal_received;
40 frsh_signal_info_t info_received;
42 struct timespec work_interval = {3, 200000000}; // 3.2 seconds
44 frsh_signal_t signal_set[1];
48 memset(&attr, 0, sizeof(attr) );
49 memset(&tid, 0, sizeof(tid) );
50 memset(&signal_received, 0, sizeof(signal_received) );
51 memset(&info_received, 0, sizeof(info_received) );
53 memset(&start_execution, 0xFF, sizeof(start_execution) );
54 memset(&signal_received, 0xFF, sizeof(signal_received) );
56 /* Set the signal mask */
57 /***********************/
58 signal_set[0] = SIGNAL_TIMER;
59 if (fosa_set_accepted_signals(signal_set, 1) !=0)
61 printf ("Error while setting the signal mask\n");
65 /* Create the thread attributes and define its priority */
67 /* At the same time ensure that the main priority is */
69 /********************************************************/
70 if (frsh_thread_attr_init (&attr) != 0) {
71 printf("Error while initializing the attributes\n");
75 if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
76 printf("Error while setting schedparam\n");
80 /* create the periodic thread. It won't execute yet */
81 /* because it has lower priority than main(). */
82 /*****************************************************/
83 err = fosa_thread_create(&tid, &attr, thread_body, NULL);
85 printf("pthread_create failed thread\n");
90 /* We execute a little bit to ensure that execution time */
91 /* and real time differ */
92 /*********************************************************/
93 printf("Main works for some time...\n");
94 frsh_eat(&work_interval);
96 /* Now we do the wait in order to allow the thread to run */
97 /**********************************************************/
98 printf("About to do the wait\n");
99 err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
101 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
102 decr_timespec(&signal_reception, &start_execution);
104 printf("signal received=%d value=%d (42?), err=%d\n",
105 signal_received, info_received.sival_int, err);
107 printf("Elapsed time between sigwait and timer expiration: %d %d\n",
108 signal_reception.tv_sec, signal_reception.tv_nsec);
114 // ----------------------------------------------------------------
116 static void * thread_body(void *thread_arg)
118 struct timespec before_work_time = {-1, -1};
119 struct timespec after_work_time = {-1, -1};
120 struct timespec work_interval = {1, 400000000}; // 1.4 seconds
122 fosa_clock_id_t clockid;
123 fosa_timer_id_t timerid;
124 struct timespec budget = {-1, -1};
126 frsh_signal_info_t info_programmed;
128 memset(&clockid, 0, sizeof(clockid) );
129 memset(&timerid, 0, sizeof(timerid) );
130 memset(&info_programmed, 0, sizeof(info_programmed) );
134 /* Get the thread's cputime clock */
135 /**********************************/
136 if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0)
138 exit(RT_ERROR_TIMER);
141 /* Create a timer and arm it with a given budget */
142 /*************************************************/
143 info_programmed.sival_int = 42;
144 err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
145 printf("timer created, err=%d\n", err);
149 budget.tv_nsec = 500000000;
150 err = fosa_timer_arm(timerid, 0, &budget);
151 printf("timer armed for 2.5 secs, err=%d\n", err);
154 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
158 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
160 printf("Start periodic work at %d, %d\n",
161 before_work_time.tv_sec, before_work_time.tv_nsec);
163 frsh_eat(&work_interval);
165 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
168 printf("End periodic work at %d, %d\n",
169 after_work_time.tv_sec, after_work_time.tv_nsec);
171 decr_timespec(&after_work_time, &before_work_time);
172 printf("Elapsed time: %d %d\n", after_work_time.tv_sec,
173 after_work_time.tv_nsec);