Root/schedule.ccp

1#pypp 0
2// Iris: micro-kernel for a capability-based operating system.
3// schedule.ccp: Thread scheduling.
4// Copyright 2009 Bas Wijnen <wijnen@debian.org>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19#include "kernel.hh"
20
21static void run_thread (kThread *thread):
22    thread->schedule_next = first_scheduled
23    if thread->schedule_next:
24        thread->schedule_next->schedule_prev = thread
25    thread->schedule_prev = NULL
26    first_scheduled = thread
27
28static void unrun_thread (kThread *thread):
29    if current == thread:
30        current = thread->schedule_next
31    if thread->schedule_prev:
32        thread->schedule_prev->schedule_next = thread->schedule_next
33    else:
34        first_scheduled = thread->schedule_next
35    if thread->schedule_next:
36        thread->schedule_next->schedule_prev = thread->schedule_prev
37
38void kThread::run ():
39    if flags & Iris::Thread::RUNNING:
40        return
41    flags |= Iris::Thread::RUNNING
42    if is_waiting ():
43        return
44    run_thread (this)
45
46void kThread::unrun ():
47    if !(flags & Iris::Thread::RUNNING):
48        return
49    flags &= ~Iris::Thread::RUNNING
50    if is_waiting ():
51        return
52    unrun_thread (this)
53
54void kThread::unwait ():
55    flags &= ~Iris::Thread::WAITING
56    if flags & Iris::Thread::RUNNING:
57        run_thread (this)
58
59static void alarm_tick (kReceiver *recv):
60    if !recv->alarm_count:
61        // Send message and stop counting.
62        kCapability::Context c
63        for unsigned i = 0; i < 2; ++i:
64            c.data[i] = 0
65        recv->send_message (~0, &c)
66        if recv->prev_alarm:
67            recv->prev_alarm->next_alarm = recv->next_alarm
68        else:
69            first_alarm = recv->next_alarm
70        if recv->next_alarm:
71            recv->next_alarm->prev_alarm = recv->prev_alarm
72        // Fall through to let alarm_count be ~0. This is required, because it is the indicator for no running alarm.
73    --recv->alarm_count
74
75void kThread::wait ():
76    if flags & Iris::Thread::RUNNING:
77        unrun_thread (this)
78    flags |= Iris::Thread::WAITING
79    // Try to receive a message from a kReceiver immediately.
80    for kReceiver *r = receivers; r; r = r->next_owned:
81        if r->try_deliver ():
82            return
83
84void schedule ():
85    if current:
86        current = current->schedule_next
87    if !current:
88        current = first_scheduled
89
90void timer_interrupt ():
91    kReceiver *recv, *next
92    for recv = first_alarm; recv; recv = next:
93        next = (kReceiver *)recv->next_alarm
94        alarm_tick (recv)
95    //schedule ()
96

Archive Download this file

Branches:
master



interactive