annotate DEPENDENCIES/generic/include/boost/interprocess/detail/atomic.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2006-2012
Chris@16 4 // (C) Copyright Markus Schoepflin 2007
Chris@16 5 // (C) Copyright Bryce Lelbach 2010
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 8 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 9 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 10 //
Chris@16 11 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 12 //
Chris@16 13 //////////////////////////////////////////////////////////////////////////////
Chris@16 14
Chris@16 15 #ifndef BOOST_INTERPROCESS_DETAIL_ATOMIC_HPP
Chris@16 16 #define BOOST_INTERPROCESS_DETAIL_ATOMIC_HPP
Chris@16 17
Chris@16 18 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 19 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 20 #include <boost/cstdint.hpp>
Chris@16 21
Chris@16 22 namespace boost{
Chris@16 23 namespace interprocess{
Chris@16 24 namespace ipcdetail{
Chris@16 25
Chris@16 26 //! Atomically increment an boost::uint32_t by 1
Chris@16 27 //! "mem": pointer to the object
Chris@16 28 //! Returns the old value pointed to by mem
Chris@16 29 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem);
Chris@16 30
Chris@16 31 //! Atomically read an boost::uint32_t from memory
Chris@16 32 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem);
Chris@16 33
Chris@16 34 //! Atomically set an boost::uint32_t in memory
Chris@16 35 //! "mem": pointer to the object
Chris@16 36 //! "param": val value that the object will assume
Chris@16 37 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val);
Chris@16 38
Chris@16 39 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 40 //! If they are the same swap the value with "with"
Chris@16 41 //! "mem": pointer to the value
Chris@16 42 //! "with": what to swap it with
Chris@16 43 //! "cmp": the value to compare it to
Chris@16 44 //! Returns the old value of *mem
Chris@16 45 inline boost::uint32_t atomic_cas32
Chris@16 46 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp);
Chris@16 47
Chris@16 48 } //namespace ipcdetail{
Chris@16 49 } //namespace interprocess{
Chris@16 50 } //namespace boost{
Chris@16 51
Chris@16 52 #if (defined BOOST_INTERPROCESS_WINDOWS)
Chris@16 53
Chris@16 54 #include <boost/interprocess/detail/win32_api.hpp>
Chris@16 55
Chris@16 56 namespace boost{
Chris@16 57 namespace interprocess{
Chris@16 58 namespace ipcdetail{
Chris@16 59
Chris@16 60 //! Atomically decrement an boost::uint32_t by 1
Chris@16 61 //! "mem": pointer to the atomic value
Chris@16 62 //! Returns the old value pointed to by mem
Chris@16 63 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 64 { return winapi::interlocked_decrement(reinterpret_cast<volatile long*>(mem)) + 1; }
Chris@16 65
Chris@16 66 //! Atomically increment an apr_uint32_t by 1
Chris@16 67 //! "mem": pointer to the object
Chris@16 68 //! Returns the old value pointed to by mem
Chris@16 69 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 70 { return winapi::interlocked_increment(reinterpret_cast<volatile long*>(mem))-1; }
Chris@16 71
Chris@16 72 //! Atomically read an boost::uint32_t from memory
Chris@16 73 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 74 { return *mem; }
Chris@16 75
Chris@16 76 //! Atomically set an boost::uint32_t in memory
Chris@16 77 //! "mem": pointer to the object
Chris@16 78 //! "param": val value that the object will assume
Chris@16 79 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 80 { winapi::interlocked_exchange(reinterpret_cast<volatile long*>(mem), val); }
Chris@16 81
Chris@16 82 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 83 //! If they are the same swap the value with "with"
Chris@16 84 //! "mem": pointer to the value
Chris@16 85 //! "with": what to swap it with
Chris@16 86 //! "cmp": the value to compare it to
Chris@16 87 //! Returns the old value of *mem
Chris@16 88 inline boost::uint32_t atomic_cas32
Chris@16 89 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 90 { return winapi::interlocked_compare_exchange(reinterpret_cast<volatile long*>(mem), with, cmp); }
Chris@16 91
Chris@16 92 } //namespace ipcdetail{
Chris@16 93 } //namespace interprocess{
Chris@16 94 } //namespace boost{
Chris@16 95
Chris@16 96 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
Chris@16 97
Chris@16 98 namespace boost {
Chris@16 99 namespace interprocess {
Chris@16 100 namespace ipcdetail{
Chris@16 101
Chris@16 102 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 103 //! If they are the same swap the value with "with"
Chris@16 104 //! "mem": pointer to the value
Chris@16 105 //! "with" what to swap it with
Chris@16 106 //! "cmp": the value to compare it to
Chris@16 107 //! Returns the old value of *mem
Chris@16 108 inline boost::uint32_t atomic_cas32
Chris@16 109 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 110 {
Chris@16 111 boost::uint32_t prev = cmp;
Chris@16 112 // This version by Mans Rullgard of Pathscale
Chris@16 113 __asm__ __volatile__ ( "lock\n\t"
Chris@16 114 "cmpxchg %2,%0"
Chris@16 115 : "+m"(*mem), "+a"(prev)
Chris@16 116 : "r"(with)
Chris@16 117 : "cc");
Chris@16 118
Chris@16 119 return prev;
Chris@16 120 }
Chris@16 121
Chris@16 122 //! Atomically add 'val' to an boost::uint32_t
Chris@16 123 //! "mem": pointer to the object
Chris@16 124 //! "val": amount to add
Chris@16 125 //! Returns the old value pointed to by mem
Chris@16 126 inline boost::uint32_t atomic_add32
Chris@16 127 (volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 128 {
Chris@16 129 // int r = *pw;
Chris@16 130 // *mem += val;
Chris@16 131 // return r;
Chris@16 132 int r;
Chris@16 133
Chris@16 134 asm volatile
Chris@16 135 (
Chris@16 136 "lock\n\t"
Chris@16 137 "xadd %1, %0":
Chris@16 138 "+m"( *mem ), "=r"( r ): // outputs (%0, %1)
Chris@16 139 "1"( val ): // inputs (%2 == %1)
Chris@16 140 "memory", "cc" // clobbers
Chris@16 141 );
Chris@16 142
Chris@16 143 return r;
Chris@16 144 }
Chris@16 145
Chris@16 146 //! Atomically increment an apr_uint32_t by 1
Chris@16 147 //! "mem": pointer to the object
Chris@16 148 //! Returns the old value pointed to by mem
Chris@16 149 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 150 { return atomic_add32(mem, 1); }
Chris@16 151
Chris@16 152 //! Atomically decrement an boost::uint32_t by 1
Chris@16 153 //! "mem": pointer to the atomic value
Chris@16 154 //! Returns the old value pointed to by mem
Chris@16 155 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 156 { return atomic_add32(mem, (boost::uint32_t)-1); }
Chris@16 157
Chris@16 158 //! Atomically read an boost::uint32_t from memory
Chris@16 159 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 160 { return *mem; }
Chris@16 161
Chris@16 162 //! Atomically set an boost::uint32_t in memory
Chris@16 163 //! "mem": pointer to the object
Chris@16 164 //! "param": val value that the object will assume
Chris@16 165 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 166 { *mem = val; }
Chris@16 167
Chris@16 168 } //namespace ipcdetail{
Chris@16 169 } //namespace interprocess{
Chris@16 170 } //namespace boost{
Chris@16 171
Chris@16 172 #elif defined(__GNUC__) && (defined(__PPC__) || defined(__ppc__))
Chris@16 173
Chris@16 174 namespace boost {
Chris@16 175 namespace interprocess {
Chris@16 176 namespace ipcdetail{
Chris@16 177
Chris@16 178 //! Atomically add 'val' to an boost::uint32_t
Chris@16 179 //! "mem": pointer to the object
Chris@16 180 //! "val": amount to add
Chris@16 181 //! Returns the old value pointed to by mem
Chris@16 182 inline boost::uint32_t atomic_add32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 183 {
Chris@16 184 boost::uint32_t prev, temp;
Chris@16 185
Chris@16 186 asm volatile ("1:\n\t"
Chris@16 187 "lwarx %0,0,%2\n\t"
Chris@16 188 "add %1,%0,%3\n\t"
Chris@16 189 "stwcx. %1,0,%2\n\t"
Chris@16 190 "bne- 1b"
Chris@16 191 : "=&r" (prev), "=&r" (temp)
Chris@16 192 : "b" (mem), "r" (val)
Chris@16 193 : "cc", "memory");
Chris@16 194 return prev;
Chris@16 195 }
Chris@16 196
Chris@16 197 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 198 //! If they are the same swap the value with "with"
Chris@16 199 //! "mem": pointer to the value
Chris@16 200 //! "with" what to swap it with
Chris@16 201 //! "cmp": the value to compare it to
Chris@16 202 //! Returns the old value of *mem
Chris@16 203 inline boost::uint32_t atomic_cas32
Chris@16 204 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 205 {
Chris@16 206 boost::uint32_t prev;
Chris@16 207
Chris@16 208 asm volatile ("1:\n\t"
Chris@16 209 "lwarx %0,0,%1\n\t"
Chris@16 210 "cmpw %0,%3\n\t"
Chris@16 211 "bne- 2f\n\t"
Chris@16 212 "stwcx. %2,0,%1\n\t"
Chris@16 213 "bne- 1b\n\t"
Chris@16 214 "2:"
Chris@16 215 : "=&r"(prev)
Chris@16 216 : "b" (mem), "r" (with), "r" (cmp)
Chris@16 217 : "cc", "memory");
Chris@16 218 return prev;
Chris@16 219 }
Chris@16 220
Chris@16 221 //! Atomically increment an apr_uint32_t by 1
Chris@16 222 //! "mem": pointer to the object
Chris@16 223 //! Returns the old value pointed to by mem
Chris@16 224 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 225 { return atomic_add32(mem, 1); }
Chris@16 226
Chris@16 227 //! Atomically decrement an boost::uint32_t by 1
Chris@16 228 //! "mem": pointer to the atomic value
Chris@16 229 //! Returns the old value pointed to by mem
Chris@16 230 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 231 { return atomic_add32(mem, boost::uint32_t(-1u)); }
Chris@16 232
Chris@16 233 //! Atomically read an boost::uint32_t from memory
Chris@16 234 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 235 { return *mem; }
Chris@16 236
Chris@16 237 //! Atomically set an boost::uint32_t in memory
Chris@16 238 //! "mem": pointer to the object
Chris@16 239 //! "param": val value that the object will assume
Chris@16 240 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 241 { *mem = val; }
Chris@16 242
Chris@16 243 } //namespace ipcdetail{
Chris@16 244 } //namespace interprocess{
Chris@16 245 } //namespace boost{
Chris@16 246
Chris@16 247 #elif (defined(sun) || defined(__sun))
Chris@16 248
Chris@16 249 #include <atomic.h>
Chris@16 250
Chris@16 251 namespace boost{
Chris@16 252 namespace interprocess{
Chris@16 253 namespace ipcdetail{
Chris@16 254
Chris@16 255 //! Atomically add 'val' to an boost::uint32_t
Chris@16 256 //! "mem": pointer to the object
Chris@16 257 //! "val": amount to add
Chris@16 258 //! Returns the old value pointed to by mem
Chris@16 259 inline boost::uint32_t atomic_add32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 260 { return atomic_add_32_nv(reinterpret_cast<volatile ::uint32_t*>(mem), (int32_t)val) - val; }
Chris@16 261
Chris@16 262 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 263 //! If they are the same swap the value with "with"
Chris@16 264 //! "mem": pointer to the value
Chris@16 265 //! "with" what to swap it with
Chris@16 266 //! "cmp": the value to compare it to
Chris@16 267 //! Returns the old value of *mem
Chris@16 268 inline boost::uint32_t atomic_cas32
Chris@16 269 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 270 { return atomic_cas_32(reinterpret_cast<volatile ::uint32_t*>(mem), cmp, with); }
Chris@16 271
Chris@16 272 //! Atomically increment an apr_uint32_t by 1
Chris@16 273 //! "mem": pointer to the object
Chris@16 274 //! Returns the old value pointed to by mem
Chris@16 275 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 276 { return atomic_add_32_nv(reinterpret_cast<volatile ::uint32_t*>(mem), 1) - 1; }
Chris@16 277
Chris@16 278 //! Atomically decrement an boost::uint32_t by 1
Chris@16 279 //! "mem": pointer to the atomic value
Chris@16 280 //! Returns the old value pointed to by mem
Chris@16 281 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 282 { return atomic_add_32_nv(reinterpret_cast<volatile ::uint32_t*>(mem), (boost::uint32_t)-1) + 1; }
Chris@16 283
Chris@16 284 //! Atomically read an boost::uint32_t from memory
Chris@16 285 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 286 { return *mem; }
Chris@16 287
Chris@16 288 //! Atomically set an boost::uint32_t in memory
Chris@16 289 //! "mem": pointer to the object
Chris@16 290 //! "param": val value that the object will assume
Chris@16 291 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 292 { *mem = val; }
Chris@16 293
Chris@16 294 } //namespace ipcdetail{
Chris@16 295 } //namespace interprocess{
Chris@16 296 } //namespace boost{
Chris@16 297
Chris@16 298 #elif defined(__osf__) && defined(__DECCXX)
Chris@16 299
Chris@16 300 #include <machine/builtins.h>
Chris@16 301 #include <c_asm.h>
Chris@16 302
Chris@16 303 namespace boost{
Chris@16 304 namespace interprocess{
Chris@16 305 namespace ipcdetail{
Chris@16 306
Chris@16 307 //! Atomically decrement a uint32_t by 1
Chris@16 308 //! "mem": pointer to the atomic value
Chris@16 309 //! Returns the old value pointed to by mem
Chris@16 310 //! Acquire, memory barrier after decrement.
Chris@16 311 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 312 { boost::uint32_t old_val = __ATOMIC_DECREMENT_LONG(mem); __MB(); return old_val; }
Chris@16 313
Chris@16 314 //! Atomically increment a uint32_t by 1
Chris@16 315 //! "mem": pointer to the object
Chris@16 316 //! Returns the old value pointed to by mem
Chris@16 317 //! Release, memory barrier before increment.
Chris@16 318 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 319 { __MB(); return __ATOMIC_INCREMENT_LONG(mem); }
Chris@16 320
Chris@16 321 // Rational for the implementation of the atomic read and write functions.
Chris@16 322 //
Chris@16 323 // 1. The Alpha Architecture Handbook requires that access to a byte,
Chris@16 324 // an aligned word, an aligned longword, or an aligned quadword is
Chris@16 325 // atomic. (See 'Alpha Architecture Handbook', version 4, chapter 5.2.2.)
Chris@16 326 //
Chris@16 327 // 2. The CXX User's Guide states that volatile quantities are accessed
Chris@16 328 // with single assembler instructions, and that a compilation error
Chris@16 329 // occurs when declaring a quantity as volatile which is not properly
Chris@16 330 // aligned.
Chris@16 331
Chris@16 332 //! Atomically read an boost::uint32_t from memory
Chris@16 333 //! Acquire, memory barrier after load.
Chris@16 334 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 335 { boost::uint32_t old_val = *mem; __MB(); return old_val; }
Chris@16 336
Chris@16 337 //! Atomically set an boost::uint32_t in memory
Chris@16 338 //! "mem": pointer to the object
Chris@16 339 //! "param": val value that the object will assume
Chris@16 340 //! Release, memory barrier before store.
Chris@16 341 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 342 { __MB(); *mem = val; }
Chris@16 343
Chris@16 344 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 345 //! If they are the same swap the value with "with"
Chris@16 346 //! "mem": pointer to the value
Chris@16 347 //! "with" what to swap it with
Chris@16 348 //! "cmp": the value to compare it to
Chris@16 349 //! Returns the old value of *mem
Chris@16 350 //! Memory barrier between load and store.
Chris@16 351 inline boost::uint32_t atomic_cas32(
Chris@16 352 volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 353 {
Chris@16 354 // Note:
Chris@16 355 //
Chris@16 356 // Branch prediction prefers backward branches, and the Alpha Architecture
Chris@16 357 // Handbook explicitely states that the loop should not be implemented like
Chris@16 358 // it is below. (See chapter 4.2.5.) Therefore the code should probably look
Chris@16 359 // like this:
Chris@16 360 //
Chris@16 361 // return asm(
Chris@16 362 // "10: ldl_l %v0,(%a0) ;"
Chris@16 363 // " cmpeq %v0,%a2,%t0 ;"
Chris@16 364 // " beq %t0,20f ;"
Chris@16 365 // " mb ;"
Chris@16 366 // " mov %a1,%t0 ;"
Chris@16 367 // " stl_c %t0,(%a0) ;"
Chris@16 368 // " beq %t0,30f ;"
Chris@16 369 // "20: ret ;"
Chris@16 370 // "30: br 10b;",
Chris@16 371 // mem, with, cmp);
Chris@16 372 //
Chris@16 373 // But as the compiler always transforms this into the form where a backward
Chris@16 374 // branch is taken on failure, we can as well implement it in the straight
Chris@16 375 // forward form, as this is what it will end up in anyway.
Chris@16 376
Chris@16 377 return asm(
Chris@16 378 "10: ldl_l %v0,(%a0) ;" // load prev value from mem and lock mem
Chris@16 379 " cmpeq %v0,%a2,%t0 ;" // compare with given value
Chris@16 380 " beq %t0,20f ;" // if not equal, we're done
Chris@16 381 " mb ;" // memory barrier
Chris@16 382 " mov %a1,%t0 ;" // load new value into scratch register
Chris@16 383 " stl_c %t0,(%a0) ;" // store new value to locked mem (overwriting scratch)
Chris@16 384 " beq %t0,10b ;" // store failed because lock has been stolen, retry
Chris@16 385 "20: ",
Chris@16 386 mem, with, cmp);
Chris@16 387 }
Chris@16 388
Chris@16 389 } //namespace ipcdetail{
Chris@16 390 } //namespace interprocess{
Chris@16 391 } //namespace boost{
Chris@16 392
Chris@16 393 #elif defined(__IBMCPP__) && (__IBMCPP__ >= 800) && defined(_AIX)
Chris@16 394
Chris@16 395 #include <builtins.h>
Chris@16 396
Chris@16 397 namespace boost {
Chris@16 398 namespace interprocess {
Chris@16 399 namespace ipcdetail{
Chris@16 400
Chris@16 401 //first define boost::uint32_t versions of __lwarx and __stwcx to avoid poluting
Chris@16 402 //all the functions with casts
Chris@16 403
Chris@16 404 //! From XLC documenation :
Chris@16 405 //! This function can be used with a subsequent stwcxu call to implement a
Chris@16 406 //! read-modify-write on a specified memory location. The two functions work
Chris@16 407 //! together to ensure that if the store is successfully performed, no other
Chris@16 408 //! processor or mechanism can modify the target doubleword between the time
Chris@16 409 //! lwarxu function is executed and the time the stwcxu functio ncompletes.
Chris@16 410 //! "mem" : pointer to the object
Chris@16 411 //! Returns the value at pointed to by mem
Chris@16 412 inline boost::uint32_t lwarxu(volatile boost::uint32_t *mem)
Chris@16 413 {
Chris@16 414 return static_cast<boost::uint32_t>(__lwarx(reinterpret_cast<volatile int*>(mem)));
Chris@16 415 }
Chris@16 416
Chris@16 417 //! "mem" : pointer to the object
Chris@16 418 //! "val" : the value to store
Chris@16 419 //! Returns true if the update of mem is successful and false if it is
Chris@16 420 //!unsuccessful
Chris@16 421 inline bool stwcxu(volatile boost::uint32_t* mem, boost::uint32_t val)
Chris@16 422 {
Chris@16 423 return (__stwcx(reinterpret_cast<volatile int*>(mem), static_cast<int>(val)) != 0);
Chris@16 424 }
Chris@16 425
Chris@16 426 //! "mem": pointer to the object
Chris@16 427 //! "val": amount to add
Chris@16 428 //! Returns the old value pointed to by mem
Chris@16 429 inline boost::uint32_t atomic_add32
Chris@16 430 (volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 431 {
Chris@16 432 boost::uint32_t oldValue;
Chris@16 433 do
Chris@16 434 {
Chris@16 435 oldValue = lwarxu(mem);
Chris@16 436 }while (!stwcxu(mem, oldValue+val));
Chris@16 437 return oldValue;
Chris@16 438 }
Chris@16 439
Chris@16 440 //! Atomically increment an apr_uint32_t by 1
Chris@16 441 //! "mem": pointer to the object
Chris@16 442 //! Returns the old value pointed to by mem
Chris@16 443 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 444 { return atomic_add32(mem, 1); }
Chris@16 445
Chris@16 446 //! Atomically decrement an boost::uint32_t by 1
Chris@16 447 //! "mem": pointer to the atomic value
Chris@16 448 //! Returns the old value pointed to by mem
Chris@16 449 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 450 { return atomic_add32(mem, (boost::uint32_t)-1); }
Chris@16 451
Chris@16 452 //! Atomically read an boost::uint32_t from memory
Chris@16 453 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 454 { return *mem; }
Chris@16 455
Chris@16 456 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 457 //! If they are the same swap the value with "with"
Chris@16 458 //! "mem": pointer to the value
Chris@16 459 //! "with" what to swap it with
Chris@16 460 //! "cmp": the value to compare it to
Chris@16 461 //! Returns the old value of *mem
Chris@16 462 inline boost::uint32_t atomic_cas32
Chris@16 463 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 464 {
Chris@16 465 boost::uint32_t oldValue;
Chris@16 466 boost::uint32_t valueToStore;
Chris@16 467 do
Chris@16 468 {
Chris@16 469 oldValue = lwarxu(mem);
Chris@16 470 } while (!stwcxu(mem, (oldValue == with) ? cmp : oldValue));
Chris@16 471
Chris@16 472 return oldValue;
Chris@16 473 }
Chris@16 474
Chris@16 475 //! Atomically set an boost::uint32_t in memory
Chris@16 476 //! "mem": pointer to the object
Chris@16 477 //! "param": val value that the object will assume
Chris@16 478 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 479 { *mem = val; }
Chris@16 480
Chris@16 481 } //namespace ipcdetail
Chris@16 482 } //namespace interprocess
Chris@16 483 } //namespace boost
Chris@16 484
Chris@16 485 #elif defined(__GNUC__) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
Chris@16 486
Chris@16 487 namespace boost {
Chris@16 488 namespace interprocess {
Chris@16 489 namespace ipcdetail{
Chris@16 490
Chris@16 491 //! Atomically add 'val' to an boost::uint32_t
Chris@16 492 //! "mem": pointer to the object
Chris@16 493 //! "val": amount to add
Chris@16 494 //! Returns the old value pointed to by mem
Chris@16 495 inline boost::uint32_t atomic_add32
Chris@16 496 (volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 497 { return __sync_fetch_and_add(const_cast<boost::uint32_t *>(mem), val); }
Chris@16 498
Chris@16 499 //! Atomically increment an apr_uint32_t by 1
Chris@16 500 //! "mem": pointer to the object
Chris@16 501 //! Returns the old value pointed to by mem
Chris@16 502 inline boost::uint32_t atomic_inc32(volatile boost::uint32_t *mem)
Chris@16 503 { return atomic_add32(mem, 1); }
Chris@16 504
Chris@16 505 //! Atomically decrement an boost::uint32_t by 1
Chris@16 506 //! "mem": pointer to the atomic value
Chris@16 507 //! Returns the old value pointed to by mem
Chris@16 508 inline boost::uint32_t atomic_dec32(volatile boost::uint32_t *mem)
Chris@16 509 { return atomic_add32(mem, (boost::uint32_t)-1); }
Chris@16 510
Chris@16 511 //! Atomically read an boost::uint32_t from memory
Chris@16 512 inline boost::uint32_t atomic_read32(volatile boost::uint32_t *mem)
Chris@16 513 { return *mem; }
Chris@16 514
Chris@16 515 //! Compare an boost::uint32_t's value with "cmp".
Chris@16 516 //! If they are the same swap the value with "with"
Chris@16 517 //! "mem": pointer to the value
Chris@16 518 //! "with" what to swap it with
Chris@16 519 //! "cmp": the value to compare it to
Chris@16 520 //! Returns the old value of *mem
Chris@16 521 inline boost::uint32_t atomic_cas32
Chris@16 522 (volatile boost::uint32_t *mem, boost::uint32_t with, boost::uint32_t cmp)
Chris@16 523 { return __sync_val_compare_and_swap(const_cast<boost::uint32_t *>(mem), cmp, with); }
Chris@16 524
Chris@16 525 //! Atomically set an boost::uint32_t in memory
Chris@16 526 //! "mem": pointer to the object
Chris@16 527 //! "param": val value that the object will assume
Chris@16 528 inline void atomic_write32(volatile boost::uint32_t *mem, boost::uint32_t val)
Chris@16 529 { *mem = val; }
Chris@16 530
Chris@16 531 } //namespace ipcdetail{
Chris@16 532 } //namespace interprocess{
Chris@16 533 } //namespace boost{
Chris@16 534
Chris@16 535 #else
Chris@16 536
Chris@16 537 #error No atomic operations implemented for this platform, sorry!
Chris@16 538
Chris@16 539 #endif
Chris@16 540
Chris@16 541 namespace boost{
Chris@16 542 namespace interprocess{
Chris@16 543 namespace ipcdetail{
Chris@16 544
Chris@16 545 inline bool atomic_add_unless32
Chris@16 546 (volatile boost::uint32_t *mem, boost::uint32_t value, boost::uint32_t unless_this)
Chris@16 547 {
Chris@16 548 boost::uint32_t old, c(atomic_read32(mem));
Chris@16 549 while(c != unless_this && (old = atomic_cas32(mem, c + value, c)) != c){
Chris@16 550 c = old;
Chris@16 551 }
Chris@16 552 return c != unless_this;
Chris@16 553 }
Chris@16 554
Chris@16 555 } //namespace ipcdetail
Chris@16 556 } //namespace interprocess
Chris@16 557 } //namespace boost
Chris@16 558
Chris@16 559
Chris@16 560 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 561
Chris@16 562 #endif //BOOST_INTERPROCESS_DETAIL_ATOMIC_HPP