1 2 // Copyright Oliver Kowalke 2013. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // based on tss.hpp from boost.thread 8 9 #ifndef BOOST_FIBERS_DETAIL_FSS_H 10 #define BOOST_FIBERS_DETAIL_FSS_H 11 12 #include <atomic> 13 #include <cstddef> 14 15 #include <boost/config.hpp> 16 #include <boost/intrusive_ptr.hpp> 17 18 #ifdef BOOST_HAS_ABI_HEADERS 19 # include BOOST_ABI_PREFIX 20 #endif 21 22 namespace boost { 23 namespace fibers { 24 namespace detail { 25 26 class fss_cleanup_function { 27 private: 28 std::atomic< std::size_t > use_count_{ 0 }; 29 30 public: 31 typedef intrusive_ptr< fss_cleanup_function > ptr_t; 32 33 fss_cleanup_function() noexcept = default; 34 35 virtual ~fss_cleanup_function() = default; 36 37 virtual void operator()( void * data) = 0; 38 39 friend inline intrusive_ptr_add_ref(fss_cleanup_function * p)40 void intrusive_ptr_add_ref( fss_cleanup_function * p) noexcept { 41 p->use_count_.fetch_add( 1, std::memory_order_relaxed); 42 } 43 44 friend inline intrusive_ptr_release(fss_cleanup_function * p)45 void intrusive_ptr_release( fss_cleanup_function * p) noexcept { 46 if ( 1 == p->use_count_.fetch_sub( 1, std::memory_order_release) ) { 47 std::atomic_thread_fence( std::memory_order_acquire); 48 delete p; 49 } 50 } 51 }; 52 53 }}} 54 55 #ifdef BOOST_HAS_ABI_HEADERS 56 # include BOOST_ABI_SUFFIX 57 #endif 58 59 #endif // BOOST_FIBERS_DETAIL_FSS_H 60