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);
27 static struct timespec start_execution;
28 static struct timespec signal_reception;
34 fosa_thread_attr_t attr;
37 fosa_signal_t signal_received;
38 fosa_signal_info_t info_programmed, info_received;
39 struct timespec work_interval = {3, 200000000}; // 3.2 seconds
40 fosa_clock_id_t clockid;
41 fosa_timer_id_t timerid;
43 struct timespec budget = {-1, -1};
44 fosa_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_programmed, 0, sizeof(info_programmed) );
52 memset(&info_received, 0, sizeof(info_received) );
53 memset(&clockid, 0, sizeof(clockid) );
54 memset(&timerid, 0, sizeof(timerid) );
56 memset(&start_execution, 0xFF, sizeof(start_execution) );
57 memset(&signal_received, 0xFF, sizeof(signal_received) );
59 /* Set the signal mask */
60 /***********************/
61 signal_set[0] = SIGNAL_TIMER;
62 if (fosa_set_accepted_signals(signal_set, 1) !=0)
64 printf ("Error while setting the signal mask\n");
68 /* Create the thread attributes and define its priority */
69 /********************************************************/
70 if (fosa_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");
89 /* Get the thread's cputime clock */
90 /**********************************/
91 if (fosa_thread_get_cputime_clock(tid, &clockid) !=0)
96 /* Create a timer and arm it with a given budget */
97 /*************************************************/
98 info_programmed.sival_int = 42;
99 err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
100 printf("timer created, err=%d\n", err);
104 budget.tv_nsec = 500000000;
105 err = fosa_timer_arm(timerid, 0, &budget);
106 printf("timer armed for 2.5 secs, err=%d\n", err);
109 /* We execute a little bit to ensure that execution time */
110 /* and real time differ */
111 /*********************************************************/
112 printf("Main works for some time...\n");
113 fosa_eat(&work_interval);
115 /* Now we do the wait in order to allow the thread to run */
116 /**********************************************************/
117 printf("About to do the wait\n");
118 err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
120 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
121 decr_timespec(&signal_reception, &start_execution);
123 printf("signal received=%d value=%d (42?), err=%d\n",
124 signal_received, info_received.sival_int, err);
126 printf("Elapsed time between sigwait and timer expiration: %d %d\n",
127 signal_reception.tv_sec, signal_reception.tv_nsec);
133 // ----------------------------------------------------------------
135 static void * thread_body(void *thread_arg)
137 struct timespec before_work_time = {-1, -1};
138 struct timespec after_work_time = {-1, -1};
139 struct timespec work_interval = {1, 400000000}; // 1.4 seconds
143 fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
147 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
149 printf("Start periodic work at %d, %d\n",
150 before_work_time.tv_sec, before_work_time.tv_nsec);
152 fosa_eat(&work_interval);
154 err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
157 printf("End periodic work at %d, %d\n",
158 after_work_time.tv_sec, after_work_time.tv_nsec);
160 decr_timespec(&after_work_time, &before_work_time);
161 printf("Elapsed time: %d %d\n", after_work_time.tv_sec,
162 after_work_time.tv_nsec);