kernel/alarm.c (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include "memory.h"
#include "alarm.h"
#include "core.h"
#include "sched/sched.h"
struct alarm *first = NULL;
int alarm_id_next = 1000;
int alarm_add(int ticks, PROCESS_ID pid, alarm_mode m) {
struct alarm *next = malloc(sizeof(struct alarm));
next->alarm_id = alarm_id_next++;
next->ticks_remaining = ticks;
next->pid = pid;
next->m = m;
// add to list
next->next = first;
first = next;
return next->alarm_id;
}
void alarm_tick(void) {
struct alarm **next = &first;
while (*next) {
if ((*next)->ticks_remaining-- <= 0) {
// send signal to process
switch((*next)->m) {
case ALARM_WAKEUP:
sched_unblock((*next)->pid);
break;
case ALARM_KILL:
sched_kill((*next)->pid);
break;
default:
printk("Unknown alarm mode: %i\n", (int)((*next)->m));
break;
}
// remove alarm
*next = (*next)->next;
} else {
next = &((*next)->next);
}
}
}
int alarm_cancel(int alarm_id) {
struct alarm **next = &first;
while (*next) {
if ((*next)->alarm_id == alarm_id) {
// remove alarm
*next = (*next)->next;
return 0;
} else {
next = &((*next)->next);
}
}
// alarm was not found
return -1;
}