View of xos/kernel/sleep.c


XOS | Parent Directory | View | Download

/* Sommeil des 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/>. */
 
#include "sched.h"
#include "proc_struct.h"
 
#include <alarm.h>
#include <asm.h>
#include <enums.h>
#include <stddef.h>
 
static void wake_up(struct alarm_struct *alarm)
{
  int intr;
  struct proc_struct *proc;
 
  disable_intr(intr);
  proc = (void *)alarm - offsetof(struct proc_struct, alarm);
  proc->state = PS_RUNNING;
  proc->unslept = 0;
  sched_add(proc);
  restore_intr(intr);
}
 
void sleep_init(struct proc_struct *proc)
{
  alarm_init(&proc->alarm, wake_up);
}
 
unsigned long sleep(unsigned long milliseconds)
{
  int retval;
  int intr;
 
  disable_intr(intr);
  sched_remove(current);
  current->state = PS_SLEEPING;
  alarm_set(&current->alarm, milliseconds);
  schedule();
  retval = current->unslept;
  restore_intr(intr);
  return retval;
}
 
/* Interrompt le sommeil du processus, qui doit etre dans l'etat
 * PS_SLEEPING. */
void sleep_interrupt(struct proc_struct *proc)
{
  int intr;
  unsigned long unslept;
 
  disable_intr(intr);
  unslept = alarm_unset(&proc->alarm);
  proc->state = PS_RUNNING;
  proc->unslept = unslept;
  sched_add(proc);
  restore_intr(intr);
}