]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_ose/fosa_ose_implementation_specific.c
Removed fosa/marte_non_local_jump directory (now part of MaRTE OS)
[frescor/fosa.git] / src_ose / fosa_ose_implementation_specific.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politecnica  Valencia,           SPAIN
9 //    Czech Technical University in Prague,  CZECH REPUBLIC
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
18 //        The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 //        in part by the European Union Sixth Framework Programme
20 //        The European Union is not liable of any use that may be
21 //        made of this code.
22 //
23 //  All rights reserved.
24 //
25 //  Redistribution and use in source and binary forms, with or 
26 //  without modification, are permitted provided that the 
27 //  following conditions are met:
28 //
29 //    * Redistributions of source code must retain the above 
30 //      copyright notice, this list of conditions and the 
31 //      following disclaimer.
32 //    * Redistributions in binary form must reproduce the above 
33 //      copyright notice, this list of conditions and the 
34 //      following disclaimer in the documentation and/or other 
35 //      materials provided with the distribution.
36 //    * Neither the name of FRESCOR nor the names of its 
37 //      contributors may be used to endorse or promote products 
38 //      derived from this software without specific prior 
39 //      written permission.
40 //
41 //  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
42 //  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
43 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
44 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
45 //  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
46 //  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
47 //  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
48 //  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
49 //  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
50 //  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
51 //  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
52 //  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
53 //  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
54 //  POSSIBILITY OF SUCH DAMAGE.
55 // -----------------------------------------------------------------------
56 //fosa_ose_implementation_specific.c
57 //==============================================
58 //  ********  ******    ********  **********
59 //  **///// /**    **  **//////  /**     /**
60 //  **      /**    ** /**        /**     /**
61 //  ******* /**    ** /********* /**********
62 //  **////  /**    ** ////////** /**//////**
63 //  **      /**    **        /** /**     /**
64 //  **      /**    **  ********  /**     /**
65 //  //       /******/  ////////   //      // 
66 //
67 // FOSA(Frescor Operating System Adaptation layer)
68 //================================================
69
70 #include "ose.h"
71 #include "heapapi.h"
72
73 #include "fosa.h"
74 #include "fosa_ose_implementation_specific.h"
75
76 #include "frsh.h"
77 #include "frsh_scheduler.h"
78
79
80 //For debug purpose only.
81 #include "stdio.h"
82
83 //The following function is placed in the fosa_app_def_sched.c file.
84 extern int fosa_ose_adsactions_add(fosa_ads_actions_t* sched_actions,
85                             action_t            action,
86                             frsh_thread_id_t    thread,
87                             fosa_ads_urgency_t  urgency,
88                             fosa_clock_id_t     clock_id,
89                             time_t              sec,
90                             long                nsec);
91
92 /* Application Defined Scheduling Callbacks */
93 /********************************************/
94 //extern void frsh_callback_init(void *sched_data, void *arg);
95 /*
96 extern void frsh_callback_new_thread(void *sched_data, frsh_thread_id_t thread, 
97                                       fosa_ads_actions_t *actions, struct timespec *current_time);
98
99 extern static void frsh_callback_thread_terminate(void *sched_data, frsh_thread_id_t thread,
100                                      fosa_ads_actions_t *actions, struct timespec *current_time);
101  
102 extern static void frsh_callback_thread_ready(void *sched_data, frsh_thread_id_t thread,
103                                         fosa_ads_actions_t *actions, struct timespec *current_time);
104
105 extern static void frsh_callback_thread_block(void *sched_data, frsh_thread_id_t thread,
106                                         fosa_ads_actions_t *actions, struct timespec *current_time);
107
108 extern static void frsh_callback_change_sched_param_thread(void *sched_data, frsh_thread_id_t thread,
109                                               fosa_ads_actions_t *actions, 
110                                               struct timespec *current_time);
111
112 extern static void frsh_callback_explicit_call_with_data(void * sched_data,
113                                                   frsh_thread_id_t thread,
114                                                   const void * msg, size_t msg_size,
115                                                   void **reply, size_t *reply_size,
116                                                   fosa_ads_actions_t * actions,
117                                                   struct timespec *current_time);
118
119
120 extern static void frsh_callback_notification_for_thread(void * sched_data,
121                                                   frsh_thread_id_t thread,
122                                                   fosa_clock_id_t clock,
123                                                   fosa_ads_actions_t * actions,
124                                                   struct timespec *current_time);
125
126
127 extern static void frsh_callback_timeout(void *sched_data, fosa_ads_actions_t *actions,
128                                    struct timespec *current_time);
129
130 extern static void frsh_callback_signal(void *sched_data, frsh_signal_t signal, frsh_signal_info_t siginfo,
131                                   fosa_ads_actions_t *actions, struct timespec *current_time);
132
133 extern static void frsh_callback_appsched_error(void *sched_data, frsh_thread_id_t thread,
134                                           fosa_ads_error_cause_t cause, fosa_ads_actions_t *actions);
135 */
136
137
138 //Osäker på hur dessa funktioner skall implementeras...........................
139
140 /**
141  * fosa_ose_convert_prio_to_ose()
142  *
143  * Maps fosa priority to ose priority. FOSA internal function.
144  * Faster if defined as a macro?...............................................
145  **/
146 //OSPRIORITY fosa_ose_convert_prio_to_ose(int fosa_prio)
147 //{
148 //    return (OSPRIORITY) fosa_get_priority_max() + 
149 //                        fosa_get_priority_min() - fosa_prio;
150 //}
151
152 /**
153  * fosa_ose_convert_prio_to_fosa()
154  *
155  * Maps ose priority to fosa priority. FOSA internal function.
156  **/
157 //int fosa_ose_convert_prio_to_fosa(OSPRIORITY ose_prio)
158 //{
159 //    return fosa_get_priority_max() + 
160 //           fosa_get_priority_min() - (int) ose_prio;
161 //}
162
163 unsigned int process_name_number = 0;
164
165 bool fosa_ose_keys_initialized = false;
166 bool fosa_ose_keys_taken[FOSA_MAX_KEYS];
167
168 bool fosa_ose_is_process(PROCESS Pid)
169 {
170    switch (get_ptype(Pid))
171    {
172       case OS_BG_PROC:
173       case OS_PRI_PROC:
174       case OS_TI_PROC:
175       case OS_INT_PROC:
176       case OS_PHANTOM:
177      return true;
178       default:
179      return false;
180    }
181 }
182
183 void fosa_ose_ads_actions_init(fosa_ads_actions_t *sched_actions)
184 {
185     sched_actions->FirstAction = NULL;
186     sched_actions->LastAction  = NULL;
187 }
188
189 void fosa_ose_adsactions_remove_first(fosa_ads_actions_t *sched_actions)
190 {
191     fosa_ose_action_t* action_to_remove;
192     
193     action_to_remove           = sched_actions->FirstAction; 
194     sched_actions->FirstAction = sched_actions->FirstAction->NextAction;
195     if (sched_actions->FirstAction == NULL) {
196         sched_actions->LastAction  = NULL;
197     }
198     
199     heap_free_shared(action_to_remove);     
200 }
201
202 void fosa_ose_systime_to_timespec(unsigned long ticks, 
203                     unsigned long microsec, struct timespec* time)
204 {
205     // If the unsigned long are 32 bit then this function can measure up 
206     // to 1,36 years before it loops to zero.
207     unsigned long microsec_per_tick = system_tick();
208     unsigned long tics_per_sec      = (1000000 / microsec_per_tick);
209     time->tv_sec  =  ticks / tics_per_sec;
210     time->tv_nsec = (ticks % tics_per_sec) * 10000000 + microsec * 1000; 
211 }
212
213 void fosa_ose_execute_action_activate(PROCESS pid) 
214 {
215     start(pid);
216 }
217
218 void fosa_ose_execute_action_suspend(PROCESS pid)
219 {
220     /*
221      * author erth
222      *
223      * A check to see that the process have not locked a mutex before the
224      * stop command are executed would be good to avoid deadlooks.
225      */
226     stop(pid);
227 }
228
229 void fosa_ose_execute_action_reject(PROCESS pid) 
230 {
231     //Send signal to the pid with the message to reject it.
232     fosa_ose_event_signal_t *reply_sig;
233     reply_sig = (fosa_ose_event_signal_t *)
234                 alloc(sizeof(fosa_ose_event_signal_t),
235                 FOSA_OSE_REJECT);
236     send((union SIGNAL **)&reply_sig, pid);
237 }
238
239 void fosa_ose_execute_action_accept(PROCESS pid) 
240 {
241     //Send signal to the pid with the message to accept it.
242     fosa_ose_event_signal_t *reply_sig;
243     reply_sig = (fosa_ose_event_signal_t *)
244                 alloc(sizeof(fosa_ose_event_signal_t),
245                 FOSA_OSE_ACCEPT);
246     send((union SIGNAL **)&reply_sig, pid);
247 }
248
249 fosa_ose_process_info_t* fosa_ose_processes = NULL;
250
251 fosa_ose_process_info_t* fosa_ose_process_get_node(frsh_thread_id_t pid) {
252     if (fosa_ose_processes == NULL) return NULL; //Not found.
253     fosa_ose_process_info_t* node = fosa_ose_processes;
254     
255     while(node->pid != pid) {
256         node = node->NextProcess;
257         if (node == NULL) break;
258     }
259     return node;
260 }
261
262 int fosa_ose_process_list_node_remove(frsh_thread_id_t pid) {
263     if (fosa_ose_processes == NULL) return FOSA_EINVAL;
264     fosa_ose_process_info_t* node = fosa_ose_processes;
265     fosa_ose_process_info_t* prev_node = NULL;
266     
267     while(node->pid != pid) {
268         if (node->NextProcess == NULL) return FOSA_EINVAL;
269         prev_node = node;
270         node = node->NextProcess;
271     }
272     
273     if (prev_node == NULL) {
274         //New first node in list, or now list gets empty.
275         fosa_ose_processes = node->NextProcess;
276     } else {
277         //Not first node.
278         prev_node->NextProcess = node->NextProcess;
279     }
280     
281     if(node->appsched_param != NULL) 
282         heap_free_shared(node->appsched_param);
283     heap_free_shared(node);
284     
285     return 0;
286 }
287
288 int frsh_ose_callback_test() {
289     ramlog_printf("Sucess by: frsh_ose_callback_test()"); 
290     return 0;
291 }
292
293 int fosa_ose_adsactions_add_test(
294         fosa_ads_actions_t *sched_actions,
295         action_t string)
296 {
297     return fosa_ose_adsactions_add( sched_actions,      //sched_actions
298                                     string,             //action
299                                     current_process(),  //thread
300                                     0,                  //urgency
301                                     0,                  //clock_id
302                                     0,                  //sec
303                                     0);                 //nsec
304 }
305
306
307 void fosa_ose_scheduler_ops_init(fosa_ads_scheduler_ops_t *scheduler_ops)
308 {
309     //Cnage functions to point to here!..........................................
310     scheduler_ops->init             = (void *) &frsh_ose_callback_test;
311     scheduler_ops->new_thread       = (void *) &frsh_ose_callback_test;
312     scheduler_ops->thread_terminate = (void *) &frsh_ose_callback_test;
313     scheduler_ops->thread_ready     = (void *) &frsh_ose_callback_test;
314     scheduler_ops->thread_block     = (void *) &frsh_ose_callback_test;
315     scheduler_ops->signal           = (void *) &frsh_ose_callback_test;
316     scheduler_ops->change_sched_param_thread = (void *) NULL;
317     scheduler_ops->explicit_call_with_data   = (void *) NULL;
318     scheduler_ops->notification_for_thread   = (void *) NULL;
319     scheduler_ops->timeout                   = (void *) NULL;
320     scheduler_ops->appsched_error            = (void *) NULL;
321 }
322
323 void fosa_ose_send_event_to_scheduler(  PROCESS event_pid, 
324                                         SIGSELECT event_sig) 
325 {
326     //Send signal to fosa scheduler.
327     fosa_ose_event_signal_t *sig;
328     sig = (fosa_ose_event_signal_t *)
329             alloc(sizeof(fosa_ose_event_signal_t),
330             event_sig);
331     sig->pid = event_pid;        
332     PROCESS scheduler_pid;
333     hunt("fosa_scheduler_process", 0, 
334          &scheduler_pid, (union SIGNAL **) NULL);
335     if (scheduler_pid != 0) {
336         send((union SIGNAL **)&sig, scheduler_pid);
337     } else {
338         ramlog_printf(
339             "Failiure by: fosa_scheduler_process not found.\n");
340     }                                       
341 }