]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_marte/tests/test_non_local_jump/test_fosa_long_jump.c
Migrating FOSA trunk to d-ac2v2. Phase 1 moving FRSH-FOSA to FOSA
[frescor/fosa.git] / src_marte / tests / test_non_local_jump / test_fosa_long_jump.c
1 /*
2 ** testbench_long_jump.c
3 ** 
4 ** Made by (Miguel marciano)
5 ** Login   <miguel@namir.ctr.unican.es>
6 ** 
7 ** Started on  Fri Nov 23 11:42:09 2007 Miguel marciano
8 ** Last update Sun May 12 01:17:25 2002 Speed Blue
9 */
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <time.h> // For clock_nanosleep
15
16 #include "fosa.h"
17 #include "timespec_operations.h"
18
19 #include <misc/error_checks.h>
20
21
22
23 /*************************/
24 /* D E F I N I T I O N S */
25 /*************************/
26 #define PERIODIC_THREAD_PRIORITY (fosa_get_priority_min() + 3)
27 #define MAIN_THREAD_PRIORITY (fosa_get_priority_min() + 5)
28
29
30 /*************************/
31 /*  P R O T O T Y P E S  */
32 /*************************/
33 static void *periodic_code(void *thread_arg);
34
35 /**********************************/
36 /* S T A T I C  V A R I A B L E S */
37 /**********************************/
38 static fosa_long_jump_context_t context;
39 static void work_under_a_interruptible_budget();
40
41
42 int main()
43 {
44     fosa_thread_attr_t periodic_attr;
45     fosa_signal_t signal_set[1];
46     fosa_thread_id_t periodic_tid;
47
48     memset(&context, 0, sizeof(context) );
49
50     memset(&periodic_attr, 0, sizeof(periodic_attr) );
51     memset(&signal_set, 0, sizeof(signal_set) );
52     memset(&periodic_tid, 0, sizeof(periodic_tid) );
53
54
55     /* We set the signal mask */
56     signal_set[0] = FOSA_LONG_JUMP_SIGNAL;
57
58     CHK(  fosa_set_accepted_signals(signal_set, 1) );
59
60     /* We create a new thread with a given priority */
61     CHK(  fosa_thread_attr_init(&periodic_attr) );
62     CHK(  fosa_thread_attr_set_prio(&periodic_attr, PERIODIC_THREAD_PRIORITY)  );
63     CHK(  fosa_thread_create(&periodic_tid, &periodic_attr, periodic_code, NULL) );
64
65     printf("Main goes to sleep...\n");
66
67     sleep(2000);
68
69     return 0;
70 }
71
72 // ------------------------------------------------------------------------
73
74
75 static void *periodic_code(void *thread_arg)
76 {
77     struct timespec period = {2, 500000000};  // 2.5 secs
78     fosa_thread_id_t jump_handler_thread;
79
80     fosa_signal_t jump_signal;
81     fosa_signal_info_t jump_signal_info;
82
83     fosa_clock_id_t clock_id;
84     fosa_timer_id_t jump_timer;
85
86     memset(&jump_signal, 0, sizeof(jump_signal) );
87     memset(&jump_signal_info, 0, sizeof(jump_signal_info) );
88     memset(&jump_handler_thread, 0, sizeof(jump_handler_thread) );
89     memset(&clock_id, 0, sizeof(clock_id) );
90     memset(&jump_timer, 0, sizeof(jump_timer) );
91
92     
93     /* We install a long jump handler               */
94     /* - This creates the thread that will wait for */
95     /*   FOSA_JUMP_SIGNAL                           */
96     /************************************************/
97     CHK(  fosa_long_jump_install_handler(&jump_signal, &jump_handler_thread) );
98     
99     /* We create a budget timer using the thread's CPU clock */
100     /*                                                       */
101     /* When the timer expires:                               */
102     /* -  Triggers the signal corresponding to signal jump   */
103     /* -  Provides a pointer to the context in siginfo.      */
104     /*                                                       */
105     /* This signal is delivered to the handler thread.       */
106     /*********************************************************/
107     CHK(  fosa_thread_get_cputime_clock( fosa_thread_self(), &clock_id) );
108     jump_signal_info.sival_ptr = &context;
109     CHK(  fosa_timer_create_with_receiver(clock_id, jump_signal, jump_signal_info, 
110                                           &jump_timer, jump_handler_thread)  );
111
112     
113     /* Periodic loop */
114     /*****************/
115     while (1)
116     {
117         int jumped = -1;
118         struct timespec budget = {1, 400000000};  // 1.4 secs
119         struct timespec activation_time = {-1, -1};
120         struct timespec old_activation_time = {-1, -1};
121
122
123
124         jumped = 0;
125
126         /* For statistical purposes we read the activation time */
127         CHK(  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &activation_time) );
128
129
130
131         /* Start of the interruptible block */
132         /************************************/
133
134         /* We arm the jump_timer */
135         CHK(  fosa_timer_arm(jump_timer, false, &budget) );
136
137         /* This is the point where the jump returns */
138         CHK(  fosa_long_jump_save_context(&context) );
139
140         /* Query if we come from a jump */
141         CHK(  fosa_long_jump_was_performed(&context, &jumped) );
142         if (!jumped)
143         {
144             /* HERE COMES THE WORK THAT CAN BE INTERRUPTED */
145             work_under_a_interruptible_budget();
146             CHK(  fosa_timer_disarm(jump_timer, NULL) );
147             printf("NOT JUMPPED\n");
148         }
149         else
150         {
151             printf("JUMPPPPPEEED\n");
152         }
153
154         
155         /* End of interruptible work */
156         /*****************************/
157
158         printf("After interruptible block\n");
159         
160         /* Now we measure the time duration of the block */
161         /*************************************************/
162         CHK(  fosa_clock_get_time(FOSA_CLOCK_REALTIME, &old_activation_time)  );
163         decr_timespec(&old_activation_time, &activation_time);
164         printf("Execution time: %6.3f\n", t2d(old_activation_time) );
165     
166         /* And we program the next loop */
167         incr_timespec(&activation_time, &period);
168         clock_nanosleep(FOSA_CLOCK_REALTIME, TIMER_ABSTIME, &activation_time,
169                         &old_activation_time);
170     }
171         
172     return NULL;
173 }
174
175
176 // ------------------------------------------------------------------------------
177
178 static void work_under_a_interruptible_budget()
179 {
180     static int i = 0;
181     struct timespec exec_time = {1, 0};  // 1 seg
182
183     i++;
184     printf("Start regular work\n");
185
186     fosa_eat(&exec_time);
187
188     /* Once in every 5 executions we work over the budget */
189     if (i % 5 == 0)
190     {
191         fosa_eat(&exec_time);
192         fosa_eat(&exec_time);
193         fosa_eat(&exec_time);
194         fosa_eat(&exec_time);
195         fosa_eat(&exec_time);
196         fosa_eat(&exec_time);
197     }
198
199     printf("End regular work\n");
200     
201 }