2009-05-20 23:07:56 +03:00
|
|
|
#pypp 0
|
2009-06-01 15:26:42 +03:00
|
|
|
// Iris: micro-kernel for a capability-based operating system.
|
|
|
|
// schedule.ccp: Thread scheduling.
|
|
|
|
// Copyright 2009 Bas Wijnen <wijnen@debian.org>
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2009-05-20 23:07:56 +03:00
|
|
|
#include "kernel.hh"
|
|
|
|
|
2009-06-08 14:46:13 +03:00
|
|
|
static void run_thread (Thread *thread):
|
|
|
|
thread->schedule_next = first_scheduled
|
|
|
|
if thread->schedule_next:
|
|
|
|
thread->schedule_next->schedule_prev = thread
|
|
|
|
first_scheduled = thread
|
|
|
|
|
|
|
|
static void unrun_thread (Thread *thread):
|
|
|
|
if current == thread:
|
|
|
|
current = thread->schedule_next
|
|
|
|
if thread->schedule_prev:
|
|
|
|
thread->schedule_prev->schedule_next = thread->schedule_next
|
|
|
|
else:
|
|
|
|
first_scheduled = thread->schedule_next
|
|
|
|
if thread->schedule_next:
|
|
|
|
thread->schedule_next->schedule_prev = thread->schedule_prev
|
2009-07-05 11:52:44 +03:00
|
|
|
|
2009-05-25 01:31:35 +03:00
|
|
|
void Thread::run ():
|
|
|
|
if flags & THREAD_FLAG_RUNNING:
|
|
|
|
return
|
|
|
|
flags |= THREAD_FLAG_RUNNING
|
2009-07-20 01:23:45 +03:00
|
|
|
if is_waiting ():
|
2009-05-25 01:31:35 +03:00
|
|
|
return
|
2009-06-08 14:46:13 +03:00
|
|
|
run_thread (this)
|
2009-05-25 01:31:35 +03:00
|
|
|
|
|
|
|
void Thread::unrun ():
|
|
|
|
if !(flags & THREAD_FLAG_RUNNING):
|
|
|
|
return
|
|
|
|
flags &= ~THREAD_FLAG_RUNNING
|
2009-07-20 01:23:45 +03:00
|
|
|
if is_waiting ():
|
2009-06-08 14:46:13 +03:00
|
|
|
return
|
|
|
|
unrun_thread (this)
|
2009-05-25 01:31:35 +03:00
|
|
|
|
2009-07-05 11:52:44 +03:00
|
|
|
void Thread::unwait ():
|
|
|
|
flags &= ~THREAD_FLAG_WAITING
|
|
|
|
if flags & THREAD_FLAG_RUNNING:
|
|
|
|
run_thread (this)
|
|
|
|
|
2009-07-20 01:23:45 +03:00
|
|
|
static void alarm_tick (Receiver *recv):
|
|
|
|
if !recv->alarm_count:
|
|
|
|
// Send message and stop counting.
|
2009-07-05 11:52:44 +03:00
|
|
|
Capability::Context c
|
|
|
|
for unsigned i = 0; i < 4; ++i:
|
|
|
|
c.data[i] = 0
|
|
|
|
c.cap[i] = NULL
|
|
|
|
c.copy[i] = false
|
2009-07-20 01:23:45 +03:00
|
|
|
recv->send_message (~0, &c)
|
|
|
|
if recv->prev_alarm:
|
|
|
|
recv->prev_alarm->next_alarm = recv->next_alarm
|
|
|
|
else:
|
|
|
|
first_alarm = recv->next_alarm
|
|
|
|
if recv->next_alarm:
|
|
|
|
recv->next_alarm->prev_alarm = recv->prev_alarm
|
|
|
|
// Fall through to let alarm be set to ~0, so the next wait will be infinitely long again.
|
|
|
|
--recv->alarm_count
|
2009-07-05 11:52:44 +03:00
|
|
|
|
2009-05-25 01:31:35 +03:00
|
|
|
void Thread::wait ():
|
|
|
|
if flags & THREAD_FLAG_RUNNING:
|
2009-06-08 14:46:13 +03:00
|
|
|
unrun_thread (this)
|
2009-05-25 01:31:35 +03:00
|
|
|
flags |= THREAD_FLAG_WAITING
|
2009-06-01 02:12:54 +03:00
|
|
|
// Try to receive a message from a Receiver immediately.
|
|
|
|
for Receiver *r = receivers; r; r = r->next_owned:
|
|
|
|
if r->try_deliver ():
|
2009-07-05 11:52:44 +03:00
|
|
|
return
|
2009-05-25 01:31:35 +03:00
|
|
|
|
2009-07-05 11:52:44 +03:00
|
|
|
void schedule ():
|
|
|
|
Thread *old = current
|
|
|
|
if current:
|
|
|
|
current = current->schedule_next
|
|
|
|
if !current:
|
|
|
|
current = first_scheduled
|
|
|
|
if !current:
|
|
|
|
current = &idle
|
2009-05-25 01:31:35 +03:00
|
|
|
|
2009-07-04 17:21:28 +03:00
|
|
|
void timer_interrupt ():
|
2009-07-20 01:23:45 +03:00
|
|
|
Receiver *recv, *next
|
|
|
|
for recv = first_alarm; recv; recv = next:
|
|
|
|
next = recv->next
|
|
|
|
alarm_tick (recv)
|
2009-07-21 13:17:52 +03:00
|
|
|
//schedule ()
|