Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

#include "hlpdemo.h"
#include <drivers/shark_keyb26.h>

/* #define DEBUG */

/* tipologia di task e mutex */
HARD_TASK_MODEL m;

/* scala temporale del sistema in us */
#define TIMESCALE (100 * 1000)

/* fine del mondo in TIMESCALE */
#define END_OF_WORLD 280

// utility
#define CURTIME ((sys_gettime(NULL) - start) / TIMESCALE)

void anykey(int sec)
{
  /*  char c;
  cprintf("Premere il tasto 'c' per continuare\n");
  do {
    c = keyb_getch(BLOCK);
    } while(c != 'c');*/


  TIME last;
  last = sys_gettime(NULL);
  while (sys_gettime(NULL) - last < sec * 1000 * 1000);
}

/* buffer che contiene la configurazione letto in nrt mode */
extern char myfilebuf[1000];

extern int read_conf(char *, unsigned int);
extern void print_taskconf(taskconf_list_t *);

void trace_info_hlp(void);

/* tempo dell'avvio */
TIME start;

/* struttura dati contenente la configurazione */
taskconf_list_t *taskconf = NULL;

/* lista dei mutex del sistema */
mutex_list_t *mutexes = NULL;

/*--------------------------------------------------------------*/

void load(int time, char *name)
{
  int i;
  TIME last;

  for (i = 0; i < time; i++)
    {
      last = sys_gettime(NULL);
      cprintf(".");
      while (sys_gettime(NULL) - last < TIMESCALE - TIMESCALE / 100);
    }
}

TASK    Ji(void *arg)
{
  taskconf_list_t *cur = (taskconf_list_t *)arg;
  action_list_t *act;

  while (1)
    {
 
      if (CURTIME > END_OF_WORLD)
        {
          task_kill(cur->pid);
          return 0;
        }

      act = cur->actionlist;

      while (act)
        {
          switch (act->type)
            {
            case LOAD:
              cprintf("%ld -- task: %s -- NonCritic section:(exec=%.2d,Shadow=%.2d),load %d",
                          CURTIME,
                          cur->name,
                          exec,
                          exec_shadow,
                          act->time);

              load(act->time, cur->name);
              cprintf("end %ld\n", CURTIME);
              break;
             
            case LOCK: /* task lock */
              cprintf("%ld -- Task %s -- locks mutex %s\n", CURTIME, cur->name, act->mutex->name);
              mutex_lock(&act->mutex->m);
              break;
             
            case UNLOCK: /* task unlock */
              cprintf("%ld -- Task %s -- unlocks mutex %s\n", CURTIME, cur->name, act->mutex->name);
              mutex_unlock(&act->mutex->m);
              break;
            }
         
          act = act->next;
        }
      task_endcycle();
    }

  return 0;
}

void trace_info_hlp()
{
  HLP_mutex_resource_des *m;
  RLEVEL l;
  HLP_mutex_t *mutscan;
  HLP_tasklist_t *taskscan;
  int i = 0;

  if (mutexes)
    {
      l = mutexes->m.mutexlevel;
      m = (HLP_mutex_resource_des *)(resource_table[l]);
     
      for (mutscan = m->mutexlist; mutscan; mutscan = mutscan->next)
        {
          cprintf("mutex: %p owner=%d, used by\n", mutscan, mutscan->owner);
         
          for (i = 1, taskscan = mutscan->tasklist; taskscan; taskscan = taskscan->next, i++)
            {
              cprintf("task: %p, pid=%d, preempt=%ld\n",
                      taskscan,
                      taskscan->pid,
                      taskscan->preempt);
            }
        }

      cprintf("All tasks:\n");

      if (taskconf)
        for (taskscan = m->tasklist; taskscan; taskscan = taskscan->next, i++)
          {
            cprintf("task: %p, pid=%d, preempt=%ld\n",
                    taskscan,
                    taskscan->pid,
                    taskscan->preempt);
          }
    }
  else
    {
      cprintf("No mutexes\n");
    }

}

/****************************** MAIN ******************************/

int main(int argc, char **argv)
{
  int i = 0,  numtask = 0, nummutex = 0;
  taskconf_list_t *curtask = NULL;
  mutex_list_t *mutexlist = NULL, *curmut = NULL;
  PID pid = NIL;

  HLP_mutexattr_t a;
  HLP_RES_MODEL r;


  if (read_conf(myfilebuf, 1000) == -1)
    {
      sys_shutdown_message("Error reading configuration file");
      sys_end();
    }

  clear();
  print_taskconf(taskconf);
  anykey(5);

  for (curtask = taskconf; curtask != NULL; curtask = curtask->next)
    numtask++;
 
  curtask = taskconf;
 
  for (curmut = mutexes; curmut; curmut = curmut->next)
    {
      HLP_mutexattr_default(a);
      mutex_init(&(curmut->m), &a);
    }

  while (curtask) {
   
    hard_task_default_model(m);
    hard_task_def_level(m,0);
    hard_task_def_ctrl_jet(m);
    hard_task_def_periodic(m);
    hard_task_def_wcet(m, (curtask->wcet + 2) * TIMESCALE);
    hard_task_def_mit(m, curtask->period * TIMESCALE);
    hard_task_def_arg(m,(void *)curtask);
    hard_task_def_group(m, 1);
   
    /* registro i mutex per il pid */
    nummutex = 0;
    mutexlist = curtask->mutexlist;
    while(mutexlist) {
      mutexlist = mutexlist->next;
      nummutex++;
    }

    HLP_res_default_model(r, 1000 - curtask->period );

    switch (nummutex) {
    case 0:
      pid = task_create(curtask->name, Ji, &m, &r);
      break;
    case 1:
      pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
                         HLP_usemutex(&(curtask->mutexlist->m)), NULL);
      break;
     
    case 2:
      pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
                         HLP_usemutex(&(curtask->mutexlist->m)),
                         HLP_usemutex(&(curtask->mutexlist->next->m)), NULL);
      break;
    case 3:
      pid = task_createn(curtask->name, Ji, (TASK_MODEL *)&m, &r,
                         HLP_usemutex(&(curtask->mutexlist->m)),
                         HLP_usemutex(&(curtask->mutexlist->next->m)),
                         HLP_usemutex(&(curtask->mutexlist->next->next->m)), NULL);
      break;
    default:
      sys_shutdown_message("Too many mutexes! I am stupid!");
      sys_end();
    }
   
    if (pid == NIL)
      {
        sys_shutdown_message("Could not create task %s", curtask->name);
        sys_end();
        print_taskconf(curtask);
        perror("errore nella creazione del task");
        return 0;
      }
    else
      {
        cprintf("Task %s pid %d creato con successo!\n", curtask->name, pid);
      }
    curtask->pid = pid;
    curtask->i = i++;
   
    curtask = curtask->next;
  }

#ifndef USESRP
  clear();
  cprintf("informazioni dal modulo HLP:\n");
  trace_info_hlp();
  anykey(5);
#endif
  clear();
  cprintf("Attivazione dei task\n");
  anykey(2);
  start = sys_gettime(NULL);
  group_activate(1);
 
  while(keyb_getch(BLOCK) != 27); // Ascii code of ESC == 27
  anykey(20);

  sys_end();
 
  return 0;
 
}

/*--------------------------------------------------------------*/