CrystalSpace

Public API Reference

csutil/threading/atomicops_gcc_ppc.h

00001 /*
00002   Copyright (C) 2006 by Marten Svanfeldt
00003 
00004   This library is free software; you can redistribute it and/or
00005   modify it under the terms of the GNU Lesser General Public
00006   License as published by the Free Software Foundation; either
00007   version 2 of the License, or (at your option) any later version.
00008 
00009   This library 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 GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public
00015   License along with this library; if not, write to the Free
00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_CSUTIL_ATOMICOPS_GCC_PPC_H__
00020 #define __CS_CSUTIL_ATOMICOPS_GCC_PPC_H__
00021 
00022 #ifndef DOXYGEN_RUN
00023 
00024 namespace CS
00025 {
00026 namespace Threading
00027 {
00028   class AtomicOperationsPPCGCC
00029   {
00030   public:
00031     inline static int32 Set (int32* target, int32 value)
00032     {
00033       __asm__ __volatile__
00034         (
00035         "       lwsync \n"
00036         "1:     lwarx   %0,0,%2 \n"
00037         "       dcbt     0,%2 \n"
00038         "       stwcx.  %3,0,%2 \n"
00039         "       bne-    1b\n"
00040         "       isync \n"
00041         : "=&r" (value), "=m" (*(unsigned int *)target)
00042         : "r" (target), "r" (value), "m" (*(unsigned int *)target)
00043         : "cc", "memory"
00044         );
00045       return value;
00046     }
00047 
00048     inline static void* Set (void** target, void* value)
00049     {
00050       return (void*)Set ((int32*)target, (int32)value);
00051     }
00052 
00053     inline static int32 CompareAndSet (int32* target, int32 value,
00054       int32 comparand)
00055     {
00056       int32 prev;
00057 
00058       __asm__ __volatile__ (
00059       "       lwsync \n"
00060       "1:     lwarx   %0,0,%2\n"
00061       "       cmpw    0,%0,%3\n"
00062       "       bne-    2f\n"
00063       "       dcbt     0,%2 \n"
00064       "       stwcx.  %4,0,%2\n"
00065       "       bne-    1b\n"
00066       "       isync     \n"
00067       "2:"
00068       : "=&r" (prev), "=m" (*target)
00069       : "r" (target), "r" (comparand), "r" (value), "m" (*target)
00070       : "cc", "memory");
00071       return prev;
00072     }
00073 
00074     inline static void* CompareAndSet (void** target, void* value,
00075       void* comparand)
00076     {
00077       return (void*)CompareAndSet ((int32*)target, (int32)value, 
00078         (int32)comparand);
00079     }
00080 
00081     inline static int32 Increment (int32* target, int32 incr = 1)
00082     {
00083       //@@Potentially dangerous code, needs to be revisited
00084       int32 prevValue, currValue, nextValue;
00085       do 
00086       {
00087         currValue = *target;
00088         nextValue = currValue + incr;
00089         prevValue = CompareAndSet (target, nextValue, currValue);
00090       } while(prevValue == currValue);
00091       return nextValue;
00092     }
00093 
00094     inline static int32 Decrement (int32* target)
00095     {
00096       return (int32)Increment (target, -1);
00097     }
00098   };
00099 }
00100 }
00101 
00102 #endif // DOXYGEN_RUN
00103 
00104 #endif // __CS_CSUTIL_ATOMICOPS_GCC_PPC_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3