BVB Source Codes

mars Show ops_windows.hpp Source code

Return Download mars: download ops_windows.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) 2009 Helge Bahmann
  7.  * Copyright (c) 2012 Tim Blechmann
  8.  * Copyright (c) 2014 Andrey Semashev
  9.  */
  10. /*!
  11.  * \file   atomic/detail/ops_windows.hpp
  12.  *
  13.  * This header contains implementation of the \c operations template.
  14.  *
  15.  * This implementation is the most basic version for Windows. It should
  16.  * work for any non-MSVC-like compilers as long as there are Interlocked WinAPI
  17.  * functions available. This version is also used for WinCE.
  18.  *
  19.  * Notably, this implementation is not as efficient as other
  20.  * versions based on compiler intrinsics.
  21.  */
  22.  
  23. #ifndef BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
  24. #define BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
  25.  
  26. #include <boost/memory_order.hpp>
  27. #include <boost/type_traits/make_signed.hpp>
  28. #include <boost/atomic/detail/config.hpp>
  29. #include <boost/atomic/detail/interlocked.hpp>
  30. #include <boost/atomic/detail/storage_type.hpp>
  31. #include <boost/atomic/detail/operations_fwd.hpp>
  32. #include <boost/atomic/capabilities.hpp>
  33. #include <boost/atomic/detail/ops_msvc_common.hpp>
  34. #include <boost/atomic/detail/ops_extending_cas_based.hpp>
  35.  
  36. #ifdef BOOST_HAS_PRAGMA_ONCE
  37. #pragma once
  38. #endif
  39.  
  40. namespace mars_boost {} namespace boost = mars_boost; namespace mars_boost {
  41. namespace atomics {
  42. namespace detail {
  43.  
  44. struct windows_operations_base
  45. {
  46.     static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
  47.     {
  48.         long tmp;
  49.         BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&tmp, 0);
  50.     }
  51.  
  52.     static BOOST_FORCEINLINE void fence_before(memory_order) BOOST_NOEXCEPT
  53.     {
  54.         BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  55.     }
  56.  
  57.     static BOOST_FORCEINLINE void fence_after(memory_order) BOOST_NOEXCEPT
  58.     {
  59.         BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  60.     }
  61. };
  62.  
  63. template< typename T, typename Derived >
  64. struct windows_operations :
  65.     public windows_operations_base
  66. {
  67.     typedef T storage_type;
  68.  
  69.     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  70.     {
  71.         Derived::exchange(storage, v, order);
  72.     }
  73.  
  74.     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
  75.     {
  76.         return Derived::fetch_add(const_cast< storage_type volatile& >(storage), (storage_type)0, order);
  77.     }
  78.  
  79.     static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  80.     {
  81.         typedef typename make_signed< storage_type >::type signed_storage_type;
  82.         return Derived::fetch_add(storage, static_cast< storage_type >(-static_cast< signed_storage_type >(v)), order);
  83.     }
  84.  
  85.     static BOOST_FORCEINLINE bool compare_exchange_weak(
  86.         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  87.     {
  88.         return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
  89.     }
  90.  
  91.     static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  92.     {
  93.         return !!Derived::exchange(storage, (storage_type)1, order);
  94.     }
  95.  
  96.     static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  97.     {
  98.         store(storage, (storage_type)0, order);
  99.     }
  100.  
  101.     static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
  102.     {
  103.         return true;
  104.     }
  105. };
  106.  
  107. template< bool Signed >
  108. struct operations< 4u, Signed > :
  109.     public windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > >
  110. {
  111.     typedef windows_operations< typename make_storage_type< 4u, Signed >::type, operations< 4u, Signed > > base_type;
  112.     typedef typename base_type::storage_type storage_type;
  113.     typedef typename make_storage_type< 4u, Signed >::aligned aligned_storage_type;
  114.  
  115.     static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  116.     {
  117.         base_type::fence_before(order);
  118.         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE_ADD(&storage, v));
  119.         base_type::fence_after(order);
  120.         return v;
  121.     }
  122.  
  123.     static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  124.     {
  125.         base_type::fence_before(order);
  126.         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_EXCHANGE(&storage, v));
  127.         base_type::fence_after(order);
  128.         return v;
  129.     }
  130.  
  131.     static BOOST_FORCEINLINE bool compare_exchange_strong(
  132.         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
  133.     {
  134.         storage_type previous = expected;
  135.         base_type::fence_before(success_order);
  136.         storage_type old_val = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_COMPARE_EXCHANGE(&storage, desired, previous));
  137.         expected = old_val;
  138.         // The success and failure fences are the same anyway
  139.         base_type::fence_after(success_order);
  140.         return (previous == old_val);
  141.     }
  142.  
  143.     static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  144.     {
  145. #if defined(BOOST_ATOMIC_INTERLOCKED_AND)
  146.         base_type::fence_before(order);
  147.         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_AND(&storage, v));
  148.         base_type::fence_after(order);
  149.         return v;
  150. #else
  151.         storage_type res = storage;
  152.         while (!compare_exchange_strong(storage, res, res & v, order, memory_order_relaxed)) {}
  153.         return res;
  154. #endif
  155.     }
  156.  
  157.     static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  158.     {
  159. #if defined(BOOST_ATOMIC_INTERLOCKED_OR)
  160.         base_type::fence_before(order);
  161.         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_OR(&storage, v));
  162.         base_type::fence_after(order);
  163.         return v;
  164. #else
  165.         storage_type res = storage;
  166.         while (!compare_exchange_strong(storage, res, res | v, order, memory_order_relaxed)) {}
  167.         return res;
  168. #endif
  169.     }
  170.  
  171.     static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
  172.     {
  173. #if defined(BOOST_ATOMIC_INTERLOCKED_XOR)
  174.         base_type::fence_before(order);
  175.         v = static_cast< storage_type >(BOOST_ATOMIC_INTERLOCKED_XOR(&storage, v));
  176.         base_type::fence_after(order);
  177.         return v;
  178. #else
  179.         storage_type res = storage;
  180.         while (!compare_exchange_strong(storage, res, res ^ v, order, memory_order_relaxed)) {}
  181.         return res;
  182. #endif
  183.     }
  184. };
  185.  
  186. template< bool Signed >
  187. struct operations< 1u, Signed > :
  188.     public extending_cas_based_operations< operations< 4u, Signed >, 1u, Signed >
  189. {
  190. };
  191.  
  192. template< bool Signed >
  193. struct operations< 2u, Signed > :
  194.     public extending_cas_based_operations< operations< 4u, Signed >, 2u, Signed >
  195. {
  196. };
  197.  
  198. BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
  199. {
  200.     BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  201.     if (order == memory_order_seq_cst)
  202.         windows_operations_base::hardware_full_fence();
  203.     BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  204. }
  205.  
  206. BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
  207. {
  208.     if (order != memory_order_relaxed)
  209.         BOOST_ATOMIC_DETAIL_COMPILER_BARRIER();
  210. }
  211.  
  212. } // namespace detail
  213. } // namespace atomics
  214. } // namespace mars_boost
  215.  
  216. #endif // BOOST_ATOMIC_DETAIL_OPS_WINDOWS_HPP_INCLUDED_
  217.  
downloadops_windows.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