Subversion Repositories HelenOS-historic

Rev

Rev 1224 | Rev 1323 | 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. /**
  30.  * @file    spinlock.c
  31.  * @brief   Spinlocks.
  32.  */
  33.  
  34. #include <synch/spinlock.h>
  35. #include <atomic.h>
  36. #include <arch/barrier.h>
  37. #include <arch.h>
  38. #include <preemption.h>
  39. #include <print.h>
  40. #include <debug.h>
  41. #include <symtab.h>
  42.  
  43. #ifdef CONFIG_SMP
  44.  
  45. /** Initialize spinlock
  46.  *
  47.  * Initialize spinlock.
  48.  *
  49.  * @param sl Pointer to spinlock_t structure.
  50.  */
  51. void spinlock_initialize(spinlock_t *sl, char *name)
  52. {
  53.     atomic_set(&sl->val, 0);
  54. #ifdef CONFIG_DEBUG_SPINLOCK
  55.     sl->name = name;
  56. #endif 
  57. }
  58.  
  59. /** Lock spinlock
  60.  *
  61.  * Lock spinlock.
  62.  * This version has limitted ability to report
  63.  * possible occurence of deadlock.
  64.  *
  65.  * @param sl Pointer to spinlock_t structure.
  66.  */
  67. #ifdef CONFIG_DEBUG_SPINLOCK
  68. void spinlock_lock_debug(spinlock_t *sl)
  69. {
  70.     count_t i = 0;
  71.     char *symbol;
  72.     bool deadlock_reported = false;
  73.  
  74.     preemption_disable();
  75.     while (test_and_set(&sl->val)) {
  76.         if (i++ > 300000 && sl!=&printflock) {
  77.             printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p",
  78.                    CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER);
  79.             symbol = get_symtab_entry(CALLER);
  80.             if (symbol)
  81.                 printf("(%s)", symbol);
  82.             printf("\n");
  83.             i = 0;
  84.             deadlock_reported = true;
  85.         }
  86.     }
  87.  
  88.     if (deadlock_reported)
  89.         printf("cpu%d: not deadlocked\n", CPU->id);
  90.  
  91.     /*
  92.      * Prevent critical section code from bleeding out this way up.
  93.      */
  94.     CS_ENTER_BARRIER();
  95. }
  96. #endif
  97.  
  98. /** Lock spinlock conditionally
  99.  *
  100.  * Lock spinlock conditionally.
  101.  * If the spinlock is not available at the moment,
  102.  * signal failure.
  103.  *
  104.  * @param sl Pointer to spinlock_t structure.
  105.  *
  106.  * @return Zero on failure, non-zero otherwise.
  107.  */
  108. int spinlock_trylock(spinlock_t *sl)
  109. {
  110.     int rc;
  111.    
  112.     preemption_disable();
  113.     rc = !test_and_set(&sl->val);
  114.  
  115.     /*
  116.      * Prevent critical section code from bleeding out this way up.
  117.      */
  118.     CS_ENTER_BARRIER();
  119.  
  120.     if (!rc)
  121.         preemption_enable();
  122.    
  123.     return rc;
  124. }
  125.  
  126. #endif
  127.