Subversion Repositories HelenOS-historic

Rev

Rev 787 | Rev 814 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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/heap.h>
  33. #include <mm/frame.h>
  34. #include <mm/page.h>
  35. #include <arch/asm.h>
  36. #include <arch.h>
  37. #include <synch/synch.h>
  38. #include <synch/spinlock.h>
  39. #include <synch/waitq.h>
  40. #include <synch/rwlock.h>
  41. #include <cpu.h>
  42. #include <func.h>
  43. #include <context.h>
  44. #include <adt/list.h>
  45. #include <typedefs.h>
  46. #include <time/clock.h>
  47. #include <adt/list.h>
  48. #include <config.h>
  49. #include <arch/interrupt.h>
  50. #include <smp/ipi.h>
  51. #include <arch/faddr.h>
  52. #include <arch/atomic.h>
  53. #include <memstr.h>
  54. #include <print.h>
  55. #include <mm/slab.h>
  56. #include <debug.h>
  57.  
  58. char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */
  59.  
  60. SPINLOCK_INITIALIZE(threads_lock);  /**< Lock protecting threads_head list. For locking rules, see declaration thereof. */
  61. LIST_INITIALIZE(threads_head);      /**< List of all threads. */
  62.  
  63. SPINLOCK_INITIALIZE(tidlock);
  64. __u32 last_tid = 0;
  65.  
  66. static slab_cache_t *thread_slab;
  67.  
  68.  
  69. /** Thread wrapper
  70.  *
  71.  * This wrapper is provided to ensure that every thread
  72.  * makes a call to thread_exit() when its implementing
  73.  * function returns.
  74.  *
  75.  * interrupts_disable() is assumed.
  76.  *
  77.  */
  78. static void cushion(void)
  79. {
  80.     void (*f)(void *) = THREAD->thread_code;
  81.     void *arg = THREAD->thread_arg;
  82.  
  83.     /* this is where each thread wakes up after its creation */
  84.     before_thread_runs();
  85.  
  86.     spinlock_unlock(&THREAD->lock);
  87.     interrupts_enable();
  88.  
  89.     f(arg);
  90.     thread_exit();
  91.     /* not reached */
  92. }
  93.  
  94. /** Initialization and allocation for thread_t structure */
  95. static int thr_constructor(void *obj, int kmflags)
  96. {
  97.     thread_t *t = (thread_t *)obj;
  98.  
  99.     spinlock_initialize(&t->lock, "thread_t_lock");
  100.     link_initialize(&t->rq_link);
  101.     link_initialize(&t->wq_link);
  102.     link_initialize(&t->th_link);
  103.     link_initialize(&t->threads_link);
  104.    
  105.     t->kstack = (__u8 *)frame_alloc(ONE_FRAME, FRAME_KA | kmflags);
  106.     if (!t->kstack)
  107.         return -1;
  108.  
  109.     return 0;
  110. }
  111.  
  112. /** Destruction of thread_t object */
  113. static int thr_destructor(void *obj)
  114. {
  115.     thread_t *t = (thread_t *)obj;
  116.  
  117.     frame_free((__address) t->kstack);
  118.     return 1; /* One page freed */
  119. }
  120.  
  121. /** Initialize threads
  122.  *
  123.  * Initialize kernel threads support.
  124.  *
  125.  */
  126. void thread_init(void)
  127. {
  128.     THREAD = NULL;
  129.     atomic_set(&nrdy,0);
  130.     thread_slab = slab_cache_create("thread_slab",
  131.                     sizeof(thread_t),0,
  132.                     thr_constructor, thr_destructor, 0);
  133. }
  134.  
  135.  
  136. /** Make thread ready
  137.  *
  138.  * Switch thread t to the ready state.
  139.  *
  140.  * @param t Thread to make ready.
  141.  *
  142.  */
  143. void thread_ready(thread_t *t)
  144. {
  145.     cpu_t *cpu;
  146.     runq_t *r;
  147.     ipl_t ipl;
  148.     int i, avg;
  149.  
  150.     ipl = interrupts_disable();
  151.  
  152.     spinlock_lock(&t->lock);
  153.  
  154.     i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority;
  155.    
  156.     cpu = CPU;
  157.     if (t->flags & X_WIRED) {
  158.         cpu = t->cpu;
  159.     }
  160.     spinlock_unlock(&t->lock);
  161.    
  162.     /*
  163.      * Append t to respective ready queue on respective processor.
  164.      */
  165.     r = &cpu->rq[i];
  166.     spinlock_lock(&r->lock);
  167.     list_append(&t->rq_link, &r->rq_head);
  168.     r->n++;
  169.     spinlock_unlock(&r->lock);
  170.  
  171.     atomic_inc(&nrdy);
  172.     avg = atomic_get(&nrdy) / config.cpu_active;
  173.     atomic_inc(&cpu->nrdy);
  174.  
  175.     interrupts_restore(ipl);
  176. }
  177.  
  178.  
  179. /** Destroy thread memory structure
  180.  *
  181.  * Detach thread from all queues, cpus etc. and destroy it.
  182.  *
  183.  * Assume thread->lock is held!!
  184.  */
  185. void thread_destroy(thread_t *t)
  186. {
  187.     ASSERT(t->state == Exiting);
  188.     ASSERT(t->task);
  189.     ASSERT(t->cpu);
  190.  
  191.     spinlock_lock(&t->cpu->lock);
  192.     if(t->cpu->fpu_owner==t)
  193.         t->cpu->fpu_owner=NULL;
  194.     spinlock_unlock(&t->cpu->lock);
  195.  
  196.     if (t->ustack)
  197.         frame_free((__address) t->ustack);
  198.    
  199.     /*
  200.      * Detach from the containing task.
  201.      */
  202.     spinlock_lock(&t->task->lock);
  203.     list_remove(&t->th_link);
  204.     spinlock_unlock(&t->task->lock);
  205.    
  206.     spinlock_unlock(&t->lock);
  207.    
  208.     spinlock_lock(&threads_lock);
  209.     list_remove(&t->threads_link);
  210.     spinlock_unlock(&threads_lock);
  211.    
  212.     slab_free(thread_slab, t);
  213. }
  214.  
  215.  
  216. /** Create new thread
  217.  *
  218.  * Create a new thread.
  219.  *
  220.  * @param func  Thread's implementing function.
  221.  * @param arg   Thread's implementing function argument.
  222.  * @param task  Task to which the thread belongs.
  223.  * @param flags Thread flags.
  224.  *
  225.  * @return New thread's structure on success, NULL on failure.
  226.  *
  227.  */
  228. thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags)
  229. {
  230.     thread_t *t;
  231.     __address frame_us = NULL;
  232.  
  233.     t = (thread_t *) slab_alloc(thread_slab, 0);
  234.     if (t) {
  235.         ipl_t ipl;
  236.    
  237.         if (THREAD_USER_STACK & flags) {
  238.             frame_us = frame_alloc(ONE_FRAME, FRAME_KA);
  239.         }
  240.  
  241.         /* Not needed, but good for debugging */
  242.         memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0);
  243.  
  244.         ipl = interrupts_disable();
  245.         spinlock_lock(&tidlock);
  246.         t->tid = ++last_tid;
  247.         spinlock_unlock(&tidlock);
  248.         interrupts_restore(ipl);
  249.        
  250.         t->ustack = (__u8 *) frame_us;
  251.        
  252.         context_save(&t->saved_context);
  253.         context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE);
  254.        
  255.         the_initialize((the_t *) t->kstack);
  256.  
  257.         ipl = interrupts_disable();
  258.         t->saved_context.ipl = interrupts_read();
  259.         interrupts_restore(ipl);
  260.        
  261.         t->thread_code = func;
  262.         t->thread_arg = arg;
  263.         t->ticks = -1;
  264.         t->priority = -1;       /* start in rq[0] */
  265.         t->cpu = NULL;
  266.         t->flags = 0;
  267.         t->state = Entering;
  268.         t->call_me = NULL;
  269.         t->call_me_with = NULL;
  270.        
  271.         timeout_initialize(&t->sleep_timeout);
  272.         t->sleep_queue = NULL;
  273.         t->timeout_pending = 0;
  274.        
  275.         t->rwlock_holder_type = RWLOCK_NONE;
  276.        
  277.         t->task = task;
  278.        
  279.         t->fpu_context_exists=0;
  280.         t->fpu_context_engaged=0;
  281.        
  282.         /*
  283.          * Register this thread in the system-wide list.
  284.          */
  285.         ipl = interrupts_disable();
  286.         spinlock_lock(&threads_lock);
  287.         list_append(&t->threads_link, &threads_head);
  288.         spinlock_unlock(&threads_lock);
  289.  
  290.         /*
  291.          * Attach to the containing task.
  292.          */
  293.         spinlock_lock(&task->lock);
  294.         list_append(&t->th_link, &task->th_head);
  295.         spinlock_unlock(&task->lock);
  296.  
  297.         interrupts_restore(ipl);
  298.     }
  299.  
  300.     return t;
  301. }
  302.  
  303.  
  304. /** Make thread exiting
  305.  *
  306.  * End current thread execution and switch it to the exiting
  307.  * state. All pending timeouts are executed.
  308.  *
  309.  */
  310. void thread_exit(void)
  311. {
  312.     ipl_t ipl;
  313.  
  314. restart:
  315.     ipl = interrupts_disable();
  316.     spinlock_lock(&THREAD->lock);
  317.     if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */
  318.         spinlock_unlock(&THREAD->lock);
  319.         interrupts_restore(ipl);
  320.         goto restart;
  321.     }
  322.     THREAD->state = Exiting;
  323.     spinlock_unlock(&THREAD->lock);
  324.     scheduler();
  325. }
  326.  
  327.  
  328. /** Thread sleep
  329.  *
  330.  * Suspend execution of the current thread.
  331.  *
  332.  * @param sec Number of seconds to sleep.
  333.  *
  334.  */
  335. void thread_sleep(__u32 sec)
  336. {
  337.     thread_usleep(sec*1000000);
  338. }
  339.  
  340.  
  341. /** Thread usleep
  342.  *
  343.  * Suspend execution of the current thread.
  344.  *
  345.  * @param usec Number of microseconds to sleep.
  346.  *
  347.  */
  348. void thread_usleep(__u32 usec)
  349. {
  350.     waitq_t wq;
  351.                  
  352.     waitq_initialize(&wq);
  353.  
  354.     (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING);
  355. }
  356.  
  357.  
  358. /** Register thread out-of-context invocation
  359.  *
  360.  * Register a function and its argument to be executed
  361.  * on next context switch to the current thread.
  362.  *
  363.  * @param call_me      Out-of-context function.
  364.  * @param call_me_with Out-of-context function argument.
  365.  *
  366.  */
  367. void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
  368. {
  369.     ipl_t ipl;
  370.    
  371.     ipl = interrupts_disable();
  372.     spinlock_lock(&THREAD->lock);
  373.     THREAD->call_me = call_me;
  374.     THREAD->call_me_with = call_me_with;
  375.     spinlock_unlock(&THREAD->lock);
  376.     interrupts_restore(ipl);
  377. }
  378.  
  379. /** Print list of threads debug info */
  380. void thread_print_list(void)
  381. {
  382.     link_t *cur;
  383.     thread_t *t;
  384.     ipl_t ipl;
  385.    
  386.     /* Messing with thread structures, avoid deadlock */
  387.     ipl = interrupts_disable();
  388.     spinlock_lock(&threads_lock);
  389.  
  390.     for (cur=threads_head.next; cur!=&threads_head; cur=cur->next) {
  391.         t = list_get_instance(cur, thread_t, threads_link);
  392.         printf("Thr: %d(%s) ", t->tid, thread_states[t->state]);
  393.         if (t->cpu)
  394.             printf("cpu%d ", t->cpu->id);
  395.        
  396.         printf("\n");
  397.     }
  398.  
  399.     spinlock_unlock(&threads_lock);
  400.     interrupts_enable();
  401. }
  402.