/usr/include/madness/world/atomicint.h is in libmadness-dev 0.10.1~gite4aa500e-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
*/
#ifndef MADNESS_WORLD_ATOMICINT_H__INCLUDED
#define MADNESS_WORLD_ATOMICINT_H__INCLUDED
/**
\file atomicint.h
\brief Implements \c AtomicInt.
\ingroup atomics
*/
#include <madness/madness_config.h>
/// \addtogroup atomics
/// @{
/* Jeff: I suppose there is a cleaner way to do this with preprocess arithmetic,
* but I do not think it matters. This code is clear enough. */
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
# if (__GNUC__ >= 5) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
# define MADNESS_GCC_48_OR_HIGHER
# endif
#endif
#if defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__)
# if (__clang_major__ >= 4) || (__clang_major__ == 3 && __clang_minor__ >= 3)
# define MADNESS_CLANG_33_OR_HIGHER
# endif
#endif
#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1300)
#define MADNESS_ICC_130_OR_HIGHER
#endif
/* Jeff: This needs to move into config.h and have an associated configure test. */
/* Jeff: Nick does not want this used on Blue Gene/Q, so we will not allow it
* even if other tests indicate that it should work. */
#if !defined(__bgq__) && (__cplusplus >= 201103L)
# if defined(MADNESS_GCC_48_OR_HIGHER) || defined(MADNESS_GCC_48_OR_HIGHER) || defined(MADNESS_CLANG_33_OR_HIGHER)
# define HAVE_CXX_ATOMICS
# endif
#endif
#ifdef MADNESS_GCC_48_OR_HIGHER
#undef MADNESS_GCC_48_OR_HIGHER
#endif
#ifdef MADNESS_ICC_130_OR_HIGHER
#undef MADNESS_ICC_130_OR_HIGHER
#endif
#ifdef MADNESS_CLANG_33_OR_HIGHER
#undef MADNESS_CLANG_33_OR_HIGHER
#endif
#if defined(HAVE_CXX_ATOMICS)
# define MADATOMIC_USE_CXX
/* Jeff: It is odd that MADATOMIC_USE_GCC is tested before it is defined,
* at least in this file. Is this intentional? */
#elif defined(HAVE_IBMBGP) && !defined(MADATOMIC_USE_GCC)
# define MADATOMIC_USE_BGP
#elif defined(HAVE_IBMBGQ)
# define MADATOMIC_USE_BGQ
#elif defined(USE_X86_32_ASM) || defined(USE_X86_64_ASM) || defined(X86_64) || defined(X86_32)
# define MADATOMIC_USE_X86_ASM
#else
# define MADATOMIC_USE_GCC
#endif
#if defined(MADATOMIC_USE_CXX)
# include <atomic>
#elif defined(MADATOMIC_USE_BGP)
# include <bpcore/bgp_atomic_ops.h>
#elif defined (MADATOMIC_USE_BGQ)
# include "bgq_atomics.h"
#elif defined(MADATOMIC_USE_AIX)
# include <sys/atomic_op.h>
#elif defined(MADATOMIC_USE_GCC)
# ifdef GCC_ATOMICS_IN_BITS
# include <bits/atomicity.h>
# else
# include <ext/atomicity.h>
# endif
#endif
namespace madness {
/// \brief An integer with atomic set, get, read+increment, read+decrement,
/// and decrement+test operations.
/// Only the default constructor is available and it does \emph not
/// initialize the variable.
///
/// Consciously modeled after the TBB API to prepare for switching to it.
/// \todo Should we actually switch to the TBB version?
class AtomicInt {
private:
/// Storage type for the atomic integer.
#if defined(MADATOMIC_USE_CXX)
typedef std::atomic_int atomic_int;
#elif defined(MADATOMIC_USE_BGP)
typedef _BGP_Atomic atomic_int;
#elif defined(MADATOMIC_USE_BGQ)
/* It might be prudent to add the aligned/padding attribute
* here, for performance, similar to what _BGP_Atomic does. */
typedef volatile int atomic_int;
#else
typedef volatile int atomic_int;
#endif
atomic_int value; ///< The atomic integer.
/// \todo Brief description needed.
/// \todo Descriptions needed.
/// \param[in] i Description needed.
/// \return Description needed.
inline int exchange_and_add(int i) {
#if defined(MADATOMIC_USE_CXX)
return value.fetch_add(i,std::memory_order_seq_cst);
#elif defined(MADATOMIC_USE_GCC)
return __gnu_cxx::__exchange_and_add(&value,i);
#elif defined(MADATOMIC_USE_X86_ASM)
__asm__ __volatile__("lock; xaddl %0,%1" :"=r"(i) : "m"(value), "0"(i));
return i;
#elif defined(MADATOMIC_USE_AIX)
return fetch_and_add(&value,i);
#elif defined(MADATOMIC_USE_BGP)
return _bgp_fetch_and_add(&value,i);
#elif defined(MADATOMIC_USE_BGQ)
return FetchAndAddSigned32(&value,i);
#else
# error ... atomic exchange_and_add operator must be implemented for this platform;
#endif
}
public:
/// \brief Returns the value of the counter with fence, ensuring
/// subsequent operations are not moved before the load.
operator int() const volatile {
/* Jeff moved the memory barrier inside of the architecture-specific blocks
* since it may be required to use a heavier hammer on some of them. */
#if defined(MADATOMIC_USE_CXX)
return value.load(std::memory_order_seq_cst);
#elif defined(MADATOMIC_USE_BGP)
int result = value.atom;
__asm__ __volatile__ ("" : : : "memory");
return result;
#elif defined (MADATOMIC_USE_BGQ)
int result = value;
__asm__ __volatile__ ("" : : : "memory");
return result;
#else
int result = value;
// BARRIER to stop instructions migrating up
__asm__ __volatile__ ("" : : : "memory");
return result;
#endif
}
/// Sets the value of the counter, with a fence ensuring that preceding
/// operations are not moved after the store.
/// \todo Descriptions needed.
/// \param[in] other Description needed.
/// \return Description needed.
int operator=(int other) {
/* Jeff moved the memory barrier inside of the architecture-specific blocks
* since it may be required to use a heavier hammer on some of them. */
#if defined(MADATOMIC_USE_CXX)
value.store(other,std::memory_order_seq_cst);
#elif defined(MADATOMIC_USE_BGP)
// BARRIER to stop instructions migrating down
__asm__ __volatile__ ("" : : : "memory");
value.atom = other;
#elif defined (MADATOMIC_USE_BGQ)
__asm__ __volatile__ ("" : : : "memory");
value = other;
#else
__asm__ __volatile__ ("" : : : "memory");
value = other;
#endif
return other;
}
/// \brief Sets the value of the counter, with fences to ensure that
/// operations are not moved to either side of the load+store.
/// \param[in] other The value to set to.
/// \return This \c AtomicInt.
AtomicInt& operator=(const AtomicInt& other) {
*this = int(other);
return *this;
}
/// Decrements the counter and returns the original value.
/// \return The original value.
int operator--(int) {
#if defined(MADATOMIC_USE_CXX)
return value--;
#else
return exchange_and_add(-1);
#endif
}
/// Decrements the counter and returns the decremented value.
/// \return The decremented value.
int operator--() {
#if defined(MADATOMIC_USE_CXX)
return --value;
#else
return exchange_and_add(-1) - 1;
#endif
}
/// Increments the counter and returns the original value.
/// \return The original value.
int operator++(int) {
#if defined(MADATOMIC_USE_CXX)
return value++;
#else
return exchange_and_add(1);
#endif
}
/// Increments the counter and returns the incremented value.
/// \return The incremented value.
int operator++() {
#if defined(MADATOMIC_USE_CXX)
return ++value;
#else
return exchange_and_add(1) + 1;
#endif
}
/// Add \c value and return the new value.
/// \param[in] inc The value to be added.
/// \return The new value.
int operator+=(const int inc) {
#if defined(MADATOMIC_USE_CXX)
return (value.fetch_add(inc, std::memory_order_seq_cst) + inc);
#else
return exchange_and_add(inc) + inc;
#endif
}
/// Subtract \c dec and return the new value.
/// \param[in] dec The value to be subtracted.
/// \return The new value.
int operator-=(const int dec) {
#if defined(MADATOMIC_USE_CXX)
return (value.fetch_sub(dec, std::memory_order_seq_cst) - dec);
#else
return exchange_and_add(-dec) - dec;
#endif
}
/// Decrements the counter and returns true if the new value is zero,
/// \return True if the decremented value is 0; false otherwise.
bool dec_and_test() {
return ((*this)-- == 1);
}
#ifdef ATOMICINT_CAS
/// Compare and swap.
/// If `value == compare` then set `value = newval`.
/// \param[in] compare The value to compare against.
/// \param[in] newval The new value if the comparison is true.
/// \return The original value.
inline int compare_and_swap(int compare, int newval) {
#if defined(MADATOMIC_USE_CXX)
#warning The C++11 implementation of compare-and-swap has never been tested.
std::bool swapped = value.compare_exchange_strong_explicit(&compare, newval,
std::memory_order_seq_cst,
std::memory_order_seq_cst);
/* We must return the original value, which is not the return argument above.
* If swapped=true, then obj was expected before the call, so we return that.
* If swapped=false, then obj is unchanged, so we can just return that. */
return (swapped ? compare : value);
#elif defined(MADATOMIC_USE_GCC)
return __sync_val_compare_and_swap(&value, compare, newval);
#elif defined(MADATOMIC_USE_BGP)
return _bgp_compare_and_swap(&value, compare, newval);
#elif defined(MADATOMIC_USE_BGQ)
return CompareAndSwapSigned32(&value, compare, newval);
#else
#error ... atomic exchange_and_add operator must be implemented for this platform;
#endif
}
#endif
}; // class AtomicInt
}
/// @}
#endif // MADNESS_WORLD_ATOMICINT_H__INCLUDED
|