View of xos/kernel/sched.c


XOS | Parent Directory | View | Download

/* Ordonnanceur de processus */
/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
/* L'algorithme utilise ici est le round-robin de base.
 * Tous les appels a sched doivent se faire interruptions desactivees. */
 
#include "proc_queue.h"
#include "idle.h"
#include "proc_struct.h"
 
#include <asm.h>
 
static struct proc_queue_struct proc_queue;
 
struct proc_struct *current;
 
static inline void switch_to(unsigned short tss_sel)
{
  struct __attribute__ ((packed)) {
    unsigned long off;
    unsigned short sel;
  } addr;
 
  addr.sel = tss_sel;
  task_switch(addr);
}
 
void sched_init()
{
  proc_queue_init(&proc_queue);
  current = idle;
}
 
/* Ajoute un processus dans la file des processus en cours d'execution.
 * Le processus doit etre dans l'etat PS_RUNNING. */
void sched_add(struct proc_struct *proc)
{
  proc_queue_push(&proc_queue, proc);
}
 
/* Enleve un processus de la file des processus en cours d'execution.
 * Le processus doit etre dans l'etat PS_RUNNING. */
void sched_remove(struct proc_struct *proc)
{
  proc_queue_remove(&proc_queue, proc);
}
 
void schedule()
{
  struct proc_struct *next;
 
  /* On essaie de ne pas reelire le processus courant. */
  if (!(next = proc_queue_rotate(&proc_queue)))
    next = idle;
  else if (next == current)
    next = proc_queue_rotate(&proc_queue);
 
  if (next == current)
    /* une commutation de contexte vers la tache courante est interdite */
    return;
  current = next;
  switch_to(next->tss_sel);
}