posix_thread.ipp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // detail/impl/posix_thread.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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 BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_PTHREADS)
  17. #include <boost/asio/detail/posix_thread.hpp>
  18. #include <boost/asio/detail/throw_error.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. posix_thread::~posix_thread()
  25. {
  26. if (arg_)
  27. std::terminate();
  28. }
  29. void posix_thread::join()
  30. {
  31. if (arg_)
  32. {
  33. ::pthread_join(arg_->thread_, 0);
  34. arg_->destroy();
  35. arg_ = 0;
  36. }
  37. }
  38. std::size_t posix_thread::hardware_concurrency()
  39. {
  40. #if defined(_SC_NPROCESSORS_ONLN)
  41. long result = sysconf(_SC_NPROCESSORS_ONLN);
  42. if (result > 0)
  43. return result;
  44. #endif // defined(_SC_NPROCESSORS_ONLN)
  45. return 0;
  46. }
  47. posix_thread::func_base* posix_thread::start_thread(func_base* arg)
  48. {
  49. int error = ::pthread_create(&arg->thread_, 0,
  50. boost_asio_detail_posix_thread_function, arg);
  51. if (error != 0)
  52. {
  53. arg->destroy();
  54. boost::system::error_code ec(error,
  55. boost::asio::error::get_system_category());
  56. boost::asio::detail::throw_error(ec, "thread");
  57. }
  58. return arg;
  59. }
  60. void* boost_asio_detail_posix_thread_function(void* arg)
  61. {
  62. static_cast<posix_thread::func_base*>(arg)->run();
  63. return 0;
  64. }
  65. } // namespace detail
  66. } // namespace asio
  67. } // namespace boost
  68. #include <boost/asio/detail/pop_options.hpp>
  69. #endif // defined(BOOST_ASIO_HAS_PTHREADS)
  70. #endif // BOOST_ASIO_DETAIL_IMPL_POSIX_THREAD_IPP