Jack2 1.9.6

JackAtomic_os.h

00001 /*
00002 Copyright (C) 2004-2008 Grame
00003 
00004 This program is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU Lesser General Public License as published by
00006 the Free Software Foundation; either version 2.1 of the License, or
00007 (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU Lesser General Public License for more details.
00013 
00014 You should have received a copy of the GNU Lesser General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 
00018 */
00019 
00020 #ifndef __JackAtomic_linux__
00021 #define __JackAtomic_linux__
00022 
00023 #include "JackTypes.h"
00024 
00025 #ifdef __PPC__
00026 
00027 static inline int CAS(register UInt32 value, register UInt32 newvalue, register volatile void* addr)
00028 {
00029     register int result;
00030     register UInt32 tmp;
00031     asm volatile (
00032         "# CAS                                  \n"
00033         "       lwarx   %4, 0, %1       \n"         // creates a reservation on addr
00034         "       cmpw    %4, %2          \n"        //  test value at addr
00035         "       bne-    1f          \n"
00036         "       sync                    \n"         //  synchronize instructions
00037         "       stwcx.  %3, 0, %1       \n"         //  if the reservation is not altered
00038         //  stores the new value at addr
00039         "       bne-    1f          \n"
00040         "   li      %0, 1       \n"
00041         "       b               2f          \n"
00042         "1:                     \n"
00043         "   li      %0, 0       \n"
00044         "2:                     \n"
00045         : "=r" (result)
00046         : "r" (addr), "r" (value), "r" (newvalue), "r" (tmp)
00047         );
00048     return result;
00049 }
00050 
00051 #elif defined(__i386__) || defined(__x86_64__)
00052 
00053 #define LOCK "lock ; "
00054 
00055 static inline char CAS(volatile UInt32 value, UInt32 newvalue, volatile void* addr)
00056 {
00057     register char ret;
00058     __asm__ __volatile__ (
00059         "# CAS \n\t"
00060         LOCK "cmpxchg %2, (%1) \n\t"
00061         "sete %0               \n\t"
00062         : "=a" (ret)
00063         : "c" (addr), "d" (newvalue), "a" (value)
00064         );
00065     return ret;
00066 }
00067 
00068 #else
00069 
00070 static inline bool CAS(volatile UInt32 value, volatile UInt32 newvalue,
00071     volatile void *addr)
00072 {
00073         return __sync_bool_compare_and_swap((volatile UInt32 *)addr,
00074                value, newvalue);
00075 }
00076 
00077 #endif
00078 
00079 #if !defined(__i386__) && !defined(__x86_64__)  && !defined(__PPC__)
00080 #warning using builtin gcc (version > 4.1) atomic
00081 
00082 static inline char CAS(volatile UInt32 value, UInt32 newvalue, volatile void* addr)
00083 {
00084     return __sync_bool_compare_and_swap (&addr, value, newvalue);
00085 }
00086 #endif
00087 
00088 
00089 #endif
00090