properties.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright Nat Goodspeed 2014.
  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. // Define fiber_properties, a base class from which a library consumer can
  6. // derive a subclass with specific properties important to a user-coded
  7. // scheduler.
  8. #ifndef BOOST_FIBERS_PROPERTIES_HPP
  9. #define BOOST_FIBERS_PROPERTIES_HPP
  10. #include <boost/assert.hpp>
  11. #include <boost/fiber/detail/config.hpp>
  12. #ifdef BOOST_HAS_ABI_HEADERS
  13. # include BOOST_ABI_PREFIX
  14. #endif
  15. # if defined(BOOST_MSVC)
  16. # pragma warning(push)
  17. # pragma warning(disable:4275)
  18. # endif
  19. namespace boost {
  20. namespace fibers {
  21. class context;
  22. namespace algo {
  23. class algorithm;
  24. }
  25. class BOOST_FIBERS_DECL fiber_properties {
  26. protected:
  27. // initialized by constructor
  28. context * ctx_;
  29. // set every time this fiber becomes READY
  30. algo::algorithm * algo_{ nullptr };
  31. // Inform the relevant algorithm instance that something important
  32. // has changed, so it can (presumably) adjust its data structures
  33. // accordingly.
  34. void notify() noexcept;
  35. public:
  36. // Any specific property setter method, after updating the relevant
  37. // instance variable, can/should call notify().
  38. // fiber_properties, and by implication every subclass, must accept a back
  39. // pointer to its context.
  40. // For fiber_properties passed to fiber constructors, nullptr must be
  41. // used here.
  42. explicit fiber_properties( context * ctx) noexcept :
  43. ctx_{ ctx } {
  44. }
  45. // We need a virtual destructor (hence a vtable) because fiber_properties
  46. // is stored polymorphically (as fiber_properties*) in context, and
  47. // destroyed via that pointer.
  48. virtual ~fiber_properties() = default;
  49. // not really intended for public use, but algorithm_with_properties
  50. // must be able to call this
  51. void set_algorithm( algo::algorithm * algo) noexcept {
  52. algo_ = algo;
  53. }
  54. // not really intended for public use, but required to set properties
  55. // on fiber/context construction.
  56. void set_context( context* ctx ) noexcept {
  57. BOOST_ASSERT( ctx_ == nullptr );
  58. BOOST_ASSERT( ctx != nullptr );
  59. ctx_ = ctx;
  60. }
  61. };
  62. }} // namespace boost::fibers
  63. # if defined(BOOST_MSVC)
  64. # pragma warning(pop)
  65. # endif
  66. #ifdef BOOST_HAS_ABI_HEADERS
  67. # include BOOST_ABI_SUFFIX
  68. #endif
  69. #endif // BOOST_FIBERS_PROPERTIES_HPP