2009-05-20 22:07:56 +02:00
|
|
|
#pypp 0
|
2013-05-12 09:46:11 -04:00
|
|
|
// vim: set filetype=cpp : //
|
2009-06-01 14:26:42 +02: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 22:07:56 +02:00
|
|
|
#include "kernel.hh"
|
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
static void run_thread (kThread *thread):
|
2009-06-08 13:46:13 +02:00
|
|
|
thread->schedule_next = first_scheduled
|
|
|
|
if thread->schedule_next:
|
|
|
|
thread->schedule_next->schedule_prev = thread
|
2009-07-23 12:06:32 +02:00
|
|
|
thread->schedule_prev = NULL
|
2009-06-08 13:46:13 +02:00
|
|
|
first_scheduled = thread
|
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
static void unrun_thread (kThread *thread):
|
2009-06-08 13:46:13 +02:00
|
|
|
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 10:52:44 +02:00
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
void kThread::run ():
|
2010-04-30 23:13:49 +02:00
|
|
|
if flags & Iris::Thread::RUNNING:
|
2009-05-25 00:31:35 +02:00
|
|
|
return
|
2010-04-30 23:13:49 +02:00
|
|
|
flags |= Iris::Thread::RUNNING
|
2009-07-20 00:23:45 +02:00
|
|
|
if is_waiting ():
|
2009-05-25 00:31:35 +02:00
|
|
|
return
|
2009-06-08 13:46:13 +02:00
|
|
|
run_thread (this)
|
2009-05-25 00:31:35 +02:00
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
void kThread::unrun ():
|
2010-04-30 23:13:49 +02:00
|
|
|
if !(flags & Iris::Thread::RUNNING):
|
2009-05-25 00:31:35 +02:00
|
|
|
return
|
2010-04-30 23:13:49 +02:00
|
|
|
flags &= ~Iris::Thread::RUNNING
|
2009-07-20 00:23:45 +02:00
|
|
|
if is_waiting ():
|
2009-06-08 13:46:13 +02:00
|
|
|
return
|
|
|
|
unrun_thread (this)
|
2009-05-25 00:31:35 +02:00
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
void kThread::unwait ():
|
2010-04-30 23:13:49 +02:00
|
|
|
flags &= ~Iris::Thread::WAITING
|
|
|
|
if flags & Iris::Thread::RUNNING:
|
2009-07-05 10:52:44 +02:00
|
|
|
run_thread (this)
|
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
static void alarm_tick (kReceiver *recv):
|
2009-07-20 00:23:45 +02:00
|
|
|
if !recv->alarm_count:
|
|
|
|
// Send message and stop counting.
|
2009-08-17 23:11:15 +02:00
|
|
|
kCapability::Context c
|
|
|
|
for unsigned i = 0; i < 2; ++i:
|
2009-07-05 10:52:44 +02:00
|
|
|
c.data[i] = 0
|
2009-07-20 00:23:45 +02: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
|
2009-07-25 00:54:12 +02:00
|
|
|
// Fall through to let alarm_count be ~0. This is required, because it is the indicator for no running alarm.
|
2009-07-20 00:23:45 +02:00
|
|
|
--recv->alarm_count
|
2009-07-05 10:52:44 +02:00
|
|
|
|
2009-08-17 23:11:15 +02:00
|
|
|
void kThread::wait ():
|
2010-04-30 23:13:49 +02:00
|
|
|
if flags & Iris::Thread::RUNNING:
|
2009-06-08 13:46:13 +02:00
|
|
|
unrun_thread (this)
|
2010-04-30 23:13:49 +02:00
|
|
|
flags |= Iris::Thread::WAITING
|
2009-08-17 23:11:15 +02:00
|
|
|
// Try to receive a message from a kReceiver immediately.
|
|
|
|
for kReceiver *r = receivers; r; r = r->next_owned:
|
2009-06-01 01:12:54 +02:00
|
|
|
if r->try_deliver ():
|
2009-07-05 10:52:44 +02:00
|
|
|
return
|
2009-05-25 00:31:35 +02:00
|
|
|
|
2009-07-05 10:52:44 +02:00
|
|
|
void schedule ():
|
|
|
|
if current:
|
|
|
|
current = current->schedule_next
|
|
|
|
if !current:
|
|
|
|
current = first_scheduled
|
2009-07-23 12:06:32 +02:00
|
|
|
|
2009-07-04 16:21:28 +02:00
|
|
|
void timer_interrupt ():
|
2009-08-17 23:11:15 +02:00
|
|
|
kReceiver *recv, *next
|
2009-07-20 00:23:45 +02:00
|
|
|
for recv = first_alarm; recv; recv = next:
|
2009-10-10 01:31:10 +02:00
|
|
|
next = (kReceiver *)recv->next_alarm
|
2009-07-20 00:23:45 +02:00
|
|
|
alarm_tick (recv)
|
2009-07-21 12:17:52 +02:00
|
|
|
//schedule ()
|