Subversion Repositories HelenOS

Rev

Rev 2089 | Rev 2310 | 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. /** @addtogroup sync
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief   Wait queue.
  36.  *
  37.  * Wait queue is the basic synchronization primitive upon which all
  38.  * other synchronization primitives build.
  39.  *
  40.  * It allows threads to wait for an event in first-come, first-served
  41.  * fashion. Conditional operation as well as timeouts and interruptions
  42.  * are supported.
  43.  */
  44.  
  45. #include <synch/waitq.h>
  46. #include <synch/synch.h>
  47. #include <synch/spinlock.h>
  48. #include <proc/thread.h>
  49. #include <proc/scheduler.h>
  50. #include <arch/asm.h>
  51. #include <arch/types.h>
  52. #include <time/timeout.h>
  53. #include <arch.h>
  54. #include <context.h>
  55. #include <adt/list.h>
  56.  
  57. static void waitq_timeouted_sleep(void *data);
  58.  
  59. /** Initialize wait queue
  60.  *
  61.  * Initialize wait queue.
  62.  *
  63.  * @param wq Pointer to wait queue to be initialized.
  64.  */
  65. void waitq_initialize(waitq_t *wq)
  66. {
  67.     spinlock_initialize(&wq->lock, "waitq_lock");
  68.     list_initialize(&wq->head);
  69.     wq->missed_wakeups = 0;
  70. }
  71.  
  72. /** Handle timeout during waitq_sleep_timeout() call
  73.  *
  74.  * This routine is called when waitq_sleep_timeout() timeouts.
  75.  * Interrupts are disabled.
  76.  *
  77.  * It is supposed to try to remove 'its' thread from the wait queue;
  78.  * it can eventually fail to achieve this goal when these two events
  79.  * overlap. In that case it behaves just as though there was no
  80.  * timeout at all.
  81.  *
  82.  * @param data Pointer to the thread that called waitq_sleep_timeout().
  83.  */
  84. void waitq_timeouted_sleep(void *data)
  85. {
  86.     thread_t *t = (thread_t *) data;
  87.     waitq_t *wq;
  88.     bool do_wakeup = false;
  89.  
  90.     spinlock_lock(&threads_lock);
  91.     if (!thread_exists(t))
  92.         goto out;
  93.  
  94. grab_locks:
  95.     spinlock_lock(&t->lock);
  96.     if ((wq = t->sleep_queue)) {        /* assignment */
  97.         if (!spinlock_trylock(&wq->lock)) {
  98.             spinlock_unlock(&t->lock);
  99.             goto grab_locks;    /* avoid deadlock */
  100.         }
  101.  
  102.         list_remove(&t->wq_link);
  103.         t->saved_context = t->sleep_timeout_context;
  104.         do_wakeup = true;
  105.         t->sleep_queue = NULL;
  106.         spinlock_unlock(&wq->lock);
  107.     }
  108.    
  109.     t->timeout_pending = false;
  110.     spinlock_unlock(&t->lock);
  111.    
  112.     if (do_wakeup)
  113.         thread_ready(t);
  114.  
  115. out:
  116.     spinlock_unlock(&threads_lock);
  117. }
  118.  
  119. /** Interrupt sleeping thread.
  120.  *
  121.  * This routine attempts to interrupt a thread from its sleep in a waitqueue.
  122.  * If the thread is not found sleeping, no action is taken.
  123.  *
  124.  * @param t Thread to be interrupted.
  125.  */
  126. void waitq_interrupt_sleep(thread_t *t)
  127. {
  128.     waitq_t *wq;
  129.     bool do_wakeup = false;
  130.     ipl_t ipl;
  131.  
  132.     ipl = interrupts_disable();
  133.     spinlock_lock(&threads_lock);
  134.     if (!thread_exists(t))
  135.         goto out;
  136.  
  137. grab_locks:
  138.     spinlock_lock(&t->lock);
  139.     if ((wq = t->sleep_queue)) {        /* assignment */
  140.         if (!(t->sleep_interruptible)) {
  141.             /*
  142.              * The sleep cannot be interrupted.
  143.              */
  144.             spinlock_unlock(&t->lock);
  145.             goto out;
  146.         }
  147.            
  148.         if (!spinlock_trylock(&wq->lock)) {
  149.             spinlock_unlock(&t->lock);
  150.             goto grab_locks;    /* avoid deadlock */
  151.         }
  152.  
  153.         if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
  154.             t->timeout_pending = false;
  155.  
  156.         list_remove(&t->wq_link);
  157.         t->saved_context = t->sleep_interruption_context;
  158.         do_wakeup = true;
  159.         t->sleep_queue = NULL;
  160.         spinlock_unlock(&wq->lock);
  161.     }
  162.     spinlock_unlock(&t->lock);
  163.  
  164.     if (do_wakeup)
  165.         thread_ready(t);
  166.  
  167. out:
  168.     spinlock_unlock(&threads_lock);
  169.     interrupts_restore(ipl);
  170. }
  171.  
  172. /** Sleep until either wakeup, timeout or interruption occurs
  173.  *
  174.  * This is a sleep implementation which allows itself to time out or to be
  175.  * interrupted from the sleep, restoring a failover context.
  176.  *
  177.  * Sleepers are organised in a FIFO fashion in a structure called wait queue.
  178.  *
  179.  * This function is really basic in that other functions as waitq_sleep()
  180.  * and all the *_timeout() functions use it.
  181.  *
  182.  * @param wq Pointer to wait queue.
  183.  * @param usec Timeout in microseconds.
  184.  * @param flags Specify mode of the sleep.
  185.  *
  186.  * The sleep can be interrupted only if the
  187.  * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
  188.  *
  189.  * If usec is greater than zero, regardless of the value of the
  190.  * SYNCH_FLAGS_NON_BLOCKING bit in flags, the call will not return until either
  191.  * timeout, interruption or wakeup comes.
  192.  *
  193.  * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is not set in flags,
  194.  * the call will not return until wakeup or interruption comes.
  195.  *
  196.  * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
  197.  * call will immediately return, reporting either success or failure.
  198.  *
  199.  * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
  200.  * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
  201.  *
  202.  * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the
  203.  * call there was no pending wakeup.
  204.  *
  205.  * @li ESYNCH_TIMEOUT means that the sleep timed out.
  206.  *
  207.  * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
  208.  *
  209.  * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
  210.  * a pending wakeup at the time of the call. The caller was not put
  211.  * asleep at all.
  212.  *
  213.  * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
  214.  * attempted.
  215.  */
  216. int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags)
  217. {
  218.     ipl_t ipl;
  219.     int rc;
  220.    
  221.     ipl = waitq_sleep_prepare(wq);
  222.     rc = waitq_sleep_timeout_unsafe(wq, usec, flags);
  223.     waitq_sleep_finish(wq, rc, ipl);
  224.     return rc;
  225. }
  226.  
  227. /** Prepare to sleep in a waitq.
  228.  *
  229.  * This function will return holding the lock of the wait queue
  230.  * and interrupts disabled.
  231.  *
  232.  * @param wq Wait queue.
  233.  *
  234.  * @return Interrupt level as it existed on entry to this function.
  235.  */
  236. ipl_t waitq_sleep_prepare(waitq_t *wq)
  237. {
  238.     ipl_t ipl;
  239.    
  240. restart:
  241.     ipl = interrupts_disable();
  242.  
  243.     if (THREAD) {   /* needed during system initiailzation */
  244.         /*
  245.          * Busy waiting for a delayed timeout.
  246.          * This is an important fix for the race condition between
  247.          * a delayed timeout and a next call to waitq_sleep_timeout().
  248.          * Simply, the thread is not allowed to go to sleep if
  249.          * there are timeouts in progress.
  250.          */
  251.         spinlock_lock(&THREAD->lock);
  252.         if (THREAD->timeout_pending) {
  253.             spinlock_unlock(&THREAD->lock);
  254.             interrupts_restore(ipl);
  255.             goto restart;
  256.         }
  257.         spinlock_unlock(&THREAD->lock);
  258.     }
  259.                                                    
  260.     spinlock_lock(&wq->lock);
  261.     return ipl;
  262. }
  263.  
  264. /** Finish waiting in a wait queue.
  265.  *
  266.  * This function restores interrupts to the state that existed prior
  267.  * to the call to waitq_sleep_prepare(). If necessary, the wait queue
  268.  * lock is released.
  269.  *
  270.  * @param wq Wait queue.
  271.  * @param rc Return code of waitq_sleep_timeout_unsafe().
  272.  * @param ipl Interrupt level returned by waitq_sleep_prepare().
  273.  */
  274. void waitq_sleep_finish(waitq_t *wq, int rc, ipl_t ipl)
  275. {
  276.     switch (rc) {
  277.     case ESYNCH_WOULD_BLOCK:
  278.     case ESYNCH_OK_ATOMIC:
  279.         spinlock_unlock(&wq->lock);
  280.         break;
  281.     default:
  282.         break;
  283.     }
  284.     interrupts_restore(ipl);
  285. }
  286.  
  287. /** Internal implementation of waitq_sleep_timeout().
  288.  *
  289.  * This function implements logic of sleeping in a wait queue.
  290.  * This call must be preceeded by a call to waitq_sleep_prepare()
  291.  * and followed by a call to waitq_slee_finish().
  292.  *
  293.  * @param wq See waitq_sleep_timeout().
  294.  * @param usec See waitq_sleep_timeout().
  295.  * @param flags See waitq_sleep_timeout().
  296.  *
  297.  * @return See waitq_sleep_timeout().
  298.  */
  299. int waitq_sleep_timeout_unsafe(waitq_t *wq, uint32_t usec, int flags)
  300. {
  301.     /* checks whether to go to sleep at all */
  302.     if (wq->missed_wakeups) {
  303.         wq->missed_wakeups--;
  304.         return ESYNCH_OK_ATOMIC;
  305.     }
  306.     else {
  307.         if ((flags & SYNCH_FLAGS_NON_BLOCKING) && (usec == 0)) {
  308.             /* return immediatelly instead of going to sleep */
  309.             return ESYNCH_WOULD_BLOCK;
  310.         }
  311.     }
  312.    
  313.     /*
  314.      * Now we are firmly decided to go to sleep.
  315.      */
  316.     spinlock_lock(&THREAD->lock);
  317.  
  318.     if (flags & SYNCH_FLAGS_INTERRUPTIBLE) {
  319.  
  320.         /*
  321.          * If the thread was already interrupted,
  322.          * don't go to sleep at all.
  323.          */
  324.         if (THREAD->interrupted) {
  325.             spinlock_unlock(&THREAD->lock);
  326.             spinlock_unlock(&wq->lock);
  327.             return ESYNCH_INTERRUPTED;
  328.         }
  329.  
  330.         /*
  331.          * Set context that will be restored if the sleep
  332.          * of this thread is ever interrupted.
  333.          */
  334.         THREAD->sleep_interruptible = true;
  335.         if (!context_save(&THREAD->sleep_interruption_context)) {
  336.             /* Short emulation of scheduler() return code. */
  337.             spinlock_unlock(&THREAD->lock);
  338.             return ESYNCH_INTERRUPTED;
  339.         }
  340.  
  341.     } else {
  342.         THREAD->sleep_interruptible = false;
  343.     }
  344.  
  345.     if (usec) {
  346.         /* We use the timeout variant. */
  347.         if (!context_save(&THREAD->sleep_timeout_context)) {
  348.             /* Short emulation of scheduler() return code. */
  349.             spinlock_unlock(&THREAD->lock);
  350.             return ESYNCH_TIMEOUT;
  351.         }
  352.         THREAD->timeout_pending = true;
  353.         timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
  354.             waitq_timeouted_sleep, THREAD);
  355.     }
  356.  
  357.     list_append(&THREAD->wq_link, &wq->head);
  358.  
  359.     /*
  360.      * Suspend execution.
  361.      */
  362.     THREAD->state = Sleeping;
  363.     THREAD->sleep_queue = wq;
  364.  
  365.     spinlock_unlock(&THREAD->lock);
  366.  
  367.     /* wq->lock is released in scheduler_separated_stack() */
  368.     scheduler();
  369.    
  370.     return ESYNCH_OK_BLOCKED;
  371. }
  372.  
  373.  
  374. /** Wake up first thread sleeping in a wait queue
  375.  *
  376.  * Wake up first thread sleeping in a wait queue. This is the SMP- and IRQ-safe
  377.  * wrapper meant for general use.
  378.  *
  379.  * Besides its 'normal' wakeup operation, it attempts to unregister possible
  380.  * timeout.
  381.  *
  382.  * @param wq Pointer to wait queue.
  383.  * @param all If this is non-zero, all sleeping threads will be woken up and
  384.  *  missed count will be zeroed.
  385.  */
  386. void waitq_wakeup(waitq_t *wq, bool all)
  387. {
  388.     ipl_t ipl;
  389.  
  390.     ipl = interrupts_disable();
  391.     spinlock_lock(&wq->lock);
  392.  
  393.     _waitq_wakeup_unsafe(wq, all);
  394.  
  395.     spinlock_unlock(&wq->lock);
  396.     interrupts_restore(ipl);   
  397. }
  398.  
  399. /** Internal SMP- and IRQ-unsafe version of waitq_wakeup()
  400.  *
  401.  * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
  402.  * assumes wq->lock is already locked and interrupts are already disabled.
  403.  *
  404.  * @param wq Pointer to wait queue.
  405.  * @param all If this is non-zero, all sleeping threads will be woken up and
  406.  *  missed count will be zeroed.
  407.  */
  408. void _waitq_wakeup_unsafe(waitq_t *wq, bool all)
  409. {
  410.     thread_t *t;
  411.  
  412. loop:  
  413.     if (list_empty(&wq->head)) {
  414.         wq->missed_wakeups++;
  415.         if (all)
  416.             wq->missed_wakeups = 0;
  417.         return;
  418.     }
  419.  
  420.     t = list_get_instance(wq->head.next, thread_t, wq_link);
  421.    
  422.     /*
  423.      * Lock the thread prior to removing it from the wq.
  424.      * This is not necessary because of mutual exclusion
  425.      * (the link belongs to the wait queue), but because
  426.      * of synchronization with waitq_timeouted_sleep()
  427.      * and thread_interrupt_sleep().
  428.      *
  429.      * In order for these two functions to work, the following
  430.      * invariant must hold:
  431.      *
  432.      * t->sleep_queue != NULL <=> t sleeps in a wait queue
  433.      *
  434.      * For an observer who locks the thread, the invariant
  435.      * holds only when the lock is held prior to removing
  436.      * it from the wait queue.
  437.      */
  438.     spinlock_lock(&t->lock);
  439.     list_remove(&t->wq_link);
  440.    
  441.     if (t->timeout_pending && timeout_unregister(&t->sleep_timeout))
  442.         t->timeout_pending = false;
  443.     t->sleep_queue = NULL;
  444.     spinlock_unlock(&t->lock);
  445.  
  446.     thread_ready(t);
  447.  
  448.     if (all)
  449.         goto loop;
  450. }
  451.  
  452. /** @}
  453.  */
  454.