--- /dev/null
+#include "fosa_threads_and_signals.h"
+#include "fosa_clocks_and_timers.h"
+#include <unistd.h>
+#include <stdio.h>
+#include <time.h> // for nanosleep
+
+#include <assert.h>
+#include <stdlib.h> // for exit in assert
+#include <string.h> // for memset
+#include <timespec_operations.h>
+
+
+/*****************************/
+/* D E F I N I T I O N S */
+/*****************************/
+#define RT_ERROR_SIGWAIT -2
+#define RT_ERROR_TIMER -3
+
+#define SIGNAL_TIMER FOSA_SIGNAL_MAX - 1
+
+/***************************/
+/* P R O T O T Y P E S */
+/***************************/
+static void * thread_body(void *thread_arg);
+
+/*************************************/
+/* S T A T I C V A R I A B L E S */
+/*************************************/
+static struct timespec start_execution;
+static struct timespec signal_reception;
+
+int main ()
+{
+ int err = -1;
+
+ frsh_thread_attr_t attr;
+ frsh_thread_id_t tid;
+
+ frsh_signal_t signal_received;
+ frsh_signal_info_t info_received;
+
+ struct timespec work_interval = {3, 200000000}; // 3.2 seconds
+
+ frsh_signal_t signal_set[1];
+
+
+
+ memset(&attr, 0, sizeof(attr) );
+ memset(&tid, 0, sizeof(tid) );
+ memset(&signal_received, 0, sizeof(signal_received) );
+ memset(&info_received, 0, sizeof(info_received) );
+
+ memset(&start_execution, 0xFF, sizeof(start_execution) );
+ memset(&signal_received, 0xFF, sizeof(signal_received) );
+
+ /* Set the signal mask */
+ /***********************/
+ signal_set[0] = SIGNAL_TIMER;
+ if (fosa_set_accepted_signals(signal_set, 1) !=0)
+ {
+ printf ("Error while setting the signal mask\n");
+ exit (1);
+ }
+
+ /* Create the thread attributes and define its priority */
+ /* */
+ /* At the same time ensure that the main priority is */
+ /* higher */
+ /********************************************************/
+ if (frsh_thread_attr_init (&attr) != 0) {
+ printf("Error while initializing the attributes\n");
+ exit(1);
+ }
+
+ if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
+ printf("Error while setting schedparam\n");
+ exit(1);
+ }
+
+ /* create the periodic thread. It won't execute yet */
+ /* because it has lower priority than main(). */
+ /*****************************************************/
+ err = fosa_thread_create(&tid, &attr, thread_body, NULL);
+ if (err) {
+ printf("pthread_create failed thread\n");
+ exit(1);
+ }
+
+
+ /* We execute a little bit to ensure that execution time */
+ /* and real time differ */
+ /*********************************************************/
+ printf("Main works for some time...\n");
+ frsh_eat(&work_interval);
+
+ /* Now we do the wait in order to allow the thread to run */
+ /**********************************************************/
+ printf("About to do the wait\n");
+ err = fosa_signal_wait(signal_set, 1 ,&signal_received, &info_received);
+
+ fosa_clock_get_time(FOSA_CLOCK_REALTIME, &signal_reception);
+ decr_timespec(&signal_reception, &start_execution);
+
+ printf("signal received=%d value=%d (42?), err=%d\n",
+ signal_received, info_received.sival_int, err);
+
+ printf("Elapsed time between sigwait and timer expiration: %d %d\n",
+ signal_reception.tv_sec, signal_reception.tv_nsec);
+
+ return 0;
+}
+
+
+// ----------------------------------------------------------------
+
+static void * thread_body(void *thread_arg)
+{
+ struct timespec before_work_time = {-1, -1};
+ struct timespec after_work_time = {-1, -1};
+ struct timespec work_interval = {1, 400000000}; // 1.4 seconds
+
+ fosa_clock_id_t clockid;
+ fosa_timer_id_t timerid;
+ struct timespec budget = {-1, -1};
+
+ frsh_signal_info_t info_programmed;
+
+ memset(&clockid, 0, sizeof(clockid) );
+ memset(&timerid, 0, sizeof(timerid) );
+ memset(&info_programmed, 0, sizeof(info_programmed) );
+
+ int err;
+
+ /* Get the thread's cputime clock */
+ /**********************************/
+ if (fosa_thread_get_cputime_clock(fosa_thread_self(), &clockid) !=0)
+ {
+ exit(RT_ERROR_TIMER);
+ }
+
+ /* Create a timer and arm it with a given budget */
+ /*************************************************/
+ info_programmed.sival_int = 42;
+ err = fosa_timer_create(clockid, SIGNAL_TIMER, info_programmed, &timerid);
+ printf("timer created, err=%d\n", err);
+ assert(err == 0);
+
+ budget.tv_sec = 2;
+ budget.tv_nsec = 500000000;
+ err = fosa_timer_arm(timerid, 0, &budget);
+ printf("timer armed for 2.5 secs, err=%d\n", err);
+ assert(err == 0);
+
+ fosa_clock_get_time(FOSA_CLOCK_REALTIME, &start_execution);
+
+ while(1)
+ {
+ err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &before_work_time);
+ assert(err == 0);
+ printf("Start periodic work at %d, %d\n",
+ before_work_time.tv_sec, before_work_time.tv_nsec);
+
+ frsh_eat(&work_interval);
+
+ err = fosa_clock_get_time(FOSA_CLOCK_REALTIME, &after_work_time);
+ assert(err == 0);
+
+ printf("End periodic work at %d, %d\n",
+ after_work_time.tv_sec, after_work_time.tv_nsec);
+
+ decr_timespec(&after_work_time, &before_work_time);
+ printf("Elapsed time: %d %d\n", after_work_time.tv_sec,
+ after_work_time.tv_nsec);
+ } // while
+
+ return NULL;
+}
+