BVB Source Codes

mars Show ops_gcc_sync.hpp Source code

Return Download mars: download ops_gcc_sync.hpp Source code - Download mars Source code - Type:.hpp
  1. /*
  2.  * Distributed under the Boost Software License, Version 1.0.
  3.  * (See accompanying file LICENSE_1_0.txt or copy at
  4.  * http://www.boost.org/LICENSE_1_0.txt)
  5.  *
  6.  * Copyright (c) 2011 Helge Bahmann
  7.  * Copyright (c) 2013 Tim Blechmann
  8.  * Copyright (c) 2014 Andrey Semashev
  9.  */
  10. /*!
  11.  * \file   atomic/detail/ops_gcc_sync.hpp
  12.  *
  13.  * This header contains implementation of the \c operations template.
  14.  */
  15.  
  16. #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
  17. #define BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
  18.  
  19. #include <boost/memory_order.hpp>
  20. #include <boost/atomic/detail/config.hpp>
  21. #include <boost/atomic/detail/storage_type.hpp>
  22. #include <boost/atomic/detail/operations_fwd.hpp>
  23. #include <boost/atomic/detail/ops_extending_cas_based.hpp>
  24. #include <boost/atomic/capabilities.hpp>
  25.  
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29.  
  30. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost {
  31. namespace atomics {
  32. namespace detail {
  33.  
  34. struct gcc_sync_operations_base
  35. {
  36.     static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
  37.     {
  38.         if ((order & memory_order_release) != 0)
  39.             __sync_synchronize();
  40.     }
  41.  
  42.     static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
  43.     {
  44.         if (order == memory_order_seq_cst)
  45.             __sync_synchronize();
  46.     }
  47.  
  48.     static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
  49.     {
  50.         if ((order & (memory_order_acquire | memory_order_consume)) != 0)
  51.             __sync_synchronize();
  52.     }
  53. };
  54.  
  55. template< typename T >
  56. struct gcc_sync_operations :
  57.     public gcc_sync_operations_base
  58. {
  59.     typedef T storage_type;
  60.  
  61.     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  62.     {
  63.         fence_before_store(order);
  64.         storage = v;
  65.         fence_after_store(order);
  66.     }
  67.  
  68.     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  69.     {
  70.         storage_type v = storage;
  71.         fence_after_load(order);
  72.         return v;
  73.     }
  74.  
  75.     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  76.     {
  77.         return __sync_fetch_and_add(&storage, v);
  78.     }
  79.  
  80.     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  81.     {
  82.         return __sync_fetch_and_sub(&storage, v);
  83.     }
  84.  
  85.     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  86.     {
  87.         // GCC docs mention that not all architectures may support full exchange semantics for this intrinsic. However, GCC's implementation of
  88.         // std::atomic<> uses this intrinsic unconditionally. We do so as well. In case if some architectures actually don't support this, we can always
  89.         // add a check here and fall back to a CAS loop.
  90.         if ((order & memory_order_release) != 0)
  91.             __sync_synchronize();
  92.         return __sync_lock_test_and_set(&storage, v);
  93.     }
  94.  
  95.     static BOOST_FORCEINLINE bool compare_exchange_strong(
  96.         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
  97.     {
  98.         storage_type expected2 = expected;
  99.         storage_type old_val = __sync_val_compare_and_swap(&storage, expected2, desired);
  100.  
  101.         if (old_val == expected2)
  102.         {
  103.             return true;
  104.         }
  105.         else
  106.         {
  107.             expected = old_val;
  108.             return false;
  109.         }
  110.     }
  111.  
  112.     static BOOST_FORCEINLINE bool compare_exchange_weak(
  113.         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  114.     {
  115.         return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
  116.     }
  117.  
  118.     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  119.     {
  120.         return __sync_fetch_and_and(&storage, v);
  121.     }
  122.  
  123.     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  124.     {
  125.         return __sync_fetch_and_or(&storage, v);
  126.     }
  127.  
  128.     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
  129.     {
  130.         return __sync_fetch_and_xor(&storage, v);
  131.     }
  132.  
  133.     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  134.     {
  135.         if ((order & memory_order_release) != 0)
  136.             __sync_synchronize();
  137.         return !!__sync_lock_test_and_set(&storage, 1);
  138.     }
  139.  
  140.     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  141.     {
  142.         __sync_lock_release(&storage);
  143.         if (order == memory_order_seq_cst)
  144.             __sync_synchronize();
  145.     }
  146.  
  147.     static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
  148.     {
  149.         return true;
  150.     }
  151. };
  152.  
  153. #if BOOST_ATOMIC_INT8_LOCK_FREE > 0
  154. template< bool Signed >
  155. struct operations< 1u, Signed > :
  156. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
  157.     public gcc_sync_operations< typename make_storage_type< 1u, Signed >::type >
  158. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  159.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 2u, Signed >::type >, 1u, Signed >
  160. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  161.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 1u, Signed >
  162. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  163.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 1u, Signed >
  164. #else
  165.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 1u, Signed >
  166. #endif
  167. {
  168. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1)
  169.     typedef typename make_storage_type< 1u, Signed >::aligned aligned_storage_type;
  170. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  171.     typedef typename make_storage_type< 2u, Signed >::aligned aligned_storage_type;
  172. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  173.     typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
  174. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  175.     typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
  176. #else
  177.     typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
  178. #endif
  179. };
  180. #endif
  181.  
  182. #if BOOST_ATOMIC_INT16_LOCK_FREE > 0
  183. template< bool Signed >
  184. struct operations< 2u, Signed > :
  185. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  186.     public gcc_sync_operations< typename make_storage_type< 2u, Signed >::type >
  187. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  188.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >, 2u, Signed >
  189. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  190.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 2u, Signed >
  191. #else
  192.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 2u, Signed >
  193. #endif
  194. {
  195. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2)
  196.     typedef typename make_storage_type< 2u, Signed >::aligned aligned_storage_type;
  197. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  198.     typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
  199. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  200.     typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
  201. #else
  202.     typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
  203. #endif
  204. };
  205. #endif
  206.  
  207. #if BOOST_ATOMIC_INT32_LOCK_FREE > 0
  208. template< bool Signed >
  209. struct operations< 4u, Signed > :
  210. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  211.     public gcc_sync_operations< typename make_storage_type< 4u, Signed >::type >
  212. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  213.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >, 4u, Signed >
  214. #else
  215.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 4u, Signed >
  216. #endif
  217. {
  218. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  219.     typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
  220. #elif defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  221.     typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
  222. #else
  223.     typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
  224. #endif
  225. };
  226. #endif
  227.  
  228. #if BOOST_ATOMIC_INT64_LOCK_FREE > 0
  229. template< bool Signed >
  230. struct operations< 8u, Signed > :
  231. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  232.     public gcc_sync_operations< typename make_storage_type< 8u, Signed >::type >
  233. #else
  234.     public extending_cas_based_operations< gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >, 8u, Signed >
  235. #endif
  236. {
  237. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8)
  238.     typedef typename make_storage_type< 8u, Signed >::aligned aligned_storage_type;
  239. #else
  240.     typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
  241. #endif
  242. };
  243. #endif
  244.  
  245. #if BOOST_ATOMIC_INT128_LOCK_FREE > 0
  246. template< bool Signed >
  247. struct operations< 16u, Signed > :
  248.     public gcc_sync_operations< typename make_storage_type< 16u, Signed >::type >
  249. {
  250.     typedef typename make_storage_type< 16u, Signed >::aligned aligned_storage_type;
  251. };
  252. #endif
  253.  
  254. BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  255. {
  256.     if (order != memory_order_relaxed)
  257.         __sync_synchronize();
  258. }
  259.  
  260. BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  261. {
  262.     if (order != memory_order_relaxed)
  263.         __asm__ __volatile__ ("" ::: "memory");
  264. }
  265.  
  266. } // namespace detail
  267. } // namespace atomics
  268. } // namespace mars_boost
  269.  
  270. #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_SYNC_HPP_INCLUDED_
  271.  
downloadops_gcc_sync.hpp Source code - Download mars Source code
Related Source Codes/Software:
Hero - Elegant transition library for iOS & tvOS 2017-06-09
deep-photo-styletransfer - Code and data for paper "Deep Photo Style Transfer... 2017-06-09
mastodon - A GNU Social-compatible microblogging server ... 2017-06-09
plyr - A simple HTML5, YouTube and Vimeo player ... 2017-06-08
prepack - Prepack is a partial evaluator for JavaScript. Pre... 2017-06-08
Public-APIs - 2017-06-09
lottie-ios - An iOS library to natively render After Effects ve... 2017-06-09
Awesome-Hacking - A collection of various awesome lists for hackers,... 2017-06-09
algorithms - Minimal examples of data structures and algorithms... 2017-06-10
lectures - Oxford Deep NLP 2017 course 2017-06-10
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top