cas32strong.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_DETAIL_ATOMIC_CAS32STRONG_HPP
00002 #define BOOST_DETAIL_ATOMIC_CAS32STRONG_HPP
00003 
00004 //  Distributed under the Boost Software License, Version 1.0.
00005 //  See accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 //  Copyright (c) 2011 Helge Bahmann
00009 
00010 // Build 8-, 16- and 32-bit atomic operations from 
00011 // a platform_cmpxchg32_strong primitive.
00012 
00013 #include <boost/memory_order.hpp>
00014 #include <boost/atomic/detail/base.hpp>
00015 
00016 namespace boost {
00017 namespace detail {
00018 namespace atomic {
00019 
00020 /* integral types */
00021 
00022 template<typename T, bool Sign>
00023 class base_atomic<T, int, 1, Sign> {
00024         typedef base_atomic this_type;
00025         typedef T value_type;
00026         typedef T difference_type;
00027         typedef uint32_t storage_type;
00028 public:
00029         explicit base_atomic(value_type v) : v_(v) {}
00030         base_atomic(void) {}
00031         
00032         void
00033         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00034         {
00035                 platform_fence_before_store(order);
00036                 const_cast<volatile storage_type &>(v_) = v;
00037                 platform_fence_after_store(order);
00038         }
00039         
00040         value_type
00041         load(memory_order order = memory_order_seq_cst) const volatile
00042         {
00043                 value_type v = const_cast<const volatile storage_type &>(v_);
00044                 platform_fence_after_load(order);
00045                 return v;
00046         }
00047         
00048         value_type
00049         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00050         {
00051                 value_type original = load(memory_order_relaxed);
00052                 do {
00053                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00054                 return original;
00055         }
00056         
00057         bool
00058         compare_exchange_weak(
00059                 value_type & expected,
00060                 value_type desired,
00061                 memory_order success_order,
00062                 memory_order failure_order) volatile
00063         {
00064                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00065         }
00066         
00067         bool
00068         compare_exchange_strong(
00069                 value_type & expected,
00070                 value_type desired,
00071                 memory_order success_order,
00072                 memory_order failure_order) volatile
00073         {
00074                 platform_fence_before(success_order);
00075                 
00076                 storage_type expected_s = (storage_type) expected;
00077                 storage_type desired_s = (storage_type) desired;
00078                 
00079                 bool success = platform_cmpxchg32_strong(expected_s, desired_s, &v_);
00080                 
00081                 if (success) {
00082                         platform_fence_after(success_order);
00083                 } else {
00084                         platform_fence_after(failure_order);
00085                         expected = (value_type) expected_s;
00086                 }
00087                 
00088                 return success;
00089         }
00090         
00091         value_type
00092         fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile
00093         {
00094                 value_type original = load(memory_order_relaxed);
00095                 do {
00096                 } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
00097                 return original;
00098         }
00099         
00100         value_type
00101         fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile
00102         {
00103                 value_type original = load(memory_order_relaxed);
00104                 do {
00105                 } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
00106                 return original;
00107         }
00108         
00109         value_type
00110         fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile
00111         {
00112                 value_type original = load(memory_order_relaxed);
00113                 do {
00114                 } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
00115                 return original;
00116         }
00117         
00118         value_type
00119         fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile
00120         {
00121                 value_type original = load(memory_order_relaxed);
00122                 do {
00123                 } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
00124                 return original;
00125         }
00126         
00127         value_type
00128         fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile
00129         {
00130                 value_type original = load(memory_order_relaxed);
00131                 do {
00132                 } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
00133                 return original;
00134         }
00135         
00136         bool
00137         is_lock_free(void) const volatile
00138         {
00139                 return true;
00140         }
00141         
00142         BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
00143 private:
00144         base_atomic(const base_atomic &) /* = delete */ ;
00145         void operator=(const base_atomic &) /* = delete */ ;
00146         storage_type v_;
00147 };
00148 
00149 template<typename T, bool Sign>
00150 class base_atomic<T, int, 2, Sign> {
00151         typedef base_atomic this_type;
00152         typedef T value_type;
00153         typedef T difference_type;
00154         typedef uint32_t storage_type;
00155 public:
00156         explicit base_atomic(value_type v) : v_(v) {}
00157         base_atomic(void) {}
00158         
00159         void
00160         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00161         {
00162                 platform_fence_before_store(order);
00163                 const_cast<volatile storage_type &>(v_) = v;
00164                 platform_fence_after_store(order);
00165         }
00166         
00167         value_type
00168         load(memory_order order = memory_order_seq_cst) const volatile
00169         {
00170                 value_type v = const_cast<const volatile storage_type &>(v_);
00171                 platform_fence_after_load(order);
00172                 return v;
00173         }
00174         
00175         value_type
00176         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00177         {
00178                 value_type original = load(memory_order_relaxed);
00179                 do {
00180                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00181                 return original;
00182         }
00183         
00184         bool
00185         compare_exchange_weak(
00186                 value_type & expected,
00187                 value_type desired,
00188                 memory_order success_order,
00189                 memory_order failure_order) volatile
00190         {
00191                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00192         }
00193         
00194         bool
00195         compare_exchange_strong(
00196                 value_type & expected,
00197                 value_type desired,
00198                 memory_order success_order,
00199                 memory_order failure_order) volatile
00200         {
00201                 platform_fence_before(success_order);
00202                 
00203                 storage_type expected_s = (storage_type) expected;
00204                 storage_type desired_s = (storage_type) desired;
00205                 
00206                 bool success = platform_cmpxchg32_strong(expected_s, desired_s, &v_);
00207                 
00208                 if (success) {
00209                         platform_fence_after(success_order);
00210                 } else {
00211                         platform_fence_after(failure_order);
00212                         expected = (value_type) expected_s;
00213                 }
00214                 
00215                 return success;
00216         }
00217         
00218         value_type
00219         fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile
00220         {
00221                 value_type original = load(memory_order_relaxed);
00222                 do {
00223                 } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
00224                 return original;
00225         }
00226         
00227         value_type
00228         fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile
00229         {
00230                 value_type original = load(memory_order_relaxed);
00231                 do {
00232                 } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
00233                 return original;
00234         }
00235         
00236         value_type
00237         fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile
00238         {
00239                 value_type original = load(memory_order_relaxed);
00240                 do {
00241                 } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
00242                 return original;
00243         }
00244         
00245         value_type
00246         fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile
00247         {
00248                 value_type original = load(memory_order_relaxed);
00249                 do {
00250                 } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
00251                 return original;
00252         }
00253         
00254         value_type
00255         fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile
00256         {
00257                 value_type original = load(memory_order_relaxed);
00258                 do {
00259                 } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
00260                 return original;
00261         }
00262         
00263         bool
00264         is_lock_free(void) const volatile
00265         {
00266                 return true;
00267         }
00268         
00269         BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
00270 private:
00271         base_atomic(const base_atomic &) /* = delete */ ;
00272         void operator=(const base_atomic &) /* = delete */ ;
00273         storage_type v_;
00274 };
00275 
00276 template<typename T, bool Sign>
00277 class base_atomic<T, int, 4, Sign> {
00278         typedef base_atomic this_type;
00279         typedef T value_type;
00280         typedef T difference_type;
00281 public:
00282         explicit base_atomic(value_type v) : v_(v) {}
00283         base_atomic(void) {}
00284         
00285         void
00286         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00287         {
00288                 platform_fence_before_store(order);
00289                 const_cast<volatile value_type &>(v_) = v;
00290                 platform_fence_after_store(order);
00291         }
00292         
00293         value_type
00294         load(memory_order order = memory_order_seq_cst) const volatile
00295         {
00296                 value_type v = const_cast<const volatile value_type &>(v_);
00297                 platform_fence_after_load(order);
00298                 return v;
00299         }
00300         
00301         value_type
00302         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00303         {
00304                 value_type original = load(memory_order_relaxed);
00305                 do {
00306                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00307                 return original;
00308         }
00309         
00310         bool
00311         compare_exchange_weak(
00312                 value_type & expected,
00313                 value_type desired,
00314                 memory_order success_order,
00315                 memory_order failure_order) volatile
00316         {
00317                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00318         }
00319         
00320         bool
00321         compare_exchange_strong(
00322                 value_type & expected,
00323                 value_type desired,
00324                 memory_order success_order,
00325                 memory_order failure_order) volatile
00326         {
00327                 platform_fence_before(success_order);
00328                 
00329                 bool success = platform_cmpxchg32_strong(expected, desired, &v_);
00330                 
00331                 if (success) {
00332                         platform_fence_after(success_order);
00333                 } else {
00334                         platform_fence_after(failure_order);
00335                 }
00336                 
00337                 return success;
00338         }
00339         
00340         value_type
00341         fetch_add(value_type v, memory_order order = memory_order_seq_cst) volatile
00342         {
00343                 value_type original = load(memory_order_relaxed);
00344                 do {
00345                 } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
00346                 return original;
00347         }
00348         
00349         value_type
00350         fetch_sub(value_type v, memory_order order = memory_order_seq_cst) volatile
00351         {
00352                 value_type original = load(memory_order_relaxed);
00353                 do {
00354                 } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
00355                 return original;
00356         }
00357         
00358         value_type
00359         fetch_and(value_type v, memory_order order = memory_order_seq_cst) volatile
00360         {
00361                 value_type original = load(memory_order_relaxed);
00362                 do {
00363                 } while (!compare_exchange_weak(original, original & v, order, memory_order_relaxed));
00364                 return original;
00365         }
00366         
00367         value_type
00368         fetch_or(value_type v, memory_order order = memory_order_seq_cst) volatile
00369         {
00370                 value_type original = load(memory_order_relaxed);
00371                 do {
00372                 } while (!compare_exchange_weak(original, original | v, order, memory_order_relaxed));
00373                 return original;
00374         }
00375         
00376         value_type
00377         fetch_xor(value_type v, memory_order order = memory_order_seq_cst) volatile
00378         {
00379                 value_type original = load(memory_order_relaxed);
00380                 do {
00381                 } while (!compare_exchange_weak(original, original ^ v, order, memory_order_relaxed));
00382                 return original;
00383         }
00384         
00385         bool
00386         is_lock_free(void) const volatile
00387         {
00388                 return true;
00389         }
00390         
00391         BOOST_ATOMIC_DECLARE_INTEGRAL_OPERATORS
00392 private:
00393         base_atomic(const base_atomic &) /* = delete */ ;
00394         void operator=(const base_atomic &) /* = delete */ ;
00395         value_type v_;
00396 };
00397 
00398 /* pointer types */
00399 
00400 template<bool Sign>
00401 class base_atomic<void *, void *, 4, Sign> {
00402         typedef base_atomic this_type;
00403         typedef void * value_type;
00404         typedef ptrdiff_t difference_type;
00405 public:
00406         explicit base_atomic(value_type v) : v_(v) {}
00407         base_atomic(void) {}
00408         
00409         void
00410         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00411         {
00412                 platform_fence_before_store(order);
00413                 const_cast<volatile value_type &>(v_) = v;
00414                 platform_fence_after_store(order);
00415         }
00416         
00417         value_type
00418         load(memory_order order = memory_order_seq_cst) const volatile
00419         {
00420                 value_type v = const_cast<const volatile value_type &>(v_);
00421                 platform_fence_after_load(order);
00422                 return v;
00423         }
00424         
00425         value_type
00426         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00427         {
00428                 value_type original = load(memory_order_relaxed);
00429                 do {
00430                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00431                 return original;
00432         }
00433         
00434         bool
00435         compare_exchange_weak(
00436                 value_type & expected,
00437                 value_type desired,
00438                 memory_order success_order,
00439                 memory_order failure_order) volatile
00440         {
00441                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00442         }
00443         
00444         bool
00445         compare_exchange_strong(
00446                 value_type & expected,
00447                 value_type desired,
00448                 memory_order success_order,
00449                 memory_order failure_order) volatile
00450         {
00451                 platform_fence_before(success_order);
00452                 
00453                 bool success = platform_cmpxchg32_strong(expected, desired, &v_);
00454                 
00455                 if (success) {
00456                         platform_fence_after(success_order);
00457                 } else {
00458                         platform_fence_after(failure_order);
00459                 }
00460                 
00461                 return success;
00462         }
00463         
00464         value_type
00465         fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile
00466         {
00467                 value_type original = load(memory_order_relaxed);
00468                 do {
00469                 } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
00470                 return original;
00471         }
00472         
00473         value_type
00474         fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile
00475         {
00476                 value_type original = load(memory_order_relaxed);
00477                 do {
00478                 } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
00479                 return original;
00480         }
00481         
00482         bool
00483         is_lock_free(void) const volatile
00484         {
00485                 return true;
00486         }
00487         
00488         BOOST_ATOMIC_DECLARE_BASE_OPERATORS
00489 private:
00490         base_atomic(const base_atomic &) /* = delete */ ;
00491         void operator=(const base_atomic &) /* = delete */ ;
00492         value_type v_;
00493 };
00494 
00495 template<typename T, bool Sign>
00496 class base_atomic<T *, void *, 4, Sign> {
00497         typedef base_atomic this_type;
00498         typedef T * value_type;
00499         typedef ptrdiff_t difference_type;
00500 public:
00501         explicit base_atomic(value_type v) : v_(v) {}
00502         base_atomic(void) {}
00503         
00504         void
00505         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00506         {
00507                 platform_fence_before_store(order);
00508                 const_cast<volatile value_type &>(v_) = v;
00509                 platform_fence_after_store(order);
00510         }
00511         
00512         value_type
00513         load(memory_order order = memory_order_seq_cst) const volatile
00514         {
00515                 value_type v = const_cast<const volatile value_type &>(v_);
00516                 platform_fence_after_load(order);
00517                 return v;
00518         }
00519         
00520         value_type
00521         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00522         {
00523                 value_type original = load(memory_order_relaxed);
00524                 do {
00525                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00526                 return original;
00527         }
00528         
00529         bool
00530         compare_exchange_weak(
00531                 value_type & expected,
00532                 value_type desired,
00533                 memory_order success_order,
00534                 memory_order failure_order) volatile
00535         {
00536                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00537         }
00538         
00539         bool
00540         compare_exchange_strong(
00541                 value_type & expected,
00542                 value_type desired,
00543                 memory_order success_order,
00544                 memory_order failure_order) volatile
00545         {
00546                 platform_fence_before(success_order);
00547                 
00548                 bool success = platform_cmpxchg32_strong(expected, desired, &v_);
00549                 
00550                 if (success) {
00551                         platform_fence_after(success_order);
00552                 } else {
00553                         platform_fence_after(failure_order);
00554                 }
00555                 
00556                 return success;
00557         }
00558         
00559         value_type
00560         fetch_add(difference_type v, memory_order order = memory_order_seq_cst) volatile
00561         {
00562                 value_type original = load(memory_order_relaxed);
00563                 do {
00564                 } while (!compare_exchange_weak(original, original + v, order, memory_order_relaxed));
00565                 return original;
00566         }
00567         
00568         value_type
00569         fetch_sub(difference_type v, memory_order order = memory_order_seq_cst) volatile
00570         {
00571                 value_type original = load(memory_order_relaxed);
00572                 do {
00573                 } while (!compare_exchange_weak(original, original - v, order, memory_order_relaxed));
00574                 return original;
00575         }
00576         
00577         bool
00578         is_lock_free(void) const volatile
00579         {
00580                 return true;
00581         }
00582         
00583         BOOST_ATOMIC_DECLARE_POINTER_OPERATORS
00584 private:
00585         base_atomic(const base_atomic &) /* = delete */ ;
00586         void operator=(const base_atomic &) /* = delete */ ;
00587         value_type v_;
00588 };
00589 
00590 /* generic types */
00591 
00592 template<typename T, bool Sign>
00593 class base_atomic<T, void, 1, Sign> {
00594         typedef base_atomic this_type;
00595         typedef T value_type;
00596         typedef uint32_t storage_type;
00597 public:
00598         explicit base_atomic(value_type v) : v_(0)
00599         {
00600                 memcpy(&v_, &v, sizeof(value_type));
00601         }
00602         base_atomic(void) : v_(0) {}
00603         
00604         void
00605         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00606         {
00607                 storage_type tmp = 0;
00608                 memcpy(&tmp, &v, sizeof(value_type));
00609                 platform_fence_before_store(order);
00610                 const_cast<volatile storage_type &>(v_) = tmp;
00611                 platform_fence_after_store(order);
00612         }
00613         
00614         value_type
00615         load(memory_order order = memory_order_seq_cst) const volatile
00616         {
00617                 storage_type tmp = const_cast<const volatile storage_type &>(v_);
00618                 platform_fence_after_load(order);
00619                 
00620                 value_type v;
00621                 memcpy(&v, &tmp, sizeof(value_type));
00622                 return v;
00623         }
00624         
00625         value_type
00626         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00627         {
00628                 value_type original = load(memory_order_relaxed);
00629                 do {
00630                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00631                 return original;
00632         }
00633         
00634         bool
00635         compare_exchange_weak(
00636                 value_type & expected,
00637                 value_type desired,
00638                 memory_order success_order,
00639                 memory_order failure_order) volatile
00640         {
00641                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00642         }
00643         
00644         bool
00645         compare_exchange_strong(
00646                 value_type & expected,
00647                 value_type desired,
00648                 memory_order success_order,
00649                 memory_order failure_order) volatile
00650         {
00651                 
00652                 storage_type expected_s = 0, desired_s = 0;
00653                 memcpy(&expected_s, &expected, sizeof(value_type));
00654                 memcpy(&desired_s, &desired, sizeof(value_type));
00655                 
00656                 platform_fence_before(success_order);
00657                 bool success = platform_cmpxchg32_strong(expected_s, desired_s, &v_);
00658                 
00659                 if (success) {
00660                         platform_fence_after(success_order);
00661                 } else {
00662                         platform_fence_after(failure_order);
00663                         memcpy(&expected, &expected_s, sizeof(value_type));
00664                 }
00665                 
00666                 return success;
00667         }
00668         
00669         bool
00670         is_lock_free(void) const volatile
00671         {
00672                 return true;
00673         }
00674         
00675         BOOST_ATOMIC_DECLARE_BASE_OPERATORS
00676 private:
00677         base_atomic(const base_atomic &) /* = delete */ ;
00678         void operator=(const base_atomic &) /* = delete */ ;
00679         storage_type v_;
00680 };
00681 
00682 template<typename T, bool Sign>
00683 class base_atomic<T, void, 2, Sign> {
00684         typedef base_atomic this_type;
00685         typedef T value_type;
00686         typedef uint32_t storage_type;
00687 public:
00688         explicit base_atomic(value_type v) : v_(0)
00689         {
00690                 memcpy(&v_, &v, sizeof(value_type));
00691         }
00692         base_atomic(void) : v_(0) {}
00693         
00694         void
00695         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00696         {
00697                 storage_type tmp = 0;
00698                 memcpy(&tmp, &v, sizeof(value_type));
00699                 platform_fence_before_store(order);
00700                 const_cast<volatile storage_type &>(v_) = tmp;
00701                 platform_fence_after_store(order);
00702         }
00703         
00704         value_type
00705         load(memory_order order = memory_order_seq_cst) const volatile
00706         {
00707                 storage_type tmp = const_cast<const volatile storage_type &>(v_);
00708                 platform_fence_after_load(order);
00709                 
00710                 value_type v;
00711                 memcpy(&v, &tmp, sizeof(value_type));
00712                 return v;
00713         }
00714         
00715         value_type
00716         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00717         {
00718                 value_type original = load(memory_order_relaxed);
00719                 do {
00720                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00721                 return original;
00722         }
00723         
00724         bool
00725         compare_exchange_weak(
00726                 value_type & expected,
00727                 value_type desired,
00728                 memory_order success_order,
00729                 memory_order failure_order) volatile
00730         {
00731                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00732         }
00733         
00734         bool
00735         compare_exchange_strong(
00736                 value_type & expected,
00737                 value_type desired,
00738                 memory_order success_order,
00739                 memory_order failure_order) volatile
00740         {
00741                 
00742                 storage_type expected_s = 0, desired_s = 0;
00743                 memcpy(&expected_s, &expected, sizeof(value_type));
00744                 memcpy(&desired_s, &desired, sizeof(value_type));
00745                 
00746                 platform_fence_before(success_order);
00747                 bool success = platform_cmpxchg32_strong(expected_s, desired_s, &v_);
00748                 
00749                 if (success) {
00750                         platform_fence_after(success_order);
00751                 } else {
00752                         platform_fence_after(failure_order);
00753                         memcpy(&expected, &expected_s, sizeof(value_type));
00754                 }
00755                 
00756                 return success;
00757         }
00758         
00759         bool
00760         is_lock_free(void) const volatile
00761         {
00762                 return true;
00763         }
00764         
00765         BOOST_ATOMIC_DECLARE_BASE_OPERATORS
00766 private:
00767         base_atomic(const base_atomic &) /* = delete */ ;
00768         void operator=(const base_atomic &) /* = delete */ ;
00769         storage_type v_;
00770 };
00771 
00772 template<typename T, bool Sign>
00773 class base_atomic<T, void, 4, Sign> {
00774         typedef base_atomic this_type;
00775         typedef T value_type;
00776         typedef uint32_t storage_type;
00777 public:
00778         explicit base_atomic(value_type v) : v_(0)
00779         {
00780                 memcpy(&v_, &v, sizeof(value_type));
00781         }
00782         base_atomic(void) : v_(0) {}
00783         
00784         void
00785         store(value_type v, memory_order order = memory_order_seq_cst) volatile
00786         {
00787                 storage_type tmp = 0;
00788                 memcpy(&tmp, &v, sizeof(value_type));
00789                 platform_fence_before_store(order);
00790                 const_cast<volatile storage_type &>(v_) = tmp;
00791                 platform_fence_after_store(order);
00792         }
00793         
00794         value_type
00795         load(memory_order order = memory_order_seq_cst) const volatile
00796         {
00797                 storage_type tmp = const_cast<const volatile storage_type &>(v_);
00798                 platform_fence_after_load(order);
00799                 
00800                 value_type v;
00801                 memcpy(&v, &tmp, sizeof(value_type));
00802                 return v;
00803         }
00804         
00805         value_type
00806         exchange(value_type v, memory_order order = memory_order_seq_cst) volatile
00807         {
00808                 value_type original = load(memory_order_relaxed);
00809                 do {
00810                 } while (!compare_exchange_weak(original, v, order, memory_order_relaxed));
00811                 return original;
00812         }
00813         
00814         bool
00815         compare_exchange_weak(
00816                 value_type & expected,
00817                 value_type desired,
00818                 memory_order success_order,
00819                 memory_order failure_order) volatile
00820         {
00821                 return compare_exchange_strong(expected, desired, success_order, failure_order);
00822         }
00823         
00824         bool
00825         compare_exchange_strong(
00826                 value_type & expected,
00827                 value_type desired,
00828                 memory_order success_order,
00829                 memory_order failure_order) volatile
00830         {
00831                 
00832                 storage_type expected_s = 0, desired_s = 0;
00833                 memcpy(&expected_s, &expected, sizeof(value_type));
00834                 memcpy(&desired_s, &desired, sizeof(value_type));
00835                 
00836                 platform_fence_before(success_order);
00837                 bool success = platform_cmpxchg32_strong(expected_s, desired_s, &v_);
00838                 
00839                 if (success) {
00840                         platform_fence_after(success_order);
00841                 } else {
00842                         platform_fence_after(failure_order);
00843                         memcpy(&expected, &expected_s, sizeof(value_type));
00844                 }
00845                 
00846                 return success;
00847         }
00848         
00849         bool
00850         is_lock_free(void) const volatile
00851         {
00852                 return true;
00853         }
00854         
00855         BOOST_ATOMIC_DECLARE_BASE_OPERATORS
00856 private:
00857         base_atomic(const base_atomic &) /* = delete */ ;
00858         void operator=(const base_atomic &) /* = delete */ ;
00859         storage_type v_;
00860 };
00861 
00862 }
00863 }
00864 }
00865 
00866 #endif


rosatomic
Author(s): Josh Faust
autogenerated on Wed Aug 26 2015 15:59:57