spinlock.h

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 
00035 #ifndef __SPINLOCK_H__
00036 #define __SPINLOCK_H__
00037 
00038 #include <arch/types.h>
00039 #include <typedefs.h>
00040 #include <preemption.h>
00041 #include <atomic.h>
00042 #include <debug.h>
00043 
00044 #ifdef CONFIG_SMP
00045 struct spinlock {
00046 #ifdef CONFIG_DEBUG_SPINLOCK
00047         char *name;
00048 #endif
00049         atomic_t val;
00050 };
00051 
00052 /*
00053  * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
00054  * where the lock gets initialized in run time.
00055  */
00056 #define SPINLOCK_DECLARE(slname)        spinlock_t slname
00057 
00058 /*
00059  * SPINLOCK_INITIALIZE is to be used for statically allocated spinlocks.
00060  * It declares and initializes the lock.
00061  */
00062 #ifdef CONFIG_DEBUG_SPINLOCK
00063 #define SPINLOCK_INITIALIZE(slname)     \
00064         spinlock_t slname = {           \
00065                 .name = #slname,        \
00066                 .val = { 0 }            \
00067         }
00068 #else
00069 #define SPINLOCK_INITIALIZE(slname)     \
00070         spinlock_t slname = {           \
00071                 .val = { 0 }            \
00072         }
00073 #endif
00074 
00075 extern void spinlock_initialize(spinlock_t *sl, char *name);
00076 extern int spinlock_trylock(spinlock_t *sl);
00077 extern void spinlock_lock_debug(spinlock_t *sl);
00078 
00079 #ifdef CONFIG_DEBUG_SPINLOCK
00080 #  define spinlock_lock(x) spinlock_lock_debug(x)
00081 #else
00082 #  define spinlock_lock(x) atomic_lock_arch(&(x)->val)
00083 #endif
00084 
00091 static inline void spinlock_unlock(spinlock_t *sl)
00092 {
00093         ASSERT(atomic_get(&sl->val) != 0);
00094 
00095         /*
00096          * Prevent critical section code from bleeding out this way down.
00097          */
00098         CS_LEAVE_BARRIER();
00099         
00100         atomic_set(&sl->val,0);
00101         preemption_enable();
00102 }
00103 
00104 #else
00105 
00106 /* On UP systems, spinlocks are effectively left out. */
00107 #define SPINLOCK_DECLARE(name)
00108 #define SPINLOCK_INITIALIZE(name)
00109 
00110 #define spinlock_initialize(x,name)
00111 #define spinlock_lock(x)                preemption_disable()
00112 #define spinlock_trylock(x)             (preemption_disable(), 1)
00113 #define spinlock_unlock(x)              preemption_enable()
00114 
00115 #endif
00116 
00117 #endif
00118 

Generated on Sun Jun 18 17:17:05 2006 for HelenOS Kernel (ppc32) by  doxygen 1.4.6