spinlock.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2004 Jakub Jermar
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00038 #include <synch/spinlock.h>
00039 #include <atomic.h>
00040 #include <arch/barrier.h>
00041 #include <arch.h>
00042 #include <preemption.h>
00043 #include <print.h>
00044 #include <debug.h>
00045 #include <symtab.h>
00046 
00047 #ifdef CONFIG_FB
00048 #include <genarch/fb/fb.h>
00049 #endif
00050 
00051 #ifdef CONFIG_SMP
00052 
00059 void spinlock_initialize(spinlock_t *sl, char *name)
00060 {
00061         atomic_set(&sl->val, 0);
00062 #ifdef CONFIG_DEBUG_SPINLOCK
00063         sl->name = name;
00064 #endif  
00065 }
00066 
00075 #ifdef CONFIG_DEBUG_SPINLOCK
00076 #define DEADLOCK_THRESHOLD      100000000
00077 void spinlock_lock_debug(spinlock_t *sl)
00078 {
00079         count_t i = 0;
00080         char *symbol;
00081         bool deadlock_reported = false;
00082 
00083         preemption_disable();
00084         while (test_and_set(&sl->val)) {
00085 
00086                 /*
00087                  * We need to be careful about printflock and fb_lock.
00088                  * Both of them are used to report deadlocks via
00089                  * printf() and fb_putchar().
00090                  *
00091                  * We trust our code that there is no possible deadlock
00092                  * caused by these two locks (except when an exception
00093                  * is triggered for instance by printf() or fb_putchar()).
00094                  * However, we encountered false positives caused by very
00095                  * slow VESA framebuffer interaction (especially when
00096                  * run in a simulator) that caused problems with both
00097                  * printflock and fb_lock.
00098                  *
00099                  * Possible deadlocks on both printflock and fb_lock
00100                  * are therefore not reported as they would cause an
00101                  * infinite recursion.
00102                  */
00103                 if (sl == &printflock)
00104                         continue;
00105 #ifdef CONFIG_FB
00106                 if (sl == &fb_lock)
00107                         continue;
00108 #endif
00109                 if (i++ > DEADLOCK_THRESHOLD) {
00110                         printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p",
00111                                CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER);
00112                         symbol = get_symtab_entry(CALLER);
00113                         if (symbol)
00114                                 printf("(%s)", symbol);
00115                         printf("\n");
00116                         i = 0;
00117                         deadlock_reported = true;
00118                 }
00119         }
00120 
00121         if (deadlock_reported)
00122                 printf("cpu%d: not deadlocked\n", CPU->id);
00123 
00124         /*
00125          * Prevent critical section code from bleeding out this way up.
00126          */
00127         CS_ENTER_BARRIER();
00128 }
00129 #endif
00130 
00141 int spinlock_trylock(spinlock_t *sl)
00142 {
00143         int rc;
00144         
00145         preemption_disable();
00146         rc = !test_and_set(&sl->val);
00147 
00148         /*
00149          * Prevent critical section code from bleeding out this way up.
00150          */
00151         CS_ENTER_BARRIER();
00152 
00153         if (!rc)
00154                 preemption_enable();
00155         
00156         return rc;
00157 }
00158 
00159 #endif
00160 

Generated on Sun Jun 18 17:01:58 2006 for HelenOS Kernel (mips32) by  doxygen 1.4.6