system_context.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // system_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_SYSTEM_CONTEXT_HPP
  11. #define ASIO_SYSTEM_CONTEXT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/detail/scheduler.hpp"
  17. #include "asio/detail/thread_group.hpp"
  18. #include "asio/execution.hpp"
  19. #include "asio/execution_context.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. template <typename Blocking, typename Relationship, typename Allocator>
  23. class basic_system_executor;
  24. /// The executor context for the system executor.
  25. class system_context : public execution_context
  26. {
  27. public:
  28. /// The executor type associated with the context.
  29. typedef basic_system_executor<
  30. execution::blocking_t::possibly_t,
  31. execution::relationship_t::fork_t,
  32. std::allocator<void>
  33. > executor_type;
  34. /// Destructor shuts down all threads in the system thread pool.
  35. ASIO_DECL ~system_context();
  36. /// Obtain an executor for the context.
  37. executor_type get_executor() noexcept;
  38. /// Signal all threads in the system thread pool to stop.
  39. ASIO_DECL void stop();
  40. /// Determine whether the system thread pool has been stopped.
  41. ASIO_DECL bool stopped() const noexcept;
  42. /// Join all threads in the system thread pool.
  43. ASIO_DECL void join();
  44. #if defined(GENERATING_DOCUMENTATION)
  45. private:
  46. #endif // defined(GENERATING_DOCUMENTATION)
  47. // Constructor creates all threads in the system thread pool.
  48. ASIO_DECL system_context();
  49. private:
  50. template <typename, typename, typename> friend class basic_system_executor;
  51. struct thread_function;
  52. // Helper function to create the underlying scheduler.
  53. ASIO_DECL detail::scheduler& add_scheduler(detail::scheduler* s);
  54. // The underlying scheduler.
  55. detail::scheduler& scheduler_;
  56. // The threads in the system thread pool.
  57. detail::thread_group threads_;
  58. // The number of threads in the pool.
  59. std::size_t num_threads_;
  60. };
  61. } // namespace asio
  62. #include "asio/detail/pop_options.hpp"
  63. #include "asio/impl/system_context.hpp"
  64. #if defined(ASIO_HEADER_ONLY)
  65. # include "asio/impl/system_context.ipp"
  66. #endif // defined(ASIO_HEADER_ONLY)
  67. #endif // ASIO_SYSTEM_CONTEXT_HPP