]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - src_rtlinux/fosa_app_def_sched.c
Updating header text in FOSA files for the incoming final project
[frescor/fosa.git] / src_rtlinux / fosa_app_def_sched.c
1 // -----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2009 FRESCOR consortium partners:
3 //
4 //    Universidad de Cantabria,              SPAIN
5 //    University of York,                    UK
6 //    Scuola Superiore Sant'Anna,            ITALY
7 //    Kaiserslautern University,             GERMANY
8 //    Univ. Politécnica  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 for a link to partners' websites
17 //
18 //           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 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 //   FSF API web pages: http://marte.unican.es/fsf/docs
32 //                      http://shark.sssup.it/contrib/first/docs/
33 //
34 //   This file is part of FOSA (Frsh Operating System Adaption)
35 //
36 //  FOSA is free software; you can redistribute it and/or modify it
37 //  under terms of the GNU General Public License as published by the
38 //  Free Software Foundation; either version 2, or (at your option) any
39 //  later version.  FOSA is distributed in the hope that it will be
40 //  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
41 //  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
42 //  General Public License for more details. You should have received a
43 //  copy of the GNU General Public License along with FOSA; see file
44 //  COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
45 //  Cambridge, MA 02139, USA.
46 //
47 //  As a special exception, including FOSA header files in a file,
48 //  instantiating FOSA generics or templates, or linking other files
49 //  with FOSA objects to produce an executable application, does not
50 //  by itself cause the resulting executable application to be covered
51 //  by the GNU General Public License. This exception does not
52 //  however invalidate any other reasons why the executable file might be
53 //  covered by the GNU Public License.
54 // -----------------------------------------------------------------------
55 //fosa_app_def_sched.c
56 //==============================================
57 //  ********  ******    ********  **********
58 //  **///// /**    **  **//////  /**     /**
59 //  **      /**    ** /**        /**     /**
60 //  ******* /**    ** /********* /**********
61 //  **////  /**    ** ////////** /**//////**
62 //  **      /**    **        /** /**     /**
63 //  **      /**    **  ********  /**     /**
64 //  //       /******/  ////////   //      // 
65 //
66 // FOSA(Frescor Operating System Adaptation layer)
67 //================================================
68
69 #include <fosa.h>
70 #include <rtl.h>
71 //#include <rtl_sched.h>
72 #include <rtl_malloc.h>
73
74 /********************************
75  * Application-defined scheduling
76  ********************************/
77
78
79 extern fosa_ads_scheduler_ops_t fosa_scheduler_operations; // See rtl_appsched.c
80 extern void *fosa_scheduler_loop(void *arg);     // See rtl_appsched.c
81 extern void *fosa_scheduler_args;
82 extern int   fosa_scheduler_args_size;
83
84
85 int fosa_ads_scheduler_create (const fosa_ads_scheduler_ops_t * scheduler_ops, 
86                                size_t scheduler_data_size,
87                                void * init_args, 
88                                size_t init_args_size){
89     struct sched_param sched_param;
90     pthread_attr_t  attr; 
91     int ret;
92
93     // There will be only ONE single ADS scheduler facility in the system
94     fosa_scheduler_args_size=init_args_size;
95     fosa_scheduler_args=rtl_malloc(init_args_size);
96     if (!fosa_scheduler_args) { 
97         rtl_printf("ERROR: fosa_ads_scheduler_create, rtl_mallox failed\n");
98         return -1;
99     }
100     memcpy((unsigned char *)fosa_scheduler_args, 
101            (unsigned char *)init_args, init_args_size);
102
103     fosa_scheduler_operations = *scheduler_ops;
104
105     pthread_attr_init (&attr);
106     sched_param.sched_priority=100; // Only one priority will be
107                                       // used in the system.
108     pthread_attr_setappschedulerstate(&attr, PTHREAD_APPSCHEDULER);
109     pthread_attr_setschedparam (&attr, &sched_param);
110     
111     ret=pthread_create(&fosa_scheduler_thread, &attr, 
112                        fosa_scheduler_loop, NULL);
113     return ret;
114 }
115
116
117
118 int fosa_thread_attr_set_appscheduled (frsh_thread_attr_t *attr,
119                                        bool appscheduled){
120      if (attr){
121           if (appscheduled)
122                attr->policy=SCHED_APP;
123           else
124                attr->policy=SCHED_FIFO;
125           return 0;
126      } 
127      else return EINVAL;
128 }
129
130 int fosa_thread_attr_get_appscheduled (const frsh_thread_attr_t *attr,
131                                        bool *appscheduled){
132         if (attr){
133              if (attr->policy==SCHED_APP)
134                   *appscheduled=true;
135              else
136                   *appscheduled=false;
137              return 0;
138         }
139         else return EINVAL;
140 }
141
142 int fosa_thread_attr_set_appsched_params (frsh_thread_attr_t *attr,
143                                                  const void *param,
144                                                  size_t paramsize){
145      return pthread_attr_setappschedparam(attr, (void *)param, paramsize);
146 }
147
148 int fosa_thread_attr_get_appsched_params (const frsh_thread_attr_t *attr,
149                                           void *param,
150                                           size_t *paramsize){
151      return pthread_attr_getappschedparam((frsh_thread_attr_t *)attr, param, paramsize);        
152 }
153
154 int fosa_ads_set_appscheduled (frsh_thread_id_t thread,
155                                bool appscheduled){
156      pthread_t sched=(appscheduled)?fosa_scheduler_thread:0;
157      
158      return (!pthread_setappscheduler(thread, sched))?0:EINVAL;
159 }
160
161 int fosa_ads_get_appscheduled (frsh_thread_id_t thread,
162                                bool *appscheduled){
163      if (thread) {
164           *appscheduled=(appscheduler(thread))?1:0;
165           return 0;
166      }
167      return EINVAL;
168 }
169
170 int fosa_ads_set_appschedparam (frsh_thread_id_t thread,
171                                 const void *param,
172                                 size_t paramsize){
173      return (!pthread_setappschedparam(thread, (void *)param, paramsize))?0:EINVAL;
174 }
175
176 int fosa_ads_get_appsched_params (frsh_thread_id_t thread,
177                                   void *param,
178                                   size_t *paramsize){
179      return (!pthread_getappschedparam(thread, param, paramsize))?0:EINVAL;
180 }
181
182 int fosa_ads_set_appsched_params (frsh_thread_id_t thread,
183                                   const void *param,
184                                   size_t paramsize){
185      return (!pthread_setappschedparam(thread, (void *)param, paramsize))?0:EINVAL;
186 }
187
188 /*********************************
189  * ADS actions
190  *
191  * A scheduling actions object is used to specify a series of actions
192  * to be performed by the system at the end of a scheduler primitive
193  * operation. The order of the actions added to the object shall be
194  * preserved.
195  *
196  *********************************/
197
198 int fosa_adsactions_add_reject(fosa_ads_actions_t *sched_actions,
199                                frsh_thread_id_t thread){
200     return posix_appsched_actions_addreject(sched_actions, thread);
201 }
202
203 int fosa_adsactions_add_activate(fosa_ads_actions_t *sched_actions,
204                                  frsh_thread_id_t thread,
205                                  fosa_ads_urgency_t urgency){
206     RTL_PRIO(thread)=set_urgency(thread, urgency);
207     return posix_appsched_actions_addactivate(sched_actions, thread);
208 }
209
210 int fosa_adsactions_add_suspend(fosa_ads_actions_t *sched_actions,
211                                        frsh_thread_id_t thread){
212      return posix_appsched_actions_addsuspend (sched_actions, thread);
213 }
214
215 extern timer_t fosa_timeout_timer; // defined in rtl_fosa.c
216 int fosa_adsactions_add_timeout(fosa_ads_actions_t *sched_actions,
217                                 fosa_clock_id_t clock_id,
218                                 const struct timespec *at_time){
219      struct sigevent signal;
220      struct itimerspec at_itimer= (struct itimerspec)
221          {.it_interval=(struct timespec){0,0},
222           .it_value=*at_time};
223
224      if (fosa_timeout_timer)
225           return EINVAL;
226
227      signal.sigev_signo=0;
228      signal.sigev_notify=SIGEV_NONE;
229
230      timer_create(clock_id, &signal, &fosa_timeout_timer);
231      timer_settime(fosa_timeout_timer, 0, &at_itimer, 0);
232      return 0;
233 }
234
235
236 int fosa_adsactions_add_thread_notification(fosa_ads_actions_t *sched_actions,
237                                             frsh_thread_id_t thread,
238                                             fosa_clock_id_t clock_id,
239                                             const struct timespec *at_time){
240      struct sigevent signal;
241      struct itimerspec at_itimer= (struct itimerspec)
242          {.it_interval=(struct timespec){0,0},
243           .it_value=*at_time};
244      
245      if (fosa_timeout_timer)
246           return EINVAL;
247
248      signal.sigev_value.sival_ptr=(void *)clock_id;
249      signal.sigev_signo=(int)thread;
250      signal.sigev_notify=SIGEV_NONE;
251
252      timer_create(clock_id, &signal, &fosa_timeout_timer);
253      timer_settime(fosa_timeout_timer, 0, &at_itimer, 0);
254      return 0;
255 }
256 extern rtl_sigset_t fosa_scheduler_signal_bitmap; // as declared in rtl_fosa.c
257
258 int fosa_ads_set_handled_signal_set(frsh_signal_t set[], int size){
259      int x;
260
261      rtl_sigemptyset(&fosa_scheduler_signal_bitmap);
262      for (x=0; x<size; x++)
263          rtl_sigaddset(&fosa_scheduler_signal_bitmap, set[x]); 
264      return 0;
265 }
266
267
268 int fosa_ads_invoke_withdata (const void *msg, size_t msg_size, 
269                               void *reply, size_t *reply_size){
270     return posix_appsched_invoke_withdata((void *)msg, msg_size, reply, reply_size);
271 }