source: mainline/generic/src/proc/thread.c@ f76fed4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f76fed4 was f76fed4, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Added lazy fpu context allocation.

  • threads that don't use fpu, don't get allocated fpu context
  • fpu context alignment on AMD64 nicely disappeared
  • Property mode set to 100644
File size: 9.5 KB
RevLine 
[f761f1eb]1/*
2 * Copyright (C) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <proc/scheduler.h>
30#include <proc/thread.h>
31#include <proc/task.h>
32#include <mm/frame.h>
33#include <mm/page.h>
34#include <arch/asm.h>
35#include <arch.h>
36#include <synch/synch.h>
37#include <synch/spinlock.h>
38#include <synch/waitq.h>
39#include <synch/rwlock.h>
40#include <cpu.h>
41#include <func.h>
42#include <context.h>
[5c9a08b]43#include <adt/list.h>
[f761f1eb]44#include <typedefs.h>
45#include <time/clock.h>
[5c9a08b]46#include <adt/list.h>
[4ffa9e0]47#include <config.h>
48#include <arch/interrupt.h>
[26a8604f]49#include <smp/ipi.h>
[f2ffad4]50#include <arch/faddr.h>
[e3f41b6]51#include <arch/atomic.h>
[9c0a9b3]52#include <memstr.h>
[55ab0f1]53#include <print.h>
[266294a9]54#include <mm/slab.h>
55#include <debug.h>
[f761f1eb]56
[70527f1]57char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
[f761f1eb]58
[dc747e3]59SPINLOCK_INITIALIZE(threads_lock); /**< Lock protecting threads_head list. For locking rules, see declaration thereof. */
[7dd2561]60LIST_INITIALIZE(threads_head); /**< List of all threads. */
[f761f1eb]61
[dc747e3]62SPINLOCK_INITIALIZE(tidlock);
[f761f1eb]63__u32 last_tid = 0;
64
[266294a9]65static slab_cache_t *thread_slab;
[f76fed4]66#ifdef ARCH_HAS_FPU
67slab_cache_t *fpu_context_slab;
68#endif
[266294a9]69
[70527f1]70
71/** Thread wrapper
72 *
73 * This wrapper is provided to ensure that every thread
[f761f1eb]74 * makes a call to thread_exit() when its implementing
75 * function returns.
76 *
[22f7769]77 * interrupts_disable() is assumed.
[70527f1]78 *
[f761f1eb]79 */
[e16e036a]80static void cushion(void)
[f761f1eb]81{
[43114c5]82 void (*f)(void *) = THREAD->thread_code;
83 void *arg = THREAD->thread_arg;
[f761f1eb]84
[3e1607f]85 /* this is where each thread wakes up after its creation */
[cb4b61d]86 before_thread_runs();
87
[43114c5]88 spinlock_unlock(&THREAD->lock);
[22f7769]89 interrupts_enable();
[f761f1eb]90
91 f(arg);
92 thread_exit();
93 /* not reached */
94}
95
[266294a9]96/** Initialization and allocation for thread_t structure */
97static int thr_constructor(void *obj, int kmflags)
98{
99 thread_t *t = (thread_t *)obj;
[085d973]100 pfn_t pfn;
[2a46e10]101 int status;
[266294a9]102
103 spinlock_initialize(&t->lock, "thread_t_lock");
104 link_initialize(&t->rq_link);
105 link_initialize(&t->wq_link);
106 link_initialize(&t->th_link);
107 link_initialize(&t->threads_link);
108
[f76fed4]109#ifdef ARCH_HAS_FPU
110# ifdef CONFIG_FPU_LAZY
111 t->saved_fpu_context = NULL;
112# else
113 t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
114 if (!t->saved_fpu_context)
115 return -1;
116# endif
117#endif
118
[2a46e10]119 pfn = frame_alloc_rc(ONE_FRAME, FRAME_KA | kmflags,&status);
[f76fed4]120 if (status) {
121#ifdef ARCH_HAS_FPU
122 if (t->saved_fpu_context)
123 slab_free(fpu_context_slab,t->saved_fpu_context);
124#endif
[266294a9]125 return -1;
[f76fed4]126 }
[2a46e10]127 t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn));
[266294a9]128
129 return 0;
130}
131
132/** Destruction of thread_t object */
133static int thr_destructor(void *obj)
134{
135 thread_t *t = (thread_t *)obj;
136
[085d973]137 frame_free(ADDR2PFN(KA2PA(t->kstack)));
[f76fed4]138#ifdef ARCH_HAS_FPU
139 if (t->saved_fpu_context)
140 slab_free(fpu_context_slab,t->saved_fpu_context);
141#endif
[266294a9]142 return 1; /* One page freed */
143}
[70527f1]144
145/** Initialize threads
146 *
147 * Initialize kernel threads support.
148 *
149 */
[f761f1eb]150void thread_init(void)
151{
[43114c5]152 THREAD = NULL;
[80d2bdb]153 atomic_set(&nrdy,0);
[266294a9]154 thread_slab = slab_cache_create("thread_slab",
155 sizeof(thread_t),0,
156 thr_constructor, thr_destructor, 0);
[f76fed4]157#ifdef ARCH_HAS_FPU
158 fpu_context_slab = slab_cache_create("fpu_slab",
159 sizeof(fpu_context_t),
160 FPU_CONTEXT_ALIGN,
161 NULL, NULL, 0);
162#endif
[f761f1eb]163}
164
[70527f1]165
166/** Make thread ready
167 *
168 * Switch thread t to the ready state.
169 *
170 * @param t Thread to make ready.
171 *
172 */
[f761f1eb]173void thread_ready(thread_t *t)
174{
175 cpu_t *cpu;
176 runq_t *r;
[22f7769]177 ipl_t ipl;
[80d2bdb]178 int i, avg;
[f761f1eb]179
[22f7769]180 ipl = interrupts_disable();
[f761f1eb]181
182 spinlock_lock(&t->lock);
183
[22f7769]184 i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
[f761f1eb]185
[8262010]186 cpu = CPU;
[f761f1eb]187 if (t->flags & X_WIRED) {
188 cpu = t->cpu;
189 }
190 spinlock_unlock(&t->lock);
191
[70527f1]192 /*
[f761f1eb]193 * Append t to respective ready queue on respective processor.
194 */
195 r = &cpu->rq[i];
196 spinlock_lock(&r->lock);
197 list_append(&t->rq_link, &r->rq_head);
198 r->n++;
199 spinlock_unlock(&r->lock);
200
[59e07c91]201 atomic_inc(&nrdy);
[80d2bdb]202 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]203 atomic_inc(&cpu->nrdy);
[76cec1e]204
[22f7769]205 interrupts_restore(ipl);
[f761f1eb]206}
207
[70527f1]208
[266294a9]209/** Destroy thread memory structure
210 *
211 * Detach thread from all queues, cpus etc. and destroy it.
212 *
213 * Assume thread->lock is held!!
214 */
215void thread_destroy(thread_t *t)
216{
217 ASSERT(t->state == Exiting);
218 ASSERT(t->task);
219 ASSERT(t->cpu);
220
221 spinlock_lock(&t->cpu->lock);
222 if(t->cpu->fpu_owner==t)
223 t->cpu->fpu_owner=NULL;
224 spinlock_unlock(&t->cpu->lock);
225
226 /*
227 * Detach from the containing task.
228 */
229 spinlock_lock(&t->task->lock);
230 list_remove(&t->th_link);
231 spinlock_unlock(&t->task->lock);
232
233 spinlock_unlock(&t->lock);
234
235 spinlock_lock(&threads_lock);
236 list_remove(&t->threads_link);
237 spinlock_unlock(&threads_lock);
238
239 slab_free(thread_slab, t);
240}
241
242
[70527f1]243/** Create new thread
244 *
245 * Create a new thread.
246 *
247 * @param func Thread's implementing function.
248 * @param arg Thread's implementing function argument.
249 * @param task Task to which the thread belongs.
250 * @param flags Thread flags.
251 *
252 * @return New thread's structure on success, NULL on failure.
253 *
254 */
[f761f1eb]255thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags)
256{
257 thread_t *t;
[bb68433]258 ipl_t ipl;
259
[266294a9]260 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]261 if (!t)
262 return NULL;
[f761f1eb]263
[bb68433]264 /* Not needed, but good for debugging */
265 memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0);
266
267 ipl = interrupts_disable();
268 spinlock_lock(&tidlock);
269 t->tid = ++last_tid;
270 spinlock_unlock(&tidlock);
271 interrupts_restore(ipl);
272
273 context_save(&t->saved_context);
274 context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
275
276 the_initialize((the_t *) t->kstack);
277
278 ipl = interrupts_disable();
279 t->saved_context.ipl = interrupts_read();
280 interrupts_restore(ipl);
281
282 t->thread_code = func;
283 t->thread_arg = arg;
284 t->ticks = -1;
285 t->priority = -1; /* start in rq[0] */
286 t->cpu = NULL;
287 t->flags = 0;
288 t->state = Entering;
289 t->call_me = NULL;
290 t->call_me_with = NULL;
291
292 timeout_initialize(&t->sleep_timeout);
293 t->sleep_queue = NULL;
294 t->timeout_pending = 0;
295
296 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]297
[bb68433]298 t->task = task;
299
[6f8a426]300 t->fpu_context_exists = 0;
301 t->fpu_context_engaged = 0;
[bb68433]302
303 /*
304 * Register this thread in the system-wide list.
305 */
306 ipl = interrupts_disable();
307 spinlock_lock(&threads_lock);
308 list_append(&t->threads_link, &threads_head);
309 spinlock_unlock(&threads_lock);
310
311 /*
312 * Attach to the containing task.
313 */
314 spinlock_lock(&task->lock);
315 list_append(&t->th_link, &task->th_head);
316 spinlock_unlock(&task->lock);
317
318 interrupts_restore(ipl);
[6f8a426]319
[f761f1eb]320 return t;
321}
322
[70527f1]323
324/** Make thread exiting
325 *
326 * End current thread execution and switch it to the exiting
327 * state. All pending timeouts are executed.
328 *
329 */
[f761f1eb]330void thread_exit(void)
331{
[22f7769]332 ipl_t ipl;
[f761f1eb]333
334restart:
[22f7769]335 ipl = interrupts_disable();
[43114c5]336 spinlock_lock(&THREAD->lock);
337 if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
338 spinlock_unlock(&THREAD->lock);
[22f7769]339 interrupts_restore(ipl);
[f761f1eb]340 goto restart;
341 }
[43114c5]342 THREAD->state = Exiting;
343 spinlock_unlock(&THREAD->lock);
[f761f1eb]344 scheduler();
345}
346
[70527f1]347
348/** Thread sleep
349 *
350 * Suspend execution of the current thread.
351 *
352 * @param sec Number of seconds to sleep.
353 *
354 */
[f761f1eb]355void thread_sleep(__u32 sec)
356{
[76cec1e]357 thread_usleep(sec*1000000);
[f761f1eb]358}
[70527f1]359
360
361/** Thread usleep
362 *
363 * Suspend execution of the current thread.
364 *
365 * @param usec Number of microseconds to sleep.
366 *
367 */
[f761f1eb]368void thread_usleep(__u32 usec)
369{
370 waitq_t wq;
371
372 waitq_initialize(&wq);
373
374 (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING);
375}
376
[70527f1]377
378/** Register thread out-of-context invocation
379 *
380 * Register a function and its argument to be executed
381 * on next context switch to the current thread.
382 *
383 * @param call_me Out-of-context function.
384 * @param call_me_with Out-of-context function argument.
385 *
386 */
[f761f1eb]387void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
388{
[22f7769]389 ipl_t ipl;
[f761f1eb]390
[22f7769]391 ipl = interrupts_disable();
[43114c5]392 spinlock_lock(&THREAD->lock);
393 THREAD->call_me = call_me;
394 THREAD->call_me_with = call_me_with;
395 spinlock_unlock(&THREAD->lock);
[22f7769]396 interrupts_restore(ipl);
[f761f1eb]397}
[55ab0f1]398
399/** Print list of threads debug info */
400void thread_print_list(void)
401{
402 link_t *cur;
403 thread_t *t;
404 ipl_t ipl;
405
406 /* Messing with thread structures, avoid deadlock */
407 ipl = interrupts_disable();
408 spinlock_lock(&threads_lock);
409
410 for (cur=threads_head.next; cur!=&threads_head; cur=cur->next) {
411 t = list_get_instance(cur, thread_t, threads_link);
412 printf("Thr: %d(%s) ", t->tid, thread_states[t->state]);
413 if (t->cpu)
414 printf("cpu%d ", t->cpu->id);
415
416 printf("\n");
417 }
418
419 spinlock_unlock(&threads_lock);
420 interrupts_enable();
421}
Note: See TracBrowser for help on using the repository browser.