Subversion Repositories HelenOS-historic

Rev

Rev 552 | Rev 623 | 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. /** Reader/Writer locks
  30.  *
  31.  * A reader/writer lock can be held by multiple readers at a time.
  32.  * Or it can be exclusively held by a sole writer at a time.
  33.  */
  34.  
  35. /*
  36.  * These locks are not recursive.
  37.  * Neither readers nor writers will suffer starvation.
  38.  *
  39.  * If there is a writer followed by a reader waiting for the rwlock
  40.  * and the writer times out, all leading readers are automatically woken up
  41.  * and allowed in.
  42.  */
  43.  
  44. /*
  45.  * NOTE ON rwlock_holder_type
  46.  * This field is set on an attempt to acquire the exclusive mutex
  47.  * to the respective value depending whether the caller is a reader
  48.  * or a writer. The field is examined only if the thread had been
  49.  * previously blocked on the exclusive mutex. Thus it is save
  50.  * to store the rwlock type in the thread structure, because
  51.  * each thread can block on only one rwlock at a time.
  52.  */
  53.  
  54. #include <synch/rwlock.h>
  55. #include <synch/spinlock.h>
  56. #include <synch/mutex.h>
  57. #include <synch/waitq.h>
  58. #include <synch/synch.h>
  59. #include <list.h>
  60. #include <typedefs.h>
  61. #include <arch/asm.h>
  62. #include <arch.h>
  63. #include <proc/thread.h>
  64. #include <panic.h>
  65.  
  66. #define ALLOW_ALL       0
  67. #define ALLOW_READERS_ONLY  1
  68.  
  69. static void let_others_in(rwlock_t *rwl, int readers_only);
  70. static void release_spinlock(void *arg);
  71.  
  72. /** Initialize reader/writer lock
  73.  *
  74.  * Initialize reader/writer lock.
  75.  *
  76.  * @param rwl Reader/Writer lock.
  77.  */
  78. void rwlock_initialize(rwlock_t *rwl) {
  79.     spinlock_initialize(&rwl->lock, "rwlock_t");
  80.     mutex_initialize(&rwl->exclusive);
  81.     rwl->readers_in = 0;
  82. }
  83.  
  84. /** Acquire reader/writer lock for reading
  85.  *
  86.  * Acquire reader/writer lock for reading.
  87.  * Timeout and willingness to block may be specified.
  88.  *
  89.  * @param rwl Reader/Writer lock.
  90.  * @param usec Timeout in microseconds.
  91.  * @param trylock Switches between blocking and non-blocking mode.
  92.  *
  93.  * For exact description of possible combinations of
  94.  * 'usec' and 'trylock', see comment for waitq_sleep_timeout().
  95.  *
  96.  * @return See comment for waitq_sleep_timeout().
  97.  */
  98. int _rwlock_write_lock_timeout(rwlock_t *rwl, __u32 usec, int trylock)
  99. {
  100.     ipl_t ipl;
  101.     int rc;
  102.    
  103.     ipl = interrupts_disable();
  104.     spinlock_lock(&THREAD->lock);
  105.     THREAD->rwlock_holder_type = RWLOCK_WRITER;
  106.     spinlock_unlock(&THREAD->lock);
  107.     interrupts_restore(ipl);
  108.  
  109.     /*
  110.      * Writers take the easy part.
  111.      * They just need to acquire the exclusive mutex.
  112.      */
  113.     rc = _mutex_lock_timeout(&rwl->exclusive, usec, trylock);
  114.     if (SYNCH_FAILED(rc)) {
  115.  
  116.         /*
  117.          * Lock operation timed out.
  118.          * The state of rwl is UNKNOWN at this point.
  119.          * No claims about its holder can be made.
  120.          */
  121.          
  122.         ipl = interrupts_disable();
  123.         spinlock_lock(&rwl->lock);
  124.         /*
  125.          * Now when rwl is locked, we can inspect it again.
  126.          * If it is held by some readers already, we can let
  127.          * readers from the head of the wait queue in.
  128.          */
  129.         if (rwl->readers_in)
  130.             let_others_in(rwl, ALLOW_READERS_ONLY);
  131.         spinlock_unlock(&rwl->lock);
  132.         interrupts_restore(ipl);
  133.     }
  134.    
  135.     return rc;
  136. }
  137.  
  138. /** Acquire reader/writer lock for writing
  139.  *
  140.  * Acquire reader/writer lock for writing.
  141.  * Timeout and willingness to block may be specified.
  142.  *
  143.  * @param rwl Reader/Writer lock.
  144.  * @param usec Timeout in microseconds.
  145.  * @param trylock Switches between blocking and non-blocking mode.
  146.  *
  147.  * For exact description of possible combinations of
  148.  * 'usec' and 'trylock', see comment for waitq_sleep_timeout().
  149.  *
  150.  * @return See comment for waitq_sleep_timeout().
  151.  */
  152. int _rwlock_read_lock_timeout(rwlock_t *rwl, __u32 usec, int trylock)
  153. {
  154.     int rc;
  155.     ipl_t ipl;
  156.    
  157.     ipl = interrupts_disable();
  158.     spinlock_lock(&THREAD->lock);
  159.     THREAD->rwlock_holder_type = RWLOCK_READER;
  160.     spinlock_unlock(&THREAD->lock);
  161.  
  162.     spinlock_lock(&rwl->lock);
  163.  
  164.     /*
  165.      * Find out whether we can get what we want without blocking.
  166.      */
  167.     rc = mutex_trylock(&rwl->exclusive);
  168.     if (SYNCH_FAILED(rc)) {
  169.  
  170.         /*
  171.          * 'exclusive' mutex is being held by someone else.
  172.          * If the holder is a reader and there is no one
  173.          * else waiting for it, we can enter the critical
  174.          * section.
  175.          */
  176.  
  177.         if (rwl->readers_in) {
  178.             spinlock_lock(&rwl->exclusive.sem.wq.lock);
  179.             if (list_empty(&rwl->exclusive.sem.wq.head)) {
  180.                 /*
  181.                  * We can enter.
  182.                  */
  183.                 spinlock_unlock(&rwl->exclusive.sem.wq.lock);
  184.                 goto shortcut;
  185.             }
  186.             spinlock_unlock(&rwl->exclusive.sem.wq.lock);
  187.         }
  188.  
  189.         /*
  190.          * In order to prevent a race condition when a reader
  191.          * could block another reader at the head of the waitq,
  192.          * we register a function to unlock rwl->lock
  193.          * after this thread is put asleep.
  194.          */
  195.         thread_register_call_me(release_spinlock, &rwl->lock);
  196.                  
  197.         rc = _mutex_lock_timeout(&rwl->exclusive, usec, trylock);
  198.         switch (rc) {
  199.             case ESYNCH_WOULD_BLOCK:
  200.                 /*
  201.                  * release_spinlock() wasn't called
  202.                  */
  203.                 thread_register_call_me(NULL, NULL);                 
  204.                 spinlock_unlock(&rwl->lock);
  205.             case ESYNCH_TIMEOUT:
  206.                 /*
  207.                  * The sleep timeouted.
  208.                  * We just restore interrupt priority level.
  209.                  */
  210.             case ESYNCH_OK_BLOCKED:    
  211.                 /*
  212.                  * We were woken with rwl->readers_in already incremented.
  213.                  * Note that this arrangement avoids race condition between
  214.                  * two concurrent readers. (Race is avoided if 'exclusive' is
  215.                  * locked at the same time as 'readers_in' is incremented.
  216.                  * Same time means both events happen atomically when
  217.                  * rwl->lock is held.)
  218.                  */
  219.                 interrupts_restore(ipl);
  220.                 break;
  221.             case ESYNCH_OK_ATOMIC:
  222.                 panic("_mutex_lock_timeout()==ESYNCH_OK_ATOMIC\n");
  223.                 break;
  224.             dafault:
  225.                 panic("invalid ESYNCH\n");
  226.                 break;
  227.         }
  228.         return rc;
  229.     }
  230.  
  231. shortcut:
  232.  
  233.     /*
  234.      * We can increment readers_in only if we didn't go to sleep.
  235.      * For sleepers, rwlock_let_others_in() will do the job.
  236.      */
  237.     rwl->readers_in++;
  238.    
  239.     spinlock_unlock(&rwl->lock);
  240.     interrupts_restore(ipl);
  241.  
  242.     return ESYNCH_OK_ATOMIC;
  243. }
  244.  
  245. /** Release reader/writer lock held by writer
  246.  *
  247.  * Release reader/writer lock held by writer.
  248.  * Handoff reader/writer lock ownership directly
  249.  * to waiting readers or a writer.
  250.  *
  251.  * @param rwl Reader/Writer lock.
  252.  */
  253. void rwlock_write_unlock(rwlock_t *rwl)
  254. {
  255.     ipl_t ipl;
  256.    
  257.     ipl = interrupts_disable();
  258.     spinlock_lock(&rwl->lock);
  259.     let_others_in(rwl, ALLOW_ALL);
  260.     spinlock_unlock(&rwl->lock);
  261.     interrupts_restore(ipl);
  262.    
  263. }
  264.  
  265. /** Release reader/writer lock held by reader
  266.  *
  267.  * Release reader/writer lock held by reader.
  268.  * Handoff reader/writer lock ownership directly
  269.  * to a waiting writer or don't do anything if more
  270.  * readers poses the lock.
  271.  *
  272.  * @param rwl Reader/Writer lock.
  273.  */
  274. void rwlock_read_unlock(rwlock_t *rwl)
  275. {
  276.     ipl_t ipl;
  277.  
  278.     ipl = interrupts_disable();
  279.     spinlock_lock(&rwl->lock);
  280.     if (!--rwl->readers_in)
  281.         let_others_in(rwl, ALLOW_ALL);
  282.     spinlock_unlock(&rwl->lock);
  283.     interrupts_restore(ipl);
  284. }
  285.  
  286.  
  287. /** Direct handoff of reader/writer lock ownership.
  288.  *
  289.  * Direct handoff of reader/writer lock ownership
  290.  * to waiting readers or a writer.
  291.  *
  292.  * Must be called with rwl->lock locked.
  293.  * Must be called with interrupts_disable()'d.
  294.  *
  295.  * @param rwl Reader/Writer lock.
  296.  * @param readers_only See the description below.
  297.  *
  298.  * If readers_only is false: (unlock scenario)
  299.  * Let the first sleeper on 'exclusive' mutex in, no matter
  300.  * whether it is a reader or a writer. If there are more leading
  301.  * readers in line, let each of them in.
  302.  *
  303.  * Otherwise: (timeout scenario)
  304.  * Let all leading readers in.
  305.  */
  306. void let_others_in(rwlock_t *rwl, int readers_only)
  307. {
  308.     rwlock_type_t type = RWLOCK_NONE;
  309.     thread_t *t = NULL;
  310.     bool one_more = true;
  311.    
  312.     spinlock_lock(&rwl->exclusive.sem.wq.lock);
  313.  
  314.     if (!list_empty(&rwl->exclusive.sem.wq.head))
  315.         t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t, wq_link);
  316.     do {
  317.         if (t) {
  318.             spinlock_lock(&t->lock);
  319.             type = t->rwlock_holder_type;
  320.             spinlock_unlock(&t->lock);         
  321.         }
  322.    
  323.         /*
  324.          * If readers_only is true, we wake all leading readers
  325.          * if and only if rwl is locked by another reader.
  326.          * Assumption: readers_only ==> rwl->readers_in
  327.          */
  328.         if (readers_only && (type != RWLOCK_READER))
  329.             break;
  330.  
  331.  
  332.         if (type == RWLOCK_READER) {
  333.             /*
  334.              * Waking up a reader.
  335.              * We are responsible for incrementing rwl->readers_in for it.
  336.              */
  337.              rwl->readers_in++;
  338.         }
  339.  
  340.         /*
  341.          * Only the last iteration through this loop can increment
  342.          * rwl->exclusive.sem.wq.missed_wakeup's. All preceeding
  343.          * iterations will wake up a thread.
  344.          */
  345.         /* We call the internal version of waitq_wakeup, which
  346.          * relies on the fact that the waitq is already locked.
  347.          */
  348.         _waitq_wakeup_unsafe(&rwl->exclusive.sem.wq, WAKEUP_FIRST);
  349.        
  350.         t = NULL;
  351.         if (!list_empty(&rwl->exclusive.sem.wq.head)) {
  352.             t = list_get_instance(rwl->exclusive.sem.wq.head.next, thread_t, wq_link);
  353.             if (t) {
  354.                 spinlock_lock(&t->lock);
  355.                 if (t->rwlock_holder_type != RWLOCK_READER)
  356.                     one_more = false;
  357.                 spinlock_unlock(&t->lock); 
  358.             }
  359.         }
  360.     } while ((type == RWLOCK_READER) && t && one_more);
  361.  
  362.     spinlock_unlock(&rwl->exclusive.sem.wq.lock);
  363. }
  364.  
  365. /** Release spinlock callback
  366.  *
  367.  * This is a callback function invoked from the scheduler.
  368.  * The callback is registered in _rwlock_read_lock_timeout().
  369.  *
  370.  * @param arg Spinlock.
  371.  */
  372. void release_spinlock(void *arg)
  373. {
  374.     spinlock_unlock((spinlock_t *) arg);
  375. }
  376.