00001 #ifndef _XRDSYSATOMICS_ 00002 #define _XRDSYSATOMICS_ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d S y s A t o m i c s . h h */ 00006 /* */ 00007 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /* */ 00012 /* This file is part of the XRootD software suite. */ 00013 /* */ 00014 /* XRootD is free software: you can redistribute it and/or modify it under */ 00015 /* the terms of the GNU Lesser General Public License as published by the */ 00016 /* Free Software Foundation, either version 3 of the License, or (at your */ 00017 /* option) any later version. */ 00018 /* */ 00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */ 00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 00021 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */ 00022 /* License for more details. */ 00023 /* */ 00024 /* You should have received a copy of the GNU Lesser General Public License */ 00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */ 00026 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */ 00027 /* */ 00028 /* The copyright holder's institutional names and contributor's names may not */ 00029 /* be used to endorse or promote products derived from this software without */ 00030 /* specific prior written permission of the institution or contributor. */ 00031 /******************************************************************************/ 00032 00033 /* The following instruction acronyms are used: 00034 AtomicCAS() -> Compare And [if equal] Set 00035 AtomicFAZ() -> Fetch And Zero 00036 */ 00037 00038 /* Portability note: When using fetch-add or fetch-sub in the context of an 00039 assignment statement, you must use the the AtomicFAdd 00040 and AtomicFSub macros to ensure portability and correct 00041 results. Additionally, whenever you use AtomicFAZ as the 00042 only consequent of an if-else statement you *must* places 00043 braces around it otherwise the code will not be portable! 00044 Alternatively, always use the AtomicFZAP macro. 00045 */ 00046 00047 #ifdef HAVE_ATOMICS 00048 #define AtomicBeg(Mtx) 00049 #define AtomicEnd(Mtx) 00050 #define AtomicAdd(x, y) __sync_fetch_and_add(&x, y) 00051 #define AtomicFAdd(w,x,y) w = __sync_fetch_and_add(&x, y) 00052 #define AtomicCAS(x, y, z) __sync_bool_compare_and_swap(&x, y, z) 00053 #define AtomicDec(x) __sync_fetch_and_sub(&x, 1) 00054 #define AtomicFAZ(x) __sync_fetch_and_and(&x, 0) 00055 #define AtomicFZAP(w,x) w = __sync_fetch_and_and(&x, 0) 00056 #define AtomicGet(x) __sync_fetch_and_or(&x, 0) 00057 #define AtomicInc(x) __sync_fetch_and_add(&x, 1) 00058 #define AtomicSub(x, y) __sync_fetch_and_sub(&x, y) 00059 #define AtomicFSub(w,x,y) w = __sync_fetch_and_sub(&x, y) 00060 #define AtomicZAP(x) __sync_fetch_and_and(&x, 0) 00061 #define AtomicRet(mtx, x) return AtomicGet(x) 00062 #else 00063 #define AtomicBeg(Mtx) Mtx.Lock() 00064 #define AtomicEnd(Mtx) Mtx.UnLock() 00065 #define AtomicAdd(x, y) x += y // When assigning use AtomicFAdd! 00066 #define AtomicFAdd(w,x,y) {w = x; x += y;} 00067 #define AtomicCAS(x, y, z) if (x == y) x = z 00068 #define AtomicDec(x) x-- 00069 #define AtomicFAZ(x) x; x = 0 // Braces when used with if-else! 00070 #define AtomicFZAP(w,x) {w = x; x = 0;} 00071 #define AtomicGet(x) x 00072 #define AtomicInc(x) x++ 00073 #define AtomicSub(x, y) x -= y // When assigning use AtomicFSub! 00074 #define AtomicFSub(w,x,y) {w = x; x -= y;} 00075 #define AtomicZAP(x) x = 0 00076 #define AtomicRet(mtx, x) {mtx.Lock(); int _ ## x = x; \ 00077 mtx.UnLock(); return _ ## x;} 00078 #endif 00079 #endif 00080 00081 /* 00082 * The following definitions give a mechanism for using C++ atomics 00083 * (when using at least C++11). *Note* that these can't be relied 00084 * on for correct behavior as they are non-atomic for C++03 compilers. 00085 * 00086 * Only use them for standards correctness (eliminating C++11 undefined 00087 * behavior). 00088 */ 00089 #if __cplusplus >= 201103L 00090 #include <atomic> 00091 #define CPP_ATOMIC_LOAD(x, order) x.load(order) 00092 #define CPP_ATOMIC_STORE(x, val, order) x.store(val, order) 00093 #define CPP_ATOMIC_TYPE(kind) std::atomic<kind> 00094 #else 00095 #define CPP_ATOMIC_LOAD(x, order) x 00096 #define CPP_ATOMIC_STORE(x, val, order) x = val 00097 #define CPP_ATOMIC_TYPE(kind) kind 00098 #endif 00099 00100