Root/kernel/schedule.ccp

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

Archive Download this file

Branches:
master



interactive