Merge branch 'master' into net-dev (with code changes listed below besides normal...
[akaros.git] / kern / src / schedule.c
1 /*
2  * Copyright (c) 2009 The Regents of the University of California
3  * Barret Rhoden <brho@cs.berkeley.edu>
4  * See LICENSE for details.
5  *
6  * Scheduling and dispatching.
7  */
8
9 #include <schedule.h>
10 #include <process.h>
11 #include <monitor.h>
12 #include <stdio.h>
13 #include <assert.h>
14 #include <atomic.h>
15 #include <sys/queue.h>
16
17 void schedule_init(void)
18 {
19         TAILQ_INIT(&proc_runnablelist);
20         return;
21 }
22
23 void schedule_proc(struct proc *p)
24 {
25         spin_lock_irqsave(&runnablelist_lock);
26         printd("Scheduling PID: %d\n", p->env_id);
27         TAILQ_INSERT_TAIL(&proc_runnablelist, p, proc_link);
28         spin_unlock_irqsave(&runnablelist_lock);
29         return;
30 }
31
32 void deschedule_proc(struct proc *p)
33 {
34         spin_lock_irqsave(&runnablelist_lock);
35         printd("Descheduling PID: %d\n", p->env_id);
36         TAILQ_REMOVE(&proc_runnablelist, p, proc_link);
37         spin_unlock_irqsave(&runnablelist_lock);
38         return;
39 }
40
41 /*
42  * FIFO - just pop the head from the list and run it.
43  * Using irqsave spinlocks for now, since this could be called from a timer
44  * interrupt handler (though ought to be in a bottom half or something).
45  */
46 void schedule(void)
47 {
48         struct proc *p;
49         
50         spin_lock_irqsave(&runnablelist_lock);
51         p = TAILQ_FIRST(&proc_runnablelist);
52         if (p) {
53                 TAILQ_REMOVE(&proc_runnablelist, p, proc_link);
54                 spin_unlock_irqsave(&runnablelist_lock);
55                 printd("PID of proc i'm running: %d\n", p->env_id);
56                 proc_run(p);
57         } else {
58                 spin_unlock_irqsave(&runnablelist_lock);
59                 printk("No processes to schedule, enjoy the Monitor!\n");
60                 while (1)
61                         monitor(NULL);
62         }
63         return;
64 }
65
66 void dump_proclist(struct proc_list *list)
67 {
68         struct proc *p;
69         TAILQ_FOREACH(p, list, proc_link)
70                 printk("PID: %d\n", p->env_id);
71         return;
72 }