1 // -----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2008 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 //==============================================
56 // ******** ****** ******** **********
57 // **///// /** ** **////// /** /**
58 // ** /** ** /** /** /**
59 // ******* /** ** /********* /**********
60 // **//// /** ** ////////** /**//////**
61 // ** /** ** /** /** /**
62 // ** /** ** ******** /** /**
63 // // /******/ //////// // //
65 // FOSA(Frescor Operating System Adaptation layer)
66 //================================================
68 #include <fosa_configuration_parameters.h>
69 #include <fosa_app_def_sched.h>
70 #include <fosa_threads_and_signals.h>
71 #include <fosa_time.h>
79 #define REACHS(str) printf ("%s: %s: %d: %s\n", __FILE__, __FUNCTION__, __LINE__, str)
81 static pthread_t fosa_scheduler_th; // Scheduler thread
82 static fosa_ads_scheduler_ops_t fosa_scheduler_ops; // Scheduler operations
83 static void * fosa_scheduler_data; // Scheduler parameters
84 static size_t fosa_scheduler_data_size;
85 static void * fosa_init_args; // Scheduler initialisation args
86 static size_t fosa_init_args_size;
87 static sigset_t fosa_handled_signals; // Signals handled by the scheduler
89 // Reply info for 'fosa_ads_invoke_withdata ()'
94 static int fosa_reply_key;
96 void clear_actions (fosa_ads_actions_t *act)
98 posix_appsched_actions_destroy (&(act -> actions));
99 posix_appsched_actions_init (&(act -> actions));
100 act -> activated = false;
101 act -> suspended = false;
102 act -> rejected = false;
103 act -> timeout_ptr = NULL;
106 void *fosa_scheduler_main (void * args)
108 fosa_ads_actions_t actions;
109 struct posix_appsched_event event;
110 struct timespec current_time;
111 posix_appsched_eventset_t accepted_events;
113 // accept events which have a callback associated
114 posix_appsched_fillset (&accepted_events);
115 if (fosa_scheduler_ops.new_thread)
116 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_NEW);
118 if (fosa_scheduler_ops.thread_terminate)
119 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_TERMINATE);
121 if (fosa_scheduler_ops.thread_ready)
122 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_READY);
124 if (fosa_scheduler_ops.thread_block)
125 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_BLOCK);
127 if (fosa_scheduler_ops.change_sched_param_thread)
128 posix_appsched_delset (&accepted_events, POSIX_APPSCHED_CHANGE_SCHED_PARAM);
130 if (fosa_scheduler_ops.explicit_call_with_data)
131 posix_appsched_delset (&accepted_events, POSIX_APPSCHED_EXPLICIT_CALL_WITH_DATA);
133 // if (fosa_scheduler_ops.notification_for_thread)
134 // posix_appsched_delset (&accepted_events,POSIX_APPSCHED_TASK_NOTIFICATION);
136 if (fosa_scheduler_ops.timeout)
137 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_TIMEOUT);
139 if (fosa_scheduler_ops.signal)
140 posix_appsched_delset (&accepted_events,POSIX_APPSCHED_SIGNAL);
142 posix_appschedattr_seteventmask(&accepted_events);
144 // Set the clock (and its) flags used by the scheduler
145 posix_appschedattr_setclock (FOSA_CLOCK_REALTIME);
146 posix_appschedattr_setflags (POSIX_APPSCHED_ABSTIMEOUT);
149 fosa_scheduler_ops.init (fosa_scheduler_data, fosa_init_args);
150 clear_actions (&actions);
152 while (1) { // scheduler loop
153 if (posix_appsched_execute_actions(&(actions.actions), &fosa_handled_signals, actions.timeout_ptr,
154 ¤t_time, &event))
156 fosa_scheduler_ops.appsched_error
157 (fosa_scheduler_data, 0x0, FOSA_ADS_THREAD_NOT_ATTACHED, &actions);
161 clear_actions (&actions);
162 switch (event.event_code) {
163 case POSIX_APPSCHED_NEW:
164 fosa_scheduler_ops.new_thread
165 (fosa_scheduler_data,
170 if (!actions.rejected) {
172 clear_actions (&actions);
174 // alloc memory for reply info of 'fosa_ads_invoke_with_data ()'
175 struct reply_info *reply_mem = malloc (sizeof (struct reply_info));
177 posix_appsched_actions_addreject (&actions.actions, event.thread);
179 posix_appsched_actions_addaccept (&actions.actions, event.thread);
180 pthread_setspecific_for (fosa_reply_key, event.thread, reply_mem);
184 case POSIX_APPSCHED_TERMINATE:
185 fosa_scheduler_ops.thread_terminate
186 (fosa_scheduler_data,
191 case POSIX_APPSCHED_READY:
192 fosa_scheduler_ops.thread_ready
193 (fosa_scheduler_data,
198 case POSIX_APPSCHED_BLOCK:
199 fosa_scheduler_ops.thread_block
200 (fosa_scheduler_data,
205 case POSIX_APPSCHED_CHANGE_SCHED_PARAM:
206 fosa_scheduler_ops.change_sched_param_thread
207 (fosa_scheduler_data,
212 case POSIX_APPSCHED_EXPLICIT_CALL_WITH_DATA:
214 struct reply_info *fosa_reply;
216 // Get reply memory pointer
217 pthread_getspecific_from (fosa_reply_key, event.thread, (void **) &fosa_reply);
218 fosa_scheduler_ops.explicit_call_with_data
219 (fosa_scheduler_data,
221 event.event_info.info,
224 fosa_reply -> size_ptr,
228 // activate the thread unless suspended or already activated
229 if (!actions.suspended && !actions.activated) {
230 posix_appsched_actions_addactivate
231 (&actions.actions, event.thread);
235 case POSIX_APPSCHED_TIMEOUT:
236 fosa_scheduler_ops.timeout
237 (fosa_scheduler_data,
241 case POSIX_APPSCHED_SIGNAL:
242 fosa_scheduler_ops.signal
243 (fosa_scheduler_data,
244 event.event_info.siginfo.si_signo,
245 (fosa_signal_info_t)event.event_info.siginfo.si_value.sival_ptr,
251 case POSIX_APPSCHED_THREAD_NOTIFICATION:
252 fosa_scheduler_ops.notification_for_thread
253 (fosa_scheduler_data,
254 event.thread,(fosa_clock_id_t) event.event_info.info,
260 printf ("BUG: Unknown ADS event\n");
267 /********************************
268 * Application-defined scheduling
269 ********************************/
270 int fosa_ads_scheduler_create
271 (const fosa_ads_scheduler_ops_t * scheduler_ops,
272 size_t scheduler_data_size,
274 size_t init_args_size)
277 struct sched_param sp;
281 fosa_scheduler_ops = *scheduler_ops;
283 // Alloc memory for init args and scheduler data
284 fosa_init_args_size = init_args_size;
285 fosa_init_args = malloc (init_args_size);
289 memcpy (fosa_init_args, init_args, init_args_size);
291 fosa_scheduler_data_size = scheduler_data_size;
292 fosa_scheduler_data = malloc (scheduler_data_size);
293 if (!fosa_scheduler_data) {
297 memset (fosa_scheduler_data, 0, scheduler_data_size);
299 // Key for reply information index of 'fosa_reply' for each thread
300 pthread_key_create (&fosa_reply_key, NULL);
302 // Set scheduler thread parameters
303 pthread_attr_init (&attr);
304 sp.sched_priority = sched_get_priority_max (SCHED_FIFO) + FOSA_ADS_SCHEDULER_PRIO_DIFF;
305 pthread_attr_setschedparam (&attr, &sp);
306 pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
307 pthread_attr_setappschedulerstate (&attr, PTHREAD_APPSCHEDULER);
308 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
309 pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
311 if (pthread_create (&fosa_scheduler_th, &attr, fosa_scheduler_main, NULL))
317 int fosa_thread_attr_set_appscheduled
318 (fosa_thread_attr_t *attr,
323 err = pthread_attr_setappscheduler (attr, fosa_scheduler_th);
328 return pthread_attr_setschedpolicy (attr, SCHED_APP);
330 return pthread_attr_setschedpolicy (attr, SCHED_FIFO);
334 int fosa_thread_attr_get_appscheduled
335 (const fosa_thread_attr_t *attr,
340 err = pthread_attr_getschedpolicy (attr, &policy);
344 *appscheduled = (policy == SCHED_APP);
349 int fosa_thread_attr_set_appsched_params
350 (fosa_thread_attr_t *attr,
354 #if FOSA_ADS_SCHEDPARAM_MAX > POSIX_APPSCHEDPARAM_MAX
355 #error FOSA_ADS_SCHEDPARAM_MAX > POSIX_APPSCHEDPARAM_MAX
357 if (paramsize > FOSA_ADS_SCHEDPARAM_MAX)
360 return pthread_attr_setappschedparam (attr, param, paramsize);
364 int fosa_thread_attr_get_appsched_params
365 (const fosa_thread_attr_t *attr,
369 return pthread_attr_getappschedparam (attr, param, paramsize);
373 int fosa_ads_set_appscheduled
374 (fosa_thread_id_t thread,
377 struct sched_param sp;
378 int policy_old, policy_new, err;
380 err = pthread_getschedparam (thread, &policy_old, &sp);
385 err = pthread_setappscheduler (thread, fosa_scheduler_th);
389 policy_new = SCHED_APP;
391 policy_new = SCHED_FIFO;
394 if (policy_new != policy_old)
395 return pthread_setschedparam (thread, policy_new, &sp);
401 int fosa_ads_get_appscheduled
402 (fosa_thread_id_t thread,
405 struct sched_param sp;
408 err = pthread_getschedparam (thread, &policy, &sp);
412 *appscheduled = (policy == SCHED_APP);
417 int fosa_ads_set_appsched_params
418 (fosa_thread_id_t thread,
422 return pthread_setappschedparam (thread, param, paramsize);
426 int fosa_ads_get_appsched_params
427 (fosa_thread_id_t thread,
431 return pthread_getappschedparam (thread, param, paramsize);
436 /*********************************
438 *********************************/
439 int fosa_adsactions_add_reject
440 (fosa_ads_actions_t *sched_actions,
441 fosa_thread_id_t thread)
443 sched_actions -> rejected = true;
444 return posix_appsched_actions_addreject (&(sched_actions -> actions), thread);
447 extern inline int fosa2prtk (int prio, int urg);
448 int fosa_adsactions_add_activate
449 (fosa_ads_actions_t *sched_actions,
450 fosa_thread_id_t thread,
451 fosa_ads_urgency_t urgency)
453 sched_actions -> activated = true;
455 #ifdef CONFIG_URGENCY
456 #error "Urgency not supported. Disable this feature in the Makefile"
457 int fosaprio, policy;
458 struct sched_param sp;
460 pthread_getschedparam (thread, &policy, &sp);
461 fosa_thread_get_prio (thread, &fosaprio);
462 sp.sched_priority = fosa2prtk (fosaprio, urgency);
463 // printf ("policy=%d, prio=%d, urg=%d, new_prio=%d\n", policy);
464 pthread_setschedparam (thread, policy, &sp);
466 return posix_appsched_actions_addactivate (&(sched_actions -> actions), thread);
469 int fosa_adsactions_add_suspend
470 (fosa_ads_actions_t *sched_actions,
471 fosa_thread_id_t thread)
473 sched_actions -> suspended = true;
474 return posix_appsched_actions_addsuspend (&(sched_actions -> actions), thread);
477 int fosa_adsactions_add_timeout
478 (fosa_ads_actions_t *sched_actions,
479 fosa_clock_id_t clock_id,
480 const fosa_abs_time_t *at_time)
482 sched_actions -> timeout = fosa_abs_time_to_timespec (*at_time);
483 sched_actions -> timeout_ptr = &(sched_actions -> timeout);
487 int fosa_adsactions_add_thread_notification
488 (fosa_ads_actions_t *sched_actions,
489 fosa_thread_id_t thread,
490 fosa_clock_id_t clock_id,
491 const fosa_abs_time_t *at_time)
493 printf ("BUG: fosa_adsactions_add_thread_notification: Not implemented\n");
499 int fosa_ads_set_handled_signal_set (fosa_signal_t set[], int size)
503 if (!pthread_equal (pthread_self (), fosa_scheduler_th))
506 sigemptyset (&fosa_handled_signals);
507 for (i = 0; i < size; i++) {
508 if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
510 sigaddset (&fosa_handled_signals, set[i]);
512 return fosa_set_accepted_signals (set, size);
516 int fosa_signal_queue_scheduler (fosa_signal_t signal, fosa_signal_info_t info)
518 return fosa_signal_queue (signal, info, 0);
522 int fosa_ads_invoke_withdata
523 (const void *msg, size_t msg_size, void *reply, size_t *reply_size)
525 struct reply_info *fosa_reply;
527 #if FOSA_ADS_SCHEDINFO_MAX > POSIX_APPSCHEDINFO_MAX
528 #error FOSA_ADS_SCHEDINFO_MAX > POSIX_APPSCHEDINFO_MAX
531 if (msg_size > FOSA_ADS_SCHEDINFO_MAX)
534 fosa_reply = (struct reply_info *) pthread_getspecific (fosa_reply_key);
535 fosa_reply -> msg = reply;
536 fosa_reply -> size_ptr = reply_size;
538 return posix_appsched_invoke_withdata (msg, msg_size, NULL, NULL);