1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2009 FRESCOR consortium partners:
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
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org for a link to partners' websites
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
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // FSF API web pages: http://marte.unican.es/fsf/docs
32 // http://shark.sssup.it/contrib/first/docs/
34 // This file is part of FOSA (Frsh Operating System Adaption)
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.
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 // // /******/ //////// // //
66 // FOSA(Frescor Operating System Adaptation layer)
67 //================================================
71 //#include <rtl_sched.h>
72 #include <rtl_malloc.h>
74 /********************************
75 * Application-defined scheduling
76 ********************************/
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;
85 int fosa_ads_scheduler_create (const fosa_ads_scheduler_ops_t * scheduler_ops,
86 size_t scheduler_data_size,
88 size_t init_args_size){
89 struct sched_param sched_param;
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");
100 memcpy((unsigned char *)fosa_scheduler_args,
101 (unsigned char *)init_args, init_args_size);
103 fosa_scheduler_operations = *scheduler_ops;
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);
111 ret=pthread_create(&fosa_scheduler_thread, &attr,
112 fosa_scheduler_loop, NULL);
118 int fosa_thread_attr_set_appscheduled (frsh_thread_attr_t *attr,
122 attr->policy=SCHED_APP;
124 attr->policy=SCHED_FIFO;
130 int fosa_thread_attr_get_appscheduled (const frsh_thread_attr_t *attr,
133 if (attr->policy==SCHED_APP)
142 int fosa_thread_attr_set_appsched_params (frsh_thread_attr_t *attr,
145 return pthread_attr_setappschedparam(attr, (void *)param, paramsize);
148 int fosa_thread_attr_get_appsched_params (const frsh_thread_attr_t *attr,
151 return pthread_attr_getappschedparam((frsh_thread_attr_t *)attr, param, paramsize);
154 int fosa_ads_set_appscheduled (frsh_thread_id_t thread,
156 pthread_t sched=(appscheduled)?fosa_scheduler_thread:0;
158 return (!pthread_setappscheduler(thread, sched))?0:EINVAL;
161 int fosa_ads_get_appscheduled (frsh_thread_id_t thread,
164 *appscheduled=(appscheduler(thread))?1:0;
170 int fosa_ads_set_appschedparam (frsh_thread_id_t thread,
173 return (!pthread_setappschedparam(thread, (void *)param, paramsize))?0:EINVAL;
176 int fosa_ads_get_appsched_params (frsh_thread_id_t thread,
179 return (!pthread_getappschedparam(thread, param, paramsize))?0:EINVAL;
182 int fosa_ads_set_appsched_params (frsh_thread_id_t thread,
185 return (!pthread_setappschedparam(thread, (void *)param, paramsize))?0:EINVAL;
188 /*********************************
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
196 *********************************/
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);
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);
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);
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},
224 if (fosa_timeout_timer)
227 signal.sigev_signo=0;
228 signal.sigev_notify=SIGEV_NONE;
230 timer_create(clock_id, &signal, &fosa_timeout_timer);
231 timer_settime(fosa_timeout_timer, 0, &at_itimer, 0);
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},
245 if (fosa_timeout_timer)
248 signal.sigev_value.sival_ptr=(void *)clock_id;
249 signal.sigev_signo=(int)thread;
250 signal.sigev_notify=SIGEV_NONE;
252 timer_create(clock_id, &signal, &fosa_timeout_timer);
253 timer_settime(fosa_timeout_timer, 0, &at_itimer, 0);
256 extern rtl_sigset_t fosa_scheduler_signal_bitmap; // as declared in rtl_fosa.c
258 int fosa_ads_set_handled_signal_set(frsh_signal_t set[], int size){
261 rtl_sigemptyset(&fosa_scheduler_signal_bitmap);
262 for (x=0; x<size; x++)
263 rtl_sigaddset(&fosa_scheduler_signal_bitmap, set[x]);
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);