GNU Radio 3.3.0 C++ API
atomic.h
Go to the documentation of this file.
00001 /* --------------------------------------------------------------  */
00002 /* (C)Copyright 2001,2007,                                         */
00003 /* International Business Machines Corporation,                    */
00004 /* Sony Computer Entertainment, Incorporated,                      */
00005 /* Toshiba Corporation,                                            */
00006 /*                                                                 */
00007 /* All Rights Reserved.                                            */
00008 /*                                                                 */
00009 /* Redistribution and use in source and binary forms, with or      */
00010 /* without modification, are permitted provided that the           */
00011 /* following conditions are met:                                   */
00012 /*                                                                 */
00013 /* - Redistributions of source code must retain the above copyright*/
00014 /*   notice, this list of conditions and the following disclaimer. */
00015 /*                                                                 */
00016 /* - Redistributions in binary form must reproduce the above       */
00017 /*   copyright notice, this list of conditions and the following   */
00018 /*   disclaimer in the documentation and/or other materials        */
00019 /*   provided with the distribution.                               */
00020 /*                                                                 */
00021 /* - Neither the name of IBM Corporation nor the names of its      */
00022 /*   contributors may be used to endorse or promote products       */
00023 /*   derived from this software without specific prior written     */
00024 /*   permission.                                                   */
00025 /*                                                                 */
00026 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND          */
00027 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,     */
00028 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF        */
00029 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE        */
00030 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR            */
00031 /* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    */
00032 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT    */
00033 /* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;    */
00034 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)        */
00035 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN       */
00036 /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR    */
00037 /* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  */
00038 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              */
00039 /* --------------------------------------------------------------  */
00040 /* PROLOG END TAG zYx                                              */
00041 /*
00042  * atomic.h - PPE atomic SHM counter operations.
00043  *
00044  * Interfaces patterned after, and hopefully compatible
00045  * with PowerPC64-Linux atomic counter operations.  Uses
00046  * 32b values for various counters.
00047  */
00048 #ifndef _PPU_ATOMIC_H_
00049 #define _PPU_ATOMIC_H_
00050 
00051 #include <ppu_intrinsics.h>
00052 #include "sync_utils.h"
00053 
00054 typedef unsigned int atomic_t __attribute__ ((aligned (128))); 
00055 
00056 
00057 /* atomic_ea_t is a 64bit effective address  that points to 
00058  * an atomic_t variable 
00059  */
00060 typedef unsigned long long atomic_ea_t;
00061 
00062 /**
00063  * ASSUMPTIONS:
00064  * On the PPE, the size of a reservation granule is 128 bytes
00065  * (a cache-line), so when a programmer puts a reservation on an
00066  * address, that whole cacheline is reserved. Therefore both
00067  * the PPE and SPE can participate in an atomic operation as long as
00068  * lwarx and getllar operate on the same cacheline. 
00069  */ 
00070 
00071 
00072 /*
00073  * atomically loads and replaces the value v with val. 
00074  * Returns the old value at v
00075  */ 
00076 static __inline int _atomic_replace(atomic_ea_t v, int val)
00077 {
00078   int old;
00079   void *p;
00080 
00081   SYNC_ULL_TO_PTR(v, p);
00082 
00083   do {
00084     old = (int)__lwarx(p);
00085   } while (__stwcx(p, (unsigned int)val) == 0);
00086 
00087   return old;
00088 }
00089 
00090 
00091 /*
00092  * atomically loads the value at v, adds val, replaces the
00093  * value at v with the sum. Returns the old value at v
00094  */ 
00095 static __inline int _atomic_modify(atomic_ea_t v, int val)
00096 {
00097   int oldval, newval;
00098   void *p;
00099 
00100   SYNC_ULL_TO_PTR(v, p);
00101 
00102   do {
00103     oldval = (int)__lwarx(p);
00104     newval = oldval + val;
00105   } while (__stwcx(p, (unsigned int)newval) == 0);
00106 
00107   return oldval;
00108 }
00109 
00110 
00111 #endif /* _PPU_ATOMIC_H_ */
00112