spinlock.c

Go to the documentation of this file.
00001 /*****************************************************************************************
00002  Copyright (c) 2002-2004 The UbixOS Project
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are
00006  permitted provided that the following conditions are met:
00007 
00008  Redistributions of source code must retain the above copyright notice, this list of
00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
00010  form must reproduce the above copyright notice, this list of conditions, the following
00011  disclaimer and the list of authors in the documentation and/or other materials provided
00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
00013  contributors may be used to endorse or promote products derived from this software
00014  without specific prior written permission.
00015 
00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026  $Id: spinlock_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
00027 
00028 *****************************************************************************************/
00029 
00030 #include <ubixos/spinlock.h>
00031 #include <ubixos/sched.h>
00032 
00033 void spinLockInit(spinLock_t *lock) {
00034   *lock = SPIN_LOCK_INITIALIZER;
00035   }
00036 
00037 void spinUnlock(spinLock_t *lock) {
00038   *lock = 0x0;
00039   /*
00040   register int unlocked;
00041   asm volatile(
00042     "xchgl %0, %1"
00043     : "=&r" (unlocked), "=m" (*lock) : "0" (0)
00044     );
00045   */
00046   }
00047 
00048 int spinTryLock(spinLock_t *lock) {
00049   register int locked;
00050   asm volatile("xchgl %0, %1"
00051     : "=&r" (locked), "=m" (*lock) : "0" (1)
00052     );
00053   return(!locked);
00054   }
00055 
00056 void spinLock(spinLock_t *lock) {
00057   while (!spinTryLock(lock))
00058   {
00059     while (*lock == 1)
00060         sched_yield();
00061   }
00062 }
00063 
00064 void spinLock_scheduler(spinLock_t *lock) {
00065   while (!spinTryLock(lock))
00066     while (*lock == 1);
00067   }
00068 
00069 
00070 int spinLockLocked(spinLock_t *lock) {
00071   return(*lock != 0);
00072   }
00073 
00074  
00075 /***
00076  END
00077  ***/
00078 

Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by  doxygen 1.4.7