shared_work.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright Nat Goodspeed + Oliver Kowalke 2015.
  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. #ifndef BOOST_FIBERS_ALGO_SHARED_WORK_H
  6. #define BOOST_FIBERS_ALGO_SHARED_WORK_H
  7. #include <condition_variable>
  8. #include <chrono>
  9. #include <deque>
  10. #include <mutex>
  11. #include <boost/config.hpp>
  12. #include <boost/fiber/algo/algorithm.hpp>
  13. #include <boost/fiber/context.hpp>
  14. #include <boost/fiber/detail/config.hpp>
  15. #include <boost/fiber/scheduler.hpp>
  16. #ifdef BOOST_HAS_ABI_HEADERS
  17. # include BOOST_ABI_PREFIX
  18. #endif
  19. #ifdef _MSC_VER
  20. # pragma warning(push)
  21. # pragma warning(disable:4251)
  22. #endif
  23. namespace boost {
  24. namespace fibers {
  25. namespace algo {
  26. class BOOST_FIBERS_DECL shared_work : public algorithm {
  27. private:
  28. typedef std::deque< context * > rqueue_type;
  29. typedef scheduler::ready_queue_type lqueue_type;
  30. static rqueue_type rqueue_;
  31. static std::mutex rqueue_mtx_;
  32. lqueue_type lqueue_{};
  33. std::mutex mtx_{};
  34. std::condition_variable cnd_{};
  35. bool flag_{ false };
  36. bool suspend_{ false };
  37. public:
  38. shared_work() = default;
  39. shared_work( bool suspend) :
  40. suspend_{ suspend } {
  41. }
  42. shared_work( shared_work const&) = delete;
  43. shared_work( shared_work &&) = delete;
  44. shared_work & operator=( shared_work const&) = delete;
  45. shared_work & operator=( shared_work &&) = delete;
  46. void awakened( context * ctx) noexcept;
  47. context * pick_next() noexcept;
  48. bool has_ready_fibers() const noexcept {
  49. std::unique_lock< std::mutex > lock{ rqueue_mtx_ };
  50. return ! rqueue_.empty() || ! lqueue_.empty();
  51. }
  52. void suspend_until( std::chrono::steady_clock::time_point const& time_point) noexcept;
  53. void notify() noexcept;
  54. };
  55. }}}
  56. #ifdef _MSC_VER
  57. # pragma warning(pop)
  58. #endif
  59. #ifdef BOOST_HAS_ABI_HEADERS
  60. # include BOOST_ABI_SUFFIX
  61. #endif
  62. #endif // BOOST_FIBERS_ALGO_SHARED_WORK_H