2
****************************************************************************
3
* (C) 2005 - Grzegorz Milos - Intel Research Cambridge
4
****************************************************************************
7
* Author: Grzegorz Milos
8
* Changes: Robert Kaiser
12
* Environment: Xen Minimal OS
13
* Description: simple scheduler for Mini-Os
15
* The scheduler is non-preemptive (cooperative), and schedules according
16
* to Round Robin algorithm.
18
****************************************************************************
19
* Permission is hereby granted, free of charge, to any person obtaining a copy
20
* of this software and associated documentation files (the "Software"), to
21
* deal in the Software without restriction, including without limitation the
22
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
23
* sell copies of the Software, and to permit persons to whom the Software is
24
* furnished to do so, subject to the following conditions:
26
* The above copyright notice and this permission notice shall be included in
27
* all copies or substantial portions of the Software.
29
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
* DEALINGS IN THE SOFTWARE.
38
#include <mini-os/os.h>
39
#include <mini-os/hypervisor.h>
40
#include <mini-os/time.h>
41
#include <mini-os/mm.h>
42
#include <mini-os/types.h>
43
#include <mini-os/lib.h>
44
#include <mini-os/xmalloc.h>
45
#include <mini-os/list.h>
46
#include <mini-os/sched.h>
47
#include <mini-os/semaphore.h>
51
#define DEBUG(_f, _a...) \
52
printk("MINI_OS(file=sched.c, line=%d) " _f "\n", __LINE__, ## _a)
54
#define DEBUG(_f, _a...) ((void)0)
57
struct thread *idle_thread = NULL;
58
MINIOS_LIST_HEAD(exited_threads);
59
static int threads_started;
61
struct thread *main_thread;
63
void inline print_runqueue(void)
65
struct minios_list_head *it;
67
minios_list_for_each(it, &idle_thread->thread_list)
69
th = minios_list_entry(it, struct thread, thread_list);
70
printk(" Thread \"%s\", runnable=%d\n", th->name, is_runnable(th));
77
struct thread *prev, *next, *thread;
78
struct minios_list_head *iterator, *next_iterator;
82
local_irq_save(flags);
85
printk("Must not call schedule() from a callback\n");
89
printk("Must not call schedule() with IRQs disabled\n");
94
/* Examine all threads.
95
Find a runnable thread, but also wake up expired ones and find the
96
time when the next timeout expires, else use 10 seconds. */
98
s_time_t min_wakeup_time = now + SECONDS(10);
100
minios_list_for_each_safe(iterator, next_iterator, &idle_thread->thread_list)
102
thread = minios_list_entry(iterator, struct thread, thread_list);
103
if (!is_runnable(thread) && thread->wakeup_time != 0LL)
105
if (thread->wakeup_time <= now)
107
else if (thread->wakeup_time < min_wakeup_time)
108
min_wakeup_time = thread->wakeup_time;
110
if(is_runnable(thread))
113
/* Put this thread on the end of the list */
114
minios_list_del(&thread->thread_list);
115
minios_list_add_tail(&thread->thread_list, &idle_thread->thread_list);
121
/* block until the next timeout expires, or for 10 secs, whichever comes first */
122
block_domain(min_wakeup_time);
123
/* handle pending events if any */
124
force_evtchn_callback();
126
local_irq_restore(flags);
127
/* Interrupting the switch is equivalent to having the next thread
128
inturrupted at the return instruction. And therefore at safe point. */
129
if(prev != next) switch_threads(prev, next);
131
minios_list_for_each_safe(iterator, next_iterator, &exited_threads)
133
thread = minios_list_entry(iterator, struct thread, thread_list);
136
minios_list_del(&thread->thread_list);
137
free_pages(thread->stack, STACK_SIZE_PAGE_ORDER);
143
struct thread* create_thread(char *name, void (*function)(void *), void *data)
145
struct thread *thread;
147
/* Call architecture specific setup. */
148
thread = arch_create_thread(name, function, data);
149
/* Not runable, not exited, not sleeping */
151
thread->wakeup_time = 0LL;
153
_REENT_INIT_PTR((&thread->reent))
155
set_runnable(thread);
156
local_irq_save(flags);
157
if(idle_thread != NULL) {
158
minios_list_add_tail(&thread->thread_list, &idle_thread->thread_list);
159
} else if(function != idle_thread_fn)
161
printk("BUG: Not allowed to create thread before initialising scheduler.\n");
164
local_irq_restore(flags);
169
static struct _reent callback_reent;
170
struct _reent *__getreent(void)
172
struct _reent *_reent;
174
if (!threads_started)
175
_reent = _impure_ptr;
176
else if (in_callback)
177
_reent = &callback_reent;
179
_reent = &get_current()->reent;
182
#if defined(__x86_64__) || defined(__x86__)
185
register unsigned long sp asm ("rsp");
187
register unsigned long sp asm ("esp");
189
if ((sp & (STACK_SIZE-1)) < STACK_SIZE / 16) {
190
static int overflowing;
193
printk("stack overflow\n");
204
void exit_thread(void)
207
struct thread *thread = current;
208
printk("Thread \"%s\" exited.\n", thread->name);
209
local_irq_save(flags);
210
/* Remove from the thread list */
211
minios_list_del(&thread->thread_list);
212
clear_runnable(thread);
213
/* Put onto exited list */
214
minios_list_add(&thread->thread_list, &exited_threads);
215
local_irq_restore(flags);
216
/* Schedule will free the resources */
220
printk("schedule() returned! Trying again\n");
224
void block(struct thread *thread)
226
thread->wakeup_time = 0LL;
227
clear_runnable(thread);
230
void msleep(uint32_t millisecs)
232
struct thread *thread = get_current();
233
thread->wakeup_time = NOW() + MILLISECS(millisecs);
234
clear_runnable(thread);
238
void wake(struct thread *thread)
240
thread->wakeup_time = 0LL;
241
set_runnable(thread);
244
void idle_thread_fn(void *unused)
253
DECLARE_MUTEX(mutex);
255
void th_f1(void *data)
257
struct timeval tv1, tv2;
262
printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current));
264
printk("Thread \"%s\" releases the semaphore\n", current->name);
268
gettimeofday(&tv1, NULL);
271
gettimeofday(&tv2, NULL);
272
if(tv2.tv_sec - tv1.tv_sec > 2) break;
280
void th_f2(void *data)
284
printk("Thread OTHER executing, data 0x%lx\n", data);
291
void init_sched(void)
293
printk("Initialising scheduler\n");
296
_REENT_INIT_PTR((&callback_reent))
298
idle_thread = create_thread("Idle", idle_thread_fn, NULL);
299
MINIOS_INIT_LIST_HEAD(&idle_thread->thread_list);