#include "frsh_fosa.h"
#include "fosa_threads_and_signals.h"
#include "fosa_clocks_and_timers.h"
-
+#include "fosa_configuration_parameters.h"
#include "timespec_operations.h"
/************************/
static fosa_long_jump_context_t context;
-static int error_status;
+static int error_status = 0;
/************************/
/************************/
static void * thread_body(void *arg);
+static void work();
/************************************************************************/
//frsh_signal_t set[1];
frsh_thread_attr_t attr;
- frsh_thread_id_t tid2;
+ frsh_thread_id_t tid_periodic_thread;
int terror = 0;
+ frsh_signal_t signal_set[1];
+
- // set signal mask
- //set[0]=BUDGET_OVERRUN_SIGNAL;
- //if (fosa_set_accepted_signals(set, 1) !=0) {
- // printf ("Error while setting the signal mask\n");
- // exit (1);
- //}
+ memset(&attr, 0, sizeof(attr) );
+ memset(&tid_periodic_thread, 0, sizeof(tid_periodic_thread) );
+ memset(&context, 0, sizeof(context) );
+ memset(&signal_set, 0, sizeof(signal_set) );
- // Create the thread attributes object
+ /* Create the thread attributes object and assign the new thread a
+ priority lower than the main */
if (frsh_thread_attr_init (&attr) != 0) {
- printf("Error while initializing the attributes\n");
- exit(1);
+ printf("Error while initializing the attributes\n");
+ exit(1);
}
// set priority of periodic thread
if (fosa_thread_attr_set_prio (&attr,fosa_get_priority_min()+3) != 0) {
- printf("Error while setting schedparam\n");
- exit(1);
+ printf("Error while setting schedparam\n");
+ exit(1);
+ }
+
+ /* We set the priority of this main thread to a lever higher */
+ /* than the future periodic thread. */
+ /*************************************************************/
+ if (fosa_thread_set_prio(fosa_thread_self(), fosa_get_priority_min() + 4) )
+ {
+ printf("Error while changing main's priority\n");
+ exit(1);
}
/* create the periodic thread */
- terror = fosa_thread_create(&tid2, &attr, thread_body, NULL);
+ /******************************/
+ terror = fosa_thread_create(&tid_periodic_thread, &attr, thread_body, NULL);
if (terror) {
printf("pthread_create periodic thread\n");
- exit(1);
+ exit(1);
}
printf("Main goes to sleep...\n");
return 0;
}
-// work to be aborted if too long
-void work() {
- static int i=0;
- struct timespec exec_time={1,0}; // 1 second
-
- i++;
- printf("start regular code %d\n",i);
- // regular code
- // eat one second of budget
- frsh_eat(&exec_time);
- // every five cycles eat an additional one second of budget */
- if (i%5==0) {
- frsh_eat(&exec_time);
- }
-}
int err;
- fosa_clock_get_time(FOSA_CLOCK_REALTIME,&activation_time);
+ fosa_clock_get_time(FOSA_CLOCK_REALTIME, &activation_time);
if (fosa_thread_get_cputime_clock(pthread_self(),&clockid) !=0) {
- error_status=RT_ERROR_TIMER;
- pthread_exit ( (void*)&error_status);
+ error_status=RT_ERROR_TIMER;
+ pthread_exit ( (void*)&error_status);
}
// install long jump handler
if (fosa_long_jump_install_handler(&signal,&handler)!=0)
{
- error_status=RT_ERROR_HANDLER;
- pthread_exit ( (void*)&error_status);
+ error_status=RT_ERROR_HANDLER;
+ pthread_exit ( (void*)&error_status);
}
// create budget timer
siginfo.sival_ptr=(void *)(&context);
if (fosa_timer_create_with_receiver
- (clockid,signal,siginfo,&timerid,handler) != 0)
- {
- error_status=RT_ERROR_TIMER;
- pthread_exit ( (void*)&error_status);
- }
+ (clockid,signal,siginfo,&timerid,handler) != 0)
+ {
+ error_status=RT_ERROR_TIMER;
+ pthread_exit ( (void*)&error_status);
+ }
printf("Start periodic thread body\n");
// main loop
while(1) {
+
+ jumped = 0;
- // set the budget timer
- if (fosa_timer_arm(timerid, false, &budget) != 0) {
- error_status=RT_ERROR_TIMER;
- pthread_exit ( (void*)&error_status);
- }
+ // set the budget timer
+ if (fosa_timer_arm(timerid, false, &budget) != 0) {
+ error_status=RT_ERROR_TIMER;
+ pthread_exit ( (void*)&error_status);
+ }
- printf("Begin thread main loop at %d,%d\n",(int)activation_time.tv_sec,
- (int)(activation_time.tv_nsec/1000000));
+ printf("Begin thread main loop at %d,%d\n",(int)activation_time.tv_sec,
+ (int)(activation_time.tv_nsec/1000000));
- // save context
+ // save context
- err=fosa_long_jump_save_context(&context);
- if (err!=0) {
- error_status=RT_ERROR_HANDLER;
- pthread_exit ( (void*)&error_status);
- }
-
- err=fosa_long_jump_was_performed(&context,&jumped);
- if (err!=0) {
- error_status=RT_ERROR_HANDLER;
- pthread_exit ( (void*)&error_status);
- }
- if (!jumped) {
- work();
- } else {
- // code executed if asynchronous jump instruction invoked */
- printf("Aborted thread OEEEEEEEEEEEEEEEEEEEE\n");
- }
-
- printf("after abortable block\n");
- fosa_clock_get_time(FOSA_CLOCK_REALTIME,&old_activation_time);
- decr_timespec(&old_activation_time,&activation_time);
- printf("End thread %6.3f\n",old_activation_time.tv_sec+(float)
- (float)old_activation_time.tv_nsec/1000000000.0);
- // sleep for a while
- incr_timespec(&activation_time,&period);
- clock_nanosleep(FOSA_CLOCK_REALTIME,TIMER_ABSTIME,
- &activation_time,&old_activation_time);
+ err=fosa_long_jump_save_context(&context);
+ if (err!=0) {
+ error_status=RT_ERROR_HANDLER;
+ pthread_exit ( (void*)&error_status);
+ }
+
+ err=fosa_long_jump_was_performed(&context,&jumped);
+ if (err!=0) {
+ error_status=RT_ERROR_HANDLER;
+ pthread_exit ( (void*)&error_status);
+ }
+ if (!jumped) {
+ work();
+ } else {
+ // code executed if asynchronous jump instruction invoked */
+ printf("Aborted thread\n");
+ }
+
+ printf("after abortable block\n");
+ fosa_clock_get_time(FOSA_CLOCK_REALTIME,&old_activation_time);
+ decr_timespec(&old_activation_time,&activation_time);
+ printf("End thread %6.3f\n",old_activation_time.tv_sec+(float)
+ (float)old_activation_time.tv_nsec/1000000000.0);
+ // sleep for a while
+ incr_timespec(&activation_time,&period);
+ clock_nanosleep(FOSA_CLOCK_REALTIME,TIMER_ABSTIME,
+ &activation_time,&old_activation_time);
} // while
}
+
+// ---------------------------------------------------------------------
+
+// work to be aborted if too long
+static void work()
+{
+ static int i=0;
+ struct timespec exec_time={1,0}; // 1 second
+
+ i++;
+ printf("start regular code %d\n",i);
+ // regular code
+ // eat one second of budget
+ frsh_eat(&exec_time);
+ // every five cycles eat an additional one second of budget */
+ if (i%5==0) {
+ frsh_eat(&exec_time);
+ frsh_eat(&exec_time);
+ frsh_eat(&exec_time);
+ frsh_eat(&exec_time);
+ frsh_eat(&exec_time);
+ }
+}