|
||||
File indexing completed on 2025-01-18 09:51:45
0001 //---------------------------------------------------------------------------- 0002 /// @file atomic.hpp 0003 /// @brief Basic layer for to simplify the use of atomic functions 0004 /// @author Copyright(c) 2016 Francisco José Tapia (fjtapia@gmail.com )\n 0005 /// Distributed under the Boost Software License, Version 1.0.\n 0006 /// ( See accompanying file LICENSE_1_0.txt or copy at 0007 /// http://www.boost.org/LICENSE_1_0.txt ) 0008 /// @version 0.1 0009 /// 0010 /// @remarks 0011 //----------------------------------------------------------------------------- 0012 #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_ATOMIC_HPP 0013 #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_ATOMIC_HPP 0014 0015 #include <ciso646> 0016 #include <atomic> 0017 #include <cassert> 0018 #include <type_traits> 0019 0020 namespace boost 0021 { 0022 namespace sort 0023 { 0024 namespace common 0025 { 0026 namespace util 0027 { 0028 //----------------------------------------------------------------------------- 0029 // function : atomic_read 0030 /// @brief make the atomic read of an atomic variable, using a memory model 0031 /// @param at_var : atomic variable to read 0032 /// @return value obtained 0033 //----------------------------------------------------------------------------- 0034 template<typename T> 0035 inline T atomic_read(std::atomic<T> &at_var) 0036 { 0037 return std::atomic_load_explicit < T > (&at_var, std::memory_order_acquire); 0038 }; 0039 // 0040 //----------------------------------------------------------------------------- 0041 // function : atomic_add 0042 /// @brief Add a number to an atomic variable, using a memory model 0043 /// @param at_var : variable to add 0044 /// @param num : value to add to at_var 0045 /// @return result of the operation 0046 //----------------------------------------------------------------------------- 0047 template<typename T, typename T2> 0048 inline T atomic_add(std::atomic<T> &at_var, T2 num) 0049 { 0050 static_assert (std::is_integral< T2 >::value, "Bad parameter"); 0051 return std::atomic_fetch_add_explicit <T> 0052 (&at_var, (T) num, std::memory_order_acq_rel); 0053 }; 0054 // 0055 //----------------------------------------------------------------------------- 0056 // function : atomic_sub 0057 /// @brief Atomic subtract of an atomic variable using memory model 0058 /// @param at_var : Varibale to subtract 0059 /// @param num : value to sub to at_var 0060 /// @return result of the operation 0061 //----------------------------------------------------------------------------- 0062 template<typename T, typename T2> 0063 inline T atomic_sub(std::atomic<T> &at_var, T2 num) 0064 { 0065 static_assert (std::is_integral< T2 >::value, "Bad parameter"); 0066 return std::atomic_fetch_sub_explicit <T> 0067 (&at_var, (T) num, std::memory_order_acq_rel); 0068 }; 0069 // 0070 //----------------------------------------------------------------------------- 0071 // function : atomic_write 0072 /// @brief Write a value in an atomic variable using memory model 0073 /// @param at_var : varible to write 0074 /// @param num : value to write in at_var 0075 //----------------------------------------------------------------------------- 0076 template<typename T, typename T2> 0077 inline void atomic_write(std::atomic<T> &at_var, T2 num) 0078 { 0079 static_assert (std::is_integral< T2 >::value, "Bad parameter"); 0080 std::atomic_store_explicit <T> 0081 (&at_var, (T) num, std::memory_order_release); 0082 }; 0083 template<typename T> 0084 struct counter_guard 0085 { 0086 typedef std::atomic<T> atomic_t; 0087 atomic_t &count; 0088 0089 counter_guard(atomic_t & counter): count(counter) { }; 0090 ~counter_guard() {atomic_sub(count, 1); }; 0091 }; 0092 // 0093 //**************************************************************************** 0094 };// End namespace util 0095 };// End namespace common 0096 };// End namespace sort 0097 };// End namespace boost 0098 //**************************************************************************** 0099 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |