fss.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_FSS_H
  8. #define BOOST_FIBERS_FSS_H
  9. #include <boost/config.hpp>
  10. #include <boost/fiber/context.hpp>
  11. #include <boost/fiber/detail/fss.hpp>
  12. #ifdef BOOST_HAS_ABI_HEADERS
  13. # include BOOST_ABI_PREFIX
  14. #endif
  15. namespace boost {
  16. namespace fibers {
  17. template< typename T >
  18. class fiber_specific_ptr {
  19. private:
  20. struct default_cleanup_function : public detail::fss_cleanup_function {
  21. void operator()( void * data) noexcept override {
  22. delete static_cast< T * >( data);
  23. }
  24. };
  25. struct custom_cleanup_function : public detail::fss_cleanup_function {
  26. void (*fn)(T*);
  27. explicit custom_cleanup_function( void(*fn_)(T*) ) noexcept :
  28. fn{ fn_ } {
  29. }
  30. void operator()( void * data) override {
  31. if ( BOOST_LIKELY( nullptr != fn) ) {
  32. fn( static_cast< T * >( data) );
  33. }
  34. }
  35. };
  36. detail::fss_cleanup_function::ptr_t cleanup_fn_;
  37. public:
  38. using element_type = T;
  39. fiber_specific_ptr() :
  40. cleanup_fn_{ new default_cleanup_function() } {
  41. }
  42. explicit fiber_specific_ptr( void(*fn)(T*) ) :
  43. cleanup_fn_{ new custom_cleanup_function( fn) } {
  44. }
  45. ~fiber_specific_ptr() {
  46. context * active_ctx = context::active();
  47. if ( nullptr != active_ctx) {
  48. active_ctx->set_fss_data(
  49. this, cleanup_fn_, nullptr, true);
  50. }
  51. }
  52. fiber_specific_ptr( fiber_specific_ptr const&) = delete;
  53. fiber_specific_ptr & operator=( fiber_specific_ptr const&) = delete;
  54. T * get() const noexcept {
  55. BOOST_ASSERT( context::active() );
  56. void * vp = context::active()->get_fss_data( this);
  57. return static_cast< T * >( vp);
  58. }
  59. T * operator->() const noexcept {
  60. return get();
  61. }
  62. T & operator*() const noexcept {
  63. return * get();
  64. }
  65. T * release() {
  66. T * tmp = get();
  67. context::active()->set_fss_data(
  68. this, cleanup_fn_, nullptr, false);
  69. return tmp;
  70. }
  71. void reset( T * t) {
  72. T * c = get();
  73. if ( BOOST_LIKELY( c != t) ) {
  74. context::active()->set_fss_data(
  75. this, cleanup_fn_, t, true);
  76. }
  77. }
  78. };
  79. }}
  80. #ifdef BOOST_HAS_ABI_HEADERS
  81. # include BOOST_ABI_SUFFIX
  82. #endif
  83. #endif // BOOST_FIBERS_FSS_H