ia64/xen-unstable

view extras/mini-os/sched.c @ 16838:945820bfedb6

minios: POSIX fixes
Fixes some functions which are POSIX. Also make them ifndef HAVE_LIBC.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 22 14:20:22 2008 +0000 (2008-01-22)
parents aca8d453da59
children a905c582a406
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 if (in_callback) {
129 printk("Must not call schedule() from a callback\n");
130 BUG();
131 }
132 if (flags) {
133 printk("Must not call schedule() with IRQs disabled\n");
134 BUG();
135 }
136 list_for_each(iterator, &exited_threads)
137 {
138 thread = list_entry(iterator, struct thread, thread_list);
139 if(thread != prev)
140 {
141 list_del(&thread->thread_list);
142 free_pages(thread->stack, STACK_SIZE_PAGE_ORDER);
143 xfree(thread);
144 }
145 }
146 next = idle_thread;
147 /* Thread list needs to be protected */
148 list_for_each(iterator, &idle_thread->thread_list)
149 {
150 thread = list_entry(iterator, struct thread, thread_list);
151 if(is_runnable(thread))
152 {
153 next = thread;
154 /* Put this thread on the end of the list */
155 list_del(&thread->thread_list);
156 list_add_tail(&thread->thread_list, &idle_thread->thread_list);
157 break;
158 }
159 }
160 local_irq_restore(flags);
161 /* Interrupting the switch is equivalent to having the next thread
162 inturrupted at the return instruction. And therefore at safe point. */
163 if(prev != next) switch_threads(prev, next);
164 }
166 struct thread* create_thread(char *name, void (*function)(void *), void *data)
167 {
168 struct thread *thread;
169 unsigned long flags;
170 /* Call architecture specific setup. */
171 thread = arch_create_thread(name, function, data);
172 /* Not runable, not exited, not sleeping */
173 thread->flags = 0;
174 thread->wakeup_time = 0LL;
175 set_runnable(thread);
176 local_irq_save(flags);
177 if(idle_thread != NULL) {
178 list_add_tail(&thread->thread_list, &idle_thread->thread_list);
179 } else if(function != idle_thread_fn)
180 {
181 printk("BUG: Not allowed to create thread before initialising scheduler.\n");
182 BUG();
183 }
184 local_irq_restore(flags);
185 return thread;
186 }
188 void exit_thread(void)
189 {
190 unsigned long flags;
191 struct thread *thread = current;
192 printk("Thread \"%s\" exited.\n", thread->name);
193 local_irq_save(flags);
194 /* Remove from the thread list */
195 list_del(&thread->thread_list);
196 clear_runnable(thread);
197 /* Put onto exited list */
198 list_add(&thread->thread_list, &exited_threads);
199 local_irq_restore(flags);
200 /* Schedule will free the resources */
201 while(1)
202 {
203 schedule();
204 printk("schedule() returned! Trying again\n");
205 }
206 }
208 void block(struct thread *thread)
209 {
210 thread->wakeup_time = 0LL;
211 clear_runnable(thread);
212 }
214 void msleep(u32 millisecs)
215 {
216 struct thread *thread = get_current();
217 thread->wakeup_time = NOW() + MILLISECS(millisecs);
218 clear_runnable(thread);
219 schedule();
220 }
222 void wake(struct thread *thread)
223 {
224 thread->wakeup_time = 0LL;
225 set_runnable(thread);
226 }
228 void idle_thread_fn(void *unused)
229 {
230 s_time_t until;
231 unsigned long flags;
232 struct list_head *iterator;
233 struct thread *next, *thread;
234 for(;;)
235 {
236 schedule();
237 next = NULL;
238 local_irq_save(flags);
239 list_for_each(iterator, &idle_thread->thread_list)
240 {
241 thread = list_entry(iterator, struct thread, thread_list);
242 if(is_runnable(thread))
243 {
244 next = thread;
245 break;
246 }
247 }
248 if (!next) {
249 /* block until the next timeout expires, or for 10 secs, whichever comes first */
250 until = blocking_time();
251 block_domain(until);
252 }
253 local_irq_restore(flags);
254 wake_expired();
255 }
256 }
258 DECLARE_MUTEX(mutex);
260 void th_f1(void *data)
261 {
262 struct timeval tv1, tv2;
264 for(;;)
265 {
266 down(&mutex);
267 printk("Thread \"%s\" got semaphore, runnable %d\n", current->name, is_runnable(current));
268 schedule();
269 printk("Thread \"%s\" releases the semaphore\n", current->name);
270 up(&mutex);
273 gettimeofday(&tv1, NULL);
274 for(;;)
275 {
276 gettimeofday(&tv2, NULL);
277 if(tv2.tv_sec - tv1.tv_sec > 2) break;
278 }
281 schedule();
282 }
283 }
285 void th_f2(void *data)
286 {
287 for(;;)
288 {
289 printk("Thread OTHER executing, data 0x%lx\n", data);
290 schedule();
291 }
292 }
296 void init_sched(void)
297 {
298 printk("Initialising scheduler\n");
300 idle_thread = create_thread("Idle", idle_thread_fn, NULL);
301 INIT_LIST_HEAD(&idle_thread->thread_list);
302 }