ia64/xen-unstable

view extras/mini-os/sched.c @ 13096:ad886b6bdfd6

[IA64] Update sn_sal.h to 2.6.19

Signed-off-by: Alex Williamson <alex.williamson@hp.com>
author awilliam@xenbuild2.aw
date Wed Dec 13 12:23:53 2006 -0700 (2006-12-13)
parents a3c6479c87ef
children e9e327c3e81b
line source
1 /*
2 ****************************************************************************
3 * (C) 2005 - Grzegorz Milos - Intel Research Cambridge
4 ****************************************************************************
5 *
6 * File: sched.c
7 * Author: Grzegorz Milos
8 * Changes: Robert Kaiser
9 *
10 * Date: Aug 2005
11 *
12 * Environment: Xen Minimal OS
13 * Description: simple scheduler for Mini-Os
14 *
15 * The scheduler is non-preemptive (cooperative), and schedules according
16 * to Round Robin algorithm.
17 *
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:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
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.
36 */
38 #include <os.h>
39 #include <hypervisor.h>
40 #include <time.h>
41 #include <mm.h>
42 #include <types.h>
43 #include <lib.h>
44 #include <xmalloc.h>
45 #include <list.h>
46 #include <sched.h>
47 #include <semaphore.h>
50 #ifdef SCHED_DEBUG
51 #define DEBUG(_f, _a...) \
52 printk("MINI_OS(file=sched.c, line=%d) " _f "\n", __LINE__, ## _a)
53 #else
54 #define DEBUG(_f, _a...) ((void)0)
55 #endif
57 struct thread *idle_thread = NULL;
58 LIST_HEAD(exited_threads);
60 void inline print_runqueue(void)
61 {
62 struct list_head *it;
63 struct thread *th;
64 list_for_each(it, &idle_thread->thread_list)
65 {
66 th = list_entry(it, struct thread, thread_list);
67 printk(" Thread \"%s\", runnable=%d\n", th->name, is_runnable(th));
68 }
69 printk("\n");
70 }
72 /* Find the time when the next timeout expires. If this is more than
73 10 seconds from now, return 10 seconds from now. */
74 static s_time_t blocking_time(void)
75 {
76 struct thread *thread;
77 struct list_head *iterator;
78 s_time_t min_wakeup_time;
79 unsigned long flags;
80 local_irq_save(flags);
81 /* default-block the domain for 10 seconds: */
82 min_wakeup_time = NOW() + SECONDS(10);
84 /* Thread list needs to be protected */
85 list_for_each(iterator, &idle_thread->thread_list)
86 {
87 thread = list_entry(iterator, struct thread, thread_list);
88 if(!is_runnable(thread) && thread->wakeup_time != 0LL)
89 {
90 if(thread->wakeup_time < min_wakeup_time)
91 {
92 min_wakeup_time = thread->wakeup_time;
93 }
94 }
95 }
96 local_irq_restore(flags);
97 return(min_wakeup_time);
98 }
100 /* Wake up all threads with expired timeouts. */
101 static void wake_expired(void)
102 {
103 struct thread *thread;
104 struct list_head *iterator;
105 s_time_t now = NOW();
106 unsigned long flags;
107 local_irq_save(flags);
108 /* Thread list needs to be protected */
109 list_for_each(iterator, &idle_thread->thread_list)
110 {
111 thread = list_entry(iterator, struct thread, thread_list);
112 if(!is_runnable(thread) && thread->wakeup_time != 0LL)
113 {
114 if(thread->wakeup_time <= now)
115 wake(thread);
116 }
117 }
118 local_irq_restore(flags);
119 }
121 void schedule(void)
122 {
123 struct thread *prev, *next, *thread;
124 struct list_head *iterator;
125 unsigned long flags;
126 prev = current;
127 local_irq_save(flags);
128 list_for_each(iterator, &exited_threads)
129 {
130 thread = list_entry(iterator, struct thread, thread_list);
131 if(thread != prev)
132 {
133 list_del(&thread->thread_list);
134 free_pages(thread->stack, 1);
135 xfree(thread);
136 }
137 }
138 next = idle_thread;
139 /* Thread list needs to be protected */
140 list_for_each(iterator, &idle_thread->thread_list)
141 {
142 thread = list_entry(iterator, struct thread, thread_list);
143 if(is_runnable(thread))
144 {
145 next = thread;
146 /* Put this thread on the end of the list */
147 list_del(&thread->thread_list);
148 list_add_tail(&thread->thread_list, &idle_thread->thread_list);
149 break;
150 }
151 }
152 local_irq_restore(flags);
153 /* Interrupting the switch is equivalent to having the next thread
154 inturrupted at the return instruction. And therefore at safe point. */
155 if(prev != next) switch_threads(prev, next);
156 }
159 /* Gets run when a new thread is scheduled the first time ever,
160 defined in x86_[32/64].S */
161 extern void thread_starter(void);
164 void exit_thread(void)
165 {
166 unsigned long flags;
167 struct thread *thread = current;
168 printk("Thread \"%s\" exited.\n", thread->name);
169 local_irq_save(flags);
170 /* Remove from the thread list */
171 list_del(&thread->thread_list);
172 clear_runnable(thread);
173 /* Put onto exited list */
174 list_add(&thread->thread_list, &exited_threads);
175 local_irq_restore(flags);
176 /* Schedule will free the resources */
177 schedule();
178 }
180 void block(struct thread *thread)
181 {
182 thread->wakeup_time = 0LL;
183 clear_runnable(thread);
184 }
186 void sleep(u32 millisecs)
187 {
188 struct thread *thread = get_current();
189 thread->wakeup_time = NOW() + MILLISECS(millisecs);
190 clear_runnable(thread);
191 schedule();
192 }
194 void wake(struct thread *thread)
195 {
196 thread->wakeup_time = 0LL;
197 set_runnable(thread);
198 }
200 void idle_thread_fn(void *unused)
201 {
202 s_time_t until;
203 for(;;)
204 {
205 schedule();
206 /* block until the next timeout expires, or for 10 secs, whichever comes first */
207 until = blocking_time();
208 block_domain(until);
209 wake_expired();
210 }
211 }
213 DECLARE_MUTEX(mutex);
215 void th_f1(void *data)
216 {
217 struct timeval tv1, tv2;
219 for(;;)
220 {
221 down(&mutex);
222 printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current));
223 schedule();
224 printk("Thread \"%s\" releases the semaphore\n", current->name);
225 up(&mutex);
228 gettimeofday(&tv1);
229 for(;;)
230 {
231 gettimeofday(&tv2);
232 if(tv2.tv_sec - tv1.tv_sec > 2) break;
233 }
236 schedule();
237 }
238 }
240 void th_f2(void *data)
241 {
242 for(;;)
243 {
244 printk("Thread OTHER executing, data 0x%lx\n", data);
245 schedule();
246 }
247 }
251 void init_sched(void)
252 {
253 printk("Initialising scheduler\n");
255 idle_thread = create_thread("Idle", idle_thread_fn, NULL);
256 INIT_LIST_HEAD(&idle_thread->thread_list);
257 }