os161-1.99
 All Data Structures
spinlock.h
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
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  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #ifndef _MIPS_SPINLOCK_H_
00031 #define _MIPS_SPINLOCK_H_
00032 
00033 #include <cdefs.h>
00034 
00035 
00036 /* Type of value needed to actually spin on */
00037 typedef unsigned spinlock_data_t;
00038 
00039 /* Initializer for use by SPINLOCK_INITIALIZER */
00040 #define SPINLOCK_DATA_INITIALIZER       0
00041 
00042 /* Atomic operations on spinlock_data_t */
00043 void spinlock_data_set(volatile spinlock_data_t *sd, unsigned val);
00044 spinlock_data_t spinlock_data_get(volatile spinlock_data_t *sd);
00045 spinlock_data_t spinlock_data_testandset(volatile spinlock_data_t *sd);
00046 
00047 ////////////////////////////////////////////////////////////
00048 
00049 SPINLOCK_INLINE
00050 void
00051 spinlock_data_set(volatile spinlock_data_t *sd, unsigned val)
00052 {
00053         *sd = val;
00054 }
00055 
00056 SPINLOCK_INLINE
00057 spinlock_data_t
00058 spinlock_data_get(volatile spinlock_data_t *sd)
00059 {
00060         return *sd;
00061 }
00062 
00063 SPINLOCK_INLINE
00064 spinlock_data_t
00065 spinlock_data_testandset(volatile spinlock_data_t *sd)
00066 {
00067         spinlock_data_t x;
00068         spinlock_data_t y;
00069 
00070         /*
00071          * Test-and-set using LL/SC.
00072          *
00073          * Load the existing value into X, and use Y to store 1.
00074          * After the SC, Y contains 1 if the store succeeded,
00075          * 0 if it failed.
00076          *
00077          * On failure, return 1 to pretend that the spinlock
00078          * was already held.
00079          */
00080 
00081         y = 1;
00082         __asm volatile(
00083                 ".set push;"            /* save assembler mode */
00084                 ".set mips32;"          /* allow MIPS32 instructions */
00085                 ".set volatile;"        /* avoid unwanted optimization */
00086                 "ll %0, 0(%2);"         /*   x = *sd */
00087                 "sc %1, 0(%2);"         /*   *sd = y; y = success? */
00088                 ".set pop"              /* restore assembler mode */
00089                 : "=r" (x), "+r" (y) : "r" (sd));
00090         if (y == 0) {
00091                 return 1;
00092         }
00093         return x;
00094 }
00095 
00096 
00097 #endif /* _MIPS_SPINLOCK_H_ */
 All Data Structures