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_APPLE__
00021 #define __JackAtomic_APPLE__
00022 
00023 #include "JackTypes.h"
00024 
00025 #if defined(__ppc__) || defined(__ppc64__)
00026 
00027 static inline int CAS(register UInt32 value, register UInt32 newvalue, register volatile void* addr)
00028 {
00029     register int result;
00030     asm volatile (
00031         "# CAS                                  \n"
00032         "       lwarx   r0, 0, %1       \n"         // creates a reservation on addr
00033         "       cmpw    r0, %2          \n"                     //  test value at addr
00034         "       bne-    1f          \n"
00035         "       sync                    \n"         //  synchronize instructions
00036         "       stwcx.  %3, 0, %1       \n"         //  if the reservation is not altered
00037         //  stores the new value at addr
00038         "       bne-    1f          \n"
00039         "   li      %0, 1       \n"
00040         "       b               2f          \n"
00041         "1:                     \n"
00042         "   li      %0, 0       \n"
00043         "2:                     \n"
00044         : "=r" (result)
00045         : "r" (addr), "r" (value), "r" (newvalue)
00046         : "r0"
00047         );
00048     return result;
00049 }
00050 
00051 #endif
00052 
00053 #if defined(__i386__) || defined(__x86_64__)
00054 
00055 #define LOCK "lock ; "
00056 
00057 static inline char CAS(volatile UInt32 value, UInt32 newvalue, volatile void* addr)
00058 {
00059     register char ret;
00060     __asm__ __volatile__ (
00061         "# CAS \n\t"
00062         LOCK "cmpxchg %2, (%1) \n\t"
00063         "sete %0               \n\t"
00064         : "=a" (ret)
00065         : "c" (addr), "d" (newvalue), "a" (value)
00066         );
00067     return ret;
00068 }
00069 
00070 #endif
00071 
00072 #endif
00073