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