source: mainline/generic/src/proc/thread.c@ 7ee0e2f

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

Changed frame_alloc call, cleaned a lot of stuff, fixed some not-perfectly-correct error handling.

  • Property mode set to 100644
File size: 13.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
[cc73a8a1]29/** @addtogroup genericproc
[b45c443]30 * @{
31 */
32
[9179d0a]33/**
[b45c443]34 * @file
[9179d0a]35 * @brief Thread management functions.
36 */
37
[f761f1eb]38#include <proc/scheduler.h>
39#include <proc/thread.h>
40#include <proc/task.h>
[0f250f9]41#include <proc/uarg.h>
[f761f1eb]42#include <mm/frame.h>
43#include <mm/page.h>
44#include <arch/asm.h>
45#include <arch.h>
46#include <synch/synch.h>
47#include <synch/spinlock.h>
48#include <synch/waitq.h>
49#include <synch/rwlock.h>
50#include <cpu.h>
51#include <func.h>
52#include <context.h>
[016acbe]53#include <adt/btree.h>
[5c9a08b]54#include <adt/list.h>
[f761f1eb]55#include <typedefs.h>
56#include <time/clock.h>
[4ffa9e0]57#include <config.h>
58#include <arch/interrupt.h>
[26a8604f]59#include <smp/ipi.h>
[f2ffad4]60#include <arch/faddr.h>
[23684b7]61#include <atomic.h>
[9c0a9b3]62#include <memstr.h>
[55ab0f1]63#include <print.h>
[266294a9]64#include <mm/slab.h>
65#include <debug.h>
[9f52563]66#include <main/uinit.h>
[e3c762cd]67#include <syscall/copy.h>
68#include <errno.h>
[f761f1eb]69
[fe19611]70
71/** Thread states */
72char *thread_states[] = {
73 "Invalid",
74 "Running",
75 "Sleeping",
76 "Ready",
77 "Entering",
78 "Exiting",
79 "Undead"
80};
[f761f1eb]81
[88169d9]82/** Lock protecting the threads_btree B+tree. For locking rules, see declaration thereof. */
[016acbe]83SPINLOCK_INITIALIZE(threads_lock);
[88169d9]84
85/** B+tree of all threads.
86 *
87 * When a thread is found in the threads_btree B+tree, it is guaranteed to exist as long
88 * as the threads_lock is held.
89 */
90btree_t threads_btree;
[f761f1eb]91
[dc747e3]92SPINLOCK_INITIALIZE(tidlock);
[f761f1eb]93__u32 last_tid = 0;
94
[266294a9]95static slab_cache_t *thread_slab;
[f76fed4]96#ifdef ARCH_HAS_FPU
97slab_cache_t *fpu_context_slab;
98#endif
[266294a9]99
[70527f1]100/** Thread wrapper
101 *
102 * This wrapper is provided to ensure that every thread
[f761f1eb]103 * makes a call to thread_exit() when its implementing
104 * function returns.
105 *
[22f7769]106 * interrupts_disable() is assumed.
[70527f1]107 *
[f761f1eb]108 */
[e16e036a]109static void cushion(void)
[f761f1eb]110{
[43114c5]111 void (*f)(void *) = THREAD->thread_code;
112 void *arg = THREAD->thread_arg;
[f761f1eb]113
[3e1607f]114 /* this is where each thread wakes up after its creation */
[43114c5]115 spinlock_unlock(&THREAD->lock);
[22f7769]116 interrupts_enable();
[f761f1eb]117
118 f(arg);
119 thread_exit();
120 /* not reached */
121}
122
[266294a9]123/** Initialization and allocation for thread_t structure */
124static int thr_constructor(void *obj, int kmflags)
125{
126 thread_t *t = (thread_t *)obj;
127
128 spinlock_initialize(&t->lock, "thread_t_lock");
129 link_initialize(&t->rq_link);
130 link_initialize(&t->wq_link);
131 link_initialize(&t->th_link);
132
[f76fed4]133#ifdef ARCH_HAS_FPU
134# ifdef CONFIG_FPU_LAZY
135 t->saved_fpu_context = NULL;
136# else
137 t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
138 if (!t->saved_fpu_context)
139 return -1;
140# endif
141#endif
142
[e45f81a]143 t->kstack = frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
144 if (! t->kstack) {
[f76fed4]145#ifdef ARCH_HAS_FPU
146 if (t->saved_fpu_context)
147 slab_free(fpu_context_slab,t->saved_fpu_context);
148#endif
[266294a9]149 return -1;
[f76fed4]150 }
[266294a9]151
152 return 0;
153}
154
155/** Destruction of thread_t object */
156static int thr_destructor(void *obj)
157{
158 thread_t *t = (thread_t *)obj;
159
[2e9eae2]160 frame_free(KA2PA(t->kstack));
[f76fed4]161#ifdef ARCH_HAS_FPU
162 if (t->saved_fpu_context)
163 slab_free(fpu_context_slab,t->saved_fpu_context);
164#endif
[266294a9]165 return 1; /* One page freed */
166}
[70527f1]167
168/** Initialize threads
169 *
170 * Initialize kernel threads support.
171 *
172 */
[f761f1eb]173void thread_init(void)
174{
[43114c5]175 THREAD = NULL;
[80d2bdb]176 atomic_set(&nrdy,0);
[266294a9]177 thread_slab = slab_cache_create("thread_slab",
178 sizeof(thread_t),0,
179 thr_constructor, thr_destructor, 0);
[f76fed4]180#ifdef ARCH_HAS_FPU
181 fpu_context_slab = slab_cache_create("fpu_slab",
182 sizeof(fpu_context_t),
183 FPU_CONTEXT_ALIGN,
184 NULL, NULL, 0);
185#endif
[f761f1eb]186
[016acbe]187 btree_create(&threads_btree);
188}
[70527f1]189
190/** Make thread ready
191 *
192 * Switch thread t to the ready state.
193 *
194 * @param t Thread to make ready.
195 *
196 */
[f761f1eb]197void thread_ready(thread_t *t)
198{
199 cpu_t *cpu;
200 runq_t *r;
[22f7769]201 ipl_t ipl;
[80d2bdb]202 int i, avg;
[f761f1eb]203
[22f7769]204 ipl = interrupts_disable();
[f761f1eb]205
206 spinlock_lock(&t->lock);
207
[fbcfd458]208 ASSERT(! (t->state == Ready));
209
[22f7769]210 i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
[f761f1eb]211
[8262010]212 cpu = CPU;
[f761f1eb]213 if (t->flags & X_WIRED) {
214 cpu = t->cpu;
215 }
[81c4c6da]216 t->state = Ready;
[f761f1eb]217 spinlock_unlock(&t->lock);
218
[70527f1]219 /*
[f761f1eb]220 * Append t to respective ready queue on respective processor.
221 */
222 r = &cpu->rq[i];
223 spinlock_lock(&r->lock);
224 list_append(&t->rq_link, &r->rq_head);
225 r->n++;
226 spinlock_unlock(&r->lock);
227
[59e07c91]228 atomic_inc(&nrdy);
[80d2bdb]229 avg = atomic_get(&nrdy) / config.cpu_active;
[248fc1a]230 atomic_inc(&cpu->nrdy);
[76cec1e]231
[22f7769]232 interrupts_restore(ipl);
[f761f1eb]233}
234
[266294a9]235/** Destroy thread memory structure
236 *
237 * Detach thread from all queues, cpus etc. and destroy it.
238 *
239 * Assume thread->lock is held!!
240 */
241void thread_destroy(thread_t *t)
242{
[7509ddc]243 bool destroy_task = false;
244
[c778c1a]245 ASSERT(t->state == Exiting || t->state == Undead);
[266294a9]246 ASSERT(t->task);
247 ASSERT(t->cpu);
248
249 spinlock_lock(&t->cpu->lock);
250 if(t->cpu->fpu_owner==t)
251 t->cpu->fpu_owner=NULL;
252 spinlock_unlock(&t->cpu->lock);
253
[7509ddc]254 spinlock_unlock(&t->lock);
255
256 spinlock_lock(&threads_lock);
257 btree_remove(&threads_btree, (btree_key_t) ((__address ) t), NULL);
258 spinlock_unlock(&threads_lock);
259
[266294a9]260 /*
261 * Detach from the containing task.
262 */
263 spinlock_lock(&t->task->lock);
264 list_remove(&t->th_link);
[7509ddc]265 if (--t->task->refcount == 0) {
266 t->task->accept_new_threads = false;
267 destroy_task = true;
268 }
269 spinlock_unlock(&t->task->lock);
[266294a9]270
[7509ddc]271 if (destroy_task)
272 task_destroy(t->task);
[266294a9]273
274 slab_free(thread_slab, t);
275}
276
[70527f1]277/** Create new thread
278 *
279 * Create a new thread.
280 *
281 * @param func Thread's implementing function.
282 * @param arg Thread's implementing function argument.
283 * @param task Task to which the thread belongs.
284 * @param flags Thread flags.
[ff14c520]285 * @param name Symbolic name.
[70527f1]286 *
287 * @return New thread's structure on success, NULL on failure.
288 *
289 */
[ff14c520]290thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name)
[f761f1eb]291{
292 thread_t *t;
[bb68433]293 ipl_t ipl;
294
[266294a9]295 t = (thread_t *) slab_alloc(thread_slab, 0);
[2a46e10]296 if (!t)
297 return NULL;
[3fa424a9]298
299 thread_create_arch(t);
[f761f1eb]300
[bb68433]301 /* Not needed, but good for debugging */
[b6d4566]302 memsetb((__address)t->kstack, THREAD_STACK_SIZE * 1<<STACK_FRAMES, 0);
[bb68433]303
304 ipl = interrupts_disable();
305 spinlock_lock(&tidlock);
306 t->tid = ++last_tid;
307 spinlock_unlock(&tidlock);
308 interrupts_restore(ipl);
309
310 context_save(&t->saved_context);
311 context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
312
313 the_initialize((the_t *) t->kstack);
314
315 ipl = interrupts_disable();
316 t->saved_context.ipl = interrupts_read();
317 interrupts_restore(ipl);
318
[9f52563]319 memcpy(t->name, name, THREAD_NAME_BUFLEN);
320
[bb68433]321 t->thread_code = func;
322 t->thread_arg = arg;
323 t->ticks = -1;
324 t->priority = -1; /* start in rq[0] */
325 t->cpu = NULL;
326 t->flags = 0;
327 t->state = Entering;
328 t->call_me = NULL;
329 t->call_me_with = NULL;
330
331 timeout_initialize(&t->sleep_timeout);
[116d1ef4]332 t->sleep_interruptible = false;
[bb68433]333 t->sleep_queue = NULL;
334 t->timeout_pending = 0;
[e3c762cd]335
336 t->in_copy_from_uspace = false;
337 t->in_copy_to_uspace = false;
[7509ddc]338
339 t->interrupted = false;
[48e7dd6]340 t->join_type = None;
[fe19611]341 t->detached = false;
342 waitq_initialize(&t->join_wq);
343
[bb68433]344 t->rwlock_holder_type = RWLOCK_NONE;
[6a27d63]345
[bb68433]346 t->task = task;
347
[6f8a426]348 t->fpu_context_exists = 0;
349 t->fpu_context_engaged = 0;
[bb68433]350
[7509ddc]351 /*
352 * Attach to the containing task.
353 */
[0182a665]354 ipl = interrupts_disable();
[7509ddc]355 spinlock_lock(&task->lock);
356 if (!task->accept_new_threads) {
357 spinlock_unlock(&task->lock);
358 slab_free(thread_slab, t);
[0182a665]359 interrupts_restore(ipl);
[7509ddc]360 return NULL;
361 }
362 list_append(&t->th_link, &task->th_head);
[b91bb65]363 if (task->refcount++ == 0)
364 task->main_thread = t;
[7509ddc]365 spinlock_unlock(&task->lock);
366
[bb68433]367 /*
368 * Register this thread in the system-wide list.
369 */
370 spinlock_lock(&threads_lock);
[b7f364e]371 btree_insert(&threads_btree, (btree_key_t) ((__address) t), (void *) t, NULL);
[bb68433]372 spinlock_unlock(&threads_lock);
373
374 interrupts_restore(ipl);
[6f8a426]375
[f761f1eb]376 return t;
377}
378
[0182a665]379/** Terminate thread.
[70527f1]380 *
381 * End current thread execution and switch it to the exiting
382 * state. All pending timeouts are executed.
383 *
384 */
[f761f1eb]385void thread_exit(void)
386{
[22f7769]387 ipl_t ipl;
[f761f1eb]388
389restart:
[22f7769]390 ipl = interrupts_disable();
[43114c5]391 spinlock_lock(&THREAD->lock);
392 if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
393 spinlock_unlock(&THREAD->lock);
[22f7769]394 interrupts_restore(ipl);
[f761f1eb]395 goto restart;
396 }
[43114c5]397 THREAD->state = Exiting;
398 spinlock_unlock(&THREAD->lock);
[f761f1eb]399 scheduler();
[874621f]400
401 /* Not reached */
402 while (1)
403 ;
[f761f1eb]404}
405
[70527f1]406
407/** Thread sleep
408 *
409 * Suspend execution of the current thread.
410 *
411 * @param sec Number of seconds to sleep.
412 *
413 */
[f761f1eb]414void thread_sleep(__u32 sec)
415{
[76cec1e]416 thread_usleep(sec*1000000);
[f761f1eb]417}
[70527f1]418
[fe19611]419/** Wait for another thread to exit.
420 *
421 * @param t Thread to join on exit.
422 * @param usec Timeout in microseconds.
423 * @param flags Mode of operation.
424 *
425 * @return An error code from errno.h or an error code from synch.h.
426 */
427int thread_join_timeout(thread_t *t, __u32 usec, int flags)
428{
429 ipl_t ipl;
430 int rc;
431
432 if (t == THREAD)
433 return EINVAL;
434
435 /*
436 * Since thread join can only be called once on an undetached thread,
437 * the thread pointer is guaranteed to be still valid.
438 */
439
440 ipl = interrupts_disable();
441 spinlock_lock(&t->lock);
442 ASSERT(!t->detached);
443 spinlock_unlock(&t->lock);
[7b3e7f4]444 interrupts_restore(ipl);
[fe19611]445
[0182a665]446 rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
447
[fe19611]448 return rc;
449}
450
451/** Detach thread.
452 *
453 * Mark the thread as detached, if the thread is already in the Undead state,
454 * deallocate its resources.
455 *
456 * @param t Thread to be detached.
457 */
458void thread_detach(thread_t *t)
459{
460 ipl_t ipl;
461
462 /*
463 * Since the thread is expected to not be already detached,
464 * pointer to it must be still valid.
465 */
466 ipl = interrupts_disable();
467 spinlock_lock(&t->lock);
468 ASSERT(!t->detached);
469 if (t->state == Undead) {
470 thread_destroy(t); /* unlocks &t->lock */
471 interrupts_restore(ipl);
472 return;
473 } else {
474 t->detached = true;
475 }
476 spinlock_unlock(&t->lock);
477 interrupts_restore(ipl);
478}
479
[70527f1]480/** Thread usleep
481 *
482 * Suspend execution of the current thread.
483 *
484 * @param usec Number of microseconds to sleep.
485 *
486 */
[f761f1eb]487void thread_usleep(__u32 usec)
488{
489 waitq_t wq;
490
491 waitq_initialize(&wq);
492
[116d1ef4]493 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
[f761f1eb]494}
495
[70527f1]496/** Register thread out-of-context invocation
497 *
498 * Register a function and its argument to be executed
499 * on next context switch to the current thread.
500 *
501 * @param call_me Out-of-context function.
502 * @param call_me_with Out-of-context function argument.
503 *
504 */
[f761f1eb]505void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
506{
[22f7769]507 ipl_t ipl;
[f761f1eb]508
[22f7769]509 ipl = interrupts_disable();
[43114c5]510 spinlock_lock(&THREAD->lock);
511 THREAD->call_me = call_me;
512 THREAD->call_me_with = call_me_with;
513 spinlock_unlock(&THREAD->lock);
[22f7769]514 interrupts_restore(ipl);
[f761f1eb]515}
[55ab0f1]516
517/** Print list of threads debug info */
518void thread_print_list(void)
519{
520 link_t *cur;
521 ipl_t ipl;
522
523 /* Messing with thread structures, avoid deadlock */
524 ipl = interrupts_disable();
525 spinlock_lock(&threads_lock);
526
[016acbe]527 for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) {
528 btree_node_t *node;
529 int i;
530
531 node = list_get_instance(cur, btree_node_t, leaf_link);
532 for (i = 0; i < node->keys; i++) {
533 thread_t *t;
534
535 t = (thread_t *) node->value[i];
[fbf7b4c]536 printf("%s: address=%#zx, tid=%zd, state=%s, task=%#zx, code=%#zx, stack=%#zx, cpu=",
[016acbe]537 t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack);
538 if (t->cpu)
[3de6dd7a]539 printf("cpu%zd", t->cpu->id);
[016acbe]540 else
541 printf("none");
[3de6dd7a]542 if (t->state == Sleeping) {
[fbf7b4c]543 printf(", kst=%#zx", t->kstack);
544 printf(", wq=%#zx", t->sleep_queue);
[3de6dd7a]545 }
[016acbe]546 printf("\n");
547 }
[55ab0f1]548 }
549
550 spinlock_unlock(&threads_lock);
[37c57f2]551 interrupts_restore(ipl);
[55ab0f1]552}
[9f52563]553
[016acbe]554/** Check whether thread exists.
555 *
556 * Note that threads_lock must be already held and
557 * interrupts must be already disabled.
558 *
559 * @param t Pointer to thread.
560 *
561 * @return True if thread t is known to the system, false otherwise.
562 */
563bool thread_exists(thread_t *t)
564{
565 btree_node_t *leaf;
566
[b7f364e]567 return btree_search(&threads_btree, (btree_key_t) ((__address) t), &leaf) != NULL;
[016acbe]568}
569
[9f52563]570/** Process syscall to create new thread.
571 *
572 */
[0f250f9]573__native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name)
[9f52563]574{
[68091bd]575 thread_t *t;
576 char namebuf[THREAD_NAME_BUFLEN];
[45fb65c]577 uspace_arg_t *kernel_uarg;
[9f52563]578 __u32 tid;
[e3c762cd]579 int rc;
[9f52563]580
[e3c762cd]581 rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
582 if (rc != 0)
583 return (__native) rc;
[0f250f9]584
585 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
[e3c762cd]586 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
587 if (rc != 0) {
588 free(kernel_uarg);
589 return (__native) rc;
590 }
[9f52563]591
[68091bd]592 if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) {
[9f52563]593 tid = t->tid;
[68091bd]594 thread_ready(t);
[9f52563]595 return (__native) tid;
[68091bd]596 } else {
[0f250f9]597 free(kernel_uarg);
[68091bd]598 }
[9f52563]599
[e3c762cd]600 return (__native) ENOMEM;
[9f52563]601}
602
603/** Process syscall to terminate thread.
604 *
605 */
[0f250f9]606__native sys_thread_exit(int uspace_status)
[9f52563]607{
[68091bd]608 thread_exit();
609 /* Unreachable */
610 return 0;
[9f52563]611}
[b45c443]612
[cc73a8a1]613/** @}
[b45c443]614 */
Note: See TracBrowser for help on using the repository browser.