fss.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright Oliver Kowalke 2013.
  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. // based on tss.hpp from boost.thread
  7. #ifndef BOOST_FIBERS_DETAIL_FSS_H
  8. #define BOOST_FIBERS_DETAIL_FSS_H
  9. #include <atomic>
  10. #include <cstddef>
  11. #include <boost/config.hpp>
  12. #include <boost/intrusive_ptr.hpp>
  13. #ifdef BOOST_HAS_ABI_HEADERS
  14. # include BOOST_ABI_PREFIX
  15. #endif
  16. namespace boost {
  17. namespace fibers {
  18. namespace detail {
  19. class fss_cleanup_function {
  20. private:
  21. std::atomic< std::size_t > use_count_{ 0 };
  22. public:
  23. typedef intrusive_ptr< fss_cleanup_function > ptr_t;
  24. fss_cleanup_function() noexcept = default;
  25. virtual ~fss_cleanup_function() = default;
  26. virtual void operator()( void * data) = 0;
  27. friend inline
  28. void intrusive_ptr_add_ref( fss_cleanup_function * p) noexcept {
  29. p->use_count_.fetch_add( 1, std::memory_order_relaxed);
  30. }
  31. friend inline
  32. void intrusive_ptr_release( fss_cleanup_function * p) noexcept {
  33. if ( 1 == p->use_count_.fetch_sub( 1, std::memory_order_release) ) {
  34. std::atomic_thread_fence( std::memory_order_acquire);
  35. delete p;
  36. }
  37. }
  38. };
  39. }}}
  40. #ifdef BOOST_HAS_ABI_HEADERS
  41. # include BOOST_ABI_SUFFIX
  42. #endif
  43. #endif // BOOST_FIBERS_DETAIL_FSS_H