2002-08-04 03:39:50 +04:00
|
|
|
/*
|
2005-02-11 06:10:21 +03:00
|
|
|
* Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de.
|
2004-12-14 02:02:18 +03:00
|
|
|
* Copyright 2002, Angelo Mottola, a.mottola@libero.it.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2004-10-14 18:24:42 +04:00
|
|
|
/* The thread scheduler */
|
|
|
|
|
2004-12-14 02:02:18 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
#include <OS.h>
|
2004-03-17 18:30:43 +03:00
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
#include <thread.h>
|
|
|
|
#include <timer.h>
|
|
|
|
#include <int.h>
|
|
|
|
#include <smp.h>
|
|
|
|
#include <cpu.h>
|
2004-12-14 02:02:18 +03:00
|
|
|
#include <debug.h>
|
2005-03-19 04:58:05 +03:00
|
|
|
#include <util/khash.h>
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2004-03-17 18:30:43 +03:00
|
|
|
|
|
|
|
//#define TRACE_SCHEDULER
|
|
|
|
#ifdef TRACE_SCHEDULER
|
2003-01-27 06:01:35 +03:00
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// prototypes
|
|
|
|
static int dump_run_queue(int argc, char **argv);
|
2002-08-04 03:39:50 +04:00
|
|
|
static int _rand(void);
|
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// The run queue. Holds the threads ready to run ordered by priority.
|
2004-03-17 18:30:43 +03:00
|
|
|
static struct thread_queue sRunQueue = {NULL, NULL};
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
_rand(void)
|
|
|
|
{
|
|
|
|
static int next = 0;
|
|
|
|
|
|
|
|
if (next == 0)
|
|
|
|
next = system_time();
|
|
|
|
|
|
|
|
next = next * 1103515245 + 12345;
|
2004-06-10 05:39:17 +04:00
|
|
|
return (next >> 16) & 0x7FFF;
|
2003-01-27 06:01:35 +03:00
|
|
|
}
|
2002-10-31 16:20:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-01-27 06:01:35 +03:00
|
|
|
dump_run_queue(int argc, char **argv)
|
2002-10-31 16:20:00 +03:00
|
|
|
{
|
2003-01-27 06:01:35 +03:00
|
|
|
struct thread *thread;
|
|
|
|
|
2004-03-17 18:30:43 +03:00
|
|
|
thread = sRunQueue.head;
|
2003-01-27 06:01:35 +03:00
|
|
|
if (!thread)
|
2002-10-31 16:20:00 +03:00
|
|
|
dprintf("Run queue is empty!\n");
|
|
|
|
else {
|
2003-01-27 06:01:35 +03:00
|
|
|
while (thread) {
|
2004-10-14 18:24:42 +04:00
|
|
|
dprintf("Thread id: %ld - priority: %ld\n", thread->id, thread->priority);
|
2003-01-27 06:01:35 +03:00
|
|
|
thread = thread->queue_next;
|
2002-10-31 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
2003-01-27 06:01:35 +03:00
|
|
|
|
2002-10-31 16:20:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2002-08-16 17:14:29 +04:00
|
|
|
|
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
/** Enqueues the thread into the run queue.
|
2005-02-11 06:10:21 +03:00
|
|
|
* Note: thread lock must be held when entering this function
|
2003-01-06 11:08:13 +03:00
|
|
|
*/
|
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
void
|
2003-01-27 06:01:35 +03:00
|
|
|
scheduler_enqueue_in_run_queue(struct thread *thread)
|
2002-08-16 17:14:29 +04:00
|
|
|
{
|
|
|
|
struct thread *curr, *prev;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// these shouldn't exist
|
2003-01-27 06:01:35 +03:00
|
|
|
if (thread->priority > B_MAX_PRIORITY)
|
|
|
|
thread->priority = B_MAX_PRIORITY;
|
|
|
|
if (thread->priority < B_MIN_PRIORITY)
|
|
|
|
thread->priority = B_MIN_PRIORITY;
|
|
|
|
|
2004-03-17 18:30:43 +03:00
|
|
|
for (curr = sRunQueue.head, prev = NULL; curr && (curr->priority >= thread->priority); curr = curr->queue_next) {
|
2002-08-16 17:14:29 +04:00
|
|
|
if (prev)
|
2003-01-27 06:01:35 +03:00
|
|
|
prev = prev->queue_next;
|
2002-08-16 17:14:29 +04:00
|
|
|
else
|
2004-03-17 18:30:43 +03:00
|
|
|
prev = sRunQueue.head;
|
2002-08-16 17:14:29 +04:00
|
|
|
}
|
2005-02-11 06:10:21 +03:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
thread->queue_next = curr;
|
|
|
|
if (prev)
|
|
|
|
prev->queue_next = thread;
|
|
|
|
else
|
2004-03-17 18:30:43 +03:00
|
|
|
sRunQueue.head = thread;
|
2002-08-16 17:14:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
/** Removes a thread from the run queue.
|
2005-02-11 06:10:21 +03:00
|
|
|
* Note: thread lock must be held when entering this function
|
2003-01-27 06:01:35 +03:00
|
|
|
*/
|
2002-08-04 03:39:50 +04:00
|
|
|
|
|
|
|
void
|
2003-01-27 06:01:35 +03:00
|
|
|
scheduler_remove_from_run_queue(struct thread *thread)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-01-27 06:01:35 +03:00
|
|
|
struct thread *item, *prev;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
// find thread in run queue
|
2004-03-17 18:30:43 +03:00
|
|
|
for (item = sRunQueue.head, prev = NULL; item && item != thread; item = item->queue_next) {
|
2003-01-27 06:01:35 +03:00
|
|
|
if (prev)
|
|
|
|
prev = prev->queue_next;
|
|
|
|
else
|
2004-03-17 18:30:43 +03:00
|
|
|
prev = sRunQueue.head;
|
2003-01-27 06:01:35 +03:00
|
|
|
}
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
ASSERT(item == thread);
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
if (prev)
|
|
|
|
prev->queue_next = item->queue_next;
|
|
|
|
else
|
2004-03-17 18:30:43 +03:00
|
|
|
sRunQueue.head = item->queue_next;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2003-01-27 06:01:35 +03:00
|
|
|
context_switch(struct thread *fromThread, struct thread *toThread)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-01-30 00:59:37 +03:00
|
|
|
// track kernel & user time
|
2005-02-11 06:10:21 +03:00
|
|
|
bigtime_t now = system_time();
|
|
|
|
fromThread->kernel_time += now - fromThread->last_time;
|
2003-01-27 06:01:35 +03:00
|
|
|
toThread->last_time = now;
|
|
|
|
|
|
|
|
toThread->cpu = fromThread->cpu;
|
|
|
|
fromThread->cpu = NULL;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
arch_thread_set_current_thread(toThread);
|
|
|
|
arch_thread_context_switch(fromThread, toThread);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
static int32
|
2002-08-04 03:39:50 +04:00
|
|
|
reschedule_event(timer *unused)
|
|
|
|
{
|
|
|
|
// this function is called as a result of the timer event set by the scheduler
|
|
|
|
// returning this causes a reschedule on the timer event
|
2004-06-10 05:39:17 +04:00
|
|
|
thread_get_current_thread()->cpu->info.preempted = 1;
|
2002-08-04 03:39:50 +04:00
|
|
|
return B_INVOKE_SCHEDULER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
/** Runs the scheduler.
|
2005-02-11 06:10:21 +03:00
|
|
|
* Note: expects thread spinlock to be held
|
2003-01-27 06:01:35 +03:00
|
|
|
*/
|
|
|
|
|
2002-08-04 03:39:50 +04:00
|
|
|
void
|
2003-01-27 06:01:35 +03:00
|
|
|
scheduler_reschedule(void)
|
2002-08-04 03:39:50 +04:00
|
|
|
{
|
2003-01-27 06:01:35 +03:00
|
|
|
struct thread *oldThread = thread_get_current_thread();
|
|
|
|
struct thread *nextThread, *prevThread;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2004-05-27 18:46:10 +04:00
|
|
|
TRACE(("reschedule(): cpu %d, cur_thread = 0x%lx\n", smp_get_current_cpu(), thread_get_current_thread()->id));
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
switch (oldThread->next_state) {
|
2002-08-05 00:10:06 +04:00
|
|
|
case B_THREAD_RUNNING:
|
|
|
|
case B_THREAD_READY:
|
2004-05-27 18:46:10 +04:00
|
|
|
TRACE(("enqueueing thread 0x%lx into run q. pri = %d\n", oldThread->id, oldThread->priority));
|
2003-01-27 06:01:35 +03:00
|
|
|
scheduler_enqueue_in_run_queue(oldThread);
|
2002-08-04 03:39:50 +04:00
|
|
|
break;
|
2002-08-05 00:10:06 +04:00
|
|
|
case B_THREAD_SUSPENDED:
|
2003-01-27 06:01:35 +03:00
|
|
|
TRACE(("reschedule(): suspending thread 0x%lx\n", oldThread->id));
|
2002-08-04 03:39:50 +04:00
|
|
|
break;
|
|
|
|
case THREAD_STATE_FREE_ON_RESCHED:
|
|
|
|
// This will hopefully be eliminated once the slab
|
|
|
|
// allocator is done
|
2003-01-27 06:01:35 +03:00
|
|
|
thread_enqueue(oldThread, &dead_q);
|
2002-08-04 03:39:50 +04:00
|
|
|
break;
|
|
|
|
default:
|
2004-05-27 18:46:10 +04:00
|
|
|
TRACE(("not enqueueing thread 0x%lx into run q. next_state = %d\n", oldThread->id, oldThread->next_state));
|
2003-01-27 06:01:35 +03:00
|
|
|
break;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
2003-01-27 06:01:35 +03:00
|
|
|
oldThread->state = oldThread->next_state;
|
2002-08-04 03:39:50 +04:00
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// select next thread from the run queue
|
2004-03-17 18:30:43 +03:00
|
|
|
nextThread = sRunQueue.head;
|
2003-01-27 06:01:35 +03:00
|
|
|
prevThread = NULL;
|
|
|
|
while (nextThread && (nextThread->priority > B_IDLE_PRIORITY)) {
|
2002-08-16 17:14:29 +04:00
|
|
|
// always extract real time threads
|
2003-01-27 06:01:35 +03:00
|
|
|
if (nextThread->priority >= B_FIRST_REAL_TIME_PRIORITY)
|
2002-08-16 17:14:29 +04:00
|
|
|
break;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// never skip last non-idle normal thread
|
2003-01-27 06:01:35 +03:00
|
|
|
if (nextThread->queue_next && (nextThread->queue_next->priority == B_IDLE_PRIORITY))
|
2002-08-16 17:14:29 +04:00
|
|
|
break;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// skip normal threads sometimes
|
|
|
|
if (_rand() > 0x3000)
|
|
|
|
break;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
prevThread = nextThread;
|
|
|
|
nextThread = nextThread->queue_next;
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
if (!nextThread)
|
|
|
|
panic("reschedule(): run queue is empty!\n");
|
|
|
|
|
2002-08-16 17:14:29 +04:00
|
|
|
// extract selected thread from the run queue
|
2003-01-27 06:01:35 +03:00
|
|
|
if (prevThread)
|
|
|
|
prevThread->queue_next = nextThread->queue_next;
|
2002-08-16 17:14:29 +04:00
|
|
|
else
|
2004-03-17 18:30:43 +03:00
|
|
|
sRunQueue.head = nextThread->queue_next;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
nextThread->state = B_THREAD_RUNNING;
|
|
|
|
nextThread->next_state = B_THREAD_READY;
|
|
|
|
|
|
|
|
if (nextThread != oldThread || oldThread->cpu->info.preempted) {
|
|
|
|
bigtime_t quantum = 3000; // ToDo: calculate quantum!
|
|
|
|
timer *quantum_timer= &oldThread->cpu->info.quantum_timer;
|
|
|
|
|
|
|
|
if (!oldThread->cpu->info.preempted)
|
|
|
|
_local_timer_cancel_event(oldThread->cpu->info.cpu_num, quantum_timer);
|
|
|
|
|
|
|
|
oldThread->cpu->info.preempted = 0;
|
2002-08-04 03:39:50 +04:00
|
|
|
add_timer(quantum_timer, &reschedule_event, quantum, B_ONE_SHOT_RELATIVE_TIMER);
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
if (nextThread != oldThread)
|
|
|
|
context_switch(oldThread, nextThread);
|
2002-08-04 03:39:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-27 06:01:35 +03:00
|
|
|
|
|
|
|
/** This starts the scheduler. Must be run under the context of
|
|
|
|
* the initial idle thread.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
start_scheduler(void)
|
|
|
|
{
|
2005-02-11 06:10:21 +03:00
|
|
|
cpu_status state;
|
2003-01-27 06:01:35 +03:00
|
|
|
|
2005-02-11 06:10:21 +03:00
|
|
|
// ToDo: may not be the best place for this
|
2003-01-27 06:01:35 +03:00
|
|
|
// invalidate all of the other processors' TLB caches
|
|
|
|
state = disable_interrupts();
|
|
|
|
arch_cpu_global_TLB_invalidate();
|
|
|
|
smp_send_broadcast_ici(SMP_MSG_GLOBAL_INVL_PAGE, 0, 0, 0, NULL, SMP_MSG_FLAG_SYNC);
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
// start the other processors
|
|
|
|
smp_send_broadcast_ici(SMP_MSG_RESCHEDULE, 0, 0, 0, NULL, SMP_MSG_FLAG_ASYNC);
|
|
|
|
|
|
|
|
state = disable_interrupts();
|
|
|
|
GRAB_THREAD_LOCK();
|
|
|
|
|
|
|
|
scheduler_reschedule();
|
|
|
|
|
|
|
|
RELEASE_THREAD_LOCK();
|
|
|
|
restore_interrupts(state);
|
|
|
|
|
|
|
|
add_debugger_command("run_queue", &dump_run_queue, "list threads in run queue");
|
|
|
|
}
|
|
|
|
|
|
|
|
|