#include <fosa_configuration_parameters.h>
#include <fosa_app_def_sched.h>
+#include <fosa_threads_and_signals.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
-// #include <fosa_configuration_parameters.h>
#define REACHS(str) printf ("%s: %s: %d: %s\n", __FILE__, __FUNCTION__, __LINE__, str)
struct posix_appsched_event event;
struct timespec current_time;
posix_appsched_eventset_t accepted_events;
- bool active;
+// bool active;
+ printf ("SCHEDULER thread=0x%lx\n\n", (unsigned long) pthread_self ());
// accept events which have a callback associated
posix_appsched_fillset (&accepted_events);
/* active = actions.activated;
int suspend = actions.suspended; */
-#ifdef CONFIG_URGENCY
- #error "Urgency not supported. Disable this feature in the Makefile"
-#else
+// #ifndef CONFIG_URGENCY
clear_actions (&actions);
// printf ("NEW: activate (%d), suspend (%d)\n", actions.activated, actions.suspended);
-#endif
-
- posix_appsched_actions_addaccept (&actions.actions, event.thread);
-
-// printf ("activated\n");
-// if (active)
-// fosa_adsactions_add_activate (&actions, event.thread, 0);
-// if (suspend)
-// fosa_adsactions_add_suspend (&actions, event.thread);
-
-
+// // #endif
+
// alloc memory for reply info of 'fosa_ads_invoke_with_data ()'
struct reply_info *reply_mem = malloc (sizeof (struct reply_info));
if (!reply_mem) {
- printf ("BUG: not enougth dynamic memory\n");
- exit (-20);
+ posix_appsched_actions_addreject (&actions.actions, event.thread);
+ } else {
+ posix_appsched_actions_addaccept (&actions.actions, event.thread);
+ pthread_setspecific_for (fosa_reply_key, event.thread, reply_mem);
}
- pthread_setspecific_for (fosa_reply_key, event.thread, reply_mem);
}
break;
case POSIX_APPSCHED_TERMINATE:
struct reply_info *fosa_reply;
// Get reply memory pointer
-// printf ("CALL_WITH_DATA: caller 0x%x\n", event.thread);
pthread_getspecific_from (fosa_reply_key, event.thread, (void **) &fosa_reply);
-// printf ("reply_size (0x%x)=%d\n", fosa_reply -> size_ptr, (fosa_reply -> size_ptr)? *(fosa_reply -> size_ptr):-1);
fosa_scheduler_ops.explicit_call_with_data
(fosa_scheduler_data,
event.thread,
&actions,
¤t_time);
-// printf ("CALL_WITH_DATA: activate (%d), suspend (%d)\n", actions.activated, actions.suspended);
// activate the thread unless suspended or already activated
if (!actions.suspended && !actions.activated) {
posix_appsched_actions_addactivate
case POSIX_APPSCHED_THREAD_NOTIFICATION:
fosa_scheduler_ops.notification_for_thread
- (fosa_scheduler_data, event.thread,(fosa_clock_id_t) event.event_info.info, &actions, ¤t_time);
+ (fosa_scheduler_data,
+ event.thread,(fosa_clock_id_t) event.event_info.info,
+ &actions,
+ ¤t_time);
break;
case POSIX_APPSCHED_ERROR:
&actions);
break;
- */
+*/
}
-// printf ("ENd LOOP\n");
}
}
pthread_attr_t attr;
struct sched_param sp;
+
+ printf ("\nMAIN thread=0x%lx\n\n", (unsigned long)pthread_self ());
+
+
if (!scheduler_ops)
return EINVAL;
fosa_scheduler_ops = *scheduler_ops;
{
int err;
- if (err = pthread_attr_setappscheduler (attr, fosa_scheduler_th))
+ err = pthread_attr_setappscheduler (attr, fosa_scheduler_th);
+ if (err)
return err;
if (appscheduled)
{
int policy, err;
- if (err = pthread_attr_getschedpolicy (attr, &policy))
+ err = pthread_attr_getschedpolicy (attr, &policy);
+ if (err)
return err;
*appscheduled = (policy == SCHED_APP);
struct sched_param sp;
int policy_old, policy_new, err;
- if (err = pthread_getschedparam (thread, &policy_old, &sp))
+ err = pthread_getschedparam (thread, &policy_old, &sp);
+ if (err)
return err;
if (appscheduled) {
- if (err = pthread_setappscheduler (thread, fosa_scheduler_th))
+ err = pthread_setappscheduler (thread, fosa_scheduler_th);
+ if (err)
return err;
policy_new = SCHED_APP;
struct sched_param sp;
int policy, err;
- if (err = pthread_getschedparam (thread, &policy, &sp))
+ err = pthread_getschedparam (thread, &policy, &sp);
+ if (err)
return err;
*appscheduled = (policy == SCHED_APP);
return posix_appsched_actions_addreject (&(sched_actions -> actions), thread);
}
+extern inline int fosa2prtk (int prio, int urg);
int fosa_adsactions_add_activate
(fosa_ads_actions_t *sched_actions,
fosa_thread_id_t thread,
fosa_ads_urgency_t urgency)
{
sched_actions -> activated = true;
+
#ifdef CONFIG_URGENCY
#error "Urgency not supported. Disable this feature in the Makefile"
+ int fosaprio, policy;
+ struct sched_param sp;
+
+ pthread_getschedparam (thread, &policy, &sp);
+ fosa_thread_get_prio (thread, &fosaprio);
+ sp.sched_priority = fosa2prtk (fosaprio, urgency);
+ printf ("policy=%d, prio=%d, urg=%d, new_prio=%d\n", policy);
+ pthread_setschedparam (thread, policy, &sp);
#endif
-// printf ("0x%x: Activate thread 0x%x\n", pthread_self (), thread);
return posix_appsched_actions_addactivate (&(sched_actions -> actions), thread);
}
(fosa_ads_actions_t *sched_actions,
fosa_thread_id_t thread)
{
-// printf ("0x%x: Suspend thread 0x%x\n", pthread_self (), thread);
sched_actions -> suspended = true;
return posix_appsched_actions_addsuspend (&(sched_actions -> actions), thread);
}
if (!pthread_equal (pthread_self (), fosa_scheduler_th))
return EPOLICY;
- printf ("\n\nHANDLED\n");
sigemptyset (&fosa_handled_signals);
for (i = 0; i < size; i++) {
if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
* Thread scheduling
**********************/
// PaRTiKle uses decreasing values for increasing priority (0 -> maxprio)
-// TODO: planificació jerà rquica
+
+inline int fosa2prtk (int prio, int urg)
+{
+ return ((prio & 0x3f) << 4) + (urg & 0xf);
+}
+
+static inline int prtk2fprio (int prio)
+{
+ return (prio >> 4) & 0x3f;
+}
+
+static inline int prtk2furg (int prio)
+{
+ return prio & 0xf;
+}
+
int fosa_get_priority_max()
{
- return sched_get_priority_min (SCHED_FIFO);
+ return prtk2fprio (sched_get_priority_min (SCHED_FIFO));
}
int fosa_get_priority_min()
{
- return sched_get_priority_max (SCHED_FIFO);
+ return prtk2fprio (sched_get_priority_max (SCHED_FIFO));
}
int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
if (prio > fosa_get_priority_max () || prio < fosa_get_priority_min ())
return EINVAL;
- sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - prio;
+ sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
return pthread_attr_setschedparam (attr, &sp);
}
pthread_attr_getschedparam (attr, &sp);
- *prio = sched_get_priority_min (SCHED_FIFO) - sp.sched_priority;
+ *prio = prtk2fprio (sched_get_priority_min (SCHED_FIFO) - sp.sched_priority);
return 0;
}
return EINVAL;
pthread_getschedparam (tid, &policy, &sp);
- sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - prio;
+ sp.sched_priority = sched_get_priority_min (SCHED_FIFO) - fosa2prtk (prio, 0);
return pthread_setschedparam (tid, policy, &sp);
}
act.sa_flags = SA_SIGINFO;
act.sa_handler = SIG_DFL;
-// printf ("Handled\n");
-// printf ("Fosa signal range [%d,%d]\n", FOSA_SIGNAL_MIN, FOSA_SIGNAL_MAX);
for (i = 0; i < size; i ++) {
-// printf ("signal num=%d\n", set [i]);
if (FOSA_SIGNAL_MIN > set [i] || set [i] > FOSA_SIGNAL_MAX)
return EINVAL;
sigaddset (&accept_set, set [i]);
}
-// printf ("Added to accept set\n");
return pthread_sigmask (SIG_BLOCK, &accept_set, NULL);
}
(fosa_signal_t signal, fosa_signal_info_t info,
fosa_thread_id_t receiver)
{
- union sigval *nfo = (union sigval *) &info;
+ union sigval nfo = (union sigval) info.sival_ptr;
- if (sigqueue (1, signal, *nfo))
+ if (sigqueue (1, signal, nfo))
return errno;
else
return 0;
return errno;
if (info)
- *info = *((fosa_signal_info_t*) (&nfo.si_value));
+ *info = (fosa_signal_info_t) nfo.si_value.sival_ptr;
if (signal_received)
*signal_received = sig;